-
Notifications
You must be signed in to change notification settings - Fork 18
/
dodoDialogs.py
157 lines (155 loc) · 6.63 KB
/
dodoDialogs.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
from PySide.QtCore import *
from PySide.QtGui import *
from sys import platform
from os.path import join, dirname, abspath
from os import listdir
import FreeCAD, FreeCADGui, csv
class protoTypeDialog(object):
'prototype for dialogs.ui with callback function'
def __init__(self,dialog='anyFile.ui'):
dialogPath=join(dirname(abspath(__file__)),"dialogz",dialog)
self.form=FreeCADGui.PySideUic.loadUi(dialogPath)
### new shortcuts procedure
self.mw = FreeCADGui.getMainWindow()
for act in self.mw.findChildren(QAction):
if act.objectName() in ["actionX", "actionS"]:
self.mw.removeAction(act)
self.actionX = QAction(self.mw)
self.actionX.setObjectName("actionX") # define X action
self.actionX.setShortcut(QKeySequence("X"))
self.actionX.triggered.connect(self.accept)
self.mw.addAction(self.actionX)
self.actionS = QAction(self.mw)
self.actionS.setObjectName("actionS") # define S action
self.actionS.setShortcut(QKeySequence("S"))
self.actionS.triggered.connect(self.selectAction)
self.mw.addAction(self.actionS)
self.actionESC = QAction(self.mw)
FreeCAD.Console.PrintMessage('"%s" to select; "%s" to execute\n' %(self.actionS.shortcuts()[0].toString(),self.actionX.shortcuts()[0].toString()))
try:
self.view=FreeCADGui.activeDocument().activeView()
self.call=self.view.addEventCallback("SoMouseButtonEvent", self.action) # SoKeyboardEvents replaced by QAction'
except:
FreeCAD.Console.PrintError('No view available.\n')
def action(self,arg):
'Defines functions executed by the callback self.call when "SoMouseButtonEvent"'
# SoKeyboardEvents replaced by QAction':
CtrlAltShift=[arg['CtrlDown'],arg['AltDown'],arg['ShiftDown']]
if arg['Button']=='BUTTON1' and arg['State']=='DOWN': self.mouseActionB1(CtrlAltShift)
elif arg['Button']=='BUTTON2' and arg['State']=='DOWN': self.mouseActionB2(CtrlAltShift)
elif arg['Button']=='BUTTON3' and arg['State']=='DOWN': self.mouseActionB3(CtrlAltShift)
def selectAction(self):
'MUST be redefined in the child class'
print('"selectAction" performed')
pass
def mouseActionB1(self,CtrlAltShift):
'MUST be redefined in the child class'
pass
def mouseActionB2(self,CtrlAltShift):
'MUST be redefined in the child class'
pass
def mouseActionB3(self,CtrlAltShift):
'MUST be redefined in the child class'
pass
def reject(self):
'CAN be redefined to remove other attributes, such as arrow()s or label()s'
self.mw.removeAction(self.actionX)
self.mw.removeAction(self.actionS)
FreeCAD.Console.PrintMessage('Actions "%s" and "%s" removed\n' %(self.actionX.objectName(),self.actionS.objectName()))
try: self.view.removeEventCallback('SoMouseButtonEvent',self.call)
except: pass
FreeCADGui.Control.closeDialog()
if FreeCAD.ActiveDocument: FreeCAD.ActiveDocument.recompute()
class protoPypeForm(QDialog):
'prototype dialog for insert pFeatures'
def __init__(self,winTitle='Title', PType='Pipe', PRating='SCH-STD', icon='dodo.svg',x=100,y=350):
'''
__init__(self,winTitle='Title', PType='Pipe', PRating='SCH-STD')
winTitle: the window's title
PType: the pipeFeature type
PRating: the pipeFeature pressure rating class
It lookups in the directory ./tablez the file PType+"_"+PRating+".csv",
imports it's content in a list of dictionaries -> .pipeDictList and
shows a summary in the QListWidget -> .sizeList
Also create a property -> PRatingsList with the list of available PRatings for the
selected PType.
'''
super(protoPypeForm,self).__init__()
self.move(QPoint(x,y))
self.mw = FreeCADGui.getMainWindow()
self.PType=PType
self.PRating=PRating
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setWindowTitle(winTitle)
iconPath=join(dirname(abspath(__file__)),"iconz",icon)
from PySide.QtGui import QIcon
Icon=QIcon()
Icon.addFile(iconPath)
self.setWindowIcon(Icon)
self.mainHL=QHBoxLayout()
self.setLayout(self.mainHL)
self.firstCol=QWidget()
self.firstCol.setLayout(QVBoxLayout())
self.mainHL.addWidget(self.firstCol)
self.currentRatingLab=QLabel('Rating: '+self.PRating)
self.firstCol.layout().addWidget(self.currentRatingLab)
self.sizeList=QListWidget()
self.firstCol.layout().addWidget(self.sizeList)
self.pipeDictList=[]
self.fileList=listdir(join(dirname(abspath(__file__)),"tablez"))
self.fillSizes()
self.PRatingsList=[s.lstrip(PType+"_").rstrip(".csv") for s in self.fileList if s.startswith(PType) and s.endswith('.csv')]
self.secondCol=QWidget()
self.secondCol.setLayout(QVBoxLayout())
self.combo=QComboBox()
self.combo.addItem('<none>')
try:
self.combo.addItems([o.Label for o in FreeCAD.activeDocument().Objects if hasattr(o,'PType') and o.PType=='PypeLine'])
except:
None
self.combo.currentIndexChanged.connect(self.setCurrentPL)
if FreeCAD.__activePypeLine__ and FreeCAD.__activePypeLine__ in [self.combo.itemText(i) for i in range(self.combo.count())]:
self.combo.setCurrentIndex(self.combo.findText(FreeCAD.__activePypeLine__))
self.secondCol.layout().addWidget(self.combo)
self.ratingList=QListWidget()
self.ratingList.addItems(self.PRatingsList)
self.ratingList.itemClicked.connect(self.changeRating)
self.ratingList.setCurrentRow(0)
self.secondCol.layout().addWidget(self.ratingList)
self.btn1=QPushButton('Insert')
self.secondCol.layout().addWidget(self.btn1)
self.mainHL.addWidget(self.secondCol)
self.resize(max(350,int(self.mw.width()/4)),max(350,int(self.mw.height()/2)))
self.mainHL.setContentsMargins(0,0,0,0)
def setCurrentPL(self,PLName=None):
if self.combo.currentText() not in ['<none>','<new>']:
FreeCAD.__activePypeLine__= self.combo.currentText()
else:
FreeCAD.__activePypeLine__=None
def fillSizes(self):
self.sizeList.clear()
for fileName in self.fileList:
if fileName==self.PType+'_'+self.PRating+'.csv':
f=open(join(dirname(abspath(__file__)),"tablez",fileName),'r')
reader=csv.DictReader(f,delimiter=';')
self.pipeDictList=[DNx for DNx in reader]
f.close()
for row in self.pipeDictList:
s=row['PSize']
if 'OD' in row.keys():
s+=" - "+row['OD']
if 'thk' in row.keys():
s+="x"+row['thk']
self.sizeList.addItem(s)
break
def changeRating(self,item):
self.PRating=item.text()
self.currentRatingLab.setText('Rating: '+self.PRating)
self.fillSizes()
def findDN(self,DN):
result=None
for row in self.pipeDictList:
if row['PSize']==DN:
result=row
break
return result