OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / snippets / snippet.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 "snippet.h"
35
36 #include <QtCore/QLatin1Char>
37 #include <QtCore/QLatin1String>
38 #include <QtGui/QTextDocument>
39
40 using namespace TextEditor;
41
42 const QChar Snippet::kVariableDelimiter(QLatin1Char('$'));
43
44 Snippet::Snippet(const QString &groupId, const QString &id) :
45     m_isRemoved(false), m_isModified(false), m_groupId(groupId), m_id(id)
46 {}
47
48 Snippet::~Snippet()
49 {}
50
51 const QString &Snippet::id() const
52 {
53     return m_id;
54 }
55
56 const QString &Snippet::groupId() const
57 {
58     return m_groupId;
59 }
60
61 bool Snippet::isBuiltIn() const
62 {
63     return !m_id.isEmpty();
64 }
65
66 void Snippet::setTrigger(const QString &trigger)
67 {
68     m_trigger = trigger;
69 }
70
71 const QString &Snippet::trigger() const
72 {
73     return m_trigger;
74 }
75
76 void Snippet::setContent(const QString &content)
77 {
78     m_content = content;
79 }
80
81 const QString &Snippet::content() const
82 {
83     return m_content;
84 }
85
86 void Snippet::setComplement(const QString &complement)
87 {
88     m_complement = complement;
89 }
90
91 const QString &Snippet::complement() const
92 {
93     return m_complement;
94 }
95
96 void Snippet::setIsRemoved(bool removed)
97 {
98     m_isRemoved = removed;
99 }
100
101 bool Snippet::isRemoved() const
102 {
103     return m_isRemoved;
104 }
105
106 void Snippet::setIsModified(bool modified)
107 {
108     m_isModified = modified;
109 }
110
111 bool Snippet::isModified() const
112 {
113     return m_isModified;
114 }
115
116 QString Snippet::generateTip() const
117 {
118     static const QLatin1Char kNewLine('\n');
119     static const QLatin1Char kSpace(' ');
120     static const QLatin1String kBr("<br>");
121     static const QLatin1String kNbsp("&nbsp;");
122     static const QLatin1String kNoBr("<nobr>");
123     static const QLatin1String kOpenBold("<b>");
124     static const QLatin1String kCloseBold("</b>");
125     static const QLatin1String kEllipsis("...");
126
127     QString escapedContent(Qt::escape(m_content));
128     escapedContent.replace(kNewLine, kBr);
129     escapedContent.replace(kSpace, kNbsp);
130
131     QString tip(kNoBr);
132     int count = 0;
133     for (int i = 0; i < escapedContent.count(); ++i) {
134         if (escapedContent.at(i) != kVariableDelimiter) {
135             tip += escapedContent.at(i);
136             continue;
137         }
138         if (++count % 2) {
139             tip += kOpenBold;
140         } else {
141             if (escapedContent.at(i-1) == kVariableDelimiter)
142                 tip += kEllipsis;
143             tip += kCloseBold;
144         }
145     }
146
147     return tip;
148 }