diff options
author | Tomaz Canabrava <tomaz.canabrava@intel.com> | 2015-09-21 16:08:58 -0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-09-23 14:15:22 -0700 |
commit | c4c7e7a7f4d462490e8f0fea846f13bff6076845 (patch) | |
tree | 5f4ff830772fedfe8823b2a5db9c079e5067cf21 /qt-ui/locationinformation.cpp | |
parent | 012e8ccb46321258343504ecbb3f7fcca8929c24 (diff) | |
download | subsurface-c4c7e7a7f4d462490e8f0fea846f13bff6076845.tar.gz |
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 <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/locationinformation.cpp')
-rw-r--r-- | qt-ui/locationinformation.cpp | 56 |
1 files changed, 51 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 <QItemSelectionModel> #include <qmessagebox.h> #include <cstdlib> +#include <QDesktopWidget> +#include <QScrollBar> 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) { |