OSDN Git Service

CodeAssist: Relax a bit premature match
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / codeassist / basicproposalitem.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "basicproposalitem.h"
34
35 #include <texteditor/basetexteditor.h>
36 #include <texteditor/quickfix.h>
37
38 #include <QtGui/QTextCursor>
39
40 using namespace TextEditor;
41
42 BasicProposalItem::BasicProposalItem()
43     : m_order(0)
44 {}
45
46 BasicProposalItem::~BasicProposalItem()
47 {}
48
49 void BasicProposalItem::setIcon(const QIcon &icon)
50 {
51     m_icon = icon;
52 }
53
54 const QIcon &BasicProposalItem::icon() const
55 {
56     return m_icon;
57 }
58
59 void BasicProposalItem::setText(const QString &text)
60 {
61     m_text = text;
62 }
63
64 QString BasicProposalItem::text() const
65 {
66     return m_text;
67 }
68
69 void BasicProposalItem::setDetail(const QString &detail)
70 {
71     m_detail = detail;
72 }
73
74 const QString &BasicProposalItem::detail() const
75 {
76     return m_detail;
77 }
78
79 void BasicProposalItem::setData(const QVariant &var)
80 {
81     m_data = var;
82 }
83
84 const QVariant &BasicProposalItem::data() const
85 {
86     return m_data;
87 }
88
89 int BasicProposalItem::order() const
90 {
91     return m_order;
92 }
93
94 void BasicProposalItem::setOrder(int order)
95 {
96     m_order = order;
97 }
98
99 bool BasicProposalItem::implicitlyApplies() const
100 {
101     return !data().canConvert<QString>() && !data().canConvert<QuickFixOperation::Ptr>();
102 }
103
104 bool BasicProposalItem::prematurelyApplies(const QChar &c) const
105 {
106     Q_UNUSED(c);
107     return false;
108 }
109
110 void BasicProposalItem::apply(BaseTextEditor *editor, int basePosition) const
111 {
112     if (data().canConvert<QString>())
113         applySnippet(editor, basePosition);
114     else if (data().canConvert<QuickFixOperation::Ptr>())
115         applyQuickFix(editor, basePosition);
116     else
117         applyContextualContent(editor, basePosition);
118 }
119
120 void BasicProposalItem::applyContextualContent(BaseTextEditor *editor, int basePosition) const
121 {
122     const int currentPosition = editor->position();
123     editor->setCursorPosition(basePosition);
124     editor->replace(currentPosition - basePosition, text());
125 }
126
127 void BasicProposalItem::applySnippet(BaseTextEditor *editor, int basePosition) const
128 {
129     BaseTextEditorWidget *editorWidget = static_cast<BaseTextEditorWidget *>(editor->widget());
130     QTextCursor tc = editorWidget->textCursor();
131     tc.setPosition(basePosition, QTextCursor::KeepAnchor);
132     editorWidget->insertCodeSnippet(tc, data().toString());
133 }
134
135 void BasicProposalItem::applyQuickFix(BaseTextEditor *editor, int basePosition) const
136 {
137     Q_UNUSED(editor)
138     Q_UNUSED(basePosition)
139
140     QuickFixOperation::Ptr op = data().value<QuickFixOperation::Ptr>();
141     op->perform();
142 }