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->setFilterKeyColumn(DiveLocationModel::NAME);
view->setModel(proxy);
view->setModelColumn(DiveLocationModel::NAME);
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);
}
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()
{
model->resetModel();
@ -462,12 +491,17 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString& s)
void DiveLocationLineEdit::keyPressEvent(QKeyEvent *ev)
{
QLineEdit::keyPressEvent(ev);
if(ev->key() != Qt::Key_Left && ev->key() != Qt::Key_Right && !view->isVisible()) {
qDebug() << "Showing popup";
qDebug() << "Pressing key" << ev->key();
if(ev->key() != Qt::Key_Left &&
ev->key() != Qt::Key_Right &&
ev->key() != Qt::Key_Escape &&
ev->key() != Qt::Key_Return &&
!view->isVisible()) {
showPopup();
} else if (ev->key() == Qt::Key_Escape) {
view->hide();
} else {
qDebug() << "Not showing popup";
QLineEdit::keyPressEvent(ev);
}
}
@ -505,8 +539,10 @@ void DiveLocationLineEdit::showPopup()
view->setGeometry(pos.x(), pos.y(), w, h);
if (!view->isVisible())
if (!view->isVisible()) {
view->show();
view->setFocus();
}
}
DiveLocationListView::DiveLocationListView(QWidget *parent)

View file

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