Skip to content

Commit

Permalink
Merge branch 'main' into ui/vertically-optimized-widget
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux authored Nov 6, 2024
2 parents c383968 + eb21b4e commit 27c5046
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 34 deletions.
10 changes: 10 additions & 0 deletions qtribu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,13 @@ def local_path(self, base_path: Path = Path().home() / ".geotribu/cdn/") -> Path
CHEATCODE_10OCLOCK,
CHEATCODE_QGIS_PRO_LICENSE,
]

# QChat message types
QCHAT_MESSAGE_TYPE_UNCOMPLIANT = "uncompliant"
QCHAT_MESSAGE_TYPE_TEXT = "text"
QCHAT_MESSAGE_TYPE_IMAGE = "image"
QCHAT_MESSAGE_TYPE_NB_USERS = "nb_users"
QCHAT_MESSAGE_TYPE_NEWCOMER = "newcomer"
QCHAT_MESSAGE_TYPE_EXITER = "exiter"
QCHAT_MESSAGE_TYPE_LIKE = "like"
QCHAT_MESSAGE_TYPE_GEOJSON = "geojson"
58 changes: 32 additions & 26 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
CHEATCODE_IAMAROBOT,
CHEATCODE_QGIS_PRO_LICENSE,
CHEATCODES,
QCHAT_MESSAGE_TYPE_GEOJSON,
QCHAT_MESSAGE_TYPE_IMAGE,
QCHAT_MESSAGE_TYPE_LIKE,
QCHAT_MESSAGE_TYPE_NEWCOMER,
QCHAT_MESSAGE_TYPE_TEXT,
QCHAT_NICKNAME_MINLENGTH,
)
from qtribu.gui.qchat_tree_widget_items import (
Expand Down Expand Up @@ -396,7 +401,7 @@ def on_ws_connected(self, room: str) -> None:
# send newcomer message to websocket
if not self.settings.qchat_incognito_mode:
message = QChatNewcomerMessage(
type="newcomer", newcomer=self.settings.author_nickname
type=QCHAT_MESSAGE_TYPE_NEWCOMER, newcomer=self.settings.author_nickname
)
self.qchat_ws.send_message(message)

Expand Down Expand Up @@ -446,8 +451,8 @@ def on_uncompliant_message_received(self, message: QChatUncompliantMessage) -> N
),
application=self.tr("QChat"),
log_level=Qgis.Critical,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)

def on_text_message_received(self, message: QChatTextMessage) -> None:
Expand Down Expand Up @@ -476,10 +481,8 @@ def on_text_message_received(self, message: QChatTextMessage) -> None:
).format(sender=message.author, message=message.text),
application=self.tr("QChat"),
log_level=Qgis.Info,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager()
.get_plg_settings()
.notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)

# check if a notification sound should be played
Expand Down Expand Up @@ -546,8 +549,8 @@ def on_like_message_received(self, message: QChatLikeMessage) -> None:
),
application=self.tr("QChat"),
log_level=Qgis.Success,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)
# play a notification sound if enabled
if self.settings.qchat_play_sounds:
Expand Down Expand Up @@ -588,7 +591,7 @@ def on_like_message(self, liked_author: str, msg: str) -> None:
This may happen on right-click on a message
"""
message = QChatLikeMessage(
type="like",
type=QCHAT_MESSAGE_TYPE_LIKE,
liker_author=self.settings.author_nickname,
liked_author=liked_author,
message=msg,
Expand Down Expand Up @@ -701,8 +704,8 @@ def on_send_button_clicked(self) -> None:
self.log(
message=self.tr("Nickname not set : please open settings and set it"),
log_level=Qgis.Warning,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
button=True,
button_label=self.tr("Open Settings"),
button_connect=self.on_settings_button_clicked,
Expand All @@ -715,8 +718,8 @@ def on_send_button_clicked(self) -> None:
"Nickname too short : must be at least 3 characters. Please open settings and set it"
),
log_level=Qgis.Warning,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
button=True,
button_label=self.tr("Open Settings"),
button_connect=self.on_settings_button_clicked,
Expand All @@ -728,7 +731,10 @@ def on_send_button_clicked(self) -> None:

# send message to websocket
message = QChatTextMessage(
type="text", author=nickname, avatar=avatar, text=message_text.strip()
type=QCHAT_MESSAGE_TYPE_TEXT,
author=nickname,
avatar=avatar,
text=message_text.strip(),
)
self.qchat_ws.send_message(message)
self.lne_message.setText("")
Expand All @@ -749,7 +755,7 @@ def on_send_image_button_clicked(self) -> None:
with open(fp, "rb") as file:
data = file.read()
message = QChatImageMessage(
type="image",
type=QCHAT_MESSAGE_TYPE_IMAGE,
author=self.settings.author_nickname,
avatar=self.settings.author_avatar,
image_data=base64.b64encode(data).decode("utf-8"),
Expand All @@ -766,7 +772,7 @@ def on_send_screenshot_button_clicked(self) -> None:
with open(sc_fp, "rb") as file:
data = file.read()
message = QChatImageMessage(
type="image",
type=QCHAT_MESSAGE_TYPE_IMAGE,
author=self.settings.author_nickname,
avatar=self.settings.author_avatar,
image_data=base64.b64encode(data).decode("utf-8"),
Expand Down Expand Up @@ -831,8 +837,8 @@ def check_cheatcode(self, text: str) -> bool:
message=self.tr("Your QGIS Pro license is about to expire"),
application=self.tr("QGIS Pro"),
log_level=Qgis.Warning,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
button=True,
button_label=self.tr("Click here to renew it"),
button_connect=self.on_renew_clicked,
Expand Down Expand Up @@ -883,8 +889,8 @@ def on_send_layer_to_qchat(self) -> None:
),
application=self.tr("QChat"),
log_level=Qgis.Critical,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)
return
layer = self.iface.activeLayer()
Expand All @@ -893,17 +899,17 @@ def on_send_layer_to_qchat(self) -> None:
message=self.tr("No active layer in current QGIS project"),
application=self.tr("QChat"),
log_level=Qgis.Critical,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)
return
if layer.type() != QgsMapLayer.VectorLayer:
self.log(
message=self.tr("Only vector layers can be sent on QChat"),
application=self.tr("QChat"),
log_level=Qgis.Critical,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
push=self.settings.notify_push_info,
duration=self.settings.notify_push_duration,
)
return

Expand All @@ -913,7 +919,7 @@ def on_send_layer_to_qchat(self) -> None:
exporter.setTransformGeometries(True)
geojson_str = exporter.exportFeatures(layer.getFeatures())
message = QChatGeojsonMessage(
type="geojson",
type=QCHAT_MESSAGE_TYPE_GEOJSON,
author=self.settings.author_nickname,
avatar=self.settings.author_avatar,
layer_name=layer.name(),
Expand Down
33 changes: 25 additions & 8 deletions qtribu/logic/qchat_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
from json import JSONEncoder

from PyQt5 import QtWebSockets # noqa QGS103
from qgis.core import Qgis
from qgis.PyQt.QtCore import QObject, QUrl, pyqtSignal

from qtribu.constants import (
QCHAT_MESSAGE_TYPE_EXITER,
QCHAT_MESSAGE_TYPE_GEOJSON,
QCHAT_MESSAGE_TYPE_IMAGE,
QCHAT_MESSAGE_TYPE_LIKE,
QCHAT_MESSAGE_TYPE_NB_USERS,
QCHAT_MESSAGE_TYPE_NEWCOMER,
QCHAT_MESSAGE_TYPE_TEXT,
QCHAT_MESSAGE_TYPE_UNCOMPLIANT,
)
from qtribu.logic.qchat_messages import (
QChatExiterMessage,
QChatGeojsonMessage,
Expand Down Expand Up @@ -97,20 +108,26 @@ def on_message_received(self, text: str) -> None:
:param text: text message received, should be a jsonified string
"""
message = json.loads(text)
if "type" not in message:
self.log(
message="No 'type' key in received message. Please make sure your configured instance is running gischat v>=2.0.0",
log_level=Qgis.Critical,
)
return
msg_type = message["type"]
if msg_type == "uncompliant":
if msg_type == QCHAT_MESSAGE_TYPE_UNCOMPLIANT:
self.uncompliant_message_received.emit(QChatUncompliantMessage(**message))
elif msg_type == "text":
elif msg_type == QCHAT_MESSAGE_TYPE_TEXT:
self.text_message_received.emit(QChatTextMessage(**message))
elif msg_type == "image":
elif msg_type == QCHAT_MESSAGE_TYPE_IMAGE:
self.image_message_received.emit(QChatImageMessage(**message))
elif msg_type == "nb_users":
elif msg_type == QCHAT_MESSAGE_TYPE_NB_USERS:
self.nb_users_message_received.emit(QChatNbUsersMessage(**message))
elif msg_type == "newcomer":
elif msg_type == QCHAT_MESSAGE_TYPE_NEWCOMER:
self.newcomer_message_received.emit(QChatNewcomerMessage(**message))
elif msg_type == "exiter":
elif msg_type == QCHAT_MESSAGE_TYPE_EXITER:
self.exiter_message_received.emit(QChatExiterMessage(**message))
elif msg_type == "like":
elif msg_type == QCHAT_MESSAGE_TYPE_LIKE:
self.like_message_received.emit(QChatLikeMessage(**message))
elif msg_type == "geojson":
elif msg_type == QCHAT_MESSAGE_TYPE_GEOJSON:
self.geojson_message_received.emit(QChatGeojsonMessage(**message))

0 comments on commit 27c5046

Please sign in to comment.