fix udev code and some refactorings for cleaner code

This commit is contained in:
Mathias Panzenböck
2014-02-23 19:37:01 +01:00
parent 6771b75516
commit a1d2c947c7
10 changed files with 96 additions and 66 deletions

View File

@ -2,18 +2,29 @@
#include "config.h"
#include "getkey.h"
GetKey::GetKey( QString button, bool m )
:QDialog( 0 )
int GetKey::getKey(const QString& button, bool useMouse, bool *isMouse, QWidget *parent) {
GetKey getkey(button, useMouse, parent);
if (getkey.exec() == QDialog::Accepted) {
if (isMouse) {
*isMouse = getkey.choseMouse();
}
return getkey.getValue();
}
return -1;
}
GetKey::GetKey(const QString &button, bool useMouse, QWidget *parent)
: QDialog( parent ), useMouse(useMouse), wasMouse(false), value(-1)
{
//prepare the dialog
mouse = m;
setWindowTitle( tr("Choose a key") );
setWindowIcon(QIcon(QJOYPAD_ICON24));
//I'd use a QLabel, but that steals x11Events!
//So, I'll draw the text directly. That means
//I need to resolve the size of the dialog by hand:
text = (mouse ? tr("Choose a new key or mouse button for %1") : tr("Choose a new key for %1")).arg(button);
text = (useMouse ? tr("Choose a new key or mouse button for %1") : tr("Choose a new key for %1")).arg(button);
QRect rect = fontMetrics().boundingRect( text );
//I calculate the size based on the first line of text, which is longer.
//The fontMetrics function is dumb and would treat the string with a
@ -22,7 +33,7 @@ GetKey::GetKey( QString button, bool m )
text += tr("\n(Ctrl-X for no key)");
//now I add 20 pixels of padding and double the height to make room for
//two lines.
setFixedSize( QSize( rect.width() + 20, rect.height()*2 + 20 ) );
setFixedSize(rect.width() + 20, rect.height()*2 + 20);
}
bool GetKey::x11Event( XEvent* e )
@ -32,16 +43,28 @@ bool GetKey::x11Event( XEvent* e )
//On a key press, return the key and quit
//Ctrl+X == [No Key]
if (e->type == KeyRelease) {
if (XKeycodeToKeysym(QX11Info::display(),e->xkey.keycode,0) == XK_x ) {
if (e->xkey.state & ControlMask) done( 0 );
else done( e->xkey.keycode );
wasMouse = false;
if (XKeycodeToKeysym(QX11Info::display(),e->xkey.keycode,0) == XK_x) {
if (e->xkey.state & ControlMask) {
value = -1;
reject();
}
else {
value = e->xkey.keycode;
accept();
}
}
else {
value = e->xkey.keycode;
accept();
}
else done( e->xkey.keycode );
return true;
}
//if we're accepting mouse clicks and a mouse button was clicked...
if (mouse && e->type == ButtonRelease) {
done ( e->xbutton.button + MOUSE_OFFSET);
if (useMouse && e->type == ButtonRelease) {
wasMouse = true;
value = e->xbutton.button;
accept();
return true;
}
@ -50,11 +73,6 @@ bool GetKey::x11Event( XEvent* e )
return false;
}
void GetKey::closeEvent(QCloseEvent *e) {
e->ignore();
done(-1);
}
void GetKey::paintEvent ( QPaintEvent * ) {
//whenever we need to repaint, draw in our text.
QPainter paint( this );