annotate demos/browser/modelmenu.d @ 55:63c31e221118

CMake: Add forgotten files to install.
author SokoL_SD
date Mon, 18 May 2009 19:03:06 +0000
parents 71b382c10ef6
children 7bfd46c330dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
1 /****************************************************************************
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
2 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
4 ** Contact: Qt Software Information (qt-info@nokia.com)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
5 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
6 ** This file is part of the demonstration applications of the Qt Toolkit.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
7 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
8 ** $QT_BEGIN_LICENSE:LGPL$
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
9 ** Commercial Usage
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
10 ** Licensees holding valid Qt Commercial licenses may use this file in
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
11 ** accordance with the Qt Commercial License Agreement provided with the
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
12 ** Software or, alternatively, in accordance with the terms contained in
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
13 ** a written agreement between you and Nokia.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
14 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
15 ** GNU Lesser General Public License Usage
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
17 ** General Public License version 2.1 as published by the Free Software
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
19 ** packaging of this file. Please review the following information to
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
22 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
23 ** In addition, as a special exception, Nokia gives you certain
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
24 ** additional rights. These rights are described in the Nokia Qt LGPL
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
26 ** package.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
27 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
28 ** GNU General Public License Usage
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
29 ** Alternatively, this file may be used under the terms of the GNU
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
30 ** General Public License version 3.0 as published by the Free Software
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
31 ** Foundation and appearing in the file LICENSE.GPL included in the
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
32 ** packaging of this file. Please review the following information to
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
33 ** ensure the GNU General Public License version 3.0 requirements will be
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
34 ** met: http://www.gnu.org/copyleft/gpl.html.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
35 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
36 ** If you are unsure which license is appropriate for your use, please
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
37 ** contact the sales department at qt-sales@nokia.com.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
38 ** $QT_END_LICENSE$
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
39 **
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
40 ****************************************************************************/
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
41
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
42 import QtGui.QMenu
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
43 import QtCore.QAbstractItemModel;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
44
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
45 import modelmenu;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
46
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
47 import QtCore.QAbstractItemModel;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
48 import qdebug;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
49
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
50
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
51
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
52 // A QMenu that is dynamically populated from a QAbstractItemModel
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
53 class ModelMenu : public QMenu
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
54 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
55 Q_OBJECT
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
56
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
57 signals:
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
58 void activated(const QModelIndex &index);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
59 void hovered(const QString &text);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
60
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
61 public:
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
62 ModelMenu(QWidget *parent = null)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
63 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
64 super(parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
65 m_maxRows = 7;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
66 m_firstSeparator = -1;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
67 m_maxWidth = -1;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
68 m_hoverRole = 0;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
69 m_separatorRole = 0;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
70 m_model = 0;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
71 connect(this, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
72 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
73
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
74 void setModel(QAbstractItemModel *model)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
75 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
76 m_model = model;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
77 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
78
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
79 QAbstractItemModel *model() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
80 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
81 return m_model;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
82 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
83
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
84 void setMaxRows(int max)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
85 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
86 m_maxRows = max;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
87 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
88
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
89 int maxRows() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
90 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
91 return m_maxRows;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
92 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
93
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
94 void setFirstSeparator(int offset)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
95 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
96 m_firstSeparator = offset;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
97 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
98
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
99 int firstSeparator() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
100 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
101 return m_firstSeparator;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
102 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
103
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
104 void setRootIndex(const QModelIndex &index)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
105 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
106 m_root = index;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
107 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
108 QModelIndex rootIndex() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
109 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
110 return m_root;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
111 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
112
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
113 void setHoverRole(int role)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
114 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
115 m_hoverRole = role;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
116 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
117 int hoverRole() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
118 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
119 return m_hoverRole;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
120 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
121
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
122 void setSeparatorRole(int role)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
123 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
124 m_separatorRole = role;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
125 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
126
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
127 int separatorRole() const
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
128 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
129 return m_separatorRole;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
130 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
131
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
132 QAction *makeAction(const QIcon &icon, const QString &text, QObject *parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
133 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
134 QFontMetrics fm(font());
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
135 if (-1 == m_maxWidth)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
136 m_maxWidth = fm.width(QLatin1Char('m')) * 30;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
137 QString smallText = fm.elidedText(text, Qt.ElideMiddle, m_maxWidth);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
138 return new QAction(icon, smallText, parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
139 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
140
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
141 protected:
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
142 // add any actions before the tree, return true if any actions are added.
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
143 virtual bool prePopulated()
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
144 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
145 return false;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
146 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
147 // add any actions after the tree
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
148 virtual void postPopulated()
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
149 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
150 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
151
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
152 // put all of the children of parent into menu up to max
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
153 void createMenu(const QModelIndex &parent, int max, QMenu *parentMenu = null, QMenu *menu = null)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
154 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
155 if (!menu) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
156 QString title = parent.data().toString();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
157 menu = new QMenu(title, this);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
158 QIcon icon = qvariant_cast<QIcon>(parent.data(Qt.DecorationRole));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
159 menu.setIcon(icon);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
160 parentMenu.addMenu(menu);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
161 QVariant v;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
162 v.setValue(parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
163 menu.menuAction().setData(v);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
164 connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
165 return;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
166 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
167
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
168 int end = m_model.rowCount(parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
169 if (max != -1)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
170 end = qMin(max, end);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
171
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
172 connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*)));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
173 connect(menu, SIGNAL(hovered(QAction*)), this, SLOT(hovered(QAction*)));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
174
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
175 for (int i = 0; i < end; ++i) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
176 QModelIndex idx = m_model.index(i, 0, parent);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
177 if (m_model.hasChildren(idx)) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
178 createMenu(idx, -1, menu);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
179 } else {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
180 if (m_separatorRole != 0
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
181 && idx.data(m_separatorRole).toBool())
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
182 addSeparator();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
183 else
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
184 menu.addAction(makeAction(idx));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
185 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
186 if (menu == this && i == m_firstSeparator - 1)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
187 addSeparator();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
188 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
189 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
190
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
191 private slots:
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
192 Q_DECLARE_METATYPE(QModelIndex)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
193 void aboutToShow()
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
194 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
195 if (QMenu *menu = qobject_cast<QMenu*>(sender())) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
196 QVariant v = menu.menuAction().data();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
197 if (v.canConvert<QModelIndex>()) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
198 QModelIndex idx = qvariant_cast<QModelIndex>(v);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
199 createMenu(idx, -1, menu, menu);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
200 disconnect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
201 return;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
202 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
203 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
204
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
205 clear();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
206 if (prePopulated())
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
207 addSeparator();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
208 int max = m_maxRows;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
209 if (max != -1)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
210 max += m_firstSeparator;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
211 createMenu(m_root, max, this, this);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
212 postPopulated();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
213 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
214
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
215
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
216 void triggered(QAction *action)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
217 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
218 QVariant v = action.data();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
219 if (v.canConvert<QModelIndex>()) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
220 QModelIndex idx = qvariant_cast<QModelIndex>(v);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
221 emit activated(idx);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
222 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
223 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
224
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
225 void hovered(QAction *action)
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
226 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
227 QVariant v = action.data();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
228 if (v.canConvert<QModelIndex>()) {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
229 QModelIndex idx = qvariant_cast<QModelIndex>(v);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
230 QString hoveredString = idx.data(m_hoverRole).toString();
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
231 if (!hoveredString.isEmpty())
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
232 emit hovered(hoveredString);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
233 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
234 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
235
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
236 private:
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
237 QAction *makeAction(const QModelIndex &index);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
238 {
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
239 QIcon icon = qvariant_cast<QIcon>(index.data(Qt.DecorationRole));
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
240 QAction *action = makeAction(icon, index.data().toString(), this);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
241 QVariant v;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
242 v.setValue(index);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
243 action.setData(v);
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
244 return action;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
245 }
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
246
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
247 int m_maxRows;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
248 int m_firstSeparator;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
249 int m_maxWidth;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
250 int m_hoverRole;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
251 int m_separatorRole;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
252 QAbstractItemModel *m_model;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
253 QPersistentModelIndex m_root;
71b382c10ef6 add coarse and incomplete QT browser port
mandel
parents:
diff changeset
254 }