Trouble With XPM and SVG Icons With a Command

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
jmwright
Posts: 77
Joined: Thu Jun 19, 2014 2:30 am
Contact:

Trouble With XPM and SVG Icons With a Command

Post by jmwright »

I'm trying to use icons with commands that I've created for a FreeCAD module-workbench combo.

I've tried to base my work on the example here http://www.freecadweb.org/wiki/index.ph ... _workbench

This is the code I have.

Code: Select all

class CadQuerySaveScript:
    """CadQuery's command to save a script file"""

    def GetResources(self):
        return {"MenuText": "Save Script",
                "Accel": "Alt+S",
                "ToolTip": "Saves the CadQuery script to disk",
                "Pixmap": """
                /* XPM */
                static const char *test_icon[]={
                "16 16 2 1",
                "a c #000000",
                ". c None",
                "................",
                "................",
                "..############..",
                "..############..",
                "..############..",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "................",
                "................"};
                """}
When I select the workbench from the drop down in FreeCAD, I get the following error in the terminal.

Code: Select all

Cannot find icon: 
                /* XPM */
                static const char *test_icon[]={
                "16 16 2 1",
                "a c #000000",
                ". c None",
                "................",
                "................",
                "..############..",
                "..############..",
                "..############..",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "......####......",
                "................",
                "................"};
I've checked to make sure that my tabs/spaces were ok, and am not sure what else could be keeping this from working. Any thoughts?
User avatar
yorik
Founder
Posts: 13664
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Trouble With XPM and SVG Icons With a Command

Post by yorik »

I'm not sure that you can still add xpm icons inside the code like this... We don't use that mechanism anymore, and I wonder if it was not disabled at some point.

A way that should work is to save your xpm icon in a separate .xpm file, and give the path to that file instead of the actual xpm code. But consider using svg instead, they look much better!
User avatar
jmwright
Posts: 77
Joined: Thu Jun 19, 2014 2:30 am
Contact:

Re: Trouble With XPM and SVG Icons With a Command

Post by jmwright »

Ok, thanks. I've switched to trying to use SVG files. I have the open_icon.svg and CQ_Logo.svg icons under

Code: Select all

Gui/Resources/icons/
and I've created a .qrc file inside the Resources folder with the following content.

Code: Select all

<RCC>
    <qresource>
        <file>icons/open_icon.svg</file>
        <file>icons/CQ_Logo.svg</file>
    </qresource>
</RCC> 
No matter what I do though, the icons are not found. It looks like maybe I need to compile the resources, but I'm not familiar with how this works. I've tried the following code in my Workbench, but it didn't work.

Code: Select all

class CadQueryWorkbench (Workbench):
    """CadQuery workbench object"""
    MenuText = "CadQuery"
    ToolTip = "CadQuery workbench"
    Icon = "icons/CQ_Logo.svg"
Is there a line of Python code that I can add to have these resources recognized in the workbench?
User avatar
yorik
Founder
Posts: 13664
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Trouble With XPM and SVG Icons With a Command

Post by yorik »

Ah yes you need to compile the resource first.
These .qrc files cannot be used as is, they must be compiled first. There is a tool for that that comes with the pyside (or pyqt) package, that is called pyside-rcc (or pyrcc for pyqt) that compiles qt resources for python. You use it on top of your resource file to generate a compiled resource, like this:

Code: Select all

pyside-rcc -o my_resource_rc.py path/to/my_resource.qrc
This generates a my_resource_rc.py (it is a convention to name it *_rc.py), that can then be imported where you need:

Code: Select all

import my_resource_rc.py
After that, in the file where you imported it, the contents of the resource can be accessed normally, just like if it was a directory, by starting the path with ":":

Code: Select all

Icon = ":/icons/CQ_Logo.svg"
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Trouble With XPM and SVG Icons With a Command

Post by jmaustpc »

yorik wrote:Ah yes you need to compile the resource first.
Hi Yorik
Is this all due to the update you pushed a week or so ago that?

If so I was thinking that you must have done all this for Draft, Arch etc. Python WB's in Master, in which case he could look there for an example to see how to do this?


Jim
User avatar
jmwright
Posts: 77
Joined: Thu Jun 19, 2014 2:30 am
Contact:

Re: Trouble With XPM and SVG Icons With a Command

Post by jmwright »

yorik - Compiling with pyside-rcc (part of the pyside-tools package in Ubuntu 14.04) took care of this for me. Thanks for the help.
User avatar
yorik
Founder
Posts: 13664
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Trouble With XPM and SVG Icons With a Command

Post by yorik »

jmaustpc wrote:Is this all due to the update you pushed a week or so ago that?
No, actually that fix is so we don't need to do that anymore :)

But for simple cases like jmwright's, it's easier and faster to do it by hand. That way you don't have anything to compile. But once your workbench is integrated into the build system anyway, it becomes handier to use it to autogenerate those files...
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Re: Trouble With XPM and SVG Icons With a Command

Post by JAndersM »

With the help of this thread I managed to create a workbench svg-icon in a my_rc.py file but I have not figured out how to use icons in a command. In this line:

Code: Select all

    def GetResources(self):
        return {"MenuText": "Save Script",
                "Accel": "Alt+S",
                "ToolTip": "Saves the CadQuery script to disk",
                "Pixmap": ?????
How do I do?

The wiki still shows the old XPM icons. Maybe it could be updated.
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Re: Trouble With XPM and SVG Icons With a Command

Post by JAndersM »

Ok, solved my problem using "Pixmap": ":/icons/CQ_Logo.svg"

Code: Select all

 def GetResources(self):
        return {"MenuText": "Save Script",
                "Accel": "Alt+S",
                "ToolTip": "Saves the CadQuery script to disk",
                "Pixmap": ":/icons/CQ_Logo.svg"}
But the reason I was confused was that in Arch only the name is used instead of :/icons/Arch_Component.svg:

Code: Select all

return {'Pixmap'  : 'Arch_Component',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Arch_Component","Component"),
                'Accel': "C, M",
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Arch_Component","Creates an undefined architectural component")}
How does that work?
Post Reply