-
Notifications
You must be signed in to change notification settings - Fork 18
/
dodoPM.py
459 lines (435 loc) · 16.7 KB
/
dodoPM.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# Copyright (C) 2018 oddtopus (www.github.com/oddtopus/dodo)
# Following code is derived from
# PieMenu widget for FreeCAD
#
# Copyright (C) 2016, 2017 triplus @ FreeCAD
# Copyright (C) 2015,2016 looo @ FreeCAD
# Copyright (C) 2015 microelly <microelly2@freecadbuch.de>
import FreeCAD, FreeCADGui, math, platform, csv, pCmd
from PySide import QtCore
from PySide import QtGui
from os.path import join, dirname, abspath
from os import listdir
# definition of style
styleButton = ("""
QToolButton {
background-color: lightGray;
border: 1px outset silver;
}
QToolButton:disabled {
background-color: darkGray;
}
QToolButton:hover {
background-color: lightBlue;
}
QToolButton:checked {
background-color: lightGreen;
}
QToolButton::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: center center;
}
""")
styleMenuClose = ("""
QToolButton {
background-color: rgba(60,60,60,235);
border: 1px solid #1e1e1e;
}
QToolButton::menu-indicator {
subcontrol-origin: padding;
subcontrol-position: center center;
}
""")
styleContainer = ("QMenu{background: transparent}")
# styleCombo = ("""
# QComboBox {
# background: transparent;
# border: 1px solid transparent;
# }
# """)
# styleQuickMenu = ("padding: 5px")
iconClose = QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_DialogCloseButton)
def radiusSize(buttonSize):
radius = str(buttonSize / 2)
return "QToolButton {border-radius: " + radius + "px}"
def iconSize(buttonSize):
icon = buttonSize# / 3 * 2
return icon
# definition of widgets
def closeButton(buttonSize=50):
icon = iconSize(buttonSize)
radius = radiusSize(buttonSize)
button = QtGui.QToolButton()
button.setProperty("ButtonX", 0)
button.setProperty("ButtonY", 0)
button.setGeometry(0, 0, buttonSize, buttonSize)
button.setIconSize(QtCore.QSize(icon, icon))
button.setIcon(iconClose)
button.setStyleSheet(styleMenuClose + radius)
button.setAttribute(QtCore.Qt.WA_TranslucentBackground)
def onButton():
PieMenuInstance.hide()
button.clicked.connect(onButton)
return button
# main classes
class HoverButton(QtGui.QToolButton):
def __init__(self, parent=None):
super(HoverButton, self).__init__()
def enterEvent(self, event):
paramGet = FreeCAD.ParamGet("User parameter:BaseApp/PieMenu")
mode = paramGet.GetString("TriggerMode")
if self.defaultAction().isEnabled() and mode == "Hover":
PieMenuInstance.hide()
self.defaultAction().trigger()
else:
pass
def mouseReleaseEvent(self, event):
paramGet = FreeCAD.ParamGet("User parameter:BaseApp/PieMenu")
mode = paramGet.GetString("TriggerMode")
if self.defaultAction().isEnabled():
PieMenuInstance.hide()
self.defaultAction().trigger()
else:
pass
class PieMenu:
def __init__(self):
self.radius = 100
self.buttons = []
self.buttonSize = 32
self.menu = QtGui.QMenu(mw)
self.menuSize = 0
self.menu.setStyleSheet(styleContainer)
self.menu.setWindowFlags(self.menu.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.menu.setAttribute(QtCore.Qt.WA_TranslucentBackground)
if compositingManager:
pass
else:
self.menu.setAttribute(QtCore.Qt.WA_PaintOnScreen)
def add_commands(self, commands):
# paramGet = FreeCAD.ParamGet("User parameter:BaseApp/PieMenu")
for i in self.buttons:
i.deleteLater()
self.buttons = []
if len(commands) == 0:
commandNumber = 1
else:
commandNumber = len(commands)
valueRadius = 100
valueButton = 50
self.radius = valueRadius
self.buttonSize = valueButton
if commandNumber == 1:
angle = 0
buttonSize = self.buttonSize
else:
angle = 2 * math.pi / commandNumber
buttonRadius = math.sin(angle / 2) * self.radius
buttonSize = math.trunc(2 * buttonRadius / math.sqrt(2))
angleStart = 3 * math.pi / 2 - angle
if buttonSize > self.buttonSize:
buttonSize = self.buttonSize
else:
pass
radius = radiusSize(buttonSize)
icon = iconSize(buttonSize)
if windowShadow:
pass
else:
self.menuSize = valueRadius * 2 + buttonSize + 4
if self.menuSize < 90:
self.menuSize = 90
else:
pass
self.menu.setMinimumWidth(self.menuSize)
self.menu.setMinimumHeight(self.menuSize)
num = 1
for i in commands:
button = HoverButton()
button.setParent(self.menu)
button.setAttribute(QtCore.Qt.WA_Hover)
button.setStyleSheet(styleButton + radius)
button.setAttribute(QtCore.Qt.WA_TranslucentBackground)
button.setDefaultAction(commands[commands.index(i)])
button.setGeometry(0, 0, buttonSize, buttonSize)
button.setIconSize(QtCore.QSize(icon, icon))
button.setProperty("ButtonX", self.radius *
(math.cos(angle * num + angleStart)))
button.setProperty("ButtonY", self.radius *
(math.sin(angle * num + angleStart)))
self.buttons.append(button)
num = num + 1
buttonClose = closeButton()
buttonClose.setParent(self.menu)
self.buttons.append(buttonClose)
if compositingManager:
pass
else:
for i in self.buttons:
i.setAttribute(QtCore.Qt.WA_PaintOnScreen)
def hide(self):
for i in self.buttons:
i.hide()
self.menu.hide()
def showAtMouse(self):
actionDict=dict([(a.objectName(),a) for a in FreeCADGui.getMainWindow().findChildren(QtGui.QAction)])
try: actions.pop('')
except:pass
commands=[actionDict[n] for n in toolList if n in actionDict]
self.add_commands(commands)
if self.menu.isVisible():
self.hide()
else:
if windowShadow:
pos = mw.mapFromGlobal(QtGui.QCursor.pos())
self.menu.popup(QtCore.QPoint(mw.pos()))
self.menu.setGeometry(mw.geometry())
for i in self.buttons:
i.move(i.property("ButtonX") + pos.x() - i.width() / 2,
i.property("ButtonY") + pos.y() - i.height() / 2)
i.setVisible(True)
for i in self.buttons:
i.repaint()
else:
pos = QtGui.QCursor.pos()
for i in self.buttons:
i.move(i.property("ButtonX") + (self.menuSize - i.size().width()) / 2,
i.property("ButtonY") + (self.menuSize - i.size().height()) / 2)
i.setVisible(True)
self.menu.popup(QtCore.QPoint(pos.x() - self.menuSize / 2, pos.y() - self.menuSize / 2))
# QkMenus:
class QkMenu(object):
def setupUi(self, Dialog):
self.gridLayout = QtGui.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.comboRating = QtGui.QComboBox(Dialog)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.comboRating.sizePolicy().hasHeightForWidth())
self.comboRating.setSizePolicy(sizePolicy)
self.comboRating.setObjectName("comboRating")
self.gridLayout.addWidget(self.comboRating, 1, 1, 1, 1)
self.labRating = QtGui.QLabel("Rating:",Dialog)
self.labRating.setAlignment(QtCore.Qt.AlignCenter)
self.labRating.setObjectName("labRating")
self.gridLayout.addWidget(self.labRating, 1, 0, 1, 1)
self.comboPL = QtGui.QComboBox(Dialog)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.comboPL.sizePolicy().hasHeightForWidth())
self.comboPL.setSizePolicy(sizePolicy)
self.comboPL.setObjectName("comboPL")
self.comboPL.addItem("<none>")
self.gridLayout.addWidget(self.comboPL, 0, 1, 1, 1)
self.btnGO = QtGui.QPushButton("GO",Dialog)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnGO.sizePolicy().hasHeightForWidth())
self.btnGO.setSizePolicy(sizePolicy)
self.btnGO.setMinimumSize(QtCore.QSize(50, 50))
self.btnGO.setMaximumSize(QtCore.QSize(80, 80))
self.btnGO.setObjectName("btnGO")
self.gridLayout.addWidget(self.btnGO, 0, 2, 2, 1)
self.labPL = QtGui.QLabel("PypeLine: ",Dialog)
self.labPL.setAlignment(QtCore.Qt.AlignCenter)
self.labPL.setObjectName("labPL")
self.gridLayout.addWidget(self.labPL, 0, 0, 1, 1)
self.listSize = QtGui.QListWidget(Dialog)
self.listSize.setStyleSheet("QListWidget{background: transparent}")
self.listSize.setObjectName("listSize")
for i in range(3):
self.listSize.addItem("item "+str(i))
self.gridLayout.addWidget(self.listSize, 2, 0, 1, 3)
QtCore.QMetaObject.connectSlotsByName(Dialog)
Dialog.setTabOrder(self.listSize, self.btnGO)
Dialog.setTabOrder(self.btnGO, self.comboRating)
Dialog.setTabOrder(self.comboRating, self.comboPL)
class DialogQM(QtGui.QDialog):
def __init__(self, winTitle="Quick Insert", PType='Pipe'):
super(DialogQM,self).__init__(FreeCADGui.getMainWindow())
self.setWindowTitle(winTitle)
self.setObjectName("DQkMenu")
self.resize(300, 300)
self.setStyleSheet(("Qself{background: transparent}"))
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.QM=QkMenu()
self.QM.setupUi(self)
self.QM.btnGO.clicked.connect(self.go)
self.QM.comboRating.currentIndexChanged.connect(self.updateSizes)
self.QM.comboPL.currentIndexChanged.connect(self.setCurrentPL)
### pype stuff ###
self.PType=PType
self.PRating=''
self.dictList=list()
self.files=listdir(join(dirname(abspath(__file__)),"tablez"))
ratings=[s.lstrip(PType+"_").rstrip(".csv") for s in self.files if s.startswith(PType) and s.endswith('.csv')]
if ratings: # adds ratings in combo
self.QM.comboRating.addItems(ratings)
self.updateSizes()
self.updatePL()
# self.show()
def show(self):
self.updatePL()
super(DialogQM,self).show()
def setCurrentPL(self,PLName=None):
if self.QM.comboPL.currentText() not in ['<none>','<new>']:
FreeCAD.__activePypeLine__= self.QM.comboPL.currentText()
else:
FreeCAD.__activePypeLine__=None
def updateSizes(self):
self.QM.listSize.clear()
self.PRating=self.QM.comboRating.currentText()
for fileName in self.files: #adds sizes in list
if fileName==self.PType+'_'+self.PRating+'.csv':
f=open(join(dirname(abspath(__file__)),"tablez",fileName),'r')
reader=csv.DictReader(f,delimiter=';')
self.dictList=[DNx for DNx in reader]
f.close()
for row in self.dictList:
s=row['PSize']
if 'OD' in row.keys():
s+=" - "+row['OD']
if 'thk' in row.keys():
s+="x"+row['thk']
self.QM.listSize.addItem(s)
break
def updatePL(self):
if FreeCAD.activeDocument():
pypelines=[o.Label for o in FreeCAD.activeDocument().Objects if hasattr(o,'PType') and o.PType=='PypeLine']
else:
pypelines=[]
if pypelines: # updates pypelines in combo
pl=FreeCAD.__activePypeLine__
self.QM.comboPL.clear()
pypelines.insert(0,'<none>')
self.QM.comboPL.addItems(pypelines)
if pl and pl in pypelines:
self.QM.comboPL.setCurrentIndex(self.QM.comboPL.findText(pl))
def go(self):
self.close()
# DEFINITION OF QKMENUS DIALOGS: alternative to the commands created in CPipe.py
class pQM(DialogQM):
def __init__(self):
super(pQM,self).__init__('Insert pipe', 'Pipe')
self.QM.lineEdit = QtGui.QLineEdit(self)
self.QM.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.QM.lineEdit.setObjectName("lineEdit")
self.QM.gridLayout.addWidget(self.QM.lineEdit, 3, 0, 1, 3)
self.QM.lineEdit.setPlaceholderText("<length>")
self.QM.lineEdit.setValidator(QtGui.QDoubleValidator())
self.QM.lineEdit.validator().setBottom(0)
def go(self):
d=self.dictList[self.QM.listSize.currentRow()]
if self.QM.lineEdit.text():
L=float(self.QM.lineEdit.text())
else:
L=1000
pCmd.doPipes([d['PSize'],float(d['OD']),float(d['thk']),L],FreeCAD.__activePypeLine__)
super(pQM,self).go()
class eQM(DialogQM):
def __init__(self):
super(eQM,self).__init__('Insert elbow', 'Elbow')
self.QM.lineEdit = QtGui.QLineEdit(self)
self.QM.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.QM.lineEdit.setObjectName("lineEdit")
self.QM.gridLayout.addWidget(self.QM.lineEdit, 3, 0, 1, 3)
self.QM.lineEdit.setPlaceholderText("<angle>")
self.QM.lineEdit.setValidator(QtGui.QDoubleValidator())
self.QM.lineEdit.validator().setBottom(0)
self.QM.lineEdit.validator().setTop(90)
self.QM.lineEdit2 = QtGui.QLineEdit(self)
self.QM.lineEdit2.setAlignment(QtCore.Qt.AlignCenter)
self.QM.lineEdit2.setObjectName("lineEdit2")
self.QM.gridLayout.addWidget(self.QM.lineEdit2, 4, 0, 1, 3)
self.QM.lineEdit2.setPlaceholderText("<radius>")
self.QM.lineEdit2.setValidator(QtGui.QDoubleValidator())
def go(self):
d=self.dictList[self.QM.listSize.currentRow()]
if self.QM.lineEdit.text():
ang=float(self.QM.lineEdit.text())
if ang>180:
ang=180
self.QM.lineEdit.setText('180')
else:
ang=90
if self.QM.lineEdit2.text():
rad=float(self.QM.lineEdit2.text())
if rad<float(d['OD'])/2:
rad=float(d['OD'])/2*1.1
self.QM.lineEdit.setText(str(rad))
else:
rad=d['BendRadius']
pCmd.doElbow([d['PSize'],float(d['OD']),float(d['thk']),ang,rad],FreeCAD.__activePypeLine__)
super(eQM,self).go()
class fQM(DialogQM):
def __init__(self):
super(fQM,self).__init__('Insert flange', 'Flange')
def go(self):
d=self.dictList[self.QM.listSize.currentRow()]
proplist=[d['PSize'],d['FlangeType'],float(d['D']),float(d['d']),float(d['df']),float(d['f']),float(d['t']),int(d['n'])]
try: # for raised-face
proplist.append(float(d['trf']))
proplist.append(float(d['drf']))
except:
pass
try: # for welding-neck
proplist.append(float(d['twn']))
proplist.append(float(d['dwn']))
proplist.append(float(d['ODp']))
except:
pass
pCmd.doFlanges(proplist ,FreeCAD.__activePypeLine__)
super(fQM,self).go()
class vQM(DialogQM):
def __init__(self):
super(vQM,self).__init__('Insert valve', 'Valve')
self.QM.slider = QtGui.QSlider(self)
self.QM.slider.setOrientation(QtCore.Qt.Horizontal)
self.QM.slider.setObjectName("slider")
self.QM.gridLayout.addWidget(self.QM.slider, 4, 0, 1, 3)
def go(self):
d=self.dictList[self.QM.listSize.currentRow()]
proplist=[d['PSize'],d['VType'],float(d['OD']),float(d['ID']),float(d['H']),float(d['Kv'])]
pCmd.doValves(proplist ,FreeCAD.__activePypeLine__, self.QM.slider.value())
super(vQM,self).go()
class cQM(DialogQM):
def __init__(self):
super(cQM,self).__init__('Insert cap', 'Cap')
def go(self):
d=self.dictList[self.QM.listSize.currentRow()]
proplist=[ ]
pCmd.doCaps([d['PSize'],float(d['OD']),float(d['thk'])],FreeCAD.__activePypeLine__)
super(cQM,self).go()
# create instances of qkMenu dialogs
pqm=pQM()
eqm=eQM()
fqm=fQM()
vqm=vQM()
cqm=cQM()
# main
mw = FreeCADGui.getMainWindow()
toolList=["pipeQM","elbowQM","flangeQM","valveQM","capQM"]#["insertPipe","insertElbow","insertReduct","insertCap","insertValve","insertFlange","insertUbolt"]
compositingManager = True
if QtCore.qVersion() < "5":
windowShadow = False
else:
windowShadow = True
if platform.system() == "Linux":
try:
if QtGui.QX11Info.isCompositingManagerRunning():
windowShadow = True
else:
compositingManager = False
except AttributeError:
windowShadow = True
if platform.system() == "Windows":
windowShadow = True
PieMenuInstance = PieMenu()
FreeCAD.__dodoPMact__ = QtGui.QAction(mw)
FreeCAD.__dodoPMact__.setObjectName("PieTest")
FreeCAD.__dodoPMact__.setShortcut(QtGui.QKeySequence("Z"))
FreeCAD.__dodoPMact__.triggered.connect(PieMenuInstance.showAtMouse)
mw.addAction(FreeCAD.__dodoPMact__)