-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab_l_01.py
165 lines (117 loc) · 4.51 KB
/
tab_l_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
from PyQt4 import QtGui, QtCore
class Button(QtGui.QPushButton):
def __init__(self,name="", height=0, width=0, parent=None,*args):
super(Button, self).__init__()
self.setText(name)
if height:
self.setFixedHeight(height)
if width:
self.setFixedWidth(width)
else:
self.setFixedHeight(55)
self.setFixedWidth(55)
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.LeftButton:
return
mimeData = QtCore.QMimeData()
#mimeData.setText('%d,%d' % (e.x(), e.y()))
mimeData.setText(self.text())
pixmap = QtGui.QPixmap.grabWidget(self)
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setPixmap(pixmap)
drag.setHotSpot(e.pos())
if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
print 'moved'
else:
print 'copied'
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
class Shelf(QtGui.QWidget):
def __init__(self):
super(Shelf, self).__init__()
self.layout = FlowLayout()
for i in range(4):
self.layout.addWidget(Button("button_{}".format(i), height = int(i*10) ,width = (i*10) ))
self.setLayout(self.layout)
class Test(QtGui.QWidget):
def __init__(self):
super(Test,self).__init__()
self.layout = QtGui.QVBoxLayout()
self.tablayout = QtGui.QTabWidget()
for i in range(5):
self.tablayout.addTab(Shelf(),"test_{0}".format(i))
self.layout.addWidget(self.tablayout)
self.setLayout(self.layout)
def resizeEvent(self, event):
if self.height() > self.width():
self.tablayout.setTabPosition(QtGui.QTabWidget.West)
else:
self.tablayout.setTabPosition(QtGui.QTabWidget.North)
class FlowLayout(QtGui.QLayout):
def __init__(self, parent=None, margin=0, spacing=-1):
super(FlowLayout, self).__init__(parent)
if parent is not None:
self.setMargin(margin)
self.setSpacing(spacing)
self.itemList = []
def __del__(self):
item = self.takeAt(0)
while item:
item = self.takeAt(0)
def addItem(self, item):
self.itemList.append(item)
def count(self):
return len(self.itemList)
def itemAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList[index]
return None
def takeAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList.pop(index)
return None
def expandingDirections(self):
return QtCore.Qt.Orientations(QtCore.Qt.Orientation(0))
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
height = self.doLayout(QtCore.QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QtCore.QSize()
for item in self.itemList:
size = size.expandedTo(item.minimumSize())
size += QtCore.QSize(2 * self.contentsMargins().top(), 2 * self.contentsMargins().top())
return size
def doLayout(self, rect, testOnly):
x = rect.x()
y = rect.y()
lineHeight = 0
for item in self.itemList:
wid = item.widget()
spaceX = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton, QtGui.QSizePolicy.PushButton, QtCore.Qt.Horizontal)
spaceY = self.spacing() + wid.style().layoutSpacing(QtGui.QSizePolicy.PushButton, QtGui.QSizePolicy.PushButton, QtCore.Qt.Vertical)
nextX = x + item.sizeHint().width() + spaceX
if nextX - spaceX > rect.right() and lineHeight > 0:
x = rect.x()
y = y + lineHeight + spaceY
nextX = x + item.sizeHint().width() + spaceX
lineHeight = 0
if not testOnly:
item.setGeometry(QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))
x = nextX
lineHeight = max(lineHeight, item.sizeHint().height())
return y + lineHeight - rect.y()
app = QtGui.QApplication(sys.argv)
a = Test()#QtGui.QTabWidget()
a.show()
app.exec_()