on fe640cb help sign in
unity8/plugins/Unity/IndicatorsLegacy/flatmenuproxymodel.cpp
288 lines 8.0 kB history raw
1/*
2 * Copyright 2012 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Renato Araujo Oliveira Filho <renato@canonical.com>
18 */
20extern "C" {
21#include <gio/gio.h>
24#include "flatmenuproxymodel.h"
25#include <QString>
26#include <QStringList>
28FlatMenuProxyModel::FlatMenuProxyModel(QAbstractItemModel *source)
29 : QAbstractProxyModel(source),
30 m_rowCount(0)
32 m_model = new QDBusMenuModel;
33 setSourceModel(m_model);
36/*! \internal */
37FlatMenuProxyModel::~FlatMenuProxyModel()
39 delete m_model;
42void FlatMenuProxyModel::setSourceModel(QAbstractItemModel *source)
44 if (sourceModel()) {
45 QAbstractItemModel *oldSource = sourceModel();
46 if (oldSource == source) {
47 return;
48 }
50 oldSource->disconnect(this);
51 }
53 QAbstractProxyModel::setSourceModel(source);
55 if (source) {
57 // FIXME - not working correctly with dynamic inserts/removes
58 // FIXME 2 - We're using Queud connections as a workaround to QTBUG-32859
59 // The signals from qmenumodel are coming straight off the glib event loop,
60 // which is not supported and has the effect of listview delegates not to be deleted.
62 connect(source,
63 SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
64 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
65 connect(source,
66 SIGNAL(rowsInserted(QModelIndex,int,int)),
67 SLOT(onModelReset()), Qt::QueuedConnection);
69 connect(source,
70 SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
71 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
72 connect(source,
73 SIGNAL(rowsRemoved(QModelIndex,int,int)),
74 SLOT(onModelReset()), Qt::QueuedConnection);
76 connect(source,
77 SIGNAL(modelAboutToBeReset()),
78 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
79 connect(source,
80 SIGNAL(modelReset()),
81 SLOT(onModelReset()), Qt::QueuedConnection);
83 //Connection args
84 connect(source,
85 SIGNAL(busTypeChanged(DBusEnums::BusType)),
86 SIGNAL(busTypeChanged()));
88 connect(source,
89 SIGNAL(busNameChanged(const QString&)),
90 SIGNAL(busNameChanged()));
92 connect(source,
93 SIGNAL(objectPathChanged(const QString&)),
94 SIGNAL(objectPathChanged()));
96 connect(source,
97 SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
98 SIGNAL(statusChanged()), Qt::QueuedConnection);
100 // initiliaze rowCount
101 QModelIndex lastItem = source->index(source->rowCount() - 1, 0);
102 m_rowCount = recursiveRowCount(lastItem);
103 } else {
104 m_rowCount = 0;
105 }
107 Q_EMIT countChanged();
110void FlatMenuProxyModel::setBusName(const QString &busName)
112 m_model->setBusName(busName);
115QString FlatMenuProxyModel::busName() const
117 return m_model->busName();
120void FlatMenuProxyModel::setObjectPath(const QString &busPath)
122 m_model->setObjectPath(busPath);
125QString FlatMenuProxyModel::objectPath() const
127 return m_model->objectPath();
130int FlatMenuProxyModel::status() const
132 return m_model->status();
135DBusEnums::BusType FlatMenuProxyModel::busType() const
137 return m_model->busType();
140void FlatMenuProxyModel::setIntBusType(int type)
142 m_model->setBusType((DBusEnums::BusType) type);
145void FlatMenuProxyModel::start()
147 m_model->start();
150void FlatMenuProxyModel::stop()
152 m_model->stop();
155QModelIndex FlatMenuProxyModel::mapFromSource(const QModelIndex &index) const
157 if (!index.isValid()) {
158 return QModelIndex();
159 }
161 QModelIndex result = createIndex(rowOffsetOf(index, index.row()), 0);
162 return result;
165QModelIndex FlatMenuProxyModel::mapToSource(const QModelIndex &index) const
167 if (sourceModel() && m_indexCache.contains(index.row())) {
168 QString key = m_indexCache.value(index.row());
169 QStringList sections = key.split('.', QString::SkipEmptyParts);
170 QModelIndex sourceIndex = QModelIndex();
171 for(int i=0; i < sections.size(); i++) {
172 sourceIndex = sourceModel()->index(sections.at(i).toInt(), 9, sourceIndex);
173 }
174 return sourceIndex;
175 } else {
176 return QModelIndex();
177 }
180QModelIndex FlatMenuProxyModel::index(int row, int sectionRow, const QModelIndex &parent, const QString &key) const
182 for (int i = 0, count = 0; i < sourceModel()->rowCount(parent); i++) {
183 QModelIndex sourceIndex = sourceModel()->index(i, 0, parent);
184 int indexCount = recursiveRowCount(sourceIndex);
186 if (count == sectionRow) {
187 QString sectionKey = key + "." + QString::number(i);
188 m_indexCache.insert(row, sectionKey);
189 return createIndex(row, 0);
190 } else if ((count + indexCount) >= sectionRow) {
191 count++;
192 return index(row, sectionRow - count, sourceIndex, key + "." + QString::number(i));
193 }
195 count += indexCount + 1;
196 }
197 return QModelIndex();
200QModelIndex FlatMenuProxyModel::index(int row, int, const QModelIndex &) const
202 return index(row, row, QModelIndex(), "");
205QModelIndex FlatMenuProxyModel::parent(const QModelIndex &) const
207 return QModelIndex();
210int FlatMenuProxyModel::columnCount(const QModelIndex &) const
212 return 1;
215int FlatMenuProxyModel::rowCount(const QModelIndex &) const
217 return m_rowCount;
220int FlatMenuProxyModel::count() const
222 return rowCount(QModelIndex());
225void FlatMenuProxyModel::onModelAboutToBeReset()
227 beginResetModel();
230void FlatMenuProxyModel::onModelReset()
232 // initiliaze rowCount
233 m_rowCount = 0;
234 for (int i = 0, iMax = sourceModel()->rowCount(); i < iMax; i++) {
235 QModelIndex index = sourceModel()->index(i, 0);
236 m_rowCount += recursiveRowCount(index) + 1;
237 }
238 m_indexCache.clear();
239 endResetModel();
240 Q_EMIT countChanged();
243int FlatMenuProxyModel::recursiveRowCount(const QModelIndex &index) const
245 int size = sourceModel()->rowCount(index);
246 for (int i=0, iMax=size; i < iMax; i++) {
247 QModelIndex currentIndex = sourceModel()->index(i, 0, index);
248 size += recursiveRowCount(currentIndex);
249 }
251 return size;
254int FlatMenuProxyModel::rowOffsetOf(const QModelIndex &index, int row, bool inclusive) const
256 int offset = 0;
258 QModelIndex parent = index.parent();
260 // Check row 0 offset
261 while(parent.isValid()) {
262 offset += 1;
263 for (int i = 0; i < parent.row(); i++) {
264 QModelIndex currentIndex = sourceModel()->index(i, 0, parent);
265 offset += recursiveRowCount(currentIndex) + 1;
266 }
267 parent = parent.parent();
268 }
270 // Check index.row() offset
271 for(int i = 0; i < row; i++) {
272 QModelIndex currentIndex = sourceModel()->index(i, 0, index.parent());
273 offset += recursiveRowCount(currentIndex) + 1;
274 }
276 if (inclusive) {
277 // itself
278 QModelIndex currentIndex = sourceModel()->index(row, 0, index.parent());
279 offset += recursiveRowCount(currentIndex);
280 }
282 return offset;
285QVariant FlatMenuProxyModel::data(int row, int role) const
287 return QAbstractProxyModel::data(index(row, 0), role);