Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add context menu to graphical preview #26

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyqgis_resource_browser/core/resource_table_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ def contextMenuEvent(self, event: QContextMenuEvent) -> None:
if isinstance(idx, QModelIndex) and idx.isValid():
uri = idx.data(Qt.UserRole)
m = QMenu()
a = m.addAction("Copy Name")
a = m.addAction(self.tr("Copy name"))
a.triggered.connect(
lambda *args, n=os.path.basename(uri): QApplication.clipboard().setText(
n
)
)
a = m.addAction("Copy Path")
a = m.addAction(self.tr("Copy path"))
a.triggered.connect(
lambda *args, n=uri: QApplication.clipboard().setText(n)
)

a = m.addAction("Copy Icon")
a = m.addAction(self.tr("Copy icon"))
a.triggered.connect(
lambda *args, n=uri: QApplication.clipboard().setPixmap(QPixmap(n))
)
Expand Down
135 changes: 127 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,113 @@ 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(self.uri)
self.log(message=f"Text copied: {self.uri}", log_level=4)
return self.uri
if expected_format.lower() == "getthemeicon":
QApplication.clipboard().setText(
f"QgsApplication.getThemeIcon('{Path(self.uri).name}')"
)
self.log(
message=f"Text copied: QgsApplication.getThemeIcon('{Path(self.uri).name}')",
log_level=4,
)
return f"QgsApplication.getThemeIcon('{self.uri}')"
if expected_format.lower() == "qpixmap":
QApplication.clipboard().setText(f"QPixmap('{self.uri}')")
self.log(message=f"Text copied: QPixmap('{self.uri}')", log_level=4)
return f"QPixmap('{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}')"

self.log(message=f"Undefined format: {expected_format}", push=True, log_level=1)


class ResourceBrowser(QWidget):
"""
Expand All @@ -59,7 +177,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 +301,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