attached a minimal lineedit widget that captures shortcuts. it steals code from the kde one. try it by casting a lineedit in qt's designer.

i am extremely busy atm so i haven't got around to polish it, but is seems to work, and i hope it is useful and that someone else (bo? ;-) can take it from here...

regards, ed.

PS attached a screenshot of another shortcut widget that pretty clear and easy to use i think

PPS it would be good if lyx's shortcut panel uses qt's model view framework as andre suggested (also easier when subsetting it with the search)

<<inline: sc.png>>

/**
* \file ShortcutEdit.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* Parts of the code come from KDE's kkeysequencewidget.cpp
*
* \author Edwin Leuven 
*
* Full author contact details are available in file CREDITS.
*/

#include <config.h>

#include "ShortcutEdit.h"

#include <QKeyEvent>

ShortcutEdit::ShortcutEdit(QWidget * parent)
: QLineEdit(parent)
{
        reset();
}

void ShortcutEdit::keyPressEvent(QKeyEvent * e)
{
        int keyQt = e->key();

        if (e->modifiers() == Qt::NoModifier) {
                if (keyQt == Qt::Key_Backspace || keyQt == Qt::Key_Delete)
                        clear();
                return;
        } else // start new shortcut
                if (keySequence.isEmpty())
                        clear();


        uint newModifiers = e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | 
Qt::META);
        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);
                                }

                                nKey++;
                                if (nKey >= 4) {
                                        shortcut(keySequence.toString());
                                        reset();
                                        return;
                                 }
                                 
setText(keySequence.toString(QKeySequence::NativeText));
                        }
        }
}


QKeySequence ShortcutEdit::appendToSequence(const QKeySequence& seq, int keyQt)
{
        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)
{
        uint newModifiers = e->modifiers() & (Qt::SHIFT | Qt::CTRL | Qt::ALT | 
Qt::META);

        //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)
{
        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();
}

#include "ShortcutEdit_moc.cpp"
// -*- C++ -*-
/**
 * \file ShortcutEdit.h
 * This file is part of LyX, the document processor.
 * Licence details can be found in the file COPYING.
 *
 * \author Edwin Leuven
 *
 * Full author contact details are available in file CREDITS.
 */

#ifndef SHORTCUTEDIT_H
#define SHORTCUTEDIT_H

#include <QLineEdit>
#include <QKeySequence>
#include <QKeyEvent>
#include <QEvent>


/**
 * 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;
};


#endif // SHORTCUTEDIT_H

Reply via email to