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/action manager qicon import #3

Open
wants to merge 11 commits into
base: 5.0.x_dev
Choose a base branch
from
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<file>Icon_Library/autoscale.png</file>
<file>Icon_Library/b_icon.png</file>
<file>Icon_Library/back.png</file>
<file>Icon_Library/back-and-forth.png</file>
<file>Icon_Library/bg_icon.png</file>
<file>Icon_Library/Browse_Dir_Path.png</file>
<file>Icon_Library/Calculator.png</file>
Expand All @@ -58,6 +59,7 @@
<file>Icon_Library/DeleteLayer.png</file>
<file>Icon_Library/download.png</file>
<file>Icon_Library/download2.png</file>
<file>Icon_Library/EditFile.png</file>
<file>Icon_Library/EditOpen.png</file>
<file>Icon_Library/EditRedo.png</file>
<file>Icon_Library/EditUndo.png</file>
Expand All @@ -74,6 +76,7 @@
<file>Icon_Library/ExpandAll.png</file>
<file>Icon_Library/ExpandAll_32.png</file>
<file>Icon_Library/fan.png</file>
<file>Icon_Library/flip.png</file>
<file>Icon_Library/FFT.png</file>
<file>Icon_Library/filter2.png</file>
<file>Icon_Library/g_icon.png</file>
Expand Down Expand Up @@ -195,6 +198,8 @@
<file>Icon_Library/SelectPolygon.png</file>
<file>Icon_Library/sequence.png</file>
<file>Icon_Library/sequence2.png</file>
<file>Icon_Library/show.png</file>
<file>Icon_Library/shuffle.png</file>
<file>Icon_Library/Snap&amp;Save.png</file>
<file>Icon_Library/snap.png</file>
<file>Icon_Library/Snapshot.png</file>
Expand All @@ -215,6 +220,7 @@
<file>Icon_Library/surfacePlot.png</file>
<file>Icon_Library/tree.png</file>
<file>Icon_Library/updateTree.png</file>
<file>Icon_Library/unshow.png</file>
<file>Icon_Library/utility_small.png</file>
<file>Icon_Library/utility2.png</file>
<file>Icon_Library/vector.png</file>
Expand Down
253,009 changes: 126,467 additions & 126,542 deletions src/pymodaq_gui/QtDesigner_Ressources/QtDesigner_ressources_rc.py

Large diffs are not rendered by default.

35 changes: 24 additions & 11 deletions src/pymodaq_gui/managers/action_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
from pathlib import Path


def create_icon(icon_name: str):
def create_icon(icon_name: Union[str,Path]):
icon = QtGui.QIcon()
if Path(icon_name).is_file():
if Path(icon_name).is_file(): # Test if icon is in path
icon.addPixmap(QtGui.QPixmap(icon_name), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
else:
icon.addPixmap(QtGui.QPixmap(f":/icons/Icon_Library/{icon_name}.png"), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
pixmap = QtGui.QPixmap(f":/icons/Icon_Library/{icon_name}.png") # Test if icon is in pymodaq's library
if pixmap.isNull():
if hasattr(QtGui.QIcon,'ThemeIcon') and hasattr(QtGui.QIcon.ThemeIcon,icon_name): # Test if icon is in Qt's library
icon = QtGui.QIcon.fromTheme(getattr(QtGui.QIcon.ThemeIcon,icon_name))
else:
icon.addPixmap(QtGui.QPixmap(pixmap), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
return icon


Expand Down Expand Up @@ -51,7 +56,7 @@ def __repr__(self):
return f'QAction {self.text()}'


def addaction(name: str = '', icon_name: str = '', tip='', checkable=False, checked=False,
def addaction(name: str = '', icon_name: Union[str,Path,QtGui.QIcon]= '', tip='', checkable=False, checked=False,
slot: Callable = None, toolbar: QtWidgets.QToolBar = None,
menu: QtWidgets.QMenu = None, visible=True, shortcut=None,
enabled=True):
Expand All @@ -61,8 +66,10 @@ def addaction(name: str = '', icon_name: str = '', tip='', checkable=False, chec
----------
name: str
Displayed name if should be displayed (for instance in menus)
icon_name: str
png file name to produce the icon
icon_name: str / Path / QtGui.QIcon / enum name
str/Path: the png file name/path to produce the icon
QtGui.QIcon: the instance of a QIcon element
ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7)
tip: str
a tooltip to be displayed when hovering above the action
checkable: bool
Expand All @@ -82,8 +89,12 @@ def addaction(name: str = '', icon_name: str = '', tip='', checkable=False, chec
enabled: bool
set the enabled state
"""

if icon_name != '':
action = QAction(create_icon(icon_name), name, None)
if isinstance(icon_name,QtGui.QIcon):
Ashwolaa marked this conversation as resolved.
Show resolved Hide resolved
action = QAction(icon_name, name, None)
else:
action = QAction(create_icon(icon_name), name, None)
else:
action = QAction(name)

Expand Down Expand Up @@ -191,7 +202,7 @@ def setup_actions(self):
raise NotImplementedError(f'You have to define actions here in the following form:'
f'{self.setup_actions.__doc__}')

def add_action(self, short_name: str = '', name: str = '', icon_name: str = '', tip='',
def add_action(self, short_name: str = '', name: str = '', icon_name: Union[str,Path,QtGui.QIcon] = '', tip='',
checkable=False,
checked=False, toolbar=None, menu=None,
visible=True, shortcut=None, auto_toolbar=True, auto_menu=True,
Expand All @@ -204,8 +215,10 @@ def add_action(self, short_name: str = '', name: str = '', icon_name: str = '',
the name as referenced in the dict self.actions
name: str
Displayed name if should be displayed in
icon_name: str
png file name to produce the icon
icon_name: str / Path / QtGui.QIcon / enum name
str/Path: the png file name/path to produce the icon
QtGui.QIcon: the instance of a QIcon element
ThemeIcon enum: the value of QtGui.QIcon.ThemeIcon (requires Qt>=6.7)
tip: str
a tooltip to be displayed when hovering above the action
checkable: bool
Expand Down
48 changes: 48 additions & 0 deletions tests/managers/action_manager_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
1# -*- coding: utf-8 -*-
"""
Created the 08/11/2024

@author: Constant Schouder
"""
import pytest
from qtpy import QtWidgets,QtGui,QtCore
from pymodaq_gui.managers.action_manager import ActionManager

version = QtCore.qVersion()

@pytest.fixture
def ini_qt_widget(init_qt):
qtbot = init_qt
widget = QtWidgets.QWidget()
qtbot.addWidget(widget)
widget.show()
yield qtbot, widget
widget.close()

def get_icon_status(action_manager,action_name,):
action = action_manager.get_action(action_name)
return action.icon().isNull()



def test_icon(qtbot):
action_manager = ActionManager(toolbar=QtWidgets.QToolBar(),menu=QtWidgets.QMenu())

action_manager.add_action(short_name='no_icon', icon_name='')

assert get_icon_status(action_manager,'no_icon') == True

action_manager.add_action(short_name='icon_from_pymodaq', icon_name='NewFile')
assert get_icon_status(action_manager,'icon_from_pymodaq') == False

if tuple(map(int, version.split('.'))) > (6, 7):

action_manager.add_action(short_name='icon_from_Qt', icon_name='WindowClose')
assert get_icon_status(action_manager,'icon_from_Qt') == False

icon = QtGui.QIcon.fromTheme(QtGui.QIcon.ThemeIcon.WindowClose)
action_manager.add_action(short_name='icon', icon_name=icon)
assert get_icon_status(action_manager,'icon') == False