This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
macroparameterseditor.py
379 lines (333 loc) · 15.4 KB
/
macroparameterseditor.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
#!/usr/bin/env python
#############################################################################
##
## This file is part of Taurus, a Tango User Interface Library
##
## http://www.tango-controls.org/static/taurus/latest/doc/html/index.html
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus. If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################
"""
macroparameterseditor.py:
"""
import sys, inspect, glob
from taurus.qt import Qt
from taurus.core.util.singleton import Singleton
from taurus.core.tango.sardana import macro
from taurus.qt.qtgui.extra_macroexecutor.macroparameterseditor.delegate import ParamEditorDelegate
from taurus.qt.qtgui.resource import getThemeIcon
class MacroParametersEditor(object):
def __init__(self):
pass
# self._macroModel = None
# def setMacroModel(self, model):
# self._macroModel = model
# self.connect(self._macroModel, Qt.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), self.onDataChanged)
# self.connect(self._macroModel, Qt.SIGNAL("modelReset()"), self.onModelReset)
#
# def onDataChanged(self):
# pass
#
# def onModelReset(self):
# self.onDataChanged()
class StandardMacroParametersEditor(Qt.QWidget, MacroParametersEditor):
def __init__(self, parent=None, macroNode=None):
Qt.QWidget.__init__(self, parent)
self.initComponents()
def initComponents(self):
self.setLayout(Qt.QHBoxLayout())
self.layout().setContentsMargins(0,0,0,0)
self.tree = MacroParametersTree(self)
self.delegate = ParamEditorDelegate(self.tree)
self.tree.setItemDelegate(self.delegate)
self.tree.setSizePolicy(Qt.QSizePolicy.Expanding,Qt.QSizePolicy.Expanding)
self.layout().addWidget(self.tree)
actionLayout = Qt.QVBoxLayout()
actionLayout.setContentsMargins(0,0,0,0)
addButton = Qt.QToolButton()
addButton.setDefaultAction(self.tree.addAction)
actionLayout.addWidget(addButton)
deleteButton = Qt.QToolButton()
deleteButton.setDefaultAction(self.tree.deleteAction)
actionLayout.addWidget(deleteButton)
moveUpButton = Qt.QToolButton()
moveUpButton.setDefaultAction(self.tree.moveUpAction)
actionLayout.addWidget(moveUpButton)
moveDownButton = Qt.QToolButton()
moveDownButton.setDefaultAction(self.tree.moveDownAction)
actionLayout.addWidget(moveDownButton)
spacerItem = Qt.QSpacerItem(0,0,Qt.QSizePolicy.Fixed,Qt.QSizePolicy.Expanding)
actionLayout.addItem(spacerItem)
self.layout().addLayout(actionLayout)
def setModel(self, model):
self.tree.setModel(model)
self.tree.expandAll()
def macroNode(self):
return self.tree.macroNode()
def setMacroNode(self, macroNode):
self.tree.setMacroNode(macroNode)
class MacroParametersTree(Qt.QTreeView):
def __init__(self, parent=None, designMode=False):
Qt.QTreeView.__init__(self, parent)
self.setSelectionBehavior(Qt.QTreeView.SelectItems)
self.setRootIsDecorated(False)
# self.setTabKeyNavigation(True)
self.setEditTriggers(Qt.QAbstractItemView.AllEditTriggers)
self.addAction = Qt.QAction(getThemeIcon("list-add"), "Add new repetition", self)
self.connect(self.addAction, Qt.SIGNAL("triggered()"), self.onAddRepeat)
self.addAction.setToolTip("Clicking this button will add new repetition to current parameter.")
self.deleteAction = Qt.QAction(getThemeIcon("list-remove"), "Remove repetition", self)
self.connect(self.deleteAction, Qt.SIGNAL("triggered()"), self.onDelRepeat)
self.deleteAction.setToolTip("Clicking this button will remove current repetition.")
self.moveUpAction = Qt.QAction(getThemeIcon("go-up"), "Move up", self)
self.connect(self.moveUpAction, Qt.SIGNAL("triggered()"), self.onUpRepeat)
self.moveUpAction.setToolTip("Clicking this button will move current repetition up.")
self.moveDownAction = Qt.QAction(getThemeIcon("go-down"), "Move down", self)
self.connect(self.moveDownAction, Qt.SIGNAL("triggered()"), self.onDownRepeat)
self.moveDownAction.setToolTip("Clicking this button will move current repetition down.")
self.disableActions()
def disableActions(self):
self.addAction.setEnabled(False)
self.deleteAction.setEnabled(False)
self.moveUpAction.setEnabled(False)
self.moveDownAction.setEnabled(False)
def manageActions(self, currentIndex):
self.disableActions()
if currentIndex is None:
return
node = self.model().nodeFromIndex(currentIndex)
if isinstance(node, macro.RepeatNode):
self.deleteAction.setEnabled(not node.parent().isReachedMin())
self.addAction.setEnabled(False)
self.moveUpAction.setEnabled(node.isAllowedMoveUp())
self.moveDownAction.setEnabled(node.isAllowedMoveDown())
elif isinstance(node, macro.RepeatParamNode):
self.addAction.setEnabled(not node.isReachedMax())
self.deleteAction.setEnabled(False)
def currentChanged(self, current, previous):
self.manageActions(current)
Qt.QTreeView.currentChanged(self, current, previous)
#def focusInEvent(self, event):
# reason = event.reason()
# if (reason == Qt.Qt.TabFocusReason) | (reason == Qt.Qt.BacktabFocusReason):
# if reason == Qt.Qt.TabFocusReason:
# idx = self.forwardIdx(0, 1, Qt.QModelIndex())
# elif reason == Qt.Qt.BacktabFocusReason:
# idx = self.backwardIdx(len(self.root()) - 1, 1, Qt.QModelIndex())
# self.setCurrentIndex(idx)
# self.edit(idx)
# else:
# Qt.QTreeView.focusInEvent(self, event)
#
#def forwardIdx(self, row, col, parentIdx):
# try:
# proposalIdx = self.model().index(row, col, parentIdx)
# except AssertionError:
# if parentIdx.row() == -1:
# return Qt.QModelIndex()
# grandParentIdx = parentIdx.parent()
# return self.forwardIdx(parentIdx.row() + 1, col, grandParentIdx)
#
# proposalNode = self.model().nodeFromIndex(proposalIdx)
#
# if isinstance(proposalNode, macro.SingleParamNode):
# return proposalIdx
# elif isinstance(proposalNode, macro.RepeatNode):
# return self.forwardIdx(0, 1, proposalIdx)
# elif isinstance(proposalNode, macro.RepeatParamNode):
# if len(proposalNode) > 0:
# return self.forwardIdx(0, 1, proposalIdx)
# else:
# return self.forwardIdx(row + 1, col, proposalIdx)
# elif not proposalIdx.isValid():
# proposalIdx = parentIdx.sibling(parentIdx.row()+1, 0)
# if proposalIdx.isValid():
# proposalIdx = proposalIdx.child(0,1)
# else:
# while not proposalIdx.isValid():
# parentIdx = parentIdx.parent()
# if not parentIdx.isValid():
# return Qt.QModelIndex()
# proposalIdx = parentIdx.sibling(parentIdx.row()+1, 1)
#
# return proposalIdx
#
# elif isinstance(proposalNode, macro.MacroNode):
# ##self.model().setRoot(proposalNode)
# return self.forwardIdx(0,1,proposalIdx)
#
#def backwardIdx(self, row, col, parentIdx):
# try:
# proposalIdx = self.model().index(row, col, parentIdx)
# except AssertionError:
# if parentIdx.row() == -1:
# return Qt.QModelIndex()
# grandParentIdx = parentIdx.parent()
# return self.backwardIdx(parentIdx.row() - 1, col, grandParentIdx)
# proposalNode = self.model().nodeFromIndex(proposalIdx)
# if isinstance(proposalNode, macro.SingleParamNode):
# return proposalIdx
# elif isinstance(proposalNode, macro.RepeatNode):
# return self.backwardIdx(self.model().rowCount(proposalIdx) - 1, 1, proposalIdx)
# elif isinstance(proposalNode, macro.RepeatParamNode):
# return self.backwardIdx(self.model().rowCount(proposalIdx) - 1, 1, proposalIdx)
#
# elif not proposalIdx.isValid():
# proposalIdx = parentIdx.sibling(parentIdx.row()-1, 0)
# if proposalIdx.isValid():
# tempRow = 0
# proposalIdx = proposalIdx.child(tempRow,1)
# while proposalIdx.sibling(tempRow+1, 1).isValid():
# proposalIdx = proposalIdx.sibling(tempRow+1, 1)
# tempRow +=1
# else:
# while not proposalIdx.isValid():
# parentIdx = parentIdx.parent()
# if not parentIdx.isValid():
# return Qt.QModelIndex()
# proposalIdx = parentIdx.sibling(parentIdx.row()-1, 1)
#
# return proposalIdx
#
#def moveCursor (self, cursorAction, modifiers):
# ix=self.currentIndex()
# self.manageActions(ix)
# (col, row, parentIdx)=(ix.column(), ix.row(), ix.parent())
# #to start from second column
# if col == -1 and row == -1:
# if cursorAction == Qt.QAbstractItemView.MoveNext:
# return self.forwardIdx(0, 1, parentIdx)
# elif cursorAction == Qt.QAbstractItemView.MovePrevious:
# return self.backwardIdx(self.model().rowCount(parentIdx) - 1, 1, parentIdx)
# if (cursorAction == Qt.QAbstractItemView.MoveNext and
# modifiers == Qt.Qt.NoModifier):
# #This condition in case we start tabbing with cursor on first column
# if col == 0:
# currentNode = self.model().nodeFromIndex(ix)
# if isinstance(currentNode, macro.SingleParamNode):
# nextIdx = self.forwardIdx(row, 1, parentIdx)
# else:
# nextIdx = self.forwardIdx(0, 1, ix)
# else:
# nextIdx = self.forwardIdx(row + 1, 1, parentIdx)
# #this condition in case there is no next index and we want to pass focus
# #to next widget in parent obj
#
# if nextIdx == "term":
# self.focusNextPrevChild(True)
# return Qt.QModelIndex()
#
# if not nextIdx.isValid():
# self.parent().focusNextChild()
# #this condition in case the next index is valid and we want to
# #refresh state of buttons
# else:
# self.manageActions(nextIdx)
# return nextIdx
#
# elif (cursorAction == Qt.QAbstractItemView.MovePrevious and
# modifiers == Qt.Qt.NoModifier):
# backwardIdx = self.backwardIdx(row - 1, 1, parentIdx)
# #this contion in case there is no previous index and we want to pass focus
# #to previous widget in parent obj
# if backwardIdx == "term":
# self.focusNextPrevChild(False)
# return Qt.QModelIndex()
#
# if not backwardIdx.isValid():
# self.parent().focusPreviousChild()
# else:
# self.manageActions(backwardIdx)
# return backwardIdx
#
#def expanded(self):
# for column in range(self.model().columnCount(Qt.QModelIndex())):
# self.resizeColumnToContents(column)
def onAddRepeat(self):
index = self.currentIndex()
if isinstance(self.model(), Qt.QSortFilterProxyModel):
sourceIndex = self.model().mapToSource(index)
newSourceIndex = self.model()._insertRow(sourceIndex)
newIndex = self.model().mapFromSource(newSourceIndex)
else:
newIndex = self.model()._insertRow(index)
self.setCurrentIndex(newIndex)
self.expandAll()
def onDelRepeat(self):
index = self.currentIndex()
if isinstance(self.model(), Qt.QSortFilterProxyModel):
index = self.model().mapToSource(index)
self.model()._removeRow(index)
self.expandAll()
def onUpRepeat(self):
index = self.currentIndex()
if isinstance(self.model(), Qt.QSortFilterProxyModel):
sourceIndex = self.model().mapToSource(index)
newSourceIndex = self.model()._upRow(sourceIndex)
newIndex = self.model().mapFromSource(newSourceIndex)
else:
newIndex = self.model()._upRow(index)
self.setCurrentIndex(newIndex)
self.expandAll()
def onDownRepeat(self):
index = self.currentIndex()
if isinstance(self.model(), Qt.QSortFilterProxyModel):
sourceIndex = self.model().mapToSource(index)
newSourceIndex = self.model()._downRow(sourceIndex)
newIndex = self.model().mapFromSource(newSourceIndex)
else:
newIndex = self.model()._downRow(index)
self.setCurrentIndex(newIndex)
self.expandAll()
class ParamEditorManager(Singleton):
def init(self):
self._paths = []
self._macroEditorsDict = {}
def paths(self):
return self._paths
def setPaths(self, paths):
self._paths = paths
def appendPath(self, path):
self._paths.append()
def parsePaths(self, pathsString):
self.setPaths(pathsString.split(":"))
def browsePaths(self):
for path in self.paths():
modulePaths = glob.glob("%s/*.py" % path)
if not modulePaths:
continue
sys.path.insert(0,path)
for modulePath in modulePaths:
if modulePath.endswith("__init__.py"):
continue
modulePathAsArray = modulePath.split("/")
moduleFileName = modulePathAsArray[-1]
moduleName = moduleFileName[:-3]
try:
module = __import__(moduleName)
except ImportError:
continue
klass = getattr(module,"CUSTOM_EDITOR", None)
# for name, klass in inspect.getmembers(module, inspect.isclass):
# if issubclass(klass, MacroParametersEditor):
self._macroEditorsDict[moduleName] = klass
def getMacroEditor(self, macroName=None, parent=None):
editorClass = self._macroEditorsDict.get(macroName, None)
try:
return editorClass(parent=parent)
except:
return None