Start to handle keypresses

Keypress management is one of the main functions of the completer, so we
must create an event filter and hook things up properly.
key esq / enter should close the popup (and not leave us with a popup open
and no way to close it - it breaks X)

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2015-09-21 16:51:39 -03:00 committed by Dirk Hohndel
parent c4c7e7a7f4
commit fdd28fddf2
2 changed files with 42 additions and 5 deletions

View file

@ -419,12 +419,41 @@ DiveLocationLineEdit::DiveLocationLineEdit(QWidget *parent)
proxy->setSourceModel(model); proxy->setSourceModel(model);
proxy->setFilterKeyColumn(DiveLocationModel::NAME); proxy->setFilterKeyColumn(DiveLocationModel::NAME);
view->setModel(proxy); view->setModel(proxy);
view->setModelColumn(DiveLocationModel::NAME); view->setModelColumn(DiveLocationModel::NAME);
view->setItemDelegate(new LocationFilterDelegate()); view->setItemDelegate(new LocationFilterDelegate());
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setParent(0, Qt::Popup);
view->installEventFilter(this);
view->setFocusProxy(this);
connect(this, &QLineEdit::textEdited, this, &DiveLocationLineEdit::setTemporaryDiveSiteName); connect(this, &QLineEdit::textEdited, this, &DiveLocationLineEdit::setTemporaryDiveSiteName);
} }
bool DiveLocationLineEdit::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress) {
QKeyEvent *keyEv = (QKeyEvent*) e;
qDebug() << view->focusProxy()->objectName();
if (keyEv->key() == Qt::Key_Escape) {
view->hide();
return true;
}
if(keyEv->key() == Qt::Key_Return) {
view->hide();
return false;
}
}
return false;
}
void DiveLocationLineEdit::refreshDiveSiteCache() void DiveLocationLineEdit::refreshDiveSiteCache()
{ {
model->resetModel(); model->resetModel();
@ -462,12 +491,17 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString& s)
void DiveLocationLineEdit::keyPressEvent(QKeyEvent *ev) void DiveLocationLineEdit::keyPressEvent(QKeyEvent *ev)
{ {
QLineEdit::keyPressEvent(ev); qDebug() << "Pressing key" << ev->key();
if(ev->key() != Qt::Key_Left && ev->key() != Qt::Key_Right && !view->isVisible()) { if(ev->key() != Qt::Key_Left &&
qDebug() << "Showing popup"; ev->key() != Qt::Key_Right &&
ev->key() != Qt::Key_Escape &&
ev->key() != Qt::Key_Return &&
!view->isVisible()) {
showPopup(); showPopup();
} else if (ev->key() == Qt::Key_Escape) {
view->hide();
} else { } else {
qDebug() << "Not showing popup"; QLineEdit::keyPressEvent(ev);
} }
} }
@ -505,8 +539,10 @@ void DiveLocationLineEdit::showPopup()
view->setGeometry(pos.x(), pos.y(), w, h); view->setGeometry(pos.x(), pos.y(), w, h);
if (!view->isVisible()) if (!view->isVisible()) {
view->show(); view->show();
view->setFocus();
}
} }
DiveLocationListView::DiveLocationListView(QWidget *parent) DiveLocationListView::DiveLocationListView(QWidget *parent)

View file

@ -93,6 +93,7 @@ public:
DiveLocationLineEdit(QWidget *parent =0 ); DiveLocationLineEdit(QWidget *parent =0 );
void refreshDiveSiteCache(); void refreshDiveSiteCache();
void setTemporaryDiveSiteName(const QString& s); void setTemporaryDiveSiteName(const QString& s);
bool eventFilter(QObject*, QEvent*);
protected: protected:
void keyPressEvent(QKeyEvent *ev); void keyPressEvent(QKeyEvent *ev);
void showPopup(); void showPopup();