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.
This commit is contained in:
Thorbjørn Lindeijer
2008-12-18 16:56:43 +01:00
parent 6522b4b5a2
commit 719894044d
3 changed files with 26 additions and 14 deletions

View File

@@ -1603,7 +1603,6 @@ namespace TextEditor {
int firstColumn; int firstColumn;
int lastColumn; int lastColumn;
}; };
} }
} }

View File

@@ -39,8 +39,9 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QtCore/QEvent> #include <QtCore/QEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>
#include <QtGui/QKeyEvent>
#include <QtGui/QVBoxLayout> #include <QtGui/QVBoxLayout>
#include <limits.h> #include <limits.h>
@@ -130,6 +131,8 @@ CompletionWidget::CompletionWidget(CompletionSupport *support, ITextEditable *ed
layout->addWidget(this); layout->addWidget(this);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_popupFrame->setMinimumSize(1, 1);
setMinimumSize(1, 1);
} }
bool CompletionWidget::event(QEvent *e) bool CompletionWidget::event(QEvent *e)
@@ -227,20 +230,15 @@ void CompletionWidget::setCompletionItems(const QList<TextEditor::CompletionItem
void CompletionWidget::showCompletions(int startPos) void CompletionWidget::showCompletions(int startPos)
{ {
const QPoint &pos = m_editor->cursorRect(startPos).bottomLeft(); updatePositionAndSize(startPos);
m_popupFrame->move(pos.x() - 16, pos.y());
m_popupFrame->setMinimumSize(1, 1);
setMinimumSize(1, 1);
updateSize();
m_popupFrame->show(); m_popupFrame->show();
show(); show();
setFocus(); setFocus();
} }
void CompletionWidget::updateSize() void CompletionWidget::updatePositionAndSize(int startPos)
{ {
// Determine size by calculating the space of the visible items
int visibleItems = m_model->rowCount(); int visibleItems = m_model->rowCount();
if (visibleItems > NUMBER_OF_VISIBLE_ITEMS) if (visibleItems > NUMBER_OF_VISIBLE_ITEMS)
visibleItems = NUMBER_OF_VISIBLE_ITEMS; visibleItems = NUMBER_OF_VISIBLE_ITEMS;
@@ -254,10 +252,25 @@ void CompletionWidget::updateSize()
shint = tmp; shint = tmp;
} }
const int width = (shint.width() + (m_popupFrame->frameWidth() * 2) + 30); const int frameWidth = m_popupFrame->frameWidth();
const int height = (shint.height() * visibleItems) + m_popupFrame->frameWidth() * 2; const int width = shint.width() + frameWidth * 2 + 30;
const int height = shint.height() * visibleItems + frameWidth * 2;
m_popupFrame->resize(width, height); // 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->setGeometry(pos.x(), pos.y(), width, height);
} }
void CompletionWidget::completionActivated(const QModelIndex &index) void CompletionWidget::completionActivated(const QModelIndex &index)

View File

@@ -74,7 +74,7 @@ private slots:
void completionActivated(const QModelIndex &index); void completionActivated(const QModelIndex &index);
private: private:
void updateSize(); void updatePositionAndSize(int startPos);
QPointer<QFrame> m_popupFrame; QPointer<QFrame> m_popupFrame;
bool m_blockFocusOut; bool m_blockFocusOut;