Skip to content

Commit

Permalink
Add context menu on graphic view
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Apr 20, 2023
1 parent 24a83f1 commit 926b634
Showing 1 changed file with 125 additions and 8 deletions.
133 changes: 125 additions & 8 deletions pyqgis_resource_browser/gui/resource_browser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# standard lib
import os
import pathlib
import re
from functools import partial
from pathlib import Path
from typing import Literal, Union

from qgis.core import QgsApplication
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QFile, QModelIndex, QRegExp, Qt, QTextStream, pyqtSignal
from qgis.PyQt.QtGui import QPixmap
from qgis.PyQt.QtGui import QContextMenuEvent, QPixmap
from qgis.PyQt.QtSvg import QGraphicsSvgItem
from qgis.PyQt.QtWidgets import (
QAction,
Expand All @@ -17,6 +18,7 @@
QGraphicsView,
QLabel,
QLineEdit,
QMenu,
QTextBrowser,
QToolButton,
QWidget,
Expand All @@ -27,20 +29,29 @@

# plugin
from pyqgis_resource_browser.__about__ import __title__

from ..core.resource_table_model import ResourceTableFilterModel, ResourceTableModel
from ..core.resource_table_view import ResourceTableView
from ..toolbelt import PlgOptionsManager
from pyqgis_resource_browser.core.resource_table_model import (
ResourceTableFilterModel,
ResourceTableModel,
)
from pyqgis_resource_browser.core.resource_table_view import ResourceTableView
from pyqgis_resource_browser.toolbelt import PlgLogger, PlgOptionsManager


class ResourceGraphicsView(QGraphicsView):
resized = pyqtSignal()
uri: str = ""

def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self.log = PlgLogger().log
self.item = None

def setItem(self, item):
def setItem(self, item: Union[QGraphicsSvgItem, QGraphicsPixmapItem]):
"""Set view item.
:param item: _description_
:type item: Union[QGraphicsSvgItem, QGraphicsPixmapItem]
"""
self.scene().clear()
self.scene().addItem(item)
self.item = item
Expand All @@ -50,6 +61,111 @@ def resizeEvent(self, QResizeEvent):
if self.item:
self.fitInView(self.item, Qt.KeepAspectRatio)

def contextMenuEvent(self, event: QContextMenuEvent):
"""Custom menu displayed on righ-click event on widget view.
:param event: event that triggers the context-menu, typically right-click
:type event: QContextMenuEvent
"""
menu = QMenu(parent=self)

# Copy image name
action_copy_name = QAction(
icon=QgsApplication.getThemeIcon("mIconLabelQuadrantOffset.svg"),
text=self.tr("Copy name"),
parent=self,
)
action_copy_name.triggered.connect(partial(self.copy_to_clipboard, "name"))

# Copy image path
action_copy_path = QAction(
icon=QgsApplication.getThemeIcon("mIconFileLink.svg"),
text=self.tr("Copy path"),
parent=self,
)
action_copy_path.triggered.connect(partial(self.copy_to_clipboard, "path"))

# Copy as getThemeIcon syntax
action_copy_theme_icon = QAction(
icon=QgsApplication.getThemeIcon("mActionEditInsertImage.svg"),
text=self.tr("Copy as getThemeIcon syntax"),
parent=self,
)
action_copy_theme_icon.triggered.connect(
partial(self.copy_to_clipboard, "getThemeIcon")
)

# Copy as QIcon syntax
action_copy_qicon = QAction(
icon=QgsApplication.getThemeIcon("mActionEditCopy.svg"),
text=self.tr("Copy as QIcon syntax"),
parent=self,
)
action_copy_qicon.triggered.connect(partial(self.copy_to_clipboard, "qicon"))

# Copy as QPixmap syntax
action_copy_qpixmap = QAction(
icon=QgsApplication.getThemeIcon("mIconColorPicker.svg"),
text=self.tr("Copy as QPixmap syntax"),
parent=self,
)
action_copy_qpixmap.triggered.connect(
partial(self.copy_to_clipboard, "qpixmap")
)

# add actions to context menu
menu.addAction(action_copy_name)
menu.addAction(action_copy_path)
menu.addSeparator()
menu.addAction(action_copy_theme_icon)
menu.addAction(action_copy_qicon)
menu.addAction(action_copy_qpixmap)

# open the menu at the event global position
menu.exec_(event.globalPos())

def copy_to_clipboard(
self,
expected_format: Literal["getThemeIcon", "name", "path", "qicon", "qpixmap"],
) -> str:
"""Copy item uri to system clipboard in an expected format.
:param expected_format: expected format
:type expected_format: Literal["name", "path", "qicon", "qpixmap"]
:return: formatted uri in the expected format
:rtype: str
"""
self.log(
message=f"DEBUG - Copy to clipboard {self.uri} in {expected_format} format.",
log_level=4,
)
if expected_format.lower() == "name":
QApplication.clipboard().setText(Path(self.uri).name)
self.log(message=f"Text copied: {Path(self.uri).name}", log_level=4)
return Path(self.uri).name
if expected_format.lower() == "path":
QApplication.clipboard().setText(str(Path(self.uri).resolve()))
self.log(message=f"Text copied: {Path(self.uri).resolve()}", log_level=4)
return Path(self.uri).resolve()
if expected_format.lower() == "getThemeIcon":
QApplication.clipboard().setText(
f"QgsApplication.getThemeIcon('{self.uri}')"
)
self.log(
message=f"Text copied: QgsApplication.getThemeIcon('{self.uri}')",
log_level=4,
)
return f"QgsApplication.getThemeIcon('{self.uri}')"
if expected_format.lower() == "qpixmap":
QApplication.clipboard().setText(f"QIcon('{self.uri}')")
self.log(message=f"Text copied: QIcon('{self.uri}')", log_level=4)
return f"QIcon('{self.uri}')"
if expected_format.lower() == "qicon":
QApplication.clipboard().setText(f"QIcon('{self.uri}')")
self.log(message=f"Text copied: QIcon('{self.uri}')", log_level=4)
return f"QIcon('{self.uri}')"


class ResourceBrowser(QWidget):
"""
Expand All @@ -59,7 +175,7 @@ class ResourceBrowser(QWidget):
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)

pathUi = pathlib.Path(__file__).parent / "resource_browser.ui"
pathUi = Path(__file__).parent / "resource_browser.ui"
with open(pathUi.as_posix()) as uifile:
uic.loadUi(uifile, baseinstance=self)

Expand Down Expand Up @@ -183,6 +299,7 @@ def updatePreview(self, uri: str):
if item:
hasImage = True
self.graphicsView.setItem(item)
self.graphicsView.uri = uri

if re.search(r"\.(svg|html|xml|txt|js|css)$", uri, re.I) is not None:
file = QFile(uri)
Expand Down

0 comments on commit 926b634

Please sign in to comment.