yes, that's nice to know, thanks for the tip. I got the values from the PyQt documentation ... but I should have realised from the fact that QFont.Weight is an Enum, that it would have names. Fancy new Python features ... bring back v1.52, I say...
On Thursday, October 31, 2024 at 9:28:54 PM UTC [email protected] wrote: > There's a slicker way to build the dictionary, I've just learned: > > weights = {w.name: w for w in QFont.Weight} > > It turns out that PyQt enums all have name and value attributes. Very > handy. > > But I take your point about wanting to avoid a divergence between the > values in the settings file (and their documentation) and the ones in the > method. At least you can run the line above to see that name:value pairs to > copy them into both places. > > On Thursday, October 31, 2024 at 5:15:00 PM UTC-4 jkn wrote: > > On Thursday, October 31, 2024 at 8:59:31 PM UTC [email protected] wrote: > > What value does param have when the method is called? One of the > dictionary keys, I hope. > > > err, well clearly eg 'Bold', for instance. <param> is the parameter for a > line like "WEIGHT Bold" for all of the xxx_modifier functions in this part > of the code/ > > and if it is not one of the dictionary keys ... then since I use > dict.get() instead of dict[], then get() will return > QtGui.QFont.Weight.Medium > > setFont() won't be in effect if there is already a stylesheet > specifying the value, according to the docs, so let's hope that's not the > case. > > Yes, I haven't gone down that particular rabbit hole. I only wanted to > change the bit I understood... > > > You can iterate over the Weight enum: > > for w in QFont.Weight: > w_str = f'{repr(w)}' > g.es(w_str) > > > True, but I have a mild aversion to (more) run-time work like this. The > available 'parameter strings' are (supposedly) defined in the Leo > documentation, so I would rather hardcode that mapping. > Otherwise the documentation would have to say "look at the values of > QFont.Weight in the PyQt documentation", rather than "use one of: Thin, > ...Bold, ..." > > This is possibly influenced by the fact that I do a lot of embedded > programming, where every cycle counts ;-) > > > <Weight.Thin: 100> > <Weight.ExtraLight: 200> > <Weight.Light: 300> > <Weight.Normal: 400> > <Weight.Medium: 500> > <Weight.DemiBold: 600> > <Weight.Bold: 700> > <Weight.ExtraBold: 800> > <Weight.Black: 900> > > With a little string manipulation you could build the table without > hardcoding anything. That would probably be better. > On Thursday, October 31, 2024 at 4:33:42 PM UTC-4 jkn wrote: > > This works, I think, and is closer to the documented behaviour, but it is > not very beautiful. > > def weight_modifier(item: Item, param: str) -> None: > wd = {"Thin": QtGui.QFont.Weight.Thin, > "ExtraLight": QtGui.QFont.Weight.ExtraLight, > "Light": QtGui.QFont.Weight.Light, > "Normal": QtGui.QFont.Weight.Normal, > "Medium": QtGui.QFont.Weight.Medium, > "DemiBold": QtGui.QFont.Weight.DemiBold, > "Bold": QtGui.QFont.Weight.Bold, > "ExtraBold": QtGui.QFont.Weight.ExtraBold, > "Black": QtGui.QFont.Weight.Black > } > arg = wd.get(param, QtGui.QFont.Weight.Medium) > > font = item.font(0) > font.setWeight(arg) > item.setFont(0, font) > > modifier = weight_modifier > > > On Thursday, October 31, 2024 at 6:00:18 PM UTC jkn wrote: > > Indeed - I started doing this before my initial posting, I just have > limited time windows available. Since we seem to agreed that there is > something amiss here, I am over my initial question about needing an > additional package to install as well as PyQt6, and I can experiment from > there. > > Thanks for helping to confirm my suspicions... > > J^n > > > On Thursday, October 31, 2024 at 4:57:26 PM UTC [email protected] wrote: > > You will probably want to put some print statements into the > declutter_style() method to see what arguments get passed when you ask > for WEIGHT demibold or whatever. I suspect that nothing you pass in will > give any result except the default 75, but that's where the print > statements may help. Even the signature of the method contradicts how the > code works. The docstring isn't right, either. The method probably got > massively revised somewhere in the past and a few things got garbled. > > In addition, the line getattr(QtGui.QFont, param,75) queries the QFont > class, not the instance actually being used by the item whose font is > supposed to be changed, and I don't see how that makes sense. > > So add some print statements and see if they can help sort it all out. I > don't feel like spending time figuring out how to set up for decluttering > in a way that will demonstrate a visibly decluttered headline with bold > type. If I did, print statements would be my starting point. > > On Thursday, October 31, 2024 at 12:36:00 PM UTC-4 jkn wrote: > > indeed. My cheap fix is to patch and set the default to 700, as you have > done. But what if I want to see an effect line "WEIGHT DemiBold" for some > declutter patterns? > > On Thursday, October 31, 2024 at 3:26:40 PM UTC [email protected] wrote: > > I don't see how it can work because any string that goes along with WEIGHT > that feed into the function gets discarded, and then you get the default. > But as I said, it's hard to work through to be sure. "75" would give a > light to normal weight, depending on the font. > > On Thursday, October 31, 2024 at 10:47:34 AM UTC-4 jkn wrote: > > Yes, this matches my (brief) investigations. > > But using > > WEIGHT 700 > > did not work either - I had to use 700 as the default in the getattr() > call, as I wrote a few posts above. > > i am suspecting that the original code didn't properly work in PyQt5, and > any WEIGHT line would cause the default in the getattr() to be returned. > That used to be 75, but has to be er. 700 for bold in PyQt6. > > I will try whether WEIGHT 100 does anything (eg. feint, the opposite of > bold) in PyQt5. suspect not ... in which case there is a small bug here, I > think. I will attempt to fix it. > > On Thursday, October 31, 2024 at 2:24:38 PM UTC [email protected] wrote: > > I had a quick look at the way it's used, and I find it hard to > understand. I can see the intention but the layers of indirection make it > hard. Say the pattern in myLeoSettings is *WEIGHT BOLD*, as you wrote. > What string gets fed into declutter_style()? declutter_style() uses the > string in the method call param = > c.styleSheetManager.expand_css_constants(arg).split()[0]. Every string > I've given that method returns the same string, or the first word of it. > None of those strings exist of attributes of QFont, so the default always > comes back, which is 75. > > Anyway, 700 is the value to use for bold, not 75. It's an integer, not a > string. > > On Thursday, October 31, 2024 at 9:23:44 AM UTC-4 jkn wrote: > > it is the getting of the argument, from eg: > > # part of declutter-pattern > WEIGHT 700 > or ? > WEIGHT Bold # as per documentation > > => "arg = 700" > > that is not working, I think. > > On Thursday, October 31, 2024 at 1:15:28 PM UTC [email protected] wrote: > > On Thursday, October 31, 2024 at 7:45:05 AM UTC-4 Thomas Passin wrote: > > Either of these work in the sense of executing without producing an > error. I haven't tried applying the font to see the results: > > from leo.core.leoQt import QtGui, QtWidgets > QFont = QtGui.QFont > > newfont = QFont('Georgia') > newfont.setWeight(QFont.Weight.Bold) > # or > newfont.setWeight(700) > > > Now I've tried it and yes, I do get bold text. That detour with arg = > getAttr() isn't needed. > > > > On Wednesday, October 30, 2024 at 2:58:37 PM UTC-4 jkn wrote: > > ... I see that the values for the Weight enum for QFont.setWeight() seem > to have changed for PyQt6. It is now a scale of 1 to 1000, instead of 1 to > 99 as previously. > > https://doc.qt.io/qt-6/qfont.html#Weight-enum > > Changing this in qt_tree helps: > > --- a/leo/plugins/qt_tree.py > +++ b/leo/plugins/qt_tree.py > @@ -307,7 +307,7 @@ class LeoQtTree(leoFrame.LeoTree): > elif cmd == 'WEIGHT': > > def weight_modifier(item: Item, param: str) -> None: > - arg = getattr(QtGui.QFont, param, 75) > + arg = getattr(QtGui.QFont, param, 700) # WAS 75 > font = item.font(0) > font.setWeight(arg) > item.setFont(0, font) > > > -- You received this message because you are subscribed to the Google Groups "leo-editor" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/leo-editor/5b2be76a-3cd8-4768-b4b1-e557474f4d7en%40googlegroups.com.
