on cedbc3f help sign in
unity8/plugins/Unity/Indicators/flatmenuproxymodel.cpp
272 lines 7.1 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
59 connect(source,
60 SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
61 SLOT(onModelAboutToBeReset()));
62 connect(source,
63 SIGNAL(rowsInserted(QModelIndex,int,int)),
64 SLOT(onModelReset()));
66 connect(source,
67 SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
68 SLOT(onModelAboutToBeReset()));
69 connect(source,
70 SIGNAL(rowsRemoved(QModelIndex,int,int)),
71 SLOT(onModelReset()));
73 connect(source,
74 SIGNAL(modelAboutToBeReset()),
75 SLOT(onModelAboutToBeReset()));
76 connect(source,
77 SIGNAL(modelReset()),
78 SLOT(onModelReset()));
80 connect(source,
81 SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
82 SIGNAL(statusChanged()));
84 // initiliaze rowCount
85 QModelIndex lastItem = source->index(source->rowCount() - 1, 0);
86 m_rowCount = recursiveRowCount(lastItem);
87 } else {
88 m_rowCount = 0;
89 }
91 Q_EMIT countChanged();
94void FlatMenuProxyModel::setBusName(const QString &busName)
96 m_model->setBusName(busName);
99QString FlatMenuProxyModel::busName() const
101 return m_model->busName();
104void FlatMenuProxyModel::setObjectPath(const QString &busPath)
106 m_model->setObjectPath(busPath);
109QString FlatMenuProxyModel::objectPath() const
111 return m_model->objectPath();
114int FlatMenuProxyModel::status() const
116 return m_model->status();
119DBusEnums::BusType FlatMenuProxyModel::busType() const
121 return m_model->busType();
124void FlatMenuProxyModel::setIntBusType(int type)
126 m_model->setBusType((DBusEnums::BusType) type);
129void FlatMenuProxyModel::start()
131 m_model->start();
134void FlatMenuProxyModel::stop()
136 m_model->stop();
139QModelIndex FlatMenuProxyModel::mapFromSource(const QModelIndex &index) const
141 if (!index.isValid()) {
142 return QModelIndex();
143 }
145 QModelIndex result = createIndex(rowOffsetOf(index, index.row()), 0);
146 return result;
149QModelIndex FlatMenuProxyModel::mapToSource(const QModelIndex &index) const
151 if (sourceModel() && m_indexCache.contains(index.row())) {
152 QString key = m_indexCache.value(index.row());
153 QStringList sections = key.split('.', QString::SkipEmptyParts);
154 QModelIndex sourceIndex = QModelIndex();
155 for(int i=0; i < sections.size(); i++) {
156 sourceIndex = sourceModel()->index(sections.at(i).toInt(), 9, sourceIndex);
157 }
158 return sourceIndex;
159 } else {
160 return QModelIndex();
161 }
164QModelIndex FlatMenuProxyModel::index(int row, int sectionRow, const QModelIndex &parent, const QString &key) const
166 for (int i = 0, count = 0; i < sourceModel()->rowCount(parent); i++) {
167 QModelIndex sourceIndex = sourceModel()->index(i, 0, parent);
168 int indexCount = recursiveRowCount(sourceIndex);
170 if (count == sectionRow) {
171 QString sectionKey = key + "." + QString::number(i);
172 m_indexCache.insert(row, sectionKey);
173 return createIndex(row, 0);
174 } else if ((count + indexCount) >= sectionRow) {
175 count++;
176 return index(row, sectionRow - count, sourceIndex, key + "." + QString::number(i));
177 }
179 count += indexCount + 1;
180 }
181 return QModelIndex();
184QModelIndex FlatMenuProxyModel::index(int row, int, const QModelIndex &) const
186 return index(row, row, QModelIndex(), "");
189QModelIndex FlatMenuProxyModel::parent(const QModelIndex &) const
191 return QModelIndex();
194int FlatMenuProxyModel::columnCount(const QModelIndex &) const
196 return 1;
199int FlatMenuProxyModel::rowCount(const QModelIndex &) const
201 return m_rowCount;
204int FlatMenuProxyModel::count() const
206 return rowCount(QModelIndex());
209void FlatMenuProxyModel::onModelAboutToBeReset()
211 beginResetModel();
214void FlatMenuProxyModel::onModelReset()
216 // initiliaze rowCount
217 m_rowCount = 0;
218 for (int i = 0, iMax = sourceModel()->rowCount(); i < iMax; i++) {
219 QModelIndex index = sourceModel()->index(i, 0);
220 m_rowCount += recursiveRowCount(index) + 1;
221 }
222 m_indexCache.clear();
223 endResetModel();
224 Q_EMIT countChanged();
227int FlatMenuProxyModel::recursiveRowCount(const QModelIndex &index) const
229 int size = sourceModel()->rowCount(index);
230 for (int i=0, iMax=size; i < iMax; i++) {
231 QModelIndex currentIndex = sourceModel()->index(i, 0, index);
232 size += recursiveRowCount(currentIndex);
233 }
235 return size;
238int FlatMenuProxyModel::rowOffsetOf(const QModelIndex &index, int row, bool inclusive) const
240 int offset = 0;
242 QModelIndex parent = index.parent();
244 // Check row 0 offset
245 while(parent.isValid()) {
246 offset += 1;
247 for (int i = 0; i < parent.row(); i++) {
248 QModelIndex currentIndex = sourceModel()->index(i, 0, parent);
249 offset += recursiveRowCount(currentIndex) + 1;
250 }
251 parent = parent.parent();
252 }
254 // Check index.row() offset
255 for(int i = 0; i < row; i++) {
256 QModelIndex currentIndex = sourceModel()->index(i, 0, index.parent());
257 offset += recursiveRowCount(currentIndex) + 1;
258 }
260 if (inclusive) {
261 // itself
262 QModelIndex currentIndex = sourceModel()->index(row, 0, index.parent());
263 offset += recursiveRowCount(currentIndex);
264 }
266 return offset;
269QVariant FlatMenuProxyModel::data(int row, int role) const
271 return QAbstractProxyModel::data(index(row, 0), role);