Skip to content

Commit

Permalink
init stuff on widget open
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 21, 2024
1 parent 4089ebe commit b9314ac
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions qtribu/gui/wdg_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@

# plugin
from qtribu.toolbelt import PlgLogger, PlgOptionsManager
from qtribu.toolbelt.preferences import PlgSettingsStructure

# -- GLOBALS --
MARKER_VALUE = "---"
DISPLAY_DATE_FORMAT = "%H:%M:%S"


class QChatWidget(QgsDockWidget):

connected: bool = False
current_room: str = None

settings: PlgSettingsStructure
qchat_client = QChatApiClient

def __init__(self, iface: QgisInterface, parent: QWidget = None):
"""QWidget to see and post messages on chat
Expand All @@ -40,27 +48,13 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None):
self.plg_settings = PlgOptionsManager()
uic.loadUi(Path(__file__).parent / f"{Path(__file__).stem}.ui", self)

# fill fields from saved settings
self.settings = self.plg_settings.get_plg_settings()
self.load_settings()

# initialize QChat API client
self.qchat_client = QChatApiClient(self.settings.qchat_instance_uri)

# status signal listener
self.btn_status.pressed.connect(self.on_status_button_clicked)
self.btn_status.setIcon(QIcon(QgsApplication.iconPath("mIconInfo.svg")))

# load rooms
self.cb_room.addItem(MARKER_VALUE)
try:
rooms = self.qchat_client.get_rooms()
for room in rooms:
self.cb_room.addItem(room)
except Exception as exc:
self.iface.messageBar().pushCritical(self.tr("QChat error"), str(exc))
finally:
self.current_room = MARKER_VALUE
# widget opened / closed signals
self.opened.connect(self.on_widget_opened)
self.closed.connect(self.on_widget_closed)

self.cb_room.currentIndexChanged.connect(self.on_room_changed)

Expand Down Expand Up @@ -98,9 +92,6 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None):
QIcon(QgsApplication.iconPath("mActionDoubleArrowRight.svg"))
)

# widget closed signal
self.closed.connect(self.on_widget_closed)

def load_settings(self) -> None:
"""Load options from QgsSettings into UI form."""
self.lb_instance.setText(self.settings.qchat_instance_uri)
Expand All @@ -111,6 +102,32 @@ def save_settings(self) -> None:
self.settings.qchat_nickname = self.le_nickname.text()
self.plg_settings.save_from_object(self.settings)

def on_widget_opened(self) -> None:
"""
Action called when the widget is opened
"""

# fill fields from saved settings
self.settings = self.plg_settings.get_plg_settings()
self.load_settings()

# initialize QChat API client
self.qchat_client = QChatApiClient(self.settings.qchat_instance_uri)

# clear rooms combobox items
self.cb_room.clear() # delete all items from comboBox

# load rooms
self.cb_room.addItem(MARKER_VALUE)
try:
rooms = self.qchat_client.get_rooms()
for room in rooms:
self.cb_room.addItem(room)
except Exception as exc:
self.iface.messageBar().pushCritical(self.tr("QChat error"), str(exc))
finally:
self.current_room = MARKER_VALUE

def on_status_button_clicked(self) -> None:
"""
Action called when clicking on "Status" button
Expand Down Expand Up @@ -178,7 +195,9 @@ def connect_to_room(self, room: str, log: bool = True) -> None:
),
)

ws_instance_url = "ws://" + self.settings.qchat_instance_uri.split("://")[-1]
protocol, domain = self.settings.qchat_instance_uri.split("://")
ws_protocol = "wss" if protocol == "https" else "ws"
ws_instance_url = f"{ws_protocol}://{domain}"
ws_url = f"{ws_instance_url}/room/{room}/ws"
self.ws_client.open(QUrl(ws_url))
self.ws_client.connected.connect(partial(self.on_ws_connected, room))
Expand Down

0 comments on commit b9314ac

Please sign in to comment.