Skip to content

Commit 3fbba16

Browse files
committed
GetObjectDialog: new panel argument
1 parent 1019d64 commit 3fbba16

File tree

4 files changed

+156
-43
lines changed

4 files changed

+156
-43
lines changed

cdlclient/tests/get_object_dialog.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ def test_dialog():
2020
proxy = SimpleRemoteProxy()
2121
proxy.connect()
2222
with qt_app_context():
23+
# 1. Select an image or signal object
2324
dlg = GetObjectDialog(None, proxy)
2425
if dlg.exec():
25-
obj_uuid = dlg.get_current_object_uuid()
26-
obj = proxy.get_object(obj_uuid)
26+
obj = proxy.get_object(dlg.get_current_object_uuid())
27+
print(str(obj))
28+
# 2. Select a signal object only
29+
dlg = GetObjectDialog(None, proxy, panel="signal")
30+
if dlg.exec():
31+
obj = proxy.get_object(dlg.get_current_object_uuid())
32+
print(str(obj))
33+
# 3. Select an image object only
34+
dlg = GetObjectDialog(None, proxy, panel="image")
35+
if dlg.exec():
36+
obj = proxy.get_object(dlg.get_current_object_uuid())
2737
print(str(obj))
2838

2939

cdlclient/widgets/connection.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from qtpy import QtWidgets as QW
2323

2424
from cdlclient.config import _
25-
from cdlclient.widgets import datalab_banner
25+
from cdlclient.qthelpers import svgtext_to_icon
26+
from cdlclient.widgets import datalab_banner, svg_icons
2627

2728

2829
class ConnectionThread(QC.QThread):
@@ -56,6 +57,7 @@ def __init__(self, connect_callback: Callable, parent: QW.QWidget = None) -> Non
5657
super().__init__(parent)
5758
win32_fix_title_bar_background(self)
5859
self.setWindowTitle(_("Connection to DataLab"))
60+
self.setWindowIcon(svgtext_to_icon(svg_icons.DATALAB))
5961
self.resize(300, 50)
6062
self.host_label = QW.QLabel()
6163
pixmap = QG.QPixmap()
@@ -79,44 +81,44 @@ def __init__(self, connect_callback: Callable, parent: QW.QWidget = None) -> Non
7981
layout.addWidget(status)
8082
self.setLayout(layout)
8183
self.thread = ConnectionThread(connect_callback)
82-
self.thread.SIG_CONNECTION_OK.connect(self.on_connection_successful)
83-
self.thread.SIG_CONNECTION_KO.connect(self.on_connection_failed)
84+
self.thread.SIG_CONNECTION_OK.connect(self.__on_connection_successful)
85+
self.thread.SIG_CONNECTION_KO.connect(self.__on_connection_failed)
8486
button_box = QW.QDialogButtonBox(QW.QDialogButtonBox.Cancel)
8587
button_box.rejected.connect(self.reject)
8688
layout.addWidget(button_box)
8789

88-
def set_status_icon(self, name: str) -> None:
90+
def __set_status_icon(self, name: str) -> None:
8991
"""Set status icon with standard Qt icon name"""
9092
self.status_icon.setPixmap(QG.QPixmap(get_std_icon(name).pixmap(24)))
9193

92-
def exec(self) -> int:
93-
"""Execute dialog"""
94-
self.connect_to_server()
95-
return super().exec()
96-
97-
def connect_to_server(self) -> None:
94+
def __connect_to_server(self) -> None:
9895
"""Connect to server"""
9996
self.progress_bar.setRange(0, 0)
100-
self.set_status_icon("BrowserReload")
97+
self.__set_status_icon("BrowserReload")
10198
self.status_label.setText(_("Connecting to server..."))
10299
self.thread.start()
103100

104-
def on_connection_successful(self) -> None:
101+
def __on_connection_successful(self) -> None:
105102
"""Connection successful"""
106103
self.progress_bar.setRange(0, 1)
107104
self.progress_bar.setValue(1)
108-
self.set_status_icon("DialogApplyButton")
105+
self.__set_status_icon("DialogApplyButton")
109106
self.status_label.setText(_("Connection successful!"))
110107
QC.QTimer.singleShot(1000, self.accept)
111108

112-
def on_connection_failed(self) -> None:
109+
def __on_connection_failed(self) -> None:
113110
"""Connection failed"""
114111
self.progress_bar.setRange(0, 1)
115112
self.progress_bar.setValue(1)
116-
self.set_status_icon("MessageBoxCritical")
113+
self.__set_status_icon("MessageBoxCritical")
117114
self.status_label.setText(_("Connection failed."))
118115
QC.QTimer.singleShot(2000, self.reject)
119116

117+
def exec(self) -> int:
118+
"""Execute dialog"""
119+
self.__connect_to_server()
120+
return super().exec()
121+
120122

121123
if __name__ == "__main__":
122124

cdlclient/widgets/objectdialog.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,28 @@ def contextMenuEvent(
160160

161161

162162
class GetObjectDialog(QW.QDialog):
163-
"""Get object dialog box"""
163+
"""Get object dialog box
164+
165+
Args:
166+
parent: Parent widget
167+
proxy: Remote proxy
168+
panel: Panel to retrieve objects from ('signal', 'image' or None for both)
169+
title: Dialog title
170+
"""
164171

165172
def __init__(
166173
self,
167174
parent: QW.QWidget,
168175
proxy: SimpleRemoteProxy,
176+
panel: str | None = None,
169177
title: str | None = None,
170178
) -> None:
171179
super().__init__(parent)
180+
assert panel in (None, "signal", "image")
172181
self.__proxy = proxy
173182
self.__current_object_uuid: str | None = None
174183
self.setWindowTitle(_("Select object") if title is None else title)
184+
self.setWindowIcon(svgtext_to_icon(svg_icons.DATALAB))
175185
vlayout = QW.QVBoxLayout()
176186
self.setLayout(vlayout)
177187

@@ -181,31 +191,37 @@ def __init__(
181191
logo_label.setPixmap(pixmap)
182192
logo_label.setAlignment(QC.Qt.AlignCenter)
183193

184-
panelgroup = QW.QWidget()
185-
panellayout = QW.QHBoxLayout()
186-
panellayout.setContentsMargins(0, 0, 0, 0)
187-
# panellayout.setAlignment(QC.Qt.AlignCenter)
188-
panelgroup.setLayout(panellayout)
189-
190-
panelcombo = QW.QComboBox()
191-
panelcombo.addItem(svgtext_to_icon(svg_icons.SIGNAL), _("Signals"))
192-
panelcombo.addItem(svgtext_to_icon(svg_icons.IMAGE), _("Images"))
193-
panelcombo.setCurrentIndex(0 if proxy.get_current_panel() == "signal" else 1)
194-
panelcombo.currentIndexChanged.connect(self.__change_panel)
195-
196-
panellabel = QW.QLabel(_("Active panel:"))
197-
panellayout.addWidget(panellabel)
198-
panellayout.addWidget(panelcombo)
199-
panellayout.setStretch(1, 1)
194+
panelgroup = None
195+
if panel is None:
196+
panelgroup = QW.QWidget()
197+
panellayout = QW.QHBoxLayout()
198+
panellayout.setContentsMargins(0, 0, 0, 0)
199+
# panellayout.setAlignment(QC.Qt.AlignCenter)
200+
panelgroup.setLayout(panellayout)
201+
202+
panelcombo = QW.QComboBox()
203+
panelcombo.addItem(svgtext_to_icon(svg_icons.SIGNAL), _("Signals"))
204+
panelcombo.addItem(svgtext_to_icon(svg_icons.IMAGE), _("Images"))
205+
if proxy.get_current_panel() == "image":
206+
panelcombo.setCurrentIndex(1)
207+
panelcombo.currentIndexChanged.connect(self.__change_panel)
208+
209+
panellabel = QW.QLabel(_("Active panel:"))
210+
panellayout.addWidget(panellabel)
211+
panellayout.addWidget(panelcombo)
212+
panellayout.setStretch(1, 1)
213+
else:
214+
self.__proxy.set_current_panel(panel)
200215

201216
self.tree = SimpleObjectTree(parent)
202217
self.tree.init_from(proxy)
203218
self.tree.SIG_ITEM_DOUBLECLICKED.connect(lambda oid: self.accept())
204-
self.tree.itemSelectionChanged.connect(self.current_object_changed)
219+
self.tree.itemSelectionChanged.connect(self.__current_object_changed)
205220

206221
vlayout.addWidget(logo_label)
207222
vlayout.addSpacing(10)
208-
vlayout.addWidget(panelgroup)
223+
if panelgroup is not None:
224+
vlayout.addWidget(panelgroup)
209225
vlayout.addWidget(self.tree)
210226

211227
bbox = QW.QDialogButtonBox(QW.QDialogButtonBox.Ok | QW.QDialogButtonBox.Cancel)
@@ -215,21 +231,21 @@ def __init__(
215231
vlayout.addSpacing(10)
216232
vlayout.addWidget(bbox)
217233
# Update OK button state:
218-
self.current_object_changed()
234+
self.__current_object_changed()
219235

220236
def __change_panel(self, index: int) -> None:
221237
"""Change panel"""
222238
self.__proxy.set_current_panel("signal" if index == 0 else "image")
223239
self.tree.init_from(self.__proxy)
224-
self.current_object_changed()
225-
226-
def get_current_object_uuid(self) -> str:
227-
"""Return current object uuid"""
228-
return self.__current_object_uuid
240+
self.__current_object_changed()
229241

230-
def current_object_changed(self) -> None:
242+
def __current_object_changed(self) -> None:
231243
"""Item selection has changed"""
232244
self.__current_object_uuid = self.tree.get_current_item_id()
233245
self.ok_btn.setEnabled(bool(self.__current_object_uuid))
234246
self.ok_btn.setEnabled(bool(self.__current_object_uuid))
235247
self.ok_btn.setEnabled(bool(self.__current_object_uuid))
248+
249+
def get_current_object_uuid(self) -> str:
250+
"""Return current object uuid"""
251+
return self.__current_object_uuid

0 commit comments

Comments
 (0)