From c4c7e7a7f4d462490e8f0fea846f13bff6076845 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 21 Sep 2015 16:08:58 -0300 Subject: [PATCH] Display the popup in the correct place The popup should be shown beneath the QLineEdit. this code here is shamelessy stolen from the QCompleter source code because I really didn't want to rethink the correct way of doing this. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/locationinformation.cpp | 56 +++++++++++++++++++++++++++++++---- qt-ui/locationinformation.h | 3 ++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/qt-ui/locationinformation.cpp b/qt-ui/locationinformation.cpp index 087e31fc9..27cbc61a3 100644 --- a/qt-ui/locationinformation.cpp +++ b/qt-ui/locationinformation.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include LocationInformationWidget::LocationInformationWidget(QWidget *parent) : QGroupBox(parent), modified(false) { @@ -421,11 +423,6 @@ DiveLocationLineEdit::DiveLocationLineEdit(QWidget *parent) view->setModelColumn(DiveLocationModel::NAME); view->setItemDelegate(new LocationFilterDelegate()); connect(this, &QLineEdit::textEdited, this, &DiveLocationLineEdit::setTemporaryDiveSiteName); - - //HACK: - /* This is being show currently just to test. */ - qDebug() << model->rowCount() << model->columnCount(); - view->show(); } void DiveLocationLineEdit::refreshDiveSiteCache() @@ -463,6 +460,55 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString& s) proxy->invalidate(); } +void DiveLocationLineEdit::keyPressEvent(QKeyEvent *ev) +{ + QLineEdit::keyPressEvent(ev); + if(ev->key() != Qt::Key_Left && ev->key() != Qt::Key_Right && !view->isVisible()) { + qDebug() << "Showing popup"; + showPopup(); + } else { + qDebug() << "Not showing popup"; + } +} + +void DiveLocationLineEdit::showPopup() +{ + const QRect screen = QApplication::desktop()->availableGeometry(this); + const int maxVisibleItems = 5; + Qt::LayoutDirection dir = layoutDirection(); + QPoint pos; + int rh, w; + int h = (view->sizeHintForRow(0) * qMin(maxVisibleItems, view->model()->rowCount()) + 3) + 3; + QScrollBar *hsb = view->horizontalScrollBar(); + if (hsb && hsb->isVisible()) + h += view->horizontalScrollBar()->sizeHint().height(); + + rh = height(); + pos = mapToGlobal(QPoint(0, height() - 2)); + w = width(); + + if (w > screen.width()) + w = screen.width(); + if ((pos.x() + w) > (screen.x() + screen.width())) + pos.setX(screen.x() + screen.width() - w); + if (pos.x() < screen.x()) + pos.setX(screen.x()); + + int top = pos.y() - rh - screen.top() + 2; + int bottom = screen.bottom() - pos.y(); + h = qMax(h, view->minimumHeight()); + if (h > bottom) { + h = qMin(qMax(top, bottom), h); + if (top > bottom) + pos.setY(pos.y() - h - rh + 2); + } + + view->setGeometry(pos.x(), pos.y(), w, h); + + if (!view->isVisible()) + view->show(); +} + DiveLocationListView::DiveLocationListView(QWidget *parent) { diff --git a/qt-ui/locationinformation.h b/qt-ui/locationinformation.h index 16d5a3eae..b60c50606 100644 --- a/qt-ui/locationinformation.h +++ b/qt-ui/locationinformation.h @@ -93,6 +93,9 @@ public: DiveLocationLineEdit(QWidget *parent =0 ); void refreshDiveSiteCache(); void setTemporaryDiveSiteName(const QString& s); +protected: + void keyPressEvent(QKeyEvent *ev); + void showPopup(); private: DiveLocationFilterProxyModel *proxy; DiveLocationModel *model;