Commit 71989404 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

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.
parent 6522b4b5
...@@ -1603,7 +1603,6 @@ namespace TextEditor { ...@@ -1603,7 +1603,6 @@ namespace TextEditor {
int firstColumn; int firstColumn;
int lastColumn; int lastColumn;
}; };
} }
} }
......
...@@ -39,8 +39,9 @@ ...@@ -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 ...@@ -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 ...@@ -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() ...@@ -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;
// 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) void CompletionWidget::completionActivated(const QModelIndex &index)
......
...@@ -74,7 +74,7 @@ private slots: ...@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment