Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt4 import QtGui, QtCore
- class ArrowDir:
- VERTICAL = 0
- HORIZONTAL = 1
- class CollapsibleArrow(QtGui.QFrame):
- def __init__(self, parent = None):
- QtGui.QFrame.__init__(self, parent = parent)
- self.isCollapsed = False
- self.setMaximumSize(24, 24)
- self.setStyleSheet("QFrame {\
- background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #545454, stop: 1 #232323);\
- border-top: 1px solid rgba(192, 192, 192, 255);\
- border-left: 1px solid rgba(192, 192, 192, 255);\
- border-right: 1px solid rgba(32, 32, 32, 255);\
- border-bottom: 1px solid rgba(64, 64, 64, 255);\
- margin: 0px, 0px, 0px, 0px;\
- padding: 0px, 0px, 0px, 0px;}\
- QFrame:hover {background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #232323, stop: 1 #545454);\
- }")
- self.arrowPointsFhorizontal = (QtCore.QPointF(4.0, 8.0), QtCore.QPointF(20.0, 8.0), QtCore.QPointF(12.0, 16.0))
- self.arrowPointsFvertical = (QtCore.QPointF(8.0, 4.0), QtCore.QPointF(16.0, 12.0), QtCore.QPointF(8.0, 20.0))
- self.arrowPointsF = self.arrowPointsFhorizontal
- def setArrow(self, arrowDir):
- self.arrowPointsF = self.arrowPointsFhorizontal if arrowDir else self.arrowPointsFvertical
- def paintEvent(self, event):
- qp = QtGui.QPainter()
- qp.begin(self)
- qp.setBrush(QtGui.QColor(192, 192, 192))
- qp.setPen(QtGui.QColor(64, 64, 64))
- qp.drawPolygon(*self.arrowPointsF)
- qp.end()
- class TitleLabel(QtGui.QLabel):
- def __init__(self, parent = None):
- QtGui.QLabel.__init__(self, parent = parent)
- self.setStyleSheet("TitleLabel {background-color: rgba(0, 0, 0, 0);\
- color: white;\
- border-left: 1px solid rgba(128, 128, 128, 255);\
- border-top: 0px transparent;\
- border-right: 0px transparent;\
- border-bottom: 0px transparent;\
- }")
- class TitleFrame(QtGui.QFrame):
- def __init__(self, parent = None):
- QtGui.QFrame.__init__(self, parent = parent)
- self.titleLabel = None
- self.arrow = None
- self.initTitleFrame()
- def initArrow(self):
- self.arrow = CollapsibleArrow(self)
- def initTitleLabel(self):
- self.titleLabel = TitleLabel(self)
- self.titleLabel.setMinimumHeight(24)
- self.titleLabel.move(QtCore.QPoint(24, 0))
- def mousePressEvent(self, event):
- self.emit(QtCore.SIGNAL('clicked()'))
- return super(TitleFrame, self).mousePressEvent(event)
- def initTitleFrame(self):
- self.setContentsMargins(0, 0, 0, 0)
- self.setMinimumHeight(24)
- self.setStyleSheet("QFrame {\
- background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #545454, stop: 1 #232323);\
- border-top: 1px solid rgba(192, 192, 192, 255);\
- border-left: 1px solid rgba(192, 192, 192, 255);\
- border-right: 1px solid rgba(64, 64, 64, 255);\
- border-bottom: 1px solid rgba(64, 64, 64, 255);\
- margin: 0px, 0px, 0px, 0px;\
- padding: 0px, 0px, 0px, 0px;\
- }")
- self.initArrow()
- self.initTitleLabel()
- class FrameLayout(QtGui.QFrame):
- def __init__(self, parent = None, text = None):
- QtGui.QFrame.__init__(self, parent = parent)
- self.text = text
- self.isCollapsed = False
- self.mainLayout = None
- self.titleFrame = None
- self.contentFrame = None
- self.contentLayout = None
- self.label = None
- self.arrow = None
- self.initFrameLayout()
- def text(self):
- return self.text
- def addWidget(self, widget):
- self.contentLayout.addWidget(widget)
- def initMainLayout(self):
- self.mainLayout = QtGui.QVBoxLayout()
- self.mainLayout.setContentsMargins(0, 0, 0, 0)
- self.mainLayout.setSpacing(0)
- self.setLayout(self.mainLayout)
- def initTitleFrame(self):
- self.titleFrame = TitleFrame()
- self.mainLayout.addWidget(self.titleFrame)
- def initContentFrame(self):
- self.contentFrame = QtGui.QFrame()
- self.contentFrame.setContentsMargins(0, 0, 0, 0)
- self.contentFrame.setStyleSheet("QFrame {\
- background-color: grey;\
- border-top: 1px solid rgba(64, 64, 64, 255);\
- border-left: 1px solid rgba(64, 64, 64, 255);\
- border-right: 1px solid rgba(192, 192, 192, 255);\
- border-bottom: 1px solid rgba(192, 192, 192, 255);\
- margin: 0px, 0px, 0px, 0px;\
- padding: 0px, 0px, 0px, 0px;\
- }")
- self.contentLayout = QtGui.QVBoxLayout()
- self.contentLayout.setContentsMargins(0, 0, 0, 0)
- self.contentLayout.setSpacing(0)
- self.contentFrame.setLayout(self.contentLayout)
- self.mainLayout.addWidget(self.contentFrame)
- def toggleCollapsed(self):
- self.contentFrame.setVisible(not self.isCollapsed)
- self.isCollapsed = not self.isCollapsed
- self.arrow.setArrow(self.isCollapsed)
- def setText(self, text):
- self.label.setText(text)
- def initFrameLayout(self):
- self.setContentsMargins(0, 0, 0, 0)
- self.setStyleSheet("QFrame {\
- border: 0px solid;\
- margin: 0px, 0px, 0px, 0px;\
- padding: 0px, 0px, 0px, 0px;\
- }")
- self.initMainLayout()
- self.initTitleFrame()
- self.initContentFrame()
- self.arrow = self.titleFrame.arrow
- self.label = self.titleFrame.titleLabel
- QtCore.QObject.connect(self.titleFrame, QtCore.SIGNAL('clicked()'), self.toggleCollapsed)
- if self.text:
- self.setText(self.text)
- if __name__ == '__main__':
- import sys
- app = QtGui.QApplication(sys.argv)
- win = QtGui.QMainWindow()
- win.setStyleSheet("QMainWindow {background-color: green;}")
- w = QtGui.QWidget()
- win.setCentralWidget(w)
- l = QtGui.QVBoxLayout()
- l.setSpacing(0)
- l.setAlignment(QtCore.Qt.AlignTop)
- w.setLayout(l)
- f1 = FrameLayout(text = 'Apple')
- f2 = FrameLayout(text = 'Orange')
- f3 = FrameLayout(text = 'Banana')
- f1.addWidget(QtGui.QPushButton('a'))
- f1.addWidget(QtGui.QPushButton('b'))
- f1.addWidget(QtGui.QPushButton('c'))
- f2.addWidget(QtGui.QLineEdit())
- f2.addWidget(QtGui.QLineEdit())
- f2.addWidget(QtGui.QLineEdit())
- f3.addWidget(QtGui.QPushButton('1'))
- f3.addWidget(QtGui.QPushButton('2'))
- f3.addWidget(QtGui.QPushButton('3'))
- l.addWidget(f1)
- l.addWidget(f2)
- l.addWidget(f3)
- win.show()
- win.raise_()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement