[solved -- feature python] trigger call to getIcon() in view provider

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

[solved -- feature python] trigger call to getIcon() in view provider

Post by TheMarkster »

Is there a way to trigger a call to view provider class getIcon()? I would like to modify the icon (which is in xpm string format) when some boolean variable changes. Seems to me getIcon() only gets called during original object creation.

I am able to intercept the property change in view provider class updateData(self, fp, prop).

In this function I am able to parse the tree widget to find my feature python object and use qt setIcon() to change the icon dynamically, but this is failing in my ubuntu virtual machine, but works in windows. I'd rather do it in FreeCAD as opposed to using qt setIcon() on the tree widget item.

Here is a piece of the code as it stands for the moment with some debugging messages.

Code: Select all

    def updateData(self, fp, prop):
        '''If a property of the handled feature has changed we have the chance to handle this here'''
        # fp is the handled feature, prop is the name of the property that has changed
        #FreeCAD.Console.PrintMessage(prop+" is now "+str(getattr(fp,prop))+chr(10))
#        heartXPM = ['7 6 2 1','N c None','. c #e2385a','N..N..N',\
#'.......','.......','N.....N','NN...NN','NNN.NNN']
        if prop == "HasIntersection":
            from PySide import QtCore,QtGui
            win = FreeCADGui.getMainWindow()
            item = None
            trees = Gui.getMainWindow().findChildren(QtGui.QTreeWidget)
            for tree in trees:
                items = tree.findItems(fp.Label,QtCore.Qt.MatchWrap)
                if len(items) == 1:
                    item = items[0]
                    FreeCAD.Console.PrintMessage("item found\n")
                    break
            if item:
                xpm = self.getIcon().replace("\"","").replace(',','').splitlines()[4:-1]
                pixmap = QtGui.QPixmap(xpm)
                if pixmap.isNull():
                    FreeCAD.Console.PrintMessage("pixmap is null\n")
                icon = QtGui.QIcon(pixmap)
                if icon.isNull():
                    FreeCAD.Console.PrintMessage("icon is null\n")
                else:
                    FreeCAD.Console.PrintMessage("icon is not null\n")
                #item.setIcon(0,icon)
                item.setToolTip(0,"Has Intersection" if fp.HasIntersection else "No intersection or infinite intersections")
                item.setForeground(1,QtGui.QColor(0,0,0) if fp.HasIntersection else QtGui.QColor(255,0,0))
            else:
                FreeCAD.Console.PrintMessage("unable to find object in tree\n")
Notice that the xpm string has to be manipulated to get it to work with setIcon(). In the call to getIcon() I modify the colors in the xpm based on the boolean before returning it as a string.

The commented out heartXPM is an example of an xpm string that works with setIcon().

I was hoping there might be a touch() function or something similar to get the icon fetched again and redrawn.
Last edited by TheMarkster on Mon Sep 20, 2021 5:40 am, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: [feature python] trigger call to getIcon() in view provider

Post by Chris_G »

Would this help ?

Code: Select all

fp.ViewObject.signalChangeIcon()
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: [feature python] trigger call to getIcon() in view provider

Post by TheMarkster »

Chris_G wrote: Mon Sep 20, 2021 5:24 am Would this help ?

Code: Select all

fp.ViewObject.signalChangeIcon()
Yes, that did the trick. Thanks.
Post Reply