1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Denis Mingulov.
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "classviewutils.h"
34 #include "classviewconstants.h"
35 #include "classviewsymbolinformation.h"
37 // needed for the correct sorting order
38 #include <cplusplus/Icons.h>
40 #include <QtGui/QStandardItem>
41 #include <QtCore/QDebug>
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
67 } // namespace Constants
75 QList<QVariant> Utils::locationsToRole(const QSet<SymbolLocation> &locations)
77 QList<QVariant> locationsVar;
78 foreach(const SymbolLocation &loc, locations)
79 locationsVar.append(QVariant::fromValue(loc));
84 QSet<SymbolLocation> Utils::roleToLocations(const QList<QVariant> &locationsVar)
86 QSet<SymbolLocation> locations;
87 foreach(const QVariant &loc, locationsVar) {
88 if (loc.canConvert<SymbolLocation>()) {
89 locations.insert(loc.value<SymbolLocation>());
96 int Utils::iconTypeSortOrder(int icon)
98 static QHash<int, int> sortOrder;
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());
107 // if it is missing - return the same value
108 if (!sortOrder.contains(icon))
111 return sortOrder[icon];
114 QStandardItem *Utils::setSymbolInformationToItem(const SymbolInformation &information,
119 item->setData(information.name(), Constants::SymbolNameRole);
120 item->setData(information.type(), Constants::SymbolTypeRole);
121 item->setData(information.iconType(), Constants::IconTypeRole);
126 SymbolInformation Utils::symbolInformationFromItem(const QStandardItem *item)
131 return SymbolInformation();
133 const QString &name = item->data(Constants::SymbolNameRole).toString();
134 const QString &type = item->data(Constants::SymbolTypeRole).toString();
137 QVariant var = item->data(Constants::IconTypeRole);
141 value = var.toInt(&ok);
145 return SymbolInformation(name, type, iconType);
148 void Utils::fetchItemToTarget(QStandardItem *item, const QStandardItem *target)
150 if (!item || !target)
155 int itemRows = item->rowCount();
156 int targetRows = target->rowCount();
158 while (itemIndex < itemRows && targetIndex < targetRows) {
159 const QStandardItem *itemChild = item->child(itemIndex);
160 const QStandardItem *targetChild = target->child(targetIndex);
162 const SymbolInformation &itemInf = symbolInformationFromItem(itemChild);
163 const SymbolInformation &targetInf = symbolInformationFromItem(targetChild);
165 if (itemInf < targetInf) {
167 } else if (itemInf == targetInf) {
171 item->insertRow(itemIndex, targetChild->clone());
179 while (targetIndex < targetRows) {
180 item->appendRow(target->child(targetIndex)->clone());
185 void Utils::moveItemToTarget(QStandardItem *item, const QStandardItem *target)
187 if (!item || !target)
192 int itemRows = item->rowCount();
193 int targetRows = target->rowCount();
195 while (itemIndex < itemRows && targetIndex < targetRows) {
196 QStandardItem *itemChild = item->child(itemIndex);
197 const QStandardItem *targetChild = target->child(targetIndex);
199 const SymbolInformation &itemInf = Utils::symbolInformationFromItem(itemChild);
200 const SymbolInformation &targetInf = Utils::symbolInformationFromItem(targetChild);
202 if (itemInf < targetInf) {
203 item->removeRow(itemIndex);
205 } else if (itemInf == targetInf) {
206 moveItemToTarget(itemChild, targetChild);
210 item->insertRow(itemIndex, targetChild->clone());
211 moveItemToTarget(item->child(itemIndex), targetChild);
219 while (targetIndex < targetRows) {
220 item->appendRow(target->child(targetIndex)->clone());
221 moveItemToTarget(item->child(itemIndex), target->child(targetIndex));
227 // remove end of item
228 while (itemIndex < itemRows) {
229 item->removeRow(itemIndex);
234 } // namespace Internal
235 } // namespace ClassView