Skip to content

Commit 54f22fb

Browse files
committed
ENH: added dialog for retrieving simple textual info from the user
1 parent 35a9271 commit 54f22fb

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Dialog</class>
4+
<widget class="QDialog" name="Dialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>300</width>
10+
<height>94</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout_2">
17+
<item row="0" column="0">
18+
<layout class="QGridLayout" name="gridLayout">
19+
<item row="0" column="0">
20+
<widget class="QLabel" name="label">
21+
<property name="sizePolicy">
22+
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
23+
<horstretch>0</horstretch>
24+
<verstretch>0</verstretch>
25+
</sizepolicy>
26+
</property>
27+
<property name="text">
28+
<string>TextLabel</string>
29+
</property>
30+
<property name="alignment">
31+
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
32+
</property>
33+
</widget>
34+
</item>
35+
<item row="0" column="1">
36+
<widget class="QLineEdit" name="lineEdit">
37+
<property name="sizePolicy">
38+
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
39+
<horstretch>0</horstretch>
40+
<verstretch>0</verstretch>
41+
</sizepolicy>
42+
</property>
43+
</widget>
44+
</item>
45+
<item row="1" column="0" colspan="2">
46+
<widget class="QDialogButtonBox" name="buttonBox">
47+
<property name="orientation">
48+
<enum>Qt::Horizontal</enum>
49+
</property>
50+
<property name="standardButtons">
51+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
52+
</property>
53+
</widget>
54+
</item>
55+
</layout>
56+
</item>
57+
</layout>
58+
</widget>
59+
<resources/>
60+
<connections>
61+
<connection>
62+
<sender>buttonBox</sender>
63+
<signal>accepted()</signal>
64+
<receiver>Dialog</receiver>
65+
<slot>accept()</slot>
66+
<hints>
67+
<hint type="sourcelabel">
68+
<x>248</x>
69+
<y>254</y>
70+
</hint>
71+
<hint type="destinationlabel">
72+
<x>157</x>
73+
<y>274</y>
74+
</hint>
75+
</hints>
76+
</connection>
77+
<connection>
78+
<sender>buttonBox</sender>
79+
<signal>rejected()</signal>
80+
<receiver>Dialog</receiver>
81+
<slot>reject()</slot>
82+
<hints>
83+
<hint type="sourcelabel">
84+
<x>316</x>
85+
<y>260</y>
86+
</hint>
87+
<hint type="destinationlabel">
88+
<x>286</x>
89+
<y>274</y>
90+
</hint>
91+
</hints>
92+
</connection>
93+
</connections>
94+
</ui>

SlicerDevelopmentToolboxUtils/widgets.py

+41
Original file line numberDiff line numberDiff line change
@@ -1965,3 +1965,44 @@ def _disconnectButton(self, button):
19651965
def _onOptionSelected(self, value):
19661966
self.selectedOption = value
19671967
self.button(qt.QMessageBox.Ok).enabled = True
1968+
1969+
1970+
class TextInformationRequestDialog(qt.QDialog):
1971+
1972+
def __init__(self, attributeName, parent=None):
1973+
qt.QDialog.__init__(self, parent)
1974+
self._attributeName = attributeName
1975+
self.setup()
1976+
self._setupConnections()
1977+
1978+
def setup(self):
1979+
self.setLayout(qt.QGridLayout())
1980+
self._loadUI()
1981+
self.layout().addWidget(self.ui)
1982+
1983+
def _loadUI(self):
1984+
import inspect
1985+
modulePath = os.path.dirname(os.path.normpath(os.path.dirname(inspect.getfile(self.__class__))))
1986+
modulePath = modulePath.replace("SlicerDevelopmentToolboxUtils", "")
1987+
path = os.path.join(modulePath, 'Resources', 'UI', 'TextInformationRequestDialog.ui')
1988+
self.ui = slicer.util.loadUI(path)
1989+
self._lineEdit = self.ui.findChild(qt.QLineEdit, "lineEdit")
1990+
self._label = self.ui.findChild(qt.QLabel, "label")
1991+
self._label.setText(self._attributeName)
1992+
self._buttonBox = self.ui.findChild(qt.QDialogButtonBox, "buttonBox")
1993+
1994+
def _setupConnections(self):
1995+
def setupConnections(funcName="connect"):
1996+
getattr(self._buttonBox.accepted, funcName)(self.accept)
1997+
getattr(self._buttonBox.rejected, funcName)(self.reject)
1998+
getattr(self._lineEdit.textChanged, funcName)(self._onTextChanged)
1999+
2000+
setupConnections()
2001+
slicer.app.connect('aboutToQuit()', self.deleteLater)
2002+
self.destroyed.connect(lambda : setupConnections(funcName="disconnect"))
2003+
2004+
def _onTextChanged(self, text):
2005+
self._buttonBox.button(qt.QDialogButtonBox.Ok).enabled = bool(text.strip())
2006+
2007+
def getValue(self):
2008+
return self._lineEdit.text

0 commit comments

Comments
 (0)