How to use FreeCAD's bult-in icons?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
nzurbrugg
Posts: 24
Joined: Wed Dec 25, 2013 3:56 am
Location: United States

How to use FreeCAD's bult-in icons?

Post by nzurbrugg »

There's a great selection of icons here! I'm sure there's a simple way to use these for custom workbench tools, but I can't seem to find a good example...

For instance, say I want to use
Image
as a placeholder icon for a revolutionary point creation tool ;) What should I put in place of ICON_CODE below?

Code: Select all

def GetResources(self):
    return {'Pixmap' : ICON_CODE,
            'MenuText': QtCore.QT_TRANSLATE_NOOP("Reference_Point","Point"),
            'Accel': "",
            'ToolTip': QtCore.QT_TRANSLATE_NOOP("Reference_Point","Create a point")}
And my followup question will be how to add these icons to buttons in .ui files using QT Designer...
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: How to use FreeCAD's bult-in icons?

Post by shoogen »

Code: Select all

def GetResources(self):
    return {'Pixmap' : ":/icons/Part_Point_Parametric.svg",
            'MenuText': QtCore.QT_TRANSLATE_NOOP("Reference_Point","Point"),
            'Accel': "",
            'ToolTip': QtCore.QT_TRANSLATE_NOOP("Reference_Point","Create a point")}
nzurbrugg
Posts: 24
Joined: Wed Dec 25, 2013 3:56 am
Location: United States

Re: How to use FreeCAD's bult-in icons?

Post by nzurbrugg »

When I try that, I get this error when I load the workbench in FreeCAD:

Code: Select all

Cannot find icon: ::/icons/Part_Point_Parametric.svg
I'm not sure if it makes a difference, but I have my workbench in ~/.FreeCAD/Mod instead of /usr/lib/freecad/Mod
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: How to use FreeCAD's bult-in icons?

Post by shoogen »

there should have been a single colon.
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: How to use FreeCAD's bult-in icons?

Post by yorik »

Basically this works with Qt resources. All builtin icons are packed in a special Qt object called resource, that gets included in the freecad executable when compiled. From any code that uses qt, you can access the contents of a resource just like if it was a normal file, by starting the path with a ":". By default, all workbenches of freecad put their icons in an "icons" folder. So the path to all these icons starts with ":/icons/". In menu Tools > Customize > Macros, you have a small dialog that lists all the builtin icons, so you can get their name.
nzurbrugg
Posts: 24
Joined: Wed Dec 25, 2013 3:56 am
Location: United States

Re: How to use FreeCAD's bult-in icons?

Post by nzurbrugg »

I tried with a single colon, but I am still getting the same error. I took a look at Tools > Customize > Macros and I DO see a number of icons there, but the Part_Point_Parametric icon is missing. It will show up in the list though after I start the Part workbench...
With some experimenting, the icon in my workbench displays correctly if I start the Part workbench first, then start my workbench... if I reverse the order and start my workbench first, then start the Part workbench, when I switch back to my workbench it displays a black X where the icon should be. Is there something I need to add to ensure the icon is loaded into memory before it gets called in my code?
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: How to use FreeCAD's bult-in icons?

Post by yorik »

nzurbrugg wrote: Is there something I need to add to ensure the icon is loaded into memory before it gets called in my code?
Ah yes. FreeCAD uses "lazy loading" philosophy... Things are loaded only when needed. So if you need icons from a certain workbench, you must load it first. From python, it's just a matter of putting this line somewhere at the beginning of your code:

Code: Select all

import Part
I don't remember exactly f the icons are imported with the base workbench module or with its GUI counterpart, so if the above doesn't work, try this instead:

Code: Select all

import PartGui
nzurbrugg
Posts: 24
Joined: Wed Dec 25, 2013 3:56 am
Location: United States

Re: How to use FreeCAD's bult-in icons?

Post by nzurbrugg »

import PartGui did the trick! Thanks for your explanations!

Now for my followup question... can I use QT Designer to add these icons to buttons in a .ui file? In the icon area in QT Designer, it pulls up a "select resource" dialog box and it gives the option to open a resource file, but I'm not sure if this would be able to work with FreeCAD's resource setup? Maybe it would be better to set the icons in my python code? Looks like I can do something like:

Code: Select all

button.setIcon(QtGui.QIcon(':/icons/edit-select-all.svg'))
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: How to use FreeCAD's bult-in icons?

Post by yorik »

I think that in the "select resource" area of qt designer, you can add FreeCAD's .qrc files. (I didn't test, not sure about that). In FreeCAD source code, in each of the src/Mod/*/Gui folders, as well as in src/Gui folder, you should find a "Resources" folder, which contains the module's qrc file, which is actually a xml file that lists all files (icons, translations, etc) to be included into the resource.
nzurbrugg
Posts: 24
Joined: Wed Dec 25, 2013 3:56 am
Location: United States

Re: How to use FreeCAD's bult-in icons?

Post by nzurbrugg »

That worked! Thanks!
Post Reply