OSDN Git Service

c75e75a4e81ce8ab4d862c69fcc61135a6a670c9
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / itemlibrary / itemlibrarymodel.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 (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 #ifndef ITEMLIBRARYMODEL_H
35 #define ITEMLIBRARYMODEL_H
36
37 #include <QMap>
38 #include <QIcon>
39 #include <QVariant>
40 #include <QScriptEngine>
41 #include <private/qdeclarativelistmodel_p.h>
42
43 QT_FORWARD_DECLARE_CLASS(QMimeData)
44
45 namespace QmlDesigner {
46
47 class ItemLibraryInfo;
48 class ItemLibraryEntry;
49 class Model;
50
51 namespace Internal {
52
53 template <class T>
54 class ItemLibrarySortedModel: public QDeclarativeListModel {
55 public:
56     ItemLibrarySortedModel(QObject *parent = 0);
57     ~ItemLibrarySortedModel();
58
59     void clearElements();
60
61     void addElement(T *element, int libId);
62     void removeElement(int libId);
63
64     bool elementVisible(int libId) const;
65     bool setElementVisible(int libId, bool visible);
66
67     const QMap<int, T *> &elements() const;
68
69     T *elementModel(int libId);
70     int findElement(int libId) const;
71     int visibleElementPosition(int libId) const;
72
73 private:
74     struct order_struct {
75         int libId;
76         bool visible;
77     };
78
79     QMap<int, T *> m_elementModels;
80     QList<struct order_struct> m_elementOrder;
81 };
82
83
84 class ItemLibraryItemModel: public QScriptValue {
85 public:
86     ItemLibraryItemModel(QScriptEngine *scriptEngine, int itemLibId, const QString &itemName);
87     ~ItemLibraryItemModel();
88
89     int itemLibId() const;
90     QString itemName() const;
91
92     void setItemIconPath(const QString &iconPath);
93     void setItemIconSize(const QSize &itemIconSize);
94
95     bool operator<(const ItemLibraryItemModel &other) const;
96
97 private:
98     QWeakPointer<QScriptEngine> m_scriptEngine;
99     int m_libId;
100     QString m_name;
101     QString m_iconPath;
102     QSize m_iconSize;
103 };
104
105
106 class ItemLibrarySectionModel: public QScriptValue {
107 public:
108     ItemLibrarySectionModel(QScriptEngine *scriptEngine, int sectionLibId, const QString &sectionName, QObject *parent = 0);
109
110     QString sectionName() const;
111
112     void addSectionEntry(ItemLibraryItemModel *sectionEntry);
113     void removeSectionEntry(int itemLibId);
114
115     int visibleItemIndex(int itemLibId);
116     bool isItemVisible(int itemLibId);
117
118     bool updateSectionVisibility(const QString &searchText, bool *changed);
119     void updateItemIconSize(const QSize &itemIconSize);
120
121     bool operator<(const ItemLibrarySectionModel &other) const;
122
123 private:
124     QString m_name;
125     ItemLibrarySortedModel<ItemLibraryItemModel> m_sectionEntries;
126 };
127
128
129 class ItemLibraryModel: public ItemLibrarySortedModel<ItemLibrarySectionModel> {
130     Q_OBJECT
131     Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
132
133 public:
134     explicit ItemLibraryModel(QScriptEngine *scriptEngine, QObject *parent = 0);
135     ~ItemLibraryModel();
136
137     QString searchText() const;
138
139     void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
140
141     QString getTypeName(int libId);
142     QMimeData *getMimeData(int libId);
143     QIcon getIcon(int libId);
144
145 public slots:
146     void setSearchText(const QString &searchText);
147     void setItemIconSize(const QSize &itemIconSize);
148
149     int getItemSectionIndex(int itemLibId);
150     int getSectionLibId(int itemLibId);
151     bool isItemVisible(int itemLibId);
152
153 signals:
154     void qmlModelChanged();
155     void searchTextChanged();
156     void visibilityChanged();
157     void sectionVisibilityChanged(int changedSectionLibId);
158
159 private:
160     void updateVisibility();
161
162     int getWidth(const ItemLibraryEntry &entry);
163     int getHeight(const ItemLibraryEntry &entry);
164     QPixmap createDragPixmap(int width, int height);
165
166     QWeakPointer<QScriptEngine> m_scriptEngine;
167     QMap<int, ItemLibraryEntry> m_itemInfos;
168     QMap<int, int> m_sections;
169
170     QString m_searchText;
171     QSize m_itemIconSize;
172     int m_nextLibId;
173 };
174
175 } // namespace Internal
176 } // namespace QmlDesigner
177
178 #endif // ITEMLIBRARYMODEL_H
179