From 719894044d8b79cb24934747c0b7b8d1cbb9330f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com> Date: Thu, 18 Dec 2008 16:56:43 +0100 Subject: [PATCH] Keep completion popup on the screen Pop it upwards when it would otherwise go below the screen and shift it to the left when it would otherwise exit the screen on the right. --- src/plugins/texteditor/basetexteditor.cpp | 1 - src/plugins/texteditor/completionwidget.cpp | 37 ++++++++++++++------- src/plugins/texteditor/completionwidget.h | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 48de83d5ef2..ea4f665a7e9 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1603,7 +1603,6 @@ namespace TextEditor { int firstColumn; int lastColumn; }; - } } diff --git a/src/plugins/texteditor/completionwidget.cpp b/src/plugins/texteditor/completionwidget.cpp index 661cda0c322..f9057d39078 100644 --- a/src/plugins/texteditor/completionwidget.cpp +++ b/src/plugins/texteditor/completionwidget.cpp @@ -39,8 +39,9 @@ #include <utils/qtcassert.h> #include <QtCore/QEvent> -#include <QtGui/QKeyEvent> #include <QtGui/QApplication> +#include <QtGui/QDesktopWidget> +#include <QtGui/QKeyEvent> #include <QtGui/QVBoxLayout> #include <limits.h> @@ -130,6 +131,8 @@ CompletionWidget::CompletionWidget(CompletionSupport *support, ITextEditable *ed layout->addWidget(this); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_popupFrame->setMinimumSize(1, 1); + setMinimumSize(1, 1); } bool CompletionWidget::event(QEvent *e) @@ -227,20 +230,15 @@ void CompletionWidget::setCompletionItems(const QList<TextEditor::CompletionItem void CompletionWidget::showCompletions(int startPos) { - const QPoint &pos = m_editor->cursorRect(startPos).bottomLeft(); - m_popupFrame->move(pos.x() - 16, pos.y()); - m_popupFrame->setMinimumSize(1, 1); - setMinimumSize(1, 1); - - updateSize(); - + updatePositionAndSize(startPos); m_popupFrame->show(); show(); setFocus(); } -void CompletionWidget::updateSize() +void CompletionWidget::updatePositionAndSize(int startPos) { + // Determine size by calculating the space of the visible items int visibleItems = m_model->rowCount(); if (visibleItems > NUMBER_OF_VISIBLE_ITEMS) visibleItems = NUMBER_OF_VISIBLE_ITEMS; @@ -254,10 +252,25 @@ void CompletionWidget::updateSize() shint = tmp; } - const int width = (shint.width() + (m_popupFrame->frameWidth() * 2) + 30); - const int height = (shint.height() * visibleItems) + m_popupFrame->frameWidth() * 2; + const int frameWidth = m_popupFrame->frameWidth(); + const int width = shint.width() + frameWidth * 2 + 30; + const int height = shint.height() * visibleItems + frameWidth * 2; + + // Determine the position, keeping the popup on the screen + const QRect cursorRect = m_editor->cursorRect(startPos); + const QDesktopWidget *desktop = QApplication::desktop(); + const QRect screen = desktop->availableGeometry(desktop->screenNumber(this)); + + QPoint pos = cursorRect.bottomLeft(); + pos.rx() -= 16 + frameWidth; // Space for the icons + + if (pos.y() + height > screen.bottom()) + pos.setY(cursorRect.top() - height); + + if (pos.x() + width > screen.right()) + pos.setX(screen.right() - width); - m_popupFrame->resize(width, height); + m_popupFrame->setGeometry(pos.x(), pos.y(), width, height); } void CompletionWidget::completionActivated(const QModelIndex &index) diff --git a/src/plugins/texteditor/completionwidget.h b/src/plugins/texteditor/completionwidget.h index c1fb28fc1cf..b124d2e257a 100644 --- a/src/plugins/texteditor/completionwidget.h +++ b/src/plugins/texteditor/completionwidget.h @@ -74,7 +74,7 @@ private slots: void completionActivated(const QModelIndex &index); private: - void updateSize(); + void updatePositionAndSize(int startPos); QPointer<QFrame> m_popupFrame; bool m_blockFocusOut; -- GitLab