OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / classview / classviewutils.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Denis Mingulov.
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 #include "classviewutils.h"
34 #include "classviewconstants.h"
35 #include "classviewsymbolinformation.h"
36
37 // needed for the correct sorting order
38 #include <cplusplus/Icons.h>
39
40 #include <QtGui/QStandardItem>
41 #include <QtCore/QDebug>
42
43 namespace ClassView {
44 namespace Constants {
45
46 //! Default icon sort order
47 const int IconSortOrder[] = {
48     CPlusPlus::Icons::NamespaceIconType,
49     CPlusPlus::Icons::EnumIconType,
50     CPlusPlus::Icons::ClassIconType,
51     CPlusPlus::Icons::FuncPublicIconType,
52     CPlusPlus::Icons::FuncProtectedIconType,
53     CPlusPlus::Icons::FuncPrivateIconType,
54     CPlusPlus::Icons::SignalIconType,
55     CPlusPlus::Icons::SlotPublicIconType,
56     CPlusPlus::Icons::SlotProtectedIconType,
57     CPlusPlus::Icons::SlotPrivateIconType,
58     CPlusPlus::Icons::VarPublicIconType,
59     CPlusPlus::Icons::VarProtectedIconType,
60     CPlusPlus::Icons::VarPrivateIconType,
61     CPlusPlus::Icons::EnumeratorIconType,
62     CPlusPlus::Icons::KeywordIconType,
63     CPlusPlus::Icons::MacroIconType,
64     CPlusPlus::Icons::UnknownIconType
65 };
66
67 } // namespace Constants
68
69 namespace Internal {
70
71 Utils::Utils()
72 {
73 }
74
75 QList<QVariant> Utils::locationsToRole(const QSet<SymbolLocation> &locations)
76 {
77     QList<QVariant> locationsVar;
78     foreach(const SymbolLocation &loc, locations)
79         locationsVar.append(QVariant::fromValue(loc));
80
81     return locationsVar;
82 }
83
84 QSet<SymbolLocation> Utils::roleToLocations(const QList<QVariant> &locationsVar)
85 {
86     QSet<SymbolLocation> locations;
87     foreach(const QVariant &loc, locationsVar) {
88         if (loc.canConvert<SymbolLocation>()) {
89             locations.insert(loc.value<SymbolLocation>());
90         }
91     }
92
93     return locations;
94 }
95
96 int Utils::iconTypeSortOrder(int icon)
97 {
98     static QHash<int, int> sortOrder;
99
100     // initialization
101     if (sortOrder.count() == 0) {
102         for (unsigned i = 0 ;
103              i < sizeof(Constants::IconSortOrder) / sizeof(Constants::IconSortOrder[0]) ; ++i)
104             sortOrder.insert(Constants::IconSortOrder[i], sortOrder.count());
105     }
106
107     // if it is missing - return the same value
108     if (!sortOrder.contains(icon))
109         return icon;
110
111     return sortOrder[icon];
112 }
113
114 QStandardItem *Utils::setSymbolInformationToItem(const SymbolInformation &information,
115                                                  QStandardItem *item)
116 {
117     Q_ASSERT(item);
118
119     item->setData(information.name(), Constants::SymbolNameRole);
120     item->setData(information.type(), Constants::SymbolTypeRole);
121     item->setData(information.iconType(), Constants::IconTypeRole);
122
123     return item;
124 }
125
126 SymbolInformation Utils::symbolInformationFromItem(const QStandardItem *item)
127 {
128     Q_ASSERT(item);
129
130     if (!item)
131         return SymbolInformation();
132
133     const QString &name = item->data(Constants::SymbolNameRole).toString();
134     const QString &type = item->data(Constants::SymbolTypeRole).toString();
135     int iconType = 0;
136
137     QVariant var = item->data(Constants::IconTypeRole);
138     bool ok = false;
139     int value;
140     if (var.isValid())
141         value = var.toInt(&ok);
142     if (ok)
143         iconType = value;
144
145     return SymbolInformation(name, type, iconType);
146 }
147
148 void Utils::fetchItemToTarget(QStandardItem *item, const QStandardItem *target)
149 {
150     if (!item || !target)
151         return;
152
153     int itemIndex = 0;
154     int targetIndex = 0;
155     int itemRows = item->rowCount();
156     int targetRows = target->rowCount();
157
158     while (itemIndex < itemRows && targetIndex < targetRows) {
159         const QStandardItem *itemChild = item->child(itemIndex);
160         const QStandardItem *targetChild = target->child(targetIndex);
161
162         const SymbolInformation &itemInf = symbolInformationFromItem(itemChild);
163         const SymbolInformation &targetInf = symbolInformationFromItem(targetChild);
164
165         if (itemInf < targetInf) {
166             ++itemIndex;
167         } else if (itemInf == targetInf) {
168             ++itemIndex;
169             ++targetIndex;
170         } else {
171             item->insertRow(itemIndex, targetChild->clone());
172             ++itemIndex;
173             ++itemRows;
174             ++targetIndex;
175         }
176     }
177
178     // append
179     while (targetIndex < targetRows) {
180         item->appendRow(target->child(targetIndex)->clone());
181         ++targetIndex;
182     }
183 }
184
185 void Utils::moveItemToTarget(QStandardItem *item, const QStandardItem *target)
186 {
187     if (!item || !target)
188         return;
189
190     int itemIndex = 0;
191     int targetIndex = 0;
192     int itemRows = item->rowCount();
193     int targetRows = target->rowCount();
194
195     while (itemIndex < itemRows && targetIndex < targetRows) {
196         QStandardItem *itemChild = item->child(itemIndex);
197         const QStandardItem *targetChild = target->child(targetIndex);
198
199         const SymbolInformation &itemInf = Utils::symbolInformationFromItem(itemChild);
200         const SymbolInformation &targetInf = Utils::symbolInformationFromItem(targetChild);
201
202         if (itemInf < targetInf) {
203             item->removeRow(itemIndex);
204             --itemRows;
205         } else if (itemInf == targetInf) {
206             moveItemToTarget(itemChild, targetChild);
207             ++itemIndex;
208             ++targetIndex;
209         } else {
210             item->insertRow(itemIndex, targetChild->clone());
211             moveItemToTarget(item->child(itemIndex), targetChild);
212             ++itemIndex;
213             ++itemRows;
214             ++targetIndex;
215         }
216     }
217
218     // append
219     while (targetIndex < targetRows) {
220         item->appendRow(target->child(targetIndex)->clone());
221         moveItemToTarget(item->child(itemIndex), target->child(targetIndex));
222         ++itemIndex;
223         ++itemRows;
224         ++targetIndex;
225     }
226
227     // remove end of item
228     while (itemIndex < itemRows) {
229         item->removeRow(itemIndex);
230         --itemRows;
231     }
232 }
233
234 } // namespace Internal
235 } // namespace ClassView