-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.py
225 lines (175 loc) · 7.68 KB
/
field.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from PyQt5 import QtWidgets, QtCore, QtGui
import blockMenu, block
from globalUsed import msgDuration, fieldRowHeight
class field(QtWidgets.QWidget):
def __init__(self, parent, **kwargs):
super().__init__(**kwargs)
self.parent = parent
self.setFocusPolicy(QtCore.Qt.StrongFocus)
vboxLayout1 = QtWidgets.QVBoxLayout()
self.setLayout(vboxLayout1)
# Head
headWidget = QtWidgets.QWidget()
p1 = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred
, QtWidgets.QSizePolicy.Preferred)
p1.setVerticalStretch(2)
headWidget.setSizePolicy(p1)
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Background,
QtGui.QColor(200, 200, 200, 255))
headWidget.setAutoFillBackground(True)
headWidget.setPalette(palette)
addBlkBtn = QtWidgets.QPushButton()
addBlkBtn.setText("選擇方塊")
addBlkBtn.setFixedSize(QtCore.QSize(100, 50))
popupMenu = blockMenu.blockMenu(self.parent, self)
addBlkBtn.setMenu(popupMenu)
previewButton = QtWidgets.QPushButton()
previewButton.setText("預覽")
previewButton.setFixedSize(QtCore.QSize(50, 25))
previewButton.pressed.connect(parent.actions["viewExportedExcel"].trigger)
exportButton = QtWidgets.QPushButton()
exportButton.setText("輸出")
exportButton.setFixedSize(QtCore.QSize(50, 25))
exportButton.pressed.connect(parent.actions["exportExcel"].trigger)
hboxLayout1 = QtWidgets.QHBoxLayout()
hboxLayout1.addWidget(previewButton)
hboxLayout1.addWidget(exportButton)
hboxLayout1.setAlignment(QtCore.Qt.AlignRight)
hboxLayout2 = QtWidgets.QHBoxLayout()
hboxLayout2.setContentsMargins(25, 5, 25, 5)
hboxLayout2.addWidget(addBlkBtn)
hboxLayout2.addLayout(hboxLayout1)
headWidget.setLayout(hboxLayout2)
# Body
self.bodyWidget = QtWidgets.QWidget()
self.scrollArea = QtWidgets.QScrollArea()
self.scrollArea.setWidget(self.bodyWidget)
self.scrollArea.setWidgetResizable(True)
p2 = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred
, QtWidgets.QSizePolicy.Preferred)
p2.setVerticalStretch(14)
self.scrollArea.setSizePolicy(p2)
self.vboxLayout2 = QtWidgets.QVBoxLayout()
self.vboxLayout2.setAlignment(QtCore.Qt.AlignLeft)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setAlignment(QtCore.Qt.AlignTop)
self.gridLayout.setContentsMargins(25, 25, 25, 25)
self.gridLayout.setVerticalSpacing(25)
self.gridLayout.setHorizontalSpacing(0)
self.vboxLayout2.addLayout(self.gridLayout)
self.vboxLayout2.addSpacing(400)
hboxLayout3 = QtWidgets.QHBoxLayout()
hboxLayout3.addLayout(self.vboxLayout2)
hboxLayout3.addSpacing(900)
self.bodyWidget.setLayout(hboxLayout3)
# default cols
for i in range(5):
rowLayout = self.__create_row_hBoxLayout(i + 1)
self.gridLayout.addLayout(rowLayout, i, 0, QtCore.Qt.AlignLeft)
# add col button
addColBtn = QtWidgets.QPushButton()
addColBtn.setText("+")
addColBtn.setFixedSize(25, 25)
addColBtn.pressed.connect(self.on_addColBtn_pressed)
self.gridLayout.addWidget(addColBtn, self.gridLayout.rowCount(),
0, QtCore.Qt.AlignLeft)
vboxLayout1.addWidget(headWidget)
vboxLayout1.addWidget(self.scrollArea)
####################
# Slots
####################
# Add a new column when pressed
def on_addColBtn_pressed(self):
lastRow = self.gridLayout.rowCount() - 1;
addColBtn = self.gridLayout.itemAtPosition(lastRow, 0).widget()
rowLayout = self.__create_row_hBoxLayout(lastRow + 1)
self.gridLayout.addLayout(rowLayout, lastRow, 0, QtCore.Qt.AlignLeft)
self.gridLayout.addWidget(addColBtn, lastRow + 1, 0, QtCore.Qt.AlignLeft)
self.parent.statusBar().showMessage("新增一個欄位", msgDuration)
####################
# Overloadeds
####################
def mouseDoubleClickEvent(self, QMouseEvent):
if QMouseEvent.button() != QtCore.Qt.LeftButton:
return
# transform block-cord to fieldBody-cord
globPos = self.mapToGlobal(QMouseEvent.pos())
fieldBodyPos = self.bodyWidget.mapFromGlobal(globPos)
pos = fieldBodyPos
# decide row to add
startRow = self.__getGridRow(pos.y() - fieldRowHeight / 2, self.gridLayout)
if type(startRow) == int:
print("on row {0}".format(startRow))
self.parent.statusBar().showMessage("該位置不能插入列", msgDuration)
return
# Insert new row
startRow = int(startRow + 0.5)
self.__insert_new_row(startRow)
def mousePressEvent(self, QMouseEvent):
pass
####################
# Private funcs
####################
def __create_default_col(self):
le = QtWidgets.QLineEdit()
le.setPlaceholderText(self.tr("欄位名稱"))
le.setFixedSize(QtCore.QSize(125, fieldRowHeight))
return le
def __create_row_hBoxLayout(self, id):
col = self.__create_default_col()
rowLayout = QtWidgets.QHBoxLayout()
rowLayout.setContentsMargins(0, 0, 0, 0)
rowLayout.setSpacing(0)
idEdit = QtWidgets.QLineEdit()
idEdit.setText(str(id))
idEdit.setAlignment(QtCore.Qt.AlignCenter)
idEdit.setFixedSize(25, fieldRowHeight)
idEdit.setReadOnly(True)
rowLayout.addWidget(idEdit, 0, QtCore.Qt.AlignLeft)
rowLayout.addWidget(col, 0, QtCore.Qt.AlignLeft)
#rowLayout.addStretch(-1)
return rowLayout
def __insert_new_row(self, start):
print("now insert row at: {0}".format(start))
grid = self.gridLayout
# Store all rows after inserted row (included)
store = []
for i in range(start, grid.rowCount()):
item = grid.itemAtPosition(i, 0)
grid.removeItem(item)
store.append(item)
# Add new row
rowLayout = self.__create_row_hBoxLayout(start + 1)
grid.addLayout(rowLayout, start, 0, QtCore.Qt.AlignLeft)
# Restore old rows
for idx, item in enumerate(store):
if type(item) == QtWidgets.QHBoxLayout:
print("adding hbox...")
# increment index
idEdit = item.itemAt(0).widget()
newId = int(idEdit.text()) + 1
idEdit.setText(str(newId))
grid.addLayout(item, start + 1 + idx, 0, QtCore.Qt.AlignLeft)
else:
print("adding non-hbox...")
grid.addWidget(item.widget(), start + 1 + idx, 0, QtCore.Qt.AlignLeft)
#grid.addItem(item, start + 1 + idx, 0, QtCore.Qt.AlignLeft)
self.parent.statusBar().showMessage("新增一個欄位", msgDuration)
def __getGridRow(self, y, gridLayout):
l, topMargin, r, b = gridLayout.getContentsMargins()
y_eff = y - topMargin
# Not even get to first row
if y_eff < 0:
return -0.5
# Estimate row position
verSpace = gridLayout.verticalSpacing()
unit = fieldRowHeight + verSpace
row = int(y_eff / unit)
# Exceed last row (exclude the addBtn)
if row > gridLayout.rowCount() - 2:
return (gridLayout.rowCount() - 2 + 0.5)
# Not on the exact row (on spaces)
if y_eff > unit * (row + 1) - verSpace:
return (row + 0.5)
return row