Advertisement
Guest User

Untitled

a guest
May 10th, 2012
1,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. from PyQt4 import QtGui, QtCore
  2.  
  3. class ArrowDir:
  4.     VERTICAL = 0
  5.     HORIZONTAL = 1
  6.  
  7. class CollapsibleArrow(QtGui.QFrame):
  8.     def __init__(self, parent = None):
  9.         QtGui.QFrame.__init__(self, parent = parent)
  10.        
  11.         self.isCollapsed = False
  12.         self.setMaximumSize(24, 24)
  13.         self.setStyleSheet("QFrame {\
  14.        background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #545454, stop: 1 #232323);\
  15.        border-top: 1px solid rgba(192, 192, 192, 255);\
  16.        border-left: 1px solid rgba(192, 192, 192, 255);\
  17.        border-right: 1px solid rgba(32, 32, 32, 255);\
  18.        border-bottom: 1px solid rgba(64, 64, 64, 255);\
  19.        margin: 0px, 0px, 0px, 0px;\
  20.        padding: 0px, 0px, 0px, 0px;}\
  21.        QFrame:hover {background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #232323, stop: 1 #545454);\
  22.        }")
  23.        
  24.         self.arrowPointsFhorizontal = (QtCore.QPointF(4.0, 8.0), QtCore.QPointF(20.0, 8.0), QtCore.QPointF(12.0, 16.0))
  25.         self.arrowPointsFvertical = (QtCore.QPointF(8.0, 4.0), QtCore.QPointF(16.0, 12.0), QtCore.QPointF(8.0, 20.0))
  26.         self.arrowPointsF = self.arrowPointsFhorizontal
  27.        
  28.     def setArrow(self, arrowDir):
  29.         self.arrowPointsF = self.arrowPointsFhorizontal if arrowDir else self.arrowPointsFvertical        
  30.        
  31.     def paintEvent(self, event):        
  32.         qp = QtGui.QPainter()
  33.         qp.begin(self)
  34.         qp.setBrush(QtGui.QColor(192, 192, 192))
  35.         qp.setPen(QtGui.QColor(64, 64, 64))
  36.         qp.drawPolygon(*self.arrowPointsF)
  37.         qp.end()
  38.        
  39.        
  40. class TitleLabel(QtGui.QLabel):
  41.     def __init__(self, parent = None):
  42.         QtGui.QLabel.__init__(self, parent = parent)
  43.         self.setStyleSheet("TitleLabel {background-color: rgba(0, 0, 0, 0);\
  44.        color: white;\
  45.        border-left: 1px solid rgba(128, 128, 128, 255);\
  46.        border-top: 0px transparent;\
  47.        border-right: 0px transparent;\
  48.        border-bottom: 0px transparent;\
  49.        }")
  50.        
  51.  
  52. class TitleFrame(QtGui.QFrame):
  53.     def __init__(self, parent = None):
  54.         QtGui.QFrame.__init__(self, parent = parent)
  55.        
  56.         self.titleLabel = None
  57.         self.arrow = None      
  58.         self.initTitleFrame()
  59.        
  60.     def initArrow(self):
  61.         self.arrow = CollapsibleArrow(self)
  62.        
  63.     def initTitleLabel(self):
  64.         self.titleLabel = TitleLabel(self)
  65.         self.titleLabel.setMinimumHeight(24)
  66.         self.titleLabel.move(QtCore.QPoint(24, 0))
  67.        
  68.     def mousePressEvent(self, event):
  69.         self.emit(QtCore.SIGNAL('clicked()'))
  70.         return super(TitleFrame, self).mousePressEvent(event)
  71.        
  72.     def initTitleFrame(self):
  73.         self.setContentsMargins(0, 0, 0, 0)
  74.         self.setMinimumHeight(24)
  75.         self.setStyleSheet("QFrame {\
  76.        background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #545454, stop: 1 #232323);\
  77.        border-top: 1px solid rgba(192, 192, 192, 255);\
  78.        border-left: 1px solid rgba(192, 192, 192, 255);\
  79.        border-right: 1px solid rgba(64, 64, 64, 255);\
  80.        border-bottom: 1px solid rgba(64, 64, 64, 255);\
  81.        margin: 0px, 0px, 0px, 0px;\
  82.        padding: 0px, 0px, 0px, 0px;\
  83.        }")
  84.        
  85.         self.initArrow()
  86.         self.initTitleLabel()    
  87.        
  88.        
  89.        
  90. class FrameLayout(QtGui.QFrame):
  91.     def __init__(self, parent = None, text = None):
  92.         QtGui.QFrame.__init__(self, parent = parent)
  93.        
  94.         self.text = text
  95.         self.isCollapsed = False
  96.         self.mainLayout = None
  97.         self.titleFrame = None
  98.         self.contentFrame = None
  99.         self.contentLayout = None
  100.         self.label = None
  101.         self.arrow = None
  102.        
  103.         self.initFrameLayout()
  104.        
  105.     def text(self):
  106.         return self.text
  107.        
  108.     def addWidget(self, widget):
  109.         self.contentLayout.addWidget(widget)
  110.  
  111.     def initMainLayout(self):
  112.         self.mainLayout = QtGui.QVBoxLayout()
  113.         self.mainLayout.setContentsMargins(0, 0, 0, 0)
  114.         self.mainLayout.setSpacing(0)
  115.         self.setLayout(self.mainLayout)
  116.        
  117.     def initTitleFrame(self):
  118.         self.titleFrame = TitleFrame()
  119.         self.mainLayout.addWidget(self.titleFrame)
  120.        
  121.     def initContentFrame(self):
  122.         self.contentFrame = QtGui.QFrame()
  123.         self.contentFrame.setContentsMargins(0, 0, 0, 0)
  124.         self.contentFrame.setStyleSheet("QFrame {\
  125.        background-color: grey;\
  126.        border-top: 1px solid rgba(64, 64, 64, 255);\
  127.        border-left: 1px solid rgba(64, 64, 64, 255);\
  128.        border-right: 1px solid rgba(192, 192, 192, 255);\
  129.        border-bottom: 1px solid rgba(192, 192, 192, 255);\
  130.        margin: 0px, 0px, 0px, 0px;\
  131.        padding: 0px, 0px, 0px, 0px;\
  132.        }")
  133.        
  134.         self.contentLayout = QtGui.QVBoxLayout()
  135.         self.contentLayout.setContentsMargins(0, 0, 0, 0)
  136.         self.contentLayout.setSpacing(0)
  137.         self.contentFrame.setLayout(self.contentLayout)
  138.         self.mainLayout.addWidget(self.contentFrame)
  139.  
  140.     def toggleCollapsed(self):
  141.         self.contentFrame.setVisible(not self.isCollapsed)
  142.         self.isCollapsed = not self.isCollapsed
  143.         self.arrow.setArrow(self.isCollapsed)
  144.        
  145.     def setText(self, text):
  146.         self.label.setText(text)  
  147.        
  148.     def initFrameLayout(self):
  149.         self.setContentsMargins(0, 0, 0, 0)
  150.         self.setStyleSheet("QFrame {\
  151.        border: 0px solid;\
  152.        margin: 0px, 0px, 0px, 0px;\
  153.        padding: 0px, 0px, 0px, 0px;\
  154.        }")
  155.        
  156.         self.initMainLayout()
  157.         self.initTitleFrame()
  158.         self.initContentFrame()
  159.         self.arrow = self.titleFrame.arrow
  160.         self.label = self.titleFrame.titleLabel
  161.         QtCore.QObject.connect(self.titleFrame, QtCore.SIGNAL('clicked()'), self.toggleCollapsed)
  162.        
  163.         if self.text:
  164.             self.setText(self.text)
  165.          
  166.        
  167.        
  168. if __name__ == '__main__':
  169.     import sys
  170.     app = QtGui.QApplication(sys.argv)
  171.     win = QtGui.QMainWindow()
  172.     win.setStyleSheet("QMainWindow {background-color: green;}")
  173.     w = QtGui.QWidget()  
  174.     win.setCentralWidget(w)
  175.     l = QtGui.QVBoxLayout()
  176.     l.setSpacing(0)
  177.     l.setAlignment(QtCore.Qt.AlignTop)
  178.     w.setLayout(l)
  179.     f1 = FrameLayout(text = 'Apple')
  180.     f2 = FrameLayout(text = 'Orange')
  181.     f3 = FrameLayout(text = 'Banana')
  182.     f1.addWidget(QtGui.QPushButton('a'))
  183.     f1.addWidget(QtGui.QPushButton('b'))
  184.     f1.addWidget(QtGui.QPushButton('c'))
  185.     f2.addWidget(QtGui.QLineEdit())
  186.     f2.addWidget(QtGui.QLineEdit())
  187.     f2.addWidget(QtGui.QLineEdit())
  188.     f3.addWidget(QtGui.QPushButton('1'))
  189.     f3.addWidget(QtGui.QPushButton('2'))
  190.     f3.addWidget(QtGui.QPushButton('3'))  
  191.     l.addWidget(f1)
  192.     l.addWidget(f2)
  193.     l.addWidget(f3)
  194.     win.show()
  195.     win.raise_()
  196.    
  197.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement