1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
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
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "modelmenu.h"
44 #include <QtCore/QAbstractItemModel>
47 ModelMenu::ModelMenu(QWidget
* parent
)
50 , m_firstSeparator(-1)
56 connect(this, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
59 bool ModelMenu::prePopulated()
64 void ModelMenu::postPopulated()
68 void ModelMenu::setModel(QAbstractItemModel
*model
)
73 QAbstractItemModel
*ModelMenu::model() const
78 void ModelMenu::setMaxRows(int max
)
83 int ModelMenu::maxRows() const
88 void ModelMenu::setFirstSeparator(int offset
)
90 m_firstSeparator
= offset
;
93 int ModelMenu::firstSeparator() const
95 return m_firstSeparator
;
98 void ModelMenu::setRootIndex(const QModelIndex
&index
)
103 QModelIndex
ModelMenu::rootIndex() const
108 void ModelMenu::setHoverRole(int role
)
113 int ModelMenu::hoverRole() const
118 void ModelMenu::setSeparatorRole(int role
)
120 m_separatorRole
= role
;
123 int ModelMenu::separatorRole() const
125 return m_separatorRole
;
128 Q_DECLARE_METATYPE(QModelIndex
)
129 void ModelMenu::aboutToShow()
131 if (QMenu
*menu
= qobject_cast
<QMenu
*>(sender())) {
132 QVariant v
= menu
->menuAction()->data();
133 if (v
.canConvert
<QModelIndex
>()) {
134 QModelIndex idx
= qvariant_cast
<QModelIndex
>(v
);
135 createMenu(idx
, -1, menu
, menu
);
136 disconnect(menu
, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
146 max
+= m_firstSeparator
;
147 createMenu(m_root
, max
, this, this);
151 void ModelMenu::createMenu(const QModelIndex
&parent
, int max
, QMenu
*parentMenu
, QMenu
*menu
)
154 QString title
= parent
.data().toString();
155 menu
= new QMenu(title
, this);
156 QIcon icon
= qvariant_cast
<QIcon
>(parent
.data(Qt::DecorationRole
));
158 parentMenu
->addMenu(menu
);
161 menu
->menuAction()->setData(v
);
162 connect(menu
, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
166 int end
= m_model
->rowCount(parent
);
168 end
= qMin(max
, end
);
170 connect(menu
, SIGNAL(triggered(QAction
*)), this, SLOT(triggered(QAction
*)));
171 connect(menu
, SIGNAL(hovered(QAction
*)), this, SLOT(hovered(QAction
*)));
173 for (int i
= 0; i
< end
; ++i
) {
174 QModelIndex idx
= m_model
->index(i
, 0, parent
);
175 if (m_model
->hasChildren(idx
)) {
176 createMenu(idx
, -1, menu
);
178 if (m_separatorRole
!= 0
179 && idx
.data(m_separatorRole
).toBool())
182 menu
->addAction(makeAction(idx
));
184 if (menu
== this && i
== m_firstSeparator
- 1)
189 QAction
*ModelMenu::makeAction(const QModelIndex
&index
)
191 QIcon icon
= qvariant_cast
<QIcon
>(index
.data(Qt::DecorationRole
));
192 QAction
*action
= makeAction(icon
, index
.data().toString(), this);
199 QAction
*ModelMenu::makeAction(const QIcon
&icon
, const QString
&text
, QObject
*parent
)
201 QFontMetrics
fm(font());
202 if (-1 == m_maxWidth
)
203 m_maxWidth
= fm
.width(QLatin1Char('m')) * 30;
204 QString smallText
= fm
.elidedText(text
, Qt::ElideMiddle
, m_maxWidth
);
205 return new QAction(icon
, smallText
, parent
);
208 void ModelMenu::triggered(QAction
*action
)
210 QVariant v
= action
->data();
211 if (v
.canConvert
<QModelIndex
>()) {
212 QModelIndex idx
= qvariant_cast
<QModelIndex
>(v
);
217 void ModelMenu::hovered(QAction
*action
)
219 QVariant v
= action
->data();
220 if (v
.canConvert
<QModelIndex
>()) {
221 QModelIndex idx
= qvariant_cast
<QModelIndex
>(v
);
222 QString hoveredString
= idx
.data(m_hoverRole
).toString();
223 if (!hoveredString
.isEmpty())
224 emit
hovered(hoveredString
);