cat /dev/maxilys

A glance in the mind of a KDE/Linux developer to see how ideas turn into code.

2006-07-27

KDE: Beauty of code

A challenge! I want a challenge, something that would be difficult to code! To make Serenity react to a click on a label was too easy.

Less than 20 lines of code and that was it. A click on a label in front of a combobox and the drop-down menu appears, another click and the menu closes itself. A click on a label in front of any editable widget and here comes the cursor at the end of the contained text. Amazing! ;-) Well, in fact, that's Qt which is amazing.

Just to satisfy my developer's hypertrophied ego, here are the lines to put in the event filter:


if ( ::qt_cast<QLabel*>(obj) && (ev->type() == QEvent::MouseButtonPress) )
{
QMouseEvent* what = dynamic_cast<QMouseEvent*>(ev);
if (what->button() != Qt::LeftButton) // Accept only the left button.
return false;
QLabel* label = static_cast<QLabel*>(obj);
if (! label->buddy()) // No buddy, do nothing.
return false;
QWidget* buddy = label->buddy();
if (! buddy->isWidgetType()) // Safety measure.
return false;
buddy->setFocus(); // Set focus for all editable widgets.
//
// Don't click on a slider!
if (! ::qt_cast<QSlider*>(buddy))
{
// Fake a mouse click for non-editable widgets.
QMouseEvent me(QEvent::MouseButtonPress,
QPoint(0,0),
Qt::LeftButton, Qt::LeftButton);
QApplication::sendEvent(buddy, &me);
// Don't forget to release that button!
me = QMouseEvent(QEvent::MouseButtonRelease,
QPoint(0,0),
Qt::LeftButton, Qt::LeftButton);
QApplication::sendEvent(buddy, &me);
}
return false;
}


For those who understand this kind of stuff: Don't forget to polish the QLabel widget. And if I explicitely forbid a fake click on a QSlider, that's because such a click brings the slider to the far left of its groove.

Serenity made a new step in the direction of the Fitts' law. I don't know if such a thing could be implemented in KDE. I mean, it's possible to implement it in KStyle but there are some issues. Not all labels are linked to the widget they introduce and when they are, it means they must have a keyboard shortcut. That problem of liaison is easy to solve, it depends only on the interface. This problem of shortcuts is more complex. Only a limited amount of them can exist at the same time in a dialog. To make things easy, it should have to be possible to connect a label with a widget without implying a shortcut. Qt doesn't allow this. It only happen when it falls short of letters to use as shortcuts. I wouldn't call that an optimal solution.

This discussion must be brought to a "higher level". Once you've started to click on labels within KDE, you will also realize that it doesn't work within the gnomish applications, even with the GTK-Qt engine. Another problem of inconsistency on the Linux desktop...

I keep this feature as an experimental feature that belongs only to Serenity for the time being. If other style developers adopt it --and improve it-- then maybe a revolution would start. ;-)

1 Comments:

At 14 September, 2006 22:31, Blogger Florian Grässle said...

Hey, I read about your accomplishment in a comment on my blog about Fitts' laws in KDE. Cool you took the time to implement the label-clicking-focus-changing thingy :D

 

Post a Comment

<< Home