[ Bug ] Draft preferences: problems with font settings

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Draft preferences: problems with font settings

Post by yorik »

IIRC for the Draft ShapeString, the font handling is done by freefont. I don't know why the fonts are locked that way on windows, nor why freefont wouln't be able to access them read-only somehow... This needs further investigation

I'd really like to be able to get rid of that need to specify Draft ShapeString fonts by file path, and use the same Qt system as the rest of FreeCAD... Unfortunately I didn't find yet a reliable, multi-platform way of relating a Qt font with a file path...
User avatar
wandererfan
Veteran
Posts: 6309
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Draft preferences: problems with font settings

Post by wandererfan »

If there's a Windows user out there that hasn't messed too much with attributes, permissions, etc, on the Fonts directory, could they try the following please?

In Tools>Edit parameters, add a parameter to the Dialog section as shown in this screen grab:
WindowsPrefDialogParm.png
WindowsPrefDialogParm.png (62.2 KiB) Viewed 1814 times

On my Windows box, setting this parameter to true allows access to the Windows/Fonts directory. I'm not sure my machine is a valid test environment anymore since I've set and reset the Fonts properties so often.

Thanks,
wf
User avatar
Roy_043
Veteran
Posts: 8541
Joined: Thu Dec 27, 2018 12:28 pm

Re: Draft preferences: problems with font settings

Post by Roy_043 »

@wandererfan:
Yes, it works! I'll have to get used to the dialog, but I can live with that. Thank you!
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Draft preferences: problems with font settings

Post by Syres »

Roy_043 wrote: Thu Oct 10, 2019 8:03 am @wandererfan:
Yes, it works! I'll have to get used to the dialog, but I can live with that. Thank you!
I've made a crude hack to only have the non-native dialog for the ShapeString Task Panel by changing src\Mod\Draft\DraftGui.py around line 2460 from:

Code: Select all

        self.stringText = translate("draft","Default")
        self.task.leString.setText(self.stringText)
        self.task.fcFontFile.setFileName(Draft.getParam("FontFile",""))
        self.fileSpec = Draft.getParam("FontFile","")
        self.point = FreeCAD.Vector(0.0,0.0,0.0)
        self.pointPicked = False

        QtCore.QObject.connect(self.task.fcFontFile,QtCore.SIGNAL("fileNameSelected(const QString&)"),self.fileSelect)
        QtCore.QObject.connect(self.task.pbReset,QtCore.SIGNAL("clicked()"),self.resetPoint)
        self.point = None
        self.view = Draft.get3DView()
        self.call = self.view.addEventCallback("SoEvent",self.action)
        FreeCAD.Console.PrintMessage(translate("draft", "Pick ShapeString location point:")+"\n")


    def fileSelect(self, fn):
        self.fileSpec = fn

    def resetPoint(self):
        self.pointPicked = False
        origin = FreeCAD.Vector(0.0,0.0,0.0)
        self.setPoint(origin)

    def action(self,arg):
        """scene event handler"""
        import DraftTools
        if arg["Type"] == "SoKeyboardEvent":
            if arg["Key"] == "ESCAPE":
                self.reject()
        elif arg["Type"] == "SoLocation2Event": #mouse movement detection
            self.point,ctrlPoint,info = DraftTools.getPoint(self.sourceCmd,arg,noTracker=True)
            if not self.pointPicked:
                self.setPoint(self.point)
        elif arg["Type"] == "SoMouseButtonEvent":
            if (arg["State"] == "DOWN") and (arg["Button"] == "BUTTON1"):
                self.setPoint(self.point)
                self.pointPicked = True

    def setPoint(self, point):
        self.task.sbX.setProperty('rawValue',point.x)
        self.task.sbY.setProperty('rawValue',point.y)
        self.task.sbZ.setProperty('rawValue',point.z)

    def createObject(self):
        """creates object in the current doc"""
        dquote = '"'
        if sys.version_info.major < 3: # Python3: no more unicode
            String  = 'u' + dquote + str(self.task.leString.text().encode('unicode_escape')) + dquote
        else:
            String  = dquote + self.task.leString.text() + dquote
        FFile = dquote + str(self.fileSpec) + dquote

        Size = str(FreeCAD.Units.Quantity(self.task.sbHeight.text()).Value)
        Tracking = str(0.0)
        x = FreeCAD.Units.Quantity(self.task.sbX.text()).Value
        y = FreeCAD.Units.Quantity(self.task.sbY.text()).Value
        z = FreeCAD.Units.Quantity(self.task.sbZ.text()).Value
        ssBase = FreeCAD.Vector(x,y,z)
        # this try block is almost identical to the one in DraftTools
        try:
            qr,sup,points,fil = self.sourceCmd.getStrings()
            FreeCADGui.addModule("Draft")
            self.sourceCmd.commit(translate("draft","Create ShapeString"),
    ['ss=Draft.makeShapeString(String='+String+',FontFile='+FFile+',Size='+Size+',Tracking='+Tracking+')',
                         'plm=FreeCAD.Placement()',
                         'plm.Base='+DraftVecUtils.toString(ssBase),
                         'plm.Rotation.Q='+qr,
                         'ss.Placement=plm',
                         'ss.Support='+sup,
                         'Draft.autogroup(ss)'])
        except Exception as e:
            FreeCAD.Console.PrintError("Draft_ShapeString: error delaying commit\n")

    def accept(self):
        self.createObject();
        if self.call: self.view.removeEventCallback("SoEvent",self.call)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Snapper.off()
        self.sourceCmd.creator.finish(self.sourceCmd)
        return True

    def reject(self):
        if self.call: self.view.removeEventCallback("SoEvent",self.call)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Snapper.off()
        self.sourceCmd.creator.finish(self.sourceCmd)
        return True
to:

Code: Select all

        self.stringText = translate("draft","Default")
        self.task.leString.setText(self.stringText)
        tDialog = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Dialog")
        import platform
        if platform.system() == 'Windows':
            tDialog.SetBool("DontUseNativeDialog", True)
        self.task.fcFontFile.setFileName(Draft.getParam("FontFile",""))
        self.fileSpec = Draft.getParam("FontFile","")
        self.point = FreeCAD.Vector(0.0,0.0,0.0)
        self.pointPicked = False

        QtCore.QObject.connect(self.task.fcFontFile,QtCore.SIGNAL("fileNameSelected(const QString&)"),self.fileSelect)
        QtCore.QObject.connect(self.task.pbReset,QtCore.SIGNAL("clicked()"),self.resetPoint)
        self.point = None
        self.view = Draft.get3DView()
        self.call = self.view.addEventCallback("SoEvent",self.action)
        FreeCAD.Console.PrintMessage(translate("draft", "Pick ShapeString location point:")+"\n")


    def fileSelect(self, fn):
        self.fileSpec = fn

    def resetPoint(self):
        self.pointPicked = False
        origin = FreeCAD.Vector(0.0,0.0,0.0)
        self.setPoint(origin)

    def action(self,arg):
        """scene event handler"""
        import DraftTools
        if arg["Type"] == "SoKeyboardEvent":
            if arg["Key"] == "ESCAPE":
                self.reject()
        elif arg["Type"] == "SoLocation2Event": #mouse movement detection
            self.point,ctrlPoint,info = DraftTools.getPoint(self.sourceCmd,arg,noTracker=True)
            if not self.pointPicked:
                self.setPoint(self.point)
        elif arg["Type"] == "SoMouseButtonEvent":
            if (arg["State"] == "DOWN") and (arg["Button"] == "BUTTON1"):
                self.setPoint(self.point)
                self.pointPicked = True

    def setPoint(self, point):
        self.task.sbX.setProperty('rawValue',point.x)
        self.task.sbY.setProperty('rawValue',point.y)
        self.task.sbZ.setProperty('rawValue',point.z)

    def createObject(self):
        """creates object in the current doc"""
        dquote = '"'
        if sys.version_info.major < 3: # Python3: no more unicode
            String  = 'u' + dquote + str(self.task.leString.text().encode('unicode_escape')) + dquote
        else:
            String  = dquote + self.task.leString.text() + dquote
        FFile = dquote + str(self.fileSpec) + dquote

        Size = str(FreeCAD.Units.Quantity(self.task.sbHeight.text()).Value)
        Tracking = str(0.0)
        x = FreeCAD.Units.Quantity(self.task.sbX.text()).Value
        y = FreeCAD.Units.Quantity(self.task.sbY.text()).Value
        z = FreeCAD.Units.Quantity(self.task.sbZ.text()).Value
        ssBase = FreeCAD.Vector(x,y,z)
        # this try block is almost identical to the one in DraftTools
        try:
            qr,sup,points,fil = self.sourceCmd.getStrings()
            FreeCADGui.addModule("Draft")
            self.sourceCmd.commit(translate("draft","Create ShapeString"),
    ['ss=Draft.makeShapeString(String='+String+',FontFile='+FFile+',Size='+Size+',Tracking='+Tracking+')',
                         'plm=FreeCAD.Placement()',
                         'plm.Base='+DraftVecUtils.toString(ssBase),
                         'plm.Rotation.Q='+qr,
                         'ss.Placement=plm',
                         'ss.Support='+sup,
                         'Draft.autogroup(ss)'])
        except Exception as e:
            FreeCAD.Console.PrintError("Draft_ShapeString: error delaying commit\n")

    def accept(self):
        self.createObject();
        if self.call: self.view.removeEventCallback("SoEvent",self.call)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Snapper.off()
        self.sourceCmd.creator.finish(self.sourceCmd)
        tDialog = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Dialog")
        import platform
        if platform.system() == 'Windows':
            tDialog.SetBool("DontUseNativeDialog", False)
        return True

    def reject(self):
        if self.call: self.view.removeEventCallback("SoEvent",self.call)
        FreeCADGui.ActiveDocument.resetEdit()
        FreeCADGui.Snapper.off()
        self.sourceCmd.creator.finish(self.sourceCmd)
        tDialog = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Dialog")
        import platform
        if platform.system() == 'Windows':
            tDialog.SetBool("DontUseNativeDialog", False)
        return True
OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.18504 (Git)
Build type: Release
Branch: master
Hash: f66023a646db4b2502bb3637897443b3525ca3c7
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United Kingdom (en_GB)
User avatar
wandererfan
Veteran
Posts: 6309
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Draft preferences: problems with font settings

Post by wandererfan »

Syres wrote: Fri Oct 11, 2019 9:41 am I've made a crude hack to only have the non-native dialog for the ShapeString Task Panel by changing src\Mod\Draft\DraftGui.py around line 2460 from:
Nice. I've been fixated on the Preferences aspect, haven't really thought about the dialog.

I don't see your PR in the Q? :)
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Draft preferences: problems with font settings

Post by Syres »

UR_
Veteran
Posts: 1355
Joined: Tue Jan 03, 2017 8:42 pm

Re: Draft preferences: problems with font settings

Post by UR_ »

Syres wrote: Fri Oct 11, 2019 2:27 pm PR with slightly nicer code https://github.com/FreeCAD/FreeCAD/pull/2610
Just stumbled over new behaviour since this commit.
Unfortunately user parameter "DontUseNativeDialog" is overwritten after first call of "platWinDialog" and not restored.
So user settings are lost afterwards.
This is rude behaviour! :D

Please see my proposal: https://github.com/UR-0/FreeCAD/commit/ ... a0922cea29

Another user parameter "DontUseNativeFontDialog" is introduced and "DontUseNativeDialog" is restored to previous state after usage.

What's your opinion about this?
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Draft preferences: problems with font settings

Post by Syres »

UR_ wrote: Sun Jan 19, 2020 8:19 pm What's your opinion about this?
My testing wasn't thorough enough to have highlighted my major flaw!!

I've tested your DraftGui.py as an existing user and as a brand new user and I've not been able to break it. I did notice that you didn't have the Windows OS check

Code: Select all

 if platform.system() == 'Windows':
in there. I wasn't aware that Mac and Linux needed the fix hence my having that line in the code?
In either case, if you're happy to submit the PR it's fine with me, thanks for the efforts.
UR_
Veteran
Posts: 1355
Joined: Tue Jan 03, 2017 8:42 pm

Re: Draft preferences: problems with font settings

Post by UR_ »

Little PR (with some changes to previous version)

https://github.com/FreeCAD/FreeCAD/pull/2919
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Draft preferences: problems with font settings

Post by yorik »

Then I'll merge it and we fix things afterwards if it breaks for linux ;)
Post Reply