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>
This commit is contained in:
Tomaz Canabrava 2015-09-21 16:08:58 -03:00 committed by Dirk Hohndel
parent 012e8ccb46
commit c4c7e7a7f4
2 changed files with 54 additions and 5 deletions

View file

@ -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)
{

View file

@ -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;