Skip to content

Commit

Permalink
feature(qchat): auto-reconnect to last room when QGIS starts (#209)
Browse files Browse the repository at this point in the history
* feature(qchat): auto-reconnect to last room when QGIS start
  • Loading branch information
gounux authored and Guts committed Nov 6, 2024
1 parent 0b07179 commit 3b7e9b3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
27 changes: 26 additions & 1 deletion qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ class QChatWidget(QgsDockWidget):

qchat_client = QChatApiClient

def __init__(self, iface: QgisInterface, parent: QWidget = None):
def __init__(
self,
iface: QgisInterface,
parent: QWidget = None,
auto_reconnect_room: str = None,
):
"""QWidget to see and post messages on chat
:param parent: parent widget or application
Expand All @@ -59,6 +64,9 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None):
self.plg_settings = PlgOptionsManager()
uic.loadUi(Path(__file__).parent / f"{Path(__file__).stem}.ui", self)

# set room to autoreconnect to when widget will open
self.auto_reconnect_room = auto_reconnect_room

# rules and status signal listener
self.btn_rules.pressed.connect(self.on_rules_button_clicked)
self.btn_rules.setIcon(QIcon(QgsApplication.iconPath("processingResult.svg")))
Expand Down Expand Up @@ -161,6 +169,10 @@ def on_widget_opened(self) -> None:

self.cbb_room.currentIndexChanged.connect(self.on_room_changed)

# auto reconnect to room if needed
if self.auto_reconnect_room:
self.cbb_room.setCurrentText(self.auto_reconnect_room)

def on_rules_button_clicked(self) -> None:
"""
Action called when clicking on "Rules" button
Expand Down Expand Up @@ -257,6 +269,12 @@ def on_room_changed(self) -> None:
self.connect_to_room(new_room)
self.current_room = new_room

# write new room value to auto-reconnect room in settings if needed
settings = self.settings
if settings.qchat_auto_reconnect:
settings.qchat_auto_reconnect_room = new_room
self.plg_settings.save_from_object(settings)

def on_connect_button_clicked(self) -> None:
"""
Action called when clicking on "Connect" / "Disconnect" button
Expand Down Expand Up @@ -289,6 +307,13 @@ def on_ws_connected(self, room: str) -> None:
self.grb_room.setTitle(self.tr("Room: {room}").format(room=room))
self.grb_user.setEnabled(True)
self.current_room = room

# write new room value to auto-reconnect room in settings if needed
settings = self.settings
if settings.qchat_auto_reconnect:
settings.qchat_auto_reconnect_room = room
self.plg_settings.save_from_object(settings)

self.connected = True
self.log(message=f"Websocket connected to room {room}")
if self.settings.qchat_display_admin_messages:
Expand Down
2 changes: 2 additions & 0 deletions qtribu/gui/dlg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def apply(self):
settings.qchat_instance_uri = instance[0:-1]
else:
settings.qchat_instance_uri = instance
settings.qchat_auto_reconnect = self.ckb_auto_reconnect.isChecked()
settings.qchat_activate_cheatcode = self.ckb_cheatcodes.isChecked()
settings.qchat_display_admin_messages = (
self.ckb_display_admin_messages.isChecked()
Expand Down Expand Up @@ -159,6 +160,7 @@ def load_settings(self) -> None:
self.cbb_qchat_instance_uri.setCurrentIndex(instance_index)
else:
self.cbb_qchat_instance_uri.setCurrentText(settings.qchat_instance_uri)
self.ckb_auto_reconnect.setChecked(settings.qchat_auto_reconnect)
self.ckb_cheatcodes.setChecked(settings.qchat_activate_cheatcode)
self.ckb_display_admin_messages.setChecked(
settings.qchat_display_admin_messages
Expand Down
10 changes: 10 additions & 0 deletions qtribu/gui/dlg_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckb_auto_reconnect">
<property name="toolTip">
<string extracomment="Auto-reconnect the QChat when QGIS starts"/>
</property>
<property name="text">
<string>Automatically reconnect</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
12 changes: 12 additions & 0 deletions qtribu/plugin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ def post_ui_init(self):
)
return

# auto reconnect to room if needed
settings = PlgOptionsManager().get_plg_settings()
if settings.qchat_auto_reconnect and settings.qchat_auto_reconnect_room:
if not self.qchat_widget:
self.qchat_widget = QChatWidget(
iface=self.iface,
parent=self.iface.mainWindow(),
auto_reconnect_room=settings.qchat_auto_reconnect_room,
)
self.iface.addDockWidget(int(Qt.RightDockWidgetArea), self.qchat_widget)
self.qchat_widget.show()

def tr(self, message: str) -> str:
"""Get the translation for a string using Qt translation API.
Expand Down
2 changes: 2 additions & 0 deletions qtribu/toolbelt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class PlgSettingsStructure:

# QChat
qchat_instance_uri: str = "https://gischat.geotribu.net"
qchat_auto_reconnect: bool = True
qchat_auto_reconnect_room: str = None
qchat_activate_cheatcode: bool = True
qchat_display_admin_messages: bool = False
qchat_show_avatars: bool = True
Expand Down

0 comments on commit 3b7e9b3

Please sign in to comment.