Bo Peng wrote:
But how to clear the 'search ...' text when the lineedit gets focus? I
think we need some initial text in this lineedit, like what firefox
does to its search box.
not sure i follow this
better is use qt's model/view framework ...
Frankly, I have no idea what you (and Andre) are talking about... my
qt IQ is barely above 0. :-)
see here
http://doc.trolltech.com/4.1/model-view-programming.html
and here for a relevant example
http://doc.trolltech.com/4.1/itemviews-simpletreemodel.html
...
attached the latest version of the shortcut edit widget. it provides
visual feedback when inputting, overrides shortcuts in the dialog (took
me a lot of trial and error to figure this out. it works on windows but
it would be nice if other people try it as well)
for now it simply spits out qt's portable string representation of
shortcuts. i like these better than lyx's since they are more verbose so
personally i would have lyx switch those. you can of course return a
QKeySequence instead and translate them to lyx's scheme...
/**
* \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 <QApplication>
#include "ShortcutEdit.h"
#include "qlkey.h"
#include "support/qstring_helpers.h"
namespace lyx {
ShortcutEdit::ShortcutEdit(QWidget * parent)
: QLineEdit(parent)
{
reset();
QApplication::instance()->installEventFilter(this);
has_cursor_ = false;
}
bool ShortcutEdit::eventFilter(QObject * obj, QEvent * e)
{
if (!has_cursor_)
return false;
switch (e->type()) {
// swallow these if we have focus and they come from elsewhere
case QEvent::Shortcut:
case QEvent::ShortcutOverride:
if (obj != this)
return true;
default:
break;
}
return false;
}
bool ShortcutEdit::event(QEvent * e)
{
switch (e->type()) {
case QEvent::FocusOut:
has_cursor_ = false;
break;
case QEvent::FocusIn:
has_cursor_ = true;
break;
case QEvent::ShortcutOverride:
keyPressEvent(static_cast<QKeyEvent *>(e));
return true;
case QEvent::KeyRelease:
keyReleaseEvent(static_cast<QKeyEvent *>(e));
case QEvent::Shortcut:
case QEvent::KeyPress:
return true;
default: break;
}
return QLineEdit::event(e);
}
void ShortcutEdit::keyPressEvent(QKeyEvent * e)
{
int const keyQt = e->key();
if (e->modifiers() == Qt::NoModifier) {
if (keyQt == Qt::Key_Backspace || keyQt == Qt::Key_Delete) {
clear();
reset();
done();
}
return;
}
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)
updateDisplay();
break;
default:
if (nkey_ == 0)
keysequence_ = appendToSequence(keysequence_,
keyQt | modifierkeys_);
else
keysequence_ = appendToSequence(keysequence_,
keyQt);
++nkey_;
if (nkey_ >= 4) {
done();
return;
}
updateDisplay();
}
}
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;
updateDisplay();
} else
done();
}
}
void ShortcutEdit::updateDisplay()
{
QString s;
if (modifierkeys_ && nkey_ == 0)
s = appendToSequence(keysequence_,
modifierkeys_).toString();
else
s = keysequence_.toString();
if (!s.isEmpty())
s.append(" ... ");
setText(s);
}
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::done()
{
QString s = keysequence_.toString();
setText(s);
shortcut(s);
reset();
}
void ShortcutEdit::reset()
{
nkey_ = 0;
modifierkeys_ = 0;
keysequence_ = QKeySequence();
}
} // namespace lyx
#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>
namespace lyx {
/**
* A lineedit for inputting shortcuts
*/
class ShortcutEdit : public QLineEdit {
Q_OBJECT
public:
ShortcutEdit(QWidget * parent);
bool eventFilter( QObject*, QEvent* e );
protected Q_SLOTS:
bool event(QEvent* e);
void keyPressEvent(QKeyEvent * e);
void keyReleaseEvent(QKeyEvent *e);
Q_SIGNALS:
void shortcut(const QString &);
private:
QKeySequence appendToSequence(const QKeySequence & seq, int keyQt);
void done();
void reset();
void updateDisplay();
QKeySequence keysequence_;
uint nkey_;
uint modifierkeys_;
bool has_cursor_;
};
} // namespace lyx
#endif // SHORTCUTEDIT_H