1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "completionwidget.h"
35 #include "completionsupport.h"
36 #include "icompletioncollector.h"
38 #include <texteditor/itexteditor.h>
40 #include <utils/faketooltip.h>
41 #include <utils/qtcassert.h>
43 #include <QtCore/QEvent>
44 #include <QtGui/QApplication>
45 #include <QtGui/QDesktopWidget>
46 #include <QtGui/QKeyEvent>
47 #include <QtGui/QVBoxLayout>
48 #include <QtGui/QScrollBar>
49 #include <QtGui/QLabel>
50 #include <QtGui/QStylePainter>
51 #include <QtGui/QToolTip>
55 using namespace TextEditor;
56 using namespace TextEditor::Internal;
58 #define NUMBER_OF_VISIBLE_ITEMS 10
60 namespace TextEditor {
63 class AutoCompletionModel : public QAbstractListModel
66 AutoCompletionModel(QObject *parent);
68 inline const CompletionItem &itemAt(const QModelIndex &index) const
69 { return m_items.at(index.row()); }
71 void setItems(const QList<CompletionItem> &items);
73 int rowCount(const QModelIndex &parent = QModelIndex()) const;
74 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
77 QList<CompletionItem> m_items;
81 class CompletionInfoFrame : public Utils::FakeToolTip
84 CompletionInfoFrame(QWidget *parent = 0) :
85 Utils::FakeToolTip(parent),
86 m_label(new QLabel(this))
88 QVBoxLayout *layout = new QVBoxLayout(this);
90 layout->setSpacing(0);
91 layout->addWidget(m_label);
93 m_label->setForegroundRole(QPalette::ToolTipText);
94 m_label->setBackgroundRole(QPalette::ToolTipBase);
97 void setText(const QString &text)
99 m_label->setText(text);
107 } // namespace Internal
108 } // namespace TextEditor
111 AutoCompletionModel::AutoCompletionModel(QObject *parent)
112 : QAbstractListModel(parent)
116 void AutoCompletionModel::setItems(const QList<CompletionItem> &items)
122 int AutoCompletionModel::rowCount(const QModelIndex &) const
124 return m_items.count();
127 QVariant AutoCompletionModel::data(const QModelIndex &index, int role) const
129 if (index.row() >= m_items.count())
132 if (role == Qt::DisplayRole) {
133 return itemAt(index).text;
134 } else if (role == Qt::DecorationRole) {
135 return itemAt(index).icon;
136 } else if (role == Qt::WhatsThisRole) {
137 return itemAt(index).details;
144 CompletionWidget::CompletionWidget(CompletionSupport *support,
146 : QFrame(0, Qt::Popup),
149 m_completionListView(new CompletionListView(support, editor, this))
151 // We disable the frame on this list view and use a QFrame around it instead.
152 // This improves the look with QGTKStyle.
154 setFrameStyle(m_completionListView->frameStyle());
156 m_completionListView->setFrameStyle(QFrame::NoFrame);
158 setObjectName(QLatin1String("m_popupFrame"));
159 setAttribute(Qt::WA_DeleteOnClose);
160 setMinimumSize(1, 1);
161 setFont(editor->widget()->font());
163 QVBoxLayout *layout = new QVBoxLayout(this);
164 layout->setMargin(0);
166 layout->addWidget(m_completionListView);
167 setFocusProxy(m_completionListView);
169 connect(m_completionListView, SIGNAL(itemSelected(TextEditor::CompletionItem)),
170 this, SIGNAL(itemSelected(TextEditor::CompletionItem)));
171 connect(m_completionListView, SIGNAL(completionListClosed()),
172 this, SIGNAL(completionListClosed()));
173 connect(m_completionListView, SIGNAL(activated(QModelIndex)),
174 SLOT(closeList(QModelIndex)));
175 connect(editor, SIGNAL(contentsChangedBecauseOfUndo()),
176 this, SLOT(closeList()));
179 CompletionWidget::~CompletionWidget()
183 void CompletionWidget::setCompletionItems(const QList<TextEditor::CompletionItem> &completionitems)
185 m_completionListView->setCompletionItems(completionitems);
188 void CompletionWidget::closeList(const QModelIndex &index)
190 m_completionListView->closeList(index);
194 void CompletionWidget::showCompletions(int startPos)
197 updatePositionAndSize(startPos);
202 QChar CompletionWidget::typedChar() const
204 return m_completionListView->m_typedChar;
207 CompletionItem CompletionWidget::currentCompletionItem() const
209 return m_completionListView->currentCompletionItem();
212 bool CompletionWidget::explicitlySelected() const
214 return m_completionListView->explicitlySelected();
217 void CompletionWidget::setCurrentIndex(int index)
219 m_completionListView->setCurrentIndex(m_completionListView->model()->index(index, 0));
222 void CompletionWidget::updatePositionAndSize(int startPos)
224 // Determine size by calculating the space of the visible items
225 QAbstractItemModel *model = m_completionListView->model();
226 int visibleItems = model->rowCount();
227 if (visibleItems > NUMBER_OF_VISIBLE_ITEMS)
228 visibleItems = NUMBER_OF_VISIBLE_ITEMS;
230 const QStyleOptionViewItem &option = m_completionListView->viewOptions();
233 for (int i = 0; i < visibleItems; ++i) {
234 QSize tmp = m_completionListView->itemDelegate()->sizeHint(option, model->index(i, 0));
235 if (shint.width() < tmp.width())
239 const int fw = frameWidth();
240 const int width = shint.width() + fw * 2 + 30;
241 const int height = shint.height() * visibleItems + fw * 2;
243 // Determine the position, keeping the popup on the screen
244 const QRect cursorRect = m_editor->cursorRect(startPos);
245 const QDesktopWidget *desktop = QApplication::desktop();
247 QWidget *editorWidget = m_editor->widget();
250 const QRect screen = desktop->availableGeometry(desktop->screenNumber(editorWidget));
252 const QRect screen = desktop->screenGeometry(desktop->screenNumber(editorWidget));
255 QPoint pos = cursorRect.bottomLeft();
256 pos.rx() -= 16 + fw; // Space for the icons
258 if (pos.y() + height > screen.bottom())
259 pos.setY(cursorRect.top() - height);
261 if (pos.x() + width > screen.right())
262 pos.setX(screen.right() - width);
264 setGeometry(pos.x(), pos.y(), width, height);
267 CompletionListView::CompletionListView(CompletionSupport *support,
268 ITextEditor *editor, CompletionWidget *completionWidget)
269 : QListView(completionWidget),
270 m_blockFocusOut(false),
272 m_editorWidget(editor->widget()),
273 m_completionWidget(completionWidget),
274 m_model(new AutoCompletionModel(this)),
276 m_explicitlySelected(false)
278 QTC_ASSERT(m_editorWidget, return);
280 m_infoTimer.setInterval(1000);
281 m_infoTimer.setSingleShot(true);
282 connect(&m_infoTimer, SIGNAL(timeout()), SLOT(maybeShowInfoTip()));
284 setAttribute(Qt::WA_MacShowFocusRect, false);
285 setUniformItemSizes(true);
286 setSelectionBehavior(QAbstractItemView::SelectItems);
287 setSelectionMode(QAbstractItemView::SingleSelection);
288 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
289 setMinimumSize(1, 1);
292 if (horizontalScrollBar())
293 horizontalScrollBar()->setAttribute(Qt::WA_MacMiniSize);
294 if (verticalScrollBar())
295 verticalScrollBar()->setAttribute(Qt::WA_MacMiniSize);
299 CompletionListView::~CompletionListView()
303 CompletionItem CompletionListView::currentCompletionItem() const
305 int row = currentIndex().row();
306 if (row >= 0 && row < m_model->rowCount())
307 return m_model->itemAt(currentIndex());
309 return CompletionItem();
312 bool CompletionListView::explicitlySelected() const
314 return m_explicitlySelected;
317 void CompletionListView::maybeShowInfoTip()
319 QModelIndex current = currentIndex();
320 if (!current.isValid())
322 QString infoTip = current.data(Qt::WhatsThisRole).toString();
324 if (infoTip.isEmpty()) {
325 delete m_infoFrame.data();
326 m_infoTimer.setInterval(200);
330 if (m_infoFrame.isNull())
331 m_infoFrame = new CompletionInfoFrame(this);
334 QRect r = rectForIndex(current);
336 (parentWidget()->mapToGlobal(
337 parentWidget()->rect().topRight())).x() + 3,
338 mapToGlobal(r.topRight()).y() - verticalOffset()
340 m_infoFrame->setText(infoTip);
341 m_infoFrame->adjustSize();
343 m_infoFrame->raise();
345 m_infoTimer.setInterval(0);
348 void CompletionListView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
350 QListView::currentChanged(current, previous);
355 bool CompletionListView::event(QEvent *e)
358 return QListView::event(e);
360 bool forwardKeys = true;
361 if (e->type() == QEvent::FocusOut) {
363 #if defined(Q_OS_DARWIN) && ! defined(QT_MAC_USE_COCOA)
364 QFocusEvent *fe = static_cast<QFocusEvent *>(e);
365 if (fe->reason() == Qt::OtherFocusReason) {
366 // Qt/carbon workaround
367 // focus out is received before the key press event.
368 index = currentIndex();
371 m_completionWidget->closeList(index);
373 m_infoFrame->close();
375 } else if (e->type() == QEvent::ShortcutOverride) {
376 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
380 // select next/previous completion
381 if (ke->modifiers() == Qt::ControlModifier)
384 int change = (ke->key() == Qt::Key_N) ? 1 : -1;
385 int nrows = model()->rowCount();
386 int row = currentIndex().row();
387 int newRow = (row + change + nrows) % nrows;
388 if (newRow == row + change || !ke->isAutoRepeat())
389 setCurrentIndex(m_model->index(newRow));
393 } else if (e->type() == QEvent::KeyPress) {
394 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
398 // select next/previous completion - so don't pass on to editor
399 if (ke->modifiers() == Qt::ControlModifier)
404 m_completionWidget->closeList();
411 // We want these navigation keys to work in the editor, so forward them
416 //independently from style, accept current entry if return is pressed
417 if (qApp->focusWidget() == this)
418 m_completionWidget->closeList(currentIndex());
422 m_explicitlySelected = true;
423 if (!ke->isAutoRepeat()
424 && currentIndex().row() == 0) {
425 setCurrentIndex(model()->index(model()->rowCount()-1, 0));
432 m_explicitlySelected = true;
433 if (!ke->isAutoRepeat()
434 && currentIndex().row() == model()->rowCount()-1) {
435 setCurrentIndex(model()->index(0, 0));
442 case Qt::Key_PageDown:
448 // if a key is forwarded, completion widget is re-opened and selected item is reset to first,
449 // so only forward keys that insert text and refine the completed item
450 forwardKeys = !ke->text().isEmpty();
454 const CompletionPolicy policy = m_support->policy();
455 if (forwardKeys && policy != QuickFixCompletion) {
456 if (ke->text().length() == 1 && currentIndex().isValid() && qApp->focusWidget() == this) {
457 QChar typedChar = ke->text().at(0);
458 const CompletionItem &item = m_model->itemAt(currentIndex());
459 if (item.collector->typedCharCompletes(item, typedChar)) {
460 m_typedChar = typedChar;
461 m_completionWidget->closeList(currentIndex());
466 m_blockFocusOut = true;
467 QApplication::sendEvent(m_editorWidget, e);
468 m_blockFocusOut = false;
470 // Have the completion support update the list of items
471 m_support->complete(m_editor, policy, false);
476 return QListView::event(e);
479 void CompletionListView::keyboardSearch(const QString &search)
484 void CompletionListView::setCompletionItems(const QList<TextEditor::CompletionItem> &completionItems)
486 m_model->setItems(completionItems);
487 setCurrentIndex(m_model->index(0)); // Select the first item
490 void CompletionListView::closeList(const QModelIndex &index)
492 m_blockFocusOut = true;
495 emit itemSelected(m_model->itemAt(index));
497 emit completionListClosed();
499 m_blockFocusOut = false;