Skip to content

Commit

Permalink
refactor(qchat): handle custom context menu actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Oct 26, 2024
1 parent 2b57e76 commit e12d92b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 32 deletions.
45 changes: 13 additions & 32 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,6 @@ def on_message_clicked(self, item: QTreeWidgetItem, column: int) -> None:
Action called when clicking on a chat message
"""
item.on_click(column)
if type(item) is QChatImageTreeWidgetItem:
QMessageBox.warning(self, "lol", "biglol")

def on_message_double_clicked(self, item: QTreeWidgetItem, column: int) -> None:
"""
Expand All @@ -541,11 +539,6 @@ def on_message_double_clicked(self, item: QTreeWidgetItem, column: int) -> None:
self.lne_message.setText(f"{text}@{author} ")
self.lne_message.setFocus()

def mention_user(self, user: str) -> None:
text = self.lne_message.text()
self.lne_message.setText(f"{text}@{user} ")
self.lne_message.setFocus()

def on_like_message(self, liked_author: str, msg: str) -> None:
"""
Action called when the "Like message" action is triggered
Expand All @@ -564,26 +557,22 @@ def on_custom_context_menu_requested(self, point: QPoint) -> None:
Action called when right clicking on a chat message
"""
item = self.twg_chat.itemAt(point)
author = item.text(1)
message = item.text(2)

menu = QMenu(self.tr("QChat Menu"), self)

if (
author != self.settings.author_nickname
and author != ADMIN_MESSAGES_NICKNAME
):
# like message action
# like message action if possible
if item.can_be_liked:
like_action = QAction(
QgsApplication.getThemeIcon("mActionInOverview.svg"),
self.tr("Like message"),
)
like_action.triggered.connect(
partial(self.on_like_message, author, message)
partial(self.on_like_message, item.author, item.liked_message)
)
menu.addAction(like_action)

# mention user action
# mention author action if possible
if item.can_be_mentioned:
mention_action = QAction(
QgsApplication.getThemeIcon("mMessageLogRead.svg"),
self.tr("Mention user"),
Expand All @@ -592,17 +581,15 @@ def on_custom_context_menu_requested(self, point: QPoint) -> None:
partial(self.on_message_double_clicked, item, 2)
)
menu.addAction(mention_action)
menu.addSeparator()

# copy message to clipboard action
copy_action = QAction(
QgsApplication.getThemeIcon("mActionEditCopy.svg"),
self.tr("Copy message to clipboard"),
)
copy_action.triggered.connect(
partial(self.on_copy_message_to_clipboard, message)
)
menu.addAction(copy_action)
# copy message to clipboard action if possible
if item.can_be_copied_to_clipboard:
copy_action = QAction(
QgsApplication.getThemeIcon("mActionEditCopy.svg"),
self.tr("Copy message to clipboard"),
)
copy_action.triggered.connect(item.copy_to_clipboard)
menu.addAction(copy_action)

# hide message action
hide_action = QAction(
Expand All @@ -614,12 +601,6 @@ def on_custom_context_menu_requested(self, point: QPoint) -> None:

menu.exec(QCursor.pos())

def on_copy_message_to_clipboard(self, message: str) -> None:
"""
Action called when copy to clipboard menu action is triggered
"""
QgsApplication.instance().clipboard().setText(message)

def on_hide_message(self, item: QTreeWidgetItem) -> None:
"""
Action called when hide message menu action is triggered
Expand Down
66 changes: 66 additions & 0 deletions qtribu/gui/qchat_tree_widget_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ def on_click(self, column: int) -> None:
"""
Triggered when simple clicking on the item
Empty because this is the expected behaviour
:param column: column that has been clicked
"""
pass

@property
def can_be_liked(self) -> bool:
"""
Returns if the item can be liked
"""
return self.author != self.settings.author_nickname

@property
def liked_message(self) -> str:
"""
Returns the text message that was liked
"""
pass

@property
def can_be_mentioned(self) -> bool:
"""
Returns if the item can be mentioned
"""
return self.author != self.settings.author_nickname

@property
def can_be_copied_to_clipboard(self) -> bool:
"""
Returns if the item can be copied to clipboard
"""
return False

def copy_to_clipboard(self) -> None:
"""
Performs action of copying message to clipboard
If the can_be_copied_to_clipboard is enabled ofc
"""
pass

Expand All @@ -73,6 +109,14 @@ def __init__(self, parent: QTreeWidget, text: str):
self.setToolTip(MESSAGE_COLUMN, text)
self.set_foreground_color(self.settings.qchat_color_admin)

@property
def can_be_liked(self) -> bool:
return False

@property
def can_be_mentioned(self) -> bool:
return False


class QChatTextTreeWidgetItem(QChatTreeWidgetItem):
def __init__(self, parent: QTreeWidget, message: QChatTextMessage):
Expand All @@ -90,6 +134,17 @@ def __init__(self, parent: QTreeWidget, message: QChatTextMessage):
if message.author == self.settings.author_nickname:
self.set_foreground_color(self.settings.qchat_color_self)

@property
def liked_message(self) -> str:
return self.message.text

@property
def can_be_copied_to_clipboard(self) -> bool:
return True

def copy_to_clipboard(self) -> None:
QgsApplication.instance().clipboard().setPixmap(self.message.text)


class QChatImageTreeWidgetItem(QChatTreeWidgetItem):
def __init__(self, parent: QTreeWidget, message: QChatImageMessage):
Expand Down Expand Up @@ -120,3 +175,14 @@ def on_click(self, column: int) -> None:
dialog.setLayout(layout)
dialog.setModal(True)
dialog.show()

@property
def liked_message(self) -> str:
return "image"

@property
def can_be_copied_to_clipboard(self) -> bool:
return True

def copy_to_clipboard(self) -> None:
QgsApplication.instance().clipboard().setPixmap(self.pixmap)

0 comments on commit e12d92b

Please sign in to comment.