Skip to content

Commit

Permalink
fix: fix multiple widget opened signal triggered when moving widget
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Aug 21, 2024
1 parent ac0191d commit 1d756c6
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

class QChatWidget(QgsDockWidget):

initialized: bool = False
connected: bool = False
current_room: Optional[str] = None

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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}"
Expand All @@ -291,14 +292,17 @@ 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:
"""
Disconnect widget from the current room
"""
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
),
Expand All @@ -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,
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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:
"""
Expand Down

0 comments on commit 1d756c6

Please sign in to comment.