Skip to content

Commit fceb043

Browse files
authored
fix: Fix profile export/import dialogs (#761)
1 parent 214320e commit fceb043

3 files changed

Lines changed: 101 additions & 19 deletions

File tree

package/PartSeg/_roi_analysis/profile_export.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import typing
23

34
import numpy as np
45
from qtpy.QtCore import Qt
@@ -77,14 +78,6 @@ def checked_change(self, item):
7778
else:
7879
self.export_btn.setEnabled(True)
7980

80-
def get_checked(self):
81-
res = []
82-
for i in range(self.list_view.count()):
83-
it = self.list_view.item(i)
84-
if it.checkState() == Qt.Checked:
85-
res.append(str(it.text()))
86-
return res
87-
8881
def preview(self):
8982
if self.list_view.currentItem() is None:
9083
return # TODO check this
@@ -122,6 +115,9 @@ def get_export_list(self):
122115
res.append(str(item.text()))
123116
return res
124117

118+
def get_checked(self):
119+
return self.get_export_list()
120+
125121

126122
class ImportDialog(QDialog):
127123
def __init__(self, import_dict, local_dict, viewer, parent=None):
@@ -154,8 +150,8 @@ def in_func():
154150

155151
match = end_reg.match(ob_name)
156152
if match:
157-
new_name_format = match.group(1) + " ({})"
158-
i = int(match.group(2)) + 1
153+
new_name_format = match[1] + " ({})"
154+
i = int(match[2]) + 1
159155
else:
160156
new_name_format = ob_name + " ({})"
161157
i = 1
@@ -168,10 +164,7 @@ def in_func():
168164
def block_import(radio_btn, name_field):
169165
def inner_func():
170166
text = str(name_field.text()).strip()
171-
if text == "" and radio_btn.isChecked():
172-
self.import_btn.setDisabled(True)
173-
else:
174-
self.import_btn.setEnabled(True)
167+
self.import_btn.setDisabled(not text and radio_btn.isChecked())
175168

176169
return inner_func
177170

@@ -267,8 +260,8 @@ def get_import_list(self):
267260
item = self.list_view.topLevelItem(index)
268261
if item.checkState(0) == Qt.Checked:
269262
chk = self.list_view.itemWidget(item, 2)
270-
if chk is not None and chk.isChecked():
271-
res.append((str(item.text(0)), str(self.list_view.itemWidget(item, 3).text())))
263+
if chk is not None and typing.cast(QRadioButton, chk).isChecked():
264+
res.append((item.text(0), typing.cast(QLineEdit, self.list_view.itemWidget(item, 3)).text()))
272265
else:
273266
name = str(item.text(0))
274267
res.append((name, name))
@@ -278,7 +271,6 @@ def uncheck_all(self):
278271
for index in range(self.list_view.topLevelItemCount()):
279272
item = self.list_view.topLevelItem(index)
280273
item.setCheckState(0, Qt.Unchecked)
281-
self.check_state[...] = False
282274
self.import_btn.setDisabled(True)
283275
self.checked_num = 0
284276

@@ -287,7 +279,6 @@ def check_all(self):
287279
item = self.list_view.topLevelItem(index)
288280
item.setCheckState(0, Qt.Checked)
289281
self.checked_num = len(self.import_dict)
290-
self.check_state[...] = True
291282
self.import_btn.setDisabled(False)
292283

293284

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from PartSeg._roi_analysis.profile_export import ExportDialog, ImportDialog, ProfileDictViewer
2+
3+
4+
class TestImportDialog:
5+
def test_create(self, qtbot, lower_threshold_profile, border_rim_profile):
6+
dialog = ImportDialog(
7+
{lower_threshold_profile.name: lower_threshold_profile, border_rim_profile.name: border_rim_profile},
8+
{lower_threshold_profile.name: lower_threshold_profile},
9+
viewer=ProfileDictViewer,
10+
)
11+
qtbot.addWidget(dialog)
12+
13+
def test_check(self, qtbot, lower_threshold_profile, border_rim_profile):
14+
dialog = ImportDialog(
15+
{lower_threshold_profile.name: lower_threshold_profile, border_rim_profile.name: border_rim_profile},
16+
{lower_threshold_profile.name: lower_threshold_profile},
17+
viewer=ProfileDictViewer,
18+
)
19+
qtbot.addWidget(dialog)
20+
assert set(dialog.get_import_list()) == {
21+
(border_rim_profile.name, border_rim_profile.name),
22+
(lower_threshold_profile.name, lower_threshold_profile.name),
23+
}
24+
dialog.uncheck_all()
25+
assert not dialog.get_import_list()
26+
dialog.check_all()
27+
assert set(dialog.get_import_list()) == {
28+
(border_rim_profile.name, border_rim_profile.name),
29+
(lower_threshold_profile.name, lower_threshold_profile.name),
30+
}
31+
32+
def test_preview(self, qtbot, lower_threshold_profile, border_rim_profile):
33+
dialog = ImportDialog(
34+
{lower_threshold_profile.name: lower_threshold_profile, border_rim_profile.name: border_rim_profile},
35+
{lower_threshold_profile.name: lower_threshold_profile},
36+
viewer=ProfileDictViewer,
37+
)
38+
qtbot.addWidget(dialog)
39+
assert dialog.viewer.toPlainText() == ""
40+
dialog.list_view.setCurrentItem(dialog.list_view.topLevelItem(0))
41+
assert dialog.viewer.toPlainText() != ""
42+
dialog.list_view.setCurrentItem(dialog.list_view.topLevelItem(1))
43+
assert dialog.viewer.toPlainText() != ""
44+
45+
def test_rename(self, qtbot, lower_threshold_profile, border_rim_profile):
46+
dialog = ImportDialog(
47+
{lower_threshold_profile.name: lower_threshold_profile, border_rim_profile.name: border_rim_profile},
48+
{lower_threshold_profile.name: lower_threshold_profile, border_rim_profile.name: border_rim_profile},
49+
viewer=ProfileDictViewer,
50+
)
51+
qtbot.addWidget(dialog)
52+
item = dialog.list_view.topLevelItem(0)
53+
assert dialog.list_view.itemWidget(item, 1).isChecked()
54+
dialog.list_view.itemWidget(item, 2).setChecked(True)
55+
assert not dialog.list_view.itemWidget(item, 1).isChecked()
56+
57+
58+
class TestExportDialog:
59+
def test_create(self, qtbot, lower_threshold_profile):
60+
dialog = ExportDialog(
61+
export_dict={lower_threshold_profile.name: lower_threshold_profile}, viewer=ProfileDictViewer
62+
)
63+
qtbot.addWidget(dialog)
64+
65+
def test_check(self, qtbot, lower_threshold_profile, border_rim_profile):
66+
dialog = ExportDialog(
67+
export_dict={
68+
lower_threshold_profile.name: lower_threshold_profile,
69+
border_rim_profile.name: border_rim_profile,
70+
},
71+
viewer=ProfileDictViewer,
72+
)
73+
qtbot.addWidget(dialog)
74+
assert set(dialog.get_checked()) == {lower_threshold_profile.name, border_rim_profile.name}
75+
dialog.uncheck_all()
76+
assert not dialog.get_checked()
77+
dialog.check_all()
78+
assert set(dialog.get_checked()) == {lower_threshold_profile.name, border_rim_profile.name}
79+
80+
def test_preview(self, qtbot, lower_threshold_profile, border_rim_profile):
81+
dialog = ExportDialog(
82+
export_dict={
83+
lower_threshold_profile.name: lower_threshold_profile,
84+
border_rim_profile.name: border_rim_profile,
85+
},
86+
viewer=ProfileDictViewer,
87+
)
88+
qtbot.addWidget(dialog)
89+
assert dialog.viewer.toPlainText() == ""
90+
dialog.list_view.setCurrentRow(0)
91+
assert dialog.viewer.toPlainText() != ""

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ install_requires =
3333
IPython>=7.7.0
3434
PartSegCore-compiled-backend>=0.13.11
3535
PartSegData==0.10.0
36-
QtAwesome>=1.0.3
36+
QtAwesome!=1.2.0,>=1.0.3
3737
QtPy>=1.7.0
3838
SimpleITK>=1.1.0
3939
appdirs>=1.4.4

0 commit comments

Comments
 (0)