OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljsicons.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 qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmljsicons.h"
34 #include <QtCore/QDebug>
35 #include <QtCore/QDir>
36 #include <QtCore/QHash>
37 #include <QtCore/QPair>
38
39 using namespace QmlJS;
40 using namespace QmlJS::AST;
41
42 enum {
43     debug = false
44 };
45
46 namespace QmlJS {
47
48 Icons *Icons::m_instance = 0;
49
50 class IconsPrivate
51 {
52 public:
53     QIcon elementIcon;
54     QIcon propertyIcon;
55     QIcon publicMemberIcon;
56     QIcon functionDeclarationIcon;
57     QHash<QPair<QString,QString>,QIcon> iconHash;
58     QString resourcePath;
59 };
60
61 } // namespace QmlJS
62
63 Icons::Icons()
64     : m_d(new IconsPrivate)
65 {
66     m_d->elementIcon = QIcon(QLatin1String(":/qmljs/images/element.png"));
67     m_d->propertyIcon = QIcon(QLatin1String(":/qmljs/images/property.png"));
68     m_d->publicMemberIcon = QIcon(QLatin1String(":/qmljs/images/publicmember.png"));
69     m_d->functionDeclarationIcon = QIcon(QLatin1String(":/qmljs/images/func.png"));
70 }
71
72 Icons::~Icons()
73 {
74     m_instance = 0;
75     delete m_d;
76 }
77
78 Icons *Icons::instance()
79 {
80     if (!m_instance)
81         m_instance = new Icons();
82     return m_instance;
83 }
84
85 void Icons::setIconFilesPath(const QString &iconPath)
86 {
87     if (iconPath == m_d->resourcePath)
88         return;
89
90     m_d->resourcePath = iconPath;
91
92     if (debug)
93         qDebug() << "QmlJSIcons -" << "parsing" << iconPath;
94     QDir topDir(iconPath);
95     foreach (const QFileInfo &subDirInfo, topDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
96         if (debug)
97             qDebug() << "QmlJSIcons - parsing" << subDirInfo.absoluteFilePath();
98         const QString packageName = subDirInfo.fileName();
99         QDir subDir(subDirInfo.absoluteFilePath() + QLatin1String("/16x16"));
100         foreach (const QFileInfo &iconFile, subDir.entryInfoList(QDir::Files)) {
101             QIcon icon(iconFile.absoluteFilePath());
102             if (icon.isNull()) {
103                 if (debug)
104                     qDebug() << "QmlJSIcons - skipping" << iconFile.absoluteFilePath();
105                 continue;
106             }
107             if (debug)
108                 qDebug() << "QmlJSIcons - adding" << packageName << iconFile.baseName() << "icon to database";
109             QPair<QString,QString> element(packageName, iconFile.baseName());
110             m_d->iconHash.insert(element, icon);
111         }
112     }
113 }
114
115 QIcon Icons::icon(const QString &packageName, const QString typeName) const
116 {
117     QPair<QString,QString> element(packageName, typeName);
118     if (debug)
119         qDebug() << "QmlJSIcons - icon for" << packageName << typeName << "requested" << m_d->iconHash.contains(element);
120     return m_d->iconHash.value(element);
121 }
122
123 QIcon Icons::icon(Node *node) const
124 {
125     if (dynamic_cast<AST::UiObjectDefinition*>(node)) {
126         return objectDefinitionIcon();
127     }
128     if (dynamic_cast<AST::UiScriptBinding*>(node)) {
129         return scriptBindingIcon();
130     }
131
132     return QIcon();
133 }
134
135 QIcon Icons::objectDefinitionIcon() const
136 {
137     return m_d->elementIcon;
138 }
139
140 QIcon Icons::scriptBindingIcon() const
141 {
142     return m_d->propertyIcon;
143 }
144
145 QIcon Icons::publicMemberIcon() const
146 {
147     return m_d->publicMemberIcon;
148 }
149
150 QIcon Icons::functionDeclarationIcon() const
151 {
152     return m_d->functionDeclarationIcon;
153 }