OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / snippets / snippetscollection.h
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 qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef SNIPPETSCOLLECTION_H
34 #define SNIPPETSCOLLECTION_H
35
36 #include "snippet.h"
37
38 #include <QtCore/QVector>
39 #include <QtCore/QStringList>
40 #include <QtCore/QHash>
41 #include <QtCore/QXmlStreamWriter>
42
43 namespace TextEditor {
44 namespace Internal {
45
46 // Characteristics of this collection:
47 // - Store snippets by group.
48 // - Keep groups of snippets sorted.
49 // - Allow snippet insertion/replacement based on a hint.
50 // - Allow modification of snippet members that are not sorting keys.
51 // - Track removed/modified built-in snippets.
52 // - Provide fast index access.
53 // - Not thread-safe.
54
55 class SnippetsCollection : public QObject
56 {
57     Q_OBJECT
58 public:
59     virtual ~SnippetsCollection();
60
61     static SnippetsCollection *instance();
62
63     class Hint
64     {
65         friend class SnippetsCollection;
66     public:
67         int index() const;
68     private:
69         explicit Hint(int index);
70         Hint(int index, QList<Snippet>::iterator it);
71         int m_index;
72         QList<Snippet>::iterator m_it;
73     };
74
75     void insertSnippet(const Snippet &snippet);
76     void insertSnippet(const Snippet &snippet, const Hint &hint);
77     Hint computeInsertionHint(const Snippet &snippet);
78
79     // Replace snippets only within the same group.
80     void replaceSnippet(int index, const Snippet &snippet);
81     void replaceSnippet(int index, const Snippet &snippet, const Hint &hint);
82     Hint computeReplacementHint(int index, const Snippet &snippet);
83
84     void removeSnippet(int index, const QString &groupId);
85     void restoreRemovedSnippets(const QString &groupId);
86
87     void setSnippetContent(int index, const QString &groupId, const QString &content);
88
89     const Snippet &snippet(int index, const QString &groupId) const;
90     Snippet revertedSnippet(int index, const QString &groupId) const;
91
92     void reset(const QString &groupId);
93
94     int totalActiveSnippets(const QString &groupId) const;
95     int totalSnippets(const QString &groupId) const;
96
97     QList<QString> groupIds() const;
98
99     void reload();
100     void synchronize();
101
102 private slots:
103     void identifyGroups();
104
105 private:
106     SnippetsCollection();
107     Q_DISABLE_COPY(SnippetsCollection)
108
109     int groupIndex(const QString &groupId) const;
110     bool isGroupKnown(const QString &groupId) const;
111
112     void clearSnippets();
113     void clearSnippets(int groupIndex);
114
115     void updateActiveSnippetsEnd(int groupIndex);
116
117     QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
118     void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
119
120     QList<Snippet> allBuiltInSnippets() const;
121
122     static const QLatin1String kSnippet;
123     static const QLatin1String kSnippets;
124     static const QLatin1String kTrigger;
125     static const QLatin1String kId;
126     static const QLatin1String kComplement;
127     static const QLatin1String kGroup;
128     static const QLatin1String kRemoved;
129     static const QLatin1String kModified;
130
131     // Built-in snippets are specified in XMLs distributed in a system's folder. Snippets
132     // created or modified/removed (if they are built-ins) by the user are stored in user's
133     // folder.
134     QString m_userSnippetsPath;
135     QString m_userSnippetsFile;
136     QStringList m_builtInSnippetsFiles;
137
138     // Snippets for each group are kept in a list. However, not all of them are necessarily
139     // active. Specifically, removed built-in snippets are kept as the last ones (for each
140     // group there is a iterator that marks the logical end).
141     QVector<QList<Snippet> > m_snippets;
142     QVector<QList<Snippet>::iterator> m_activeSnippetsEnd;
143
144     QHash<QString, int> m_groupIndexById;
145 };
146
147 } // Internal
148 } // TextEditor
149
150 #endif // SNIPPETSCOLLECTION_H