Refactor common POSIX support out of individual qplatformdefs.h
[qt-netbsd.git] / demos / browser / modelmenu.cpp
blobfe5e750866c2899372af44bd08dc09d8625db0b2
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the demonstration applications of the Qt Toolkit.
8 **
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
14 ** this package.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "modelmenu.h"
44 #include <QtCore/QAbstractItemModel>
45 #include <qdebug.h>
47 ModelMenu::ModelMenu(QWidget * parent)
48 : QMenu(parent)
49 , m_maxRows(7)
50 , m_firstSeparator(-1)
51 , m_maxWidth(-1)
52 , m_hoverRole(0)
53 , m_separatorRole(0)
54 , m_model(0)
56 connect(this, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
59 bool ModelMenu::prePopulated()
61 return false;
64 void ModelMenu::postPopulated()
68 void ModelMenu::setModel(QAbstractItemModel *model)
70 m_model = model;
73 QAbstractItemModel *ModelMenu::model() const
75 return m_model;
78 void ModelMenu::setMaxRows(int max)
80 m_maxRows = max;
83 int ModelMenu::maxRows() const
85 return m_maxRows;
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)
100 m_root = index;
103 QModelIndex ModelMenu::rootIndex() const
105 return m_root;
108 void ModelMenu::setHoverRole(int role)
110 m_hoverRole = role;
113 int ModelMenu::hoverRole() const
115 return m_hoverRole;
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()));
137 return;
141 clear();
142 if (prePopulated())
143 addSeparator();
144 int max = m_maxRows;
145 if (max != -1)
146 max += m_firstSeparator;
147 createMenu(m_root, max, this, this);
148 postPopulated();
151 void ModelMenu::createMenu(const QModelIndex &parent, int max, QMenu *parentMenu, QMenu *menu)
153 if (!menu) {
154 QString title = parent.data().toString();
155 menu = new QMenu(title, this);
156 QIcon icon = qvariant_cast<QIcon>(parent.data(Qt::DecorationRole));
157 menu->setIcon(icon);
158 parentMenu->addMenu(menu);
159 QVariant v;
160 v.setValue(parent);
161 menu->menuAction()->setData(v);
162 connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
163 return;
166 int end = m_model->rowCount(parent);
167 if (max != -1)
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);
177 } else {
178 if (m_separatorRole != 0
179 && idx.data(m_separatorRole).toBool())
180 addSeparator();
181 else
182 menu->addAction(makeAction(idx));
184 if (menu == this && i == m_firstSeparator - 1)
185 addSeparator();
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);
193 QVariant v;
194 v.setValue(index);
195 action->setData(v);
196 return action;
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);
213 emit activated(idx);
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);