Friday, August 22, 2014

QIcon and Scaling, and Layout Spacing

One component of the Find/Replace panel (with whose layout I am still diddling around, which is pathetic) is the recall button. There are four QLineEdits, one for the string to find, the others replace strings, and for each line-edit there is a recall button that pops up a menu showing the last ten strings the user entered into that line-edit. This is a wonderfully useful feature of BBEdit's Find dialog, also appearing in Guiguts. You just have to compose a complicated search or replace string once, and then it will usually still be there, ready to use again, in that edit session and future edit sessions.

In V1 I created these rather clumsily, using a QComboBox. A combo box usually has a full-sized pushbutton with a text label in it. In order to get a small button I forced the width of the combo box to 32x32 pixels. It worked, although it looked a bit odd.

In working on V2 I discovered that Qt had a different widget, the QToolButton, which is specifically for this use of quick access to a popup menu. So I'm using that as the basis for the V2 RecallMenuButton class. And I wanted it to have an icon, some kind of clock-dial to mean "recent" (slavishly copying BBEdit here).

I burned a couple of hours around the internet looking for simple clock icons. I found a couple that would do, but when I tried them out they disappeared or turned into mush. The problem is the scaling that QIcon does. Inside the __init__ of a RecallMenuButton, it sets its icon with

        self.setIcon(QIcon(':/recent-icon.png'))

QIcon's constructor passes the path string to QPixMap. Then it scales the resulting pixmap to about 22x22 pixels, roughly. And the scaling algorithm is rather brutal. Here's the input file.

That's my own work, by the way. None of the free icons I could find online had graphic elements that were simple enough and meaty enough to survive the scale-down that QIcon does. They all turned into spatters of pixels or just nothing. I ended up making this with OpenOffice draw in about 1/5th the time I'd spent browsing for icons online.

Here's how the image ends up as an icon.

I continue to struggle with control of spacing of elements in a layout. I must stop obsessing about this and just take what Qt gives me. Here's what is bugging me. The Replace part of Find/Replace has three rows. Each row has a RecallMenuButton, a line-edit, and a QPushbutton "Replace". These three items are laid out in a QHBoxLayout. Then all three are stacked in a QVBoxLayout and the terror begins. For some reason the vbox wants to space the rows widely, thus:

That looks all puffy, I thought. Experimenting, I found that if I left out the pushbuttons, the spacing became more to my taste:

Well, I need the pushbuttons. I set them to have a vertical size policy of Fixed and tried various heights. The default height (in Mac OS) is about 32 pixels. Making them shorter makes them luck funny but doesn't change the line spacing. I'm at a loss and must get on to, you know, the actual functional code behind this UI? So I'll accept the puffy spacing for now.

No comments: