forked from BhallaLab/moose-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msearch.py
202 lines (180 loc) · 7.32 KB
/
msearch.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
# msearch.py ---
#
# Filename: msearch.py
# Description:
# Author:
# Maintainer:
# Created: Tue Jun 4 15:10:16 2013 (+0530)
# Version:
# Last-Updated: Thu Jun 6 18:26:02 2013 (+0530)
# By: subha
# Update #: 181
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
"""
Widget giving access to wildcardFind function in MOOSE
"""
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import Qt
import moose
class SearchWidget(QtGui.QWidget):
"""Widget to search MOOSE model tree using wildcards.
SIGNALS:
executed(list of elements matching search criterion).
"""
executed = QtCore.pyqtSignal(list, name='executed')
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
layout = QtGui.QVBoxLayout()
self.modeCombo = QtGui.QComboBox()
self.modeCombo.addItem('Basic', QtCore.QVariant(QtCore.QString('basic')))
self.modeCombo.addItem('Advanced', QtCore.QVariant(QtCore.QString('advanced')))
self.modeCombo.setCurrentIndex(0)
self.setLayout(layout)
layout.addWidget(self.modeCombo)
layout.addWidget(self.getBasicPanel())
layout.addWidget(self.getAdvancedPanel())
self.connect(self.modeCombo, QtCore.SIGNAL('currentIndexChanged(const QString &)'), self.switchModeSlot)
self.switchModeSlot('basic')
self.searchButton = QtGui.QPushButton('Search')
self.searchButton.clicked.connect(self.searchSlot)
layout.addWidget(self.searchButton)
self.__results = []
def switchModeSlot(self, text):
basic = (str(text).lower() == 'basic')
self.getBasicPanel().setVisible(basic)
self.getAdvancedPanel().setVisible(not basic)
def getBasicPanel(self):
"""Create a widget for basic level usage."""
if not hasattr(self, '_basicPanel'):
self._basicPanel = QtGui.QWidget()
self.searchRootEdit = QtGui.QLineEdit()
self.searchRootEdit.returnPressed.connect(self.searchSlot)
self.searchRootLabel = QtGui.QLabel('Search under')
self.typeEdit = QtGui.QLineEdit()
self.typeEdit.returnPressed.connect(self.searchSlot)
self.typeLabel = QtGui.QLabel('for elements of type')
self.fieldEdit = QtGui.QLineEdit()
self.fieldEdit.returnPressed.connect(self.searchSlot)
self.fieldLabel = QtGui.QLabel('or elements with field')
self.comparisonCombo = QtGui.QComboBox()
self.comparisonCombo.addItem('=', QtCore.QVariant(QtCore.QString('=')))
self.comparisonCombo.addItem('<', QtCore.QVariant(QtCore.QString('<')))
self.comparisonCombo.addItem('>', QtCore.QVariant(QtCore.QString('>')))
self.comparisonCombo.addItem('<=', QtCore.QVariant(QtCore.QString('<=')))
self.comparisonCombo.addItem('>=', QtCore.QVariant(QtCore.QString('>=')))
self.comparisonCombo.addItem('!=', QtCore.QVariant(QtCore.QString('!=')))
self.valueEdit = QtGui.QLineEdit()
self.valueEdit.returnPressed.connect(self.searchSlot)
self.recurseButton = QtGui.QRadioButton('Search inside children recursively')
self.recurseButton.setChecked(True)
layout = QtGui.QGridLayout()
# layout.setVerticalSpacing(2)
self._basicPanel.setLayout(layout)
layout.addWidget(self.searchRootLabel, 0, 0, 1, 1)
layout.addWidget(self.searchRootEdit, 0, 1, 1, 4)
layout.addWidget(self.typeLabel, 1, 0, 1, 1)
layout.addWidget(self.typeEdit, 1, 1, 1, 4)
layout.addWidget(self.fieldLabel, 2, 0, 1, 1)
layout.addWidget(self.fieldEdit, 2, 1, 1, 1)
layout.addWidget(self.comparisonCombo, 2, 2, 1, 1)
layout.addWidget(self.valueEdit, 2, 3, 1, 2)
layout.addWidget(self.recurseButton, 3, 0, 1, 1)
# layout.addItem(QtGui.QSpacerItem(1,1), 3, 0, 1, 7)
return self._basicPanel
def getAdvancedPanel(self):
"""Create a panel for advanced users who can enter the search string
directly."""
if not hasattr(self, '_advancedPanel'):
self._advancedPanel = QtGui.QWidget(self)
self._searchEdit = QtGui.QLineEdit()
self._searchEdit.returnPressed.connect(self.searchSlot)
self._searchLabel = QtGui.QLabel('Search string:')
layout = QtGui.QHBoxLayout()
self._advancedPanel.setLayout(layout)
layout.addWidget(self._searchLabel)
layout.addWidget(self._searchEdit)
return self._advancedPanel
def searchSlot(self):
"""Do the search and store the paths of the id's found. The results
can be obtained later with getResults()"""
search_str = ''
if str(self.modeCombo.currentText()).lower() == 'basic':
root = str(self.searchRootEdit.text()).strip()
if len(root) == 0:
root = '/'
search_str += root
if not search_str.endswith('/'):
search_str += '/'
if self.recurseButton.isChecked():
search_str += '##'
else:
search_str += '#'
type_ = str(self.typeEdit.text()).strip()
if len(type_) > 0:
search_str += '[ISA=%s]' % (type_)
field = str(self.fieldEdit.text()).strip()
if len(field) > 0:
search_str += '[FIELD(%s)'
operator = str(self.comparisonCombo.currentText())
value = str(self.valueEdit.text())
search_str += '%s%s]' % (operator, value)
else:
search_str = str(self._searchEdit.text()).strip()
ids = moose.wildcardFind(search_str)
self.__results = [el.path for el in ids]
self.executed.emit(list(self.__results))
def getResults(self):
return list(self.__results)
def setSearchRoot(self, root):
self.searchRootEdit.setText(moose.element(root).path)
def main():
"""Test main"""
model = moose.Neutral('/model')
moose.loadModel('../Demos/Genesis_files/Kholodenko.g', '/model/Kholodenko')
# tab = moose.element('/model/Kholodenko/graphs/conc1/MAPK_PP.Co')
# print tab
# for t in tab.children:
# print t
app = QtGui.QApplication(sys.argv)
mainwin = QtGui.QMainWindow()
mainwin.setWindowTitle('Model tree test')
wildcardWidget = SearchWidget()
mainwin.setCentralWidget(wildcardWidget)
mainwin.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
#
# msearch.py ends here