On Mon, Oct 22, 2007 at 05:38:55PM -0500, Bo Peng wrote: > I adapt Edwin's patch to lyx, with two glitches > > 1. I need to record multiple keystrokes. > 2. I need to write A-, instead of Alt+ > > Abdel, am I adding the ShortcutEdit correctly? I remove the lineedit > from the ui file, and copy the original code from blah_ui.h to > GuiPrefs.cpp. > > Cheers, > Bo
> Index: src/frontends/qt4/GuiPrefs.cpp > =================================================================== > --- src/frontends/qt4/GuiPrefs.cpp (revision 21127) > +++ src/frontends/qt4/GuiPrefs.cpp (working copy) > @@ -1692,6 +1692,135 @@ > // > ///////////////////////////////////////////////////////////////////// > > +ShortcutEdit::ShortcutEdit(QWidget * parent) > +: QLineEdit(parent) Please intend that line by a tab. > +{ > + reset(); > +} > + > +void ShortcutEdit::keyPressEvent(QKeyEvent * e) Two empty lines > +{ > + int keyQt = e->key(); Could be const > + > + if (e->modifiers() == Qt::NoModifier) { > + if (keyQt == Qt::Key_Backspace || keyQt == Qt::Key_Delete) > + clear(); > + return; > + } else // start new shortcut Remove '} else' and de-intend the rest. > + if (keySequence.isEmpty()) > + clear(); > + > + > + uint newModifiers = e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | > Qt::META); Line is a bit long. > + if (nKey == 0) > + modifierKeys = newModifiers; > + > + switch(keyQt) { > + case Qt::Key_AltGr: //or else we get unicode salad > + return; > + case Qt::Key_Shift: > + case Qt::Key_Control: > + case Qt::Key_Alt: > + case Qt::Key_Meta: > + // If we are editing the first key in the sequence, > + // display modifier keys which are held down > + if(nKey == 0) > + > setText(keySequence.toString(QKeySequence::NativeText)); > + break; > + default: > + if (keyQt) { > + if (nKey == 0) { > + keySequence = > appendToSequence(keySequence, keyQt | modifierKeys); > + } else { > + keySequence = > appendToSequence(keySequence, keyQt); > + } A matter of tate, but I wouldn't use braces for one-liners like that > + > + nKey++; ++nKey. > + if (nKey >= 4) { > + shortcut(keySequence.toString()); > + reset(); > + return; > + } > + > setText(keySequence.toString(QKeySequence::NativeText)); > + } > + } > +} > + > + > +QKeySequence ShortcutEdit::appendToSequence(const QKeySequence& seq, int > keyQt) Space kefore &. > +{ > + switch (seq.count()) { > + case 0: > + return QKeySequence(keyQt); > + case 1: > + return QKeySequence(seq[0], keyQt); > + case 2: > + return QKeySequence(seq[0], seq[1], keyQt); > + case 3: > + return QKeySequence(seq[0], seq[1], seq[2], keyQt); > + default: > + return seq; > + } > +} > + > + > +void ShortcutEdit::keyReleaseEvent(QKeyEvent *e) Space after *. > +{ > + uint newModifiers = e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | > Qt::META); Line is a bit long. > + > + //if a modifier that belongs to the shortcut was released... > + if ((newModifiers & modifierKeys) < modifierKeys) { > + if (nKey == 0) { > + modifierKeys = newModifiers; > + setText(keySequence.toString(QKeySequence::NativeText)); > + } else { > + shortcut(keySequence.toString()); > + reset(); > + } > + } > +} > + > + > +//prevent Qt from special casing Tab and Backtab > +bool ShortcutEdit::event(QEvent* e) Space. > +{ > + if (e->type() == QEvent::ShortcutOverride) > + return false; > + > + if (e->type() == QEvent::KeyPress) { > + keyPressEvent(static_cast<QKeyEvent *>(e)); > + return true; > + } > + > + return QLineEdit::event(e); > +} > + > + > +void ShortcutEdit::reset() > +{ > + nKey = 0; > + modifierKeys = 0; > + keySequence = QKeySequence(); > +} > + > + > +GuiShortcutDialog::GuiShortcutDialog(QWidget * parent) : QDialog(parent) > +{ > + Ui::ShortcutUi::setupUi(this); > + QDialog::setModal(true); > + // adapted from ui_ShortcutUi.h > + shortcutLE = new ShortcutEdit(parent); > + shortcutLE->setObjectName(QString::fromUtf8("shortcutLE")); > + QSizePolicy sizePolicy2(static_cast<QSizePolicy::Policy>(7), > static_cast<QSizePolicy::Policy>(0)); > + sizePolicy2.setHorizontalStretch(0); > + sizePolicy2.setVerticalStretch(0); > + > sizePolicy2.setHeightForWidth(shortcutLE->sizePolicy().hasHeightForWidth()); > + shortcutLE->setSizePolicy(sizePolicy2); > + gridLayout->addWidget(shortcutLE, 1, 1, 1, 1); Indentation looks strange. > + QWidget::setTabOrder(shortcutLE, okPB); > +} > + > + > PrefShortcuts::PrefShortcuts(GuiPreferences * form, QWidget * parent) > : PrefModule(_("Shortcuts"), form, parent) > { > Index: src/frontends/qt4/ui/ShortcutUi.ui > =================================================================== > --- src/frontends/qt4/ui/ShortcutUi.ui (revision 21127) > +++ src/frontends/qt4/ui/ShortcutUi.ui (working copy) > @@ -93,21 +93,6 @@ > </property> > </widget> > </item> > - <item row="1" column="1" > > - <widget class="QLineEdit" name="shortcutLE" > > - <property name="sizePolicy" > > - <sizepolicy> > - <hsizetype>7</hsizetype> > - <vsizetype>0</vsizetype> > - <horstretch>0</horstretch> > - <verstretch>0</verstretch> > - </sizepolicy> > - </property> > - <property name="toolTip" > > - <string>Enter BibTeX database name</string> > - </property> > - </widget> > - </item> > <item row="0" column="1" > > <widget class="QLineEdit" name="lfunLE" > > <property name="sizePolicy" > > Index: src/frontends/qt4/GuiPrefs.h > =================================================================== > --- src/frontends/qt4/GuiPrefs.h (revision 21127) > +++ src/frontends/qt4/GuiPrefs.h (working copy) > @@ -347,14 +347,33 @@ > }; > > > +/** > + * A lineedit for inputting shortcuts > + */ > +class ShortcutEdit : public QLineEdit { > + Q_OBJECT > +public: > + ShortcutEdit(QWidget * parent); > +protected Q_SLOTS: > + void keyPressEvent(QKeyEvent * e); > + void keyReleaseEvent(QKeyEvent *e); > + bool event(QEvent* e); > +Q_SIGNALS: > + void shortcut(const QString &); > +private: > + QKeySequence appendToSequence(const QKeySequence & seq, int keyQt); > + void reset(); > + QKeySequence keySequence; > + uint nKey; > + uint modifierKeys; > +}; > + > + > class GuiShortcutDialog : public QDialog, public Ui::ShortcutUi > { > public: > - GuiShortcutDialog(QWidget * parent) : QDialog(parent) > - { > - Ui::ShortcutUi::setupUi(this); > - QDialog::setModal(true); > - } > + GuiShortcutDialog(QWidget * parent); > + ShortcutEdit * shortcutLE; > }; Concept looks ok. Andre'