From 1d756c6d8aa9426bd0ef2b894a6a7d6a786f9832 Mon Sep 17 00:00:00 2001 From: gounux Date: Wed, 21 Aug 2024 09:14:17 +0200 Subject: [PATCH] fix: fix multiple widget opened signal triggered when moving widget --- qtribu/gui/dck_qchat.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index d2d3a186..229690cc 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -41,6 +41,7 @@ class QChatWidget(QgsDockWidget): + initialized: bool = False connected: bool = False current_room: Optional[str] = None @@ -133,6 +134,11 @@ def on_widget_opened(self) -> None: Action called when the widget is opened """ + # hack to bypass multiple widget opened triggers when moving widget + if self.initialized: + return + self.initialized = True + # fill fields from saved settings self.load_settings() @@ -264,15 +270,10 @@ def on_connect_button_clicked(self) -> None: return self.connect_to_room(room) - def connect_to_room(self, room: str, log: bool = True) -> None: + def connect_to_room(self, room: str) -> None: """ Connect widget to a specific room """ - if log and self.settings.qchat_display_admin_messages: - self.add_admin_message( - room, self.tr("Connected to room '{room}'").format(room=room) - ) - protocol, domain = self.settings.qchat_instance_uri.split("://") ws_protocol = "wss" if protocol == "https" else "ws" ws_instance_url = f"{ws_protocol}://{domain}" @@ -291,6 +292,10 @@ def on_ws_connected(self, room: str) -> None: self.current_room = room self.connected = True self.log(message=f"Websocket connected to room {room}") + if self.settings.qchat_display_admin_messages: + self.add_admin_message( + self.tr("Connected to room '{room}'").format(room=room) + ) def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None: """ @@ -298,7 +303,6 @@ def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None: """ if log and self.settings.qchat_display_admin_messages: self.add_admin_message( - self.current_room, self.tr("Disconnected from room '{room}'").format( room=self.current_room ), @@ -325,7 +329,7 @@ def on_ws_error(self, error_code: int) -> None: Action called when an error appears on the websocket """ if self.settings.qchat_display_admin_messages: - self.add_admin_message(self.tr("ERROR"), self.ws_client.errorString()) + self.add_admin_message(self.ws_client.errorString()) self.log( message=f"{error_code}: {self.ws_client.errorString()}", log_level=Qgis.Critical, @@ -533,7 +537,7 @@ def on_send_button_clicked(self) -> None: self.ws_client.sendTextMessage(json.dumps(message)) self.lne_message.setText("") - def add_admin_message(self, room: str, message: str) -> None: + def add_admin_message(self, message: str) -> None: """ Adds an admin message to QTreeWidget chat """ @@ -582,6 +586,7 @@ def on_widget_closed(self) -> None: if self.connected: self.disconnect_from_room() self.cbb_room.currentIndexChanged.disconnect() + self.initialized = False def check_cheatcode(self, message: dict[str, str]) -> bool: """