Merge branch 'closable_docks' into develop

This commit is contained in:
Luke Campagnola 2014-05-08 10:38:26 -04:00
commit d03dd41a8b
4 changed files with 64 additions and 55 deletions

View File

@ -49,6 +49,7 @@ pyqtgraph-0.9.9 [unreleased]
- Added configurable formatting for TableWidget - Added configurable formatting for TableWidget
- Added 'stepMode' argument to PlotDataItem() - Added 'stepMode' argument to PlotDataItem()
- Added ViewBox.invertX() - Added ViewBox.invertX()
- Docks now have optional close button
Bugfixes: Bugfixes:
- PlotCurveItem now has correct clicking behavior--clicks within a few px - PlotCurveItem now has correct clicking behavior--clicks within a few px

View File

@ -35,7 +35,7 @@ win.setWindowTitle('pyqtgraph example: dockarea')
## Note that size arguments are only a suggestion; docks will still have to ## Note that size arguments are only a suggestion; docks will still have to
## fill the entire dock area and obey the limits of their internal widgets. ## fill the entire dock area and obey the limits of their internal widgets.
d1 = Dock("Dock1", size=(1, 1)) ## give this dock the minimum possible size d1 = Dock("Dock1", size=(1, 1)) ## give this dock the minimum possible size
d2 = Dock("Dock2 - Console", size=(500,300)) d2 = Dock("Dock2 - Console", size=(500,300), closable=True)
d3 = Dock("Dock3", size=(500,400)) d3 = Dock("Dock3", size=(500,400))
d4 = Dock("Dock4 (tabbed) - Plot", size=(500,200)) d4 = Dock("Dock4 (tabbed) - Plot", size=(500,200))
d5 = Dock("Dock5 - Image", size=(500,200)) d5 = Dock("Dock5 - Image", size=(500,200))

View File

@ -22,6 +22,9 @@ class Container(object):
return None return None
def insert(self, new, pos=None, neighbor=None): def insert(self, new, pos=None, neighbor=None):
# remove from existing parent first
new.setParent(None)
if not isinstance(new, list): if not isinstance(new, list):
new = [new] new = [new]
if neighbor is None: if neighbor is None:

View File

@ -8,11 +8,13 @@ class Dock(QtGui.QWidget, DockDrop):
sigStretchChanged = QtCore.Signal() sigStretchChanged = QtCore.Signal()
def __init__(self, name, area=None, size=(10, 10), widget=None, hideTitle=False, autoOrientation=True): def __init__(self, name, area=None, size=(10, 10), widget=None, hideTitle=False, autoOrientation=True, closable=False):
QtGui.QWidget.__init__(self) QtGui.QWidget.__init__(self)
DockDrop.__init__(self) DockDrop.__init__(self)
self.area = area self.area = area
self.label = DockLabel(name, self) self.label = DockLabel(name, self, closable)
if closable:
self.label.sigCloseClicked.connect(self.close)
self.labelHidden = False self.labelHidden = False
self.moveLabel = True ## If false, the dock is no longer allowed to move the label. self.moveLabel = True ## If false, the dock is no longer allowed to move the label.
self.autoOrient = autoOrientation self.autoOrient = autoOrientation
@ -35,30 +37,30 @@ class Dock(QtGui.QWidget, DockDrop):
#self.titlePos = 'top' #self.titlePos = 'top'
self.raiseOverlay() self.raiseOverlay()
self.hStyle = """ self.hStyle = """
Dock > QWidget { Dock > QWidget {
border: 1px solid #000; border: 1px solid #000;
border-radius: 5px; border-radius: 5px;
border-top-left-radius: 0px; border-top-left-radius: 0px;
border-top-right-radius: 0px; border-top-right-radius: 0px;
border-top-width: 0px; border-top-width: 0px;
}""" }"""
self.vStyle = """ self.vStyle = """
Dock > QWidget { Dock > QWidget {
border: 1px solid #000; border: 1px solid #000;
border-radius: 5px; border-radius: 5px;
border-top-left-radius: 0px; border-top-left-radius: 0px;
border-bottom-left-radius: 0px; border-bottom-left-radius: 0px;
border-left-width: 0px; border-left-width: 0px;
}""" }"""
self.nStyle = """ self.nStyle = """
Dock > QWidget { Dock > QWidget {
border: 1px solid #000; border: 1px solid #000;
border-radius: 5px; border-radius: 5px;
}""" }"""
self.dragStyle = """ self.dragStyle = """
Dock > QWidget { Dock > QWidget {
border: 4px solid #00F; border: 4px solid #00F;
border-radius: 5px; border-radius: 5px;
}""" }"""
self.setAutoFillBackground(False) self.setAutoFillBackground(False)
self.widgetArea.setStyleSheet(self.hStyle) self.widgetArea.setStyleSheet(self.hStyle)
@ -79,7 +81,7 @@ class Dock(QtGui.QWidget, DockDrop):
def setStretch(self, x=None, y=None): def setStretch(self, x=None, y=None):
""" """
Set the 'target' size for this Dock. Set the 'target' size for this Dock.
The actual size will be determined by comparing this Dock's The actual size will be determined by comparing this Dock's
stretch value to the rest of the docks it shares space with. stretch value to the rest of the docks it shares space with.
""" """
@ -130,7 +132,7 @@ class Dock(QtGui.QWidget, DockDrop):
Sets the orientation of the title bar for this Dock. Sets the orientation of the title bar for this Dock.
Must be one of 'auto', 'horizontal', or 'vertical'. Must be one of 'auto', 'horizontal', or 'vertical'.
By default ('auto'), the orientation is determined By default ('auto'), the orientation is determined
based on the aspect ratio of the Dock. based on the aspect ratio of the Dock.
""" """
#print self.name(), "setOrientation", o, force #print self.name(), "setOrientation", o, force
if o == 'auto' and self.autoOrient: if o == 'auto' and self.autoOrient:
@ -175,7 +177,7 @@ class Dock(QtGui.QWidget, DockDrop):
def addWidget(self, widget, row=None, col=0, rowspan=1, colspan=1): def addWidget(self, widget, row=None, col=0, rowspan=1, colspan=1):
""" """
Add a new widget to the interior of this Dock. Add a new widget to the interior of this Dock.
Each Dock uses a QGridLayout to arrange widgets within. Each Dock uses a QGridLayout to arrange widgets within.
""" """
if row is None: if row is None:
@ -239,12 +241,13 @@ class Dock(QtGui.QWidget, DockDrop):
def dropEvent(self, *args): def dropEvent(self, *args):
DockDrop.dropEvent(self, *args) DockDrop.dropEvent(self, *args)
class DockLabel(VerticalLabel): class DockLabel(VerticalLabel):
sigClicked = QtCore.Signal(object, object) sigClicked = QtCore.Signal(object, object)
sigCloseClicked = QtCore.Signal()
def __init__(self, text, dock): def __init__(self, text, dock, showCloseButton):
self.startedDrag = False
self.dim = False self.dim = False
self.fixedWidth = False self.fixedWidth = False
VerticalLabel.__init__(self, text, orientation='horizontal', forceWidth=False) VerticalLabel.__init__(self, text, orientation='horizontal', forceWidth=False)
@ -252,10 +255,13 @@ class DockLabel(VerticalLabel):
self.dock = dock self.dock = dock
self.updateStyle() self.updateStyle()
self.setAutoFillBackground(False) self.setAutoFillBackground(False)
self.startedDrag = False
#def minimumSizeHint(self): self.closeButton = None
##sh = QtGui.QWidget.minimumSizeHint(self) if showCloseButton:
#return QtCore.QSize(20, 20) self.closeButton = QtGui.QToolButton(self)
self.closeButton.clicked.connect(self.sigCloseClicked)
self.closeButton.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_TitleBarCloseButton))
def updateStyle(self): def updateStyle(self):
r = '3px' r = '3px'
@ -269,28 +275,28 @@ class DockLabel(VerticalLabel):
border = '#55B' border = '#55B'
if self.orientation == 'vertical': if self.orientation == 'vertical':
self.vStyle = """DockLabel { self.vStyle = """DockLabel {
background-color : %s; background-color : %s;
color : %s; color : %s;
border-top-right-radius: 0px; border-top-right-radius: 0px;
border-top-left-radius: %s; border-top-left-radius: %s;
border-bottom-right-radius: 0px; border-bottom-right-radius: 0px;
border-bottom-left-radius: %s; border-bottom-left-radius: %s;
border-width: 0px; border-width: 0px;
border-right: 2px solid %s; border-right: 2px solid %s;
padding-top: 3px; padding-top: 3px;
padding-bottom: 3px; padding-bottom: 3px;
}""" % (bg, fg, r, r, border) }""" % (bg, fg, r, r, border)
self.setStyleSheet(self.vStyle) self.setStyleSheet(self.vStyle)
else: else:
self.hStyle = """DockLabel { self.hStyle = """DockLabel {
background-color : %s; background-color : %s;
color : %s; color : %s;
border-top-right-radius: %s; border-top-right-radius: %s;
border-top-left-radius: %s; border-top-left-radius: %s;
border-bottom-right-radius: 0px; border-bottom-right-radius: 0px;
border-bottom-left-radius: 0px; border-bottom-left-radius: 0px;
border-width: 0px; border-width: 0px;
border-bottom: 2px solid %s; border-bottom: 2px solid %s;
padding-left: 3px; padding-left: 3px;
padding-right: 3px; padding-right: 3px;
@ -316,11 +322,9 @@ class DockLabel(VerticalLabel):
if not self.startedDrag and (ev.pos() - self.pressPos).manhattanLength() > QtGui.QApplication.startDragDistance(): if not self.startedDrag and (ev.pos() - self.pressPos).manhattanLength() > QtGui.QApplication.startDragDistance():
self.dock.startDrag() self.dock.startDrag()
ev.accept() ev.accept()
#print ev.pos()
def mouseReleaseEvent(self, ev): def mouseReleaseEvent(self, ev):
if not self.startedDrag: if not self.startedDrag:
#self.emit(QtCore.SIGNAL('clicked'), self, ev)
self.sigClicked.emit(self, ev) self.sigClicked.emit(self, ev)
ev.accept() ev.accept()
@ -328,13 +332,14 @@ class DockLabel(VerticalLabel):
if ev.button() == QtCore.Qt.LeftButton: if ev.button() == QtCore.Qt.LeftButton:
self.dock.float() self.dock.float()
#def paintEvent(self, ev): def resizeEvent (self, ev):
#p = QtGui.QPainter(self) if self.closeButton:
##p.setBrush(QtGui.QBrush(QtGui.QColor(100, 100, 200))) if self.orientation == 'vertical':
#p.setPen(QtGui.QPen(QtGui.QColor(50, 50, 100))) size = ev.size().width()
#p.drawRect(self.rect().adjusted(0, 0, -1, -1)) pos = QtCore.QPoint(0, 0)
else:
#VerticalLabel.paintEvent(self, ev) size = ev.size().height()
pos = QtCore.QPoint(ev.size().width() - size, 0)
self.closeButton.setFixedSize(QtCore.QSize(size, size))
self.closeButton.move(pos)
super(DockLabel,self).resizeEvent(ev)