OSDN Git Service

ae3dc748d08fa1e6c38cdf4f4f47091741650735
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / completionwidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "completionwidget.h"
35 #include "completionsupport.h"
36 #include "icompletioncollector.h"
37
38 #include <texteditor/itexteditor.h>
39
40 #include <utils/faketooltip.h>
41 #include <utils/qtcassert.h>
42
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>
52
53 #include <limits.h>
54
55 using namespace TextEditor;
56 using namespace TextEditor::Internal;
57
58 #define NUMBER_OF_VISIBLE_ITEMS 10
59
60 namespace TextEditor {
61 namespace Internal {
62
63 class AutoCompletionModel : public QAbstractListModel
64 {
65 public:
66     AutoCompletionModel(QObject *parent);
67
68     inline const CompletionItem &itemAt(const QModelIndex &index) const
69     { return m_items.at(index.row()); }
70
71     void setItems(const QList<CompletionItem> &items);
72
73     int rowCount(const QModelIndex &parent = QModelIndex()) const;
74     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
75
76 private:
77     QList<CompletionItem> m_items;
78 };
79
80
81 class CompletionInfoFrame : public Utils::FakeToolTip
82 {
83 public:
84     CompletionInfoFrame(QWidget *parent = 0) :
85         Utils::FakeToolTip(parent),
86         m_label(new QLabel(this))
87     {
88         QVBoxLayout *layout = new QVBoxLayout(this);
89         layout->setMargin(0);
90         layout->setSpacing(0);
91         layout->addWidget(m_label);
92
93         m_label->setForegroundRole(QPalette::ToolTipText);
94         m_label->setBackgroundRole(QPalette::ToolTipBase);
95     }
96
97     void setText(const QString &text)
98     {
99         m_label->setText(text);
100     }
101
102 private:
103     QLabel *m_label;
104 };
105
106
107 } // namespace Internal
108 } // namespace TextEditor
109
110
111 AutoCompletionModel::AutoCompletionModel(QObject *parent)
112     : QAbstractListModel(parent)
113 {
114 }
115
116 void AutoCompletionModel::setItems(const QList<CompletionItem> &items)
117 {
118     m_items = items;
119     reset();
120 }
121
122 int AutoCompletionModel::rowCount(const QModelIndex &) const
123 {
124     return m_items.count();
125 }
126
127 QVariant AutoCompletionModel::data(const QModelIndex &index, int role) const
128 {
129     if (index.row() >= m_items.count())
130         return QVariant();
131
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;
138     }
139
140     return QVariant();
141 }
142
143
144 CompletionWidget::CompletionWidget(CompletionSupport *support,
145         ITextEditor *editor)
146     : QFrame(0, Qt::Popup),
147       m_support(support),
148       m_editor(editor),
149       m_completionListView(new CompletionListView(support, editor, this))
150 {
151     // We disable the frame on this list view and use a QFrame around it instead.
152     // This improves the look with QGTKStyle.
153 #ifndef Q_WS_MAC
154     setFrameStyle(m_completionListView->frameStyle());
155 #endif
156     m_completionListView->setFrameStyle(QFrame::NoFrame);
157
158     setObjectName(QLatin1String("m_popupFrame"));
159     setAttribute(Qt::WA_DeleteOnClose);
160     setMinimumSize(1, 1);
161     setFont(editor->widget()->font());
162
163     QVBoxLayout *layout = new QVBoxLayout(this);
164     layout->setMargin(0);
165
166     layout->addWidget(m_completionListView);
167     setFocusProxy(m_completionListView);
168
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()));
177 }
178
179 CompletionWidget::~CompletionWidget()
180 {
181 }
182
183 void CompletionWidget::setCompletionItems(const QList<TextEditor::CompletionItem> &completionitems)
184 {
185     m_completionListView->setCompletionItems(completionitems);
186 }
187
188 void CompletionWidget::closeList(const QModelIndex &index)
189 {
190     m_completionListView->closeList(index);
191     close();
192 }
193
194 void CompletionWidget::showCompletions(int startPos)
195 {
196     ensurePolished();
197     updatePositionAndSize(startPos);
198     show();
199     setFocus();
200 }
201
202 QChar CompletionWidget::typedChar() const
203 {
204     return m_completionListView->m_typedChar;
205 }
206
207 CompletionItem CompletionWidget::currentCompletionItem() const
208 {
209     return m_completionListView->currentCompletionItem();
210 }
211
212 bool CompletionWidget::explicitlySelected() const
213 {
214     return m_completionListView->explicitlySelected();
215 }
216
217 void CompletionWidget::setCurrentIndex(int index)
218 {
219     m_completionListView->setCurrentIndex(m_completionListView->model()->index(index, 0));
220 }
221
222 void CompletionWidget::updatePositionAndSize(int startPos)
223 {
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;
229
230     const QStyleOptionViewItem &option = m_completionListView->viewOptions();
231
232     QSize shint;
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())
236             shint = tmp;
237     }
238
239     const int fw = frameWidth();
240     const int width = shint.width() + fw * 2 + 30;
241     const int height = shint.height() * visibleItems + fw * 2;
242
243     // Determine the position, keeping the popup on the screen
244     const QRect cursorRect = m_editor->cursorRect(startPos);
245     const QDesktopWidget *desktop = QApplication::desktop();
246
247     QWidget *editorWidget = m_editor->widget();
248
249 #ifdef Q_WS_MAC
250     const QRect screen = desktop->availableGeometry(desktop->screenNumber(editorWidget));
251 #else
252     const QRect screen = desktop->screenGeometry(desktop->screenNumber(editorWidget));
253 #endif
254
255     QPoint pos = cursorRect.bottomLeft();
256     pos.rx() -= 16 + fw;    // Space for the icons
257
258     if (pos.y() + height > screen.bottom())
259         pos.setY(cursorRect.top() - height);
260
261     if (pos.x() + width > screen.right())
262         pos.setX(screen.right() - width);
263
264     setGeometry(pos.x(), pos.y(), width, height);
265 }
266
267 CompletionListView::CompletionListView(CompletionSupport *support,
268         ITextEditor *editor, CompletionWidget *completionWidget)
269     : QListView(completionWidget),
270       m_blockFocusOut(false),
271       m_editor(editor),
272       m_editorWidget(editor->widget()),
273       m_completionWidget(completionWidget),
274       m_model(new AutoCompletionModel(this)),
275       m_support(support),
276       m_explicitlySelected(false)
277 {
278     QTC_ASSERT(m_editorWidget, return);
279
280     m_infoTimer.setInterval(1000);
281     m_infoTimer.setSingleShot(true);
282     connect(&m_infoTimer, SIGNAL(timeout()), SLOT(maybeShowInfoTip()));
283
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);
290     setModel(m_model);
291 #ifdef Q_WS_MAC
292     if (horizontalScrollBar())
293         horizontalScrollBar()->setAttribute(Qt::WA_MacMiniSize);
294     if (verticalScrollBar())
295         verticalScrollBar()->setAttribute(Qt::WA_MacMiniSize);
296 #endif
297 }
298
299 CompletionListView::~CompletionListView()
300 {
301 }
302
303 CompletionItem CompletionListView::currentCompletionItem() const
304 {
305     int row = currentIndex().row();
306     if (row >= 0 && row < m_model->rowCount())
307         return m_model->itemAt(currentIndex());
308
309     return CompletionItem();
310 }
311
312 bool CompletionListView::explicitlySelected() const
313 {
314     return m_explicitlySelected;
315 }
316
317 void CompletionListView::maybeShowInfoTip()
318 {
319     QModelIndex current = currentIndex();
320     if (!current.isValid())
321         return;
322     QString infoTip = current.data(Qt::WhatsThisRole).toString();
323
324     if (infoTip.isEmpty()) {
325         delete m_infoFrame.data();
326         m_infoTimer.setInterval(200);
327         return;
328     }
329
330     if (m_infoFrame.isNull())
331         m_infoFrame = new CompletionInfoFrame(this);
332
333
334     QRect r = rectForIndex(current);
335     m_infoFrame->move(
336             (parentWidget()->mapToGlobal(
337                     parentWidget()->rect().topRight())).x() + 3,
338             mapToGlobal(r.topRight()).y() - verticalOffset()
339             );
340     m_infoFrame->setText(infoTip);
341     m_infoFrame->adjustSize();
342     m_infoFrame->show();
343     m_infoFrame->raise();
344
345     m_infoTimer.setInterval(0);
346 }
347
348 void CompletionListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
349 {
350     QListView::currentChanged(current, previous);
351     m_infoTimer.start();
352 }
353
354
355 bool CompletionListView::event(QEvent *e)
356 {
357     if (m_blockFocusOut)
358         return QListView::event(e);
359
360     bool forwardKeys = true;
361     if (e->type() == QEvent::FocusOut) {
362         QModelIndex index;
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();
369         }
370 #endif
371         m_completionWidget->closeList(index);
372         if (m_infoFrame)
373             m_infoFrame->close();
374         return true;
375     } else if (e->type() == QEvent::ShortcutOverride) {
376         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
377         switch (ke->key()) {
378         case Qt::Key_N:
379         case Qt::Key_P:
380             // select next/previous completion
381             if (ke->modifiers() == Qt::ControlModifier)
382             {
383                 e->accept();
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));
390                 return true;
391             }
392         }
393     } else if (e->type() == QEvent::KeyPress) {
394         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
395         switch (ke->key()) {
396         case Qt::Key_N:
397         case Qt::Key_P:
398             // select next/previous completion - so don't pass on to editor
399             if (ke->modifiers() == Qt::ControlModifier)
400                 forwardKeys = false;
401             break;
402
403         case Qt::Key_Escape:
404             m_completionWidget->closeList();
405             return true;
406
407         case Qt::Key_Right:
408         case Qt::Key_Left:
409         case Qt::Key_Home:
410         case Qt::Key_End:
411             // We want these navigation keys to work in the editor, so forward them
412             break;
413
414         case Qt::Key_Tab:
415         case Qt::Key_Return:
416             //independently from style, accept current entry if return is pressed
417             if (qApp->focusWidget() == this)
418                 m_completionWidget->closeList(currentIndex());
419             return true;
420
421         case Qt::Key_Up:
422             m_explicitlySelected = true;
423             if (!ke->isAutoRepeat()
424                 && currentIndex().row() == 0) {
425                 setCurrentIndex(model()->index(model()->rowCount()-1, 0));
426                 return true;
427             }
428             forwardKeys = false;
429             break;
430
431         case Qt::Key_Down:
432             m_explicitlySelected = true;
433             if (!ke->isAutoRepeat()
434                 && currentIndex().row() == model()->rowCount()-1) {
435                 setCurrentIndex(model()->index(0, 0));
436                 return true;
437             }
438             forwardKeys = false;
439             break;
440
441         case Qt::Key_Enter:
442         case Qt::Key_PageDown:
443         case Qt::Key_PageUp:
444             forwardKeys = false;
445             break;
446
447         default:
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();
451             break;
452         }
453
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());
462                     return true;
463                 }
464             }
465
466             m_blockFocusOut = true;
467             QApplication::sendEvent(m_editorWidget, e);
468             m_blockFocusOut = false;
469
470             // Have the completion support update the list of items
471             m_support->complete(m_editor, policy, false);
472
473             return true;
474         }
475     }
476     return QListView::event(e);
477 }
478
479 void CompletionListView::keyboardSearch(const QString &search)
480 {
481     Q_UNUSED(search)
482 }
483
484 void CompletionListView::setCompletionItems(const QList<TextEditor::CompletionItem> &completionItems)
485 {
486     m_model->setItems(completionItems);
487     setCurrentIndex(m_model->index(0)); // Select the first item
488 }
489
490 void CompletionListView::closeList(const QModelIndex &index)
491 {
492     m_blockFocusOut = true;
493
494     if (index.isValid())
495         emit itemSelected(m_model->itemAt(index));
496
497     emit completionListClosed();
498
499     m_blockFocusOut = false;
500 }