2 * Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17 * Renato Araujo Oliveira Filho <renato@canonical.com>
24#include "flatmenuproxymodel.h"
28FlatMenuProxyModel::FlatMenuProxyModel(QAbstractItemModel *source)
29 : QAbstractProxyModel(source),
32 m_model = new QDBusMenuModel;
33 setSourceModel(m_model);
37FlatMenuProxyModel::~FlatMenuProxyModel()
42void FlatMenuProxyModel::setSourceModel(QAbstractItemModel *source)
45 QAbstractItemModel *oldSource = sourceModel();
46 if (oldSource == source) {
50 oldSource->disconnect(this);
53 QAbstractProxyModel::setSourceModel(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.
63 SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
64 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
66 SIGNAL(rowsInserted(QModelIndex,int,int)),
67 SLOT(onModelReset()), Qt::QueuedConnection);
70 SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
71 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
73 SIGNAL(rowsRemoved(QModelIndex,int,int)),
74 SLOT(onModelReset()), Qt::QueuedConnection);
77 SIGNAL(modelAboutToBeReset()),
78 SLOT(onModelAboutToBeReset()), Qt::QueuedConnection);
81 SLOT(onModelReset()), Qt::QueuedConnection);
84 SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
85 SIGNAL(statusChanged()), Qt::QueuedConnection);
87 // initiliaze rowCount
88 QModelIndex lastItem = source->index(source->rowCount() - 1, 0);
89 m_rowCount = recursiveRowCount(lastItem);
94 Q_EMIT countChanged();
97void FlatMenuProxyModel::setBusName(const QString &busName)
99 m_model->setBusName(busName);
102QString FlatMenuProxyModel::busName() const
104 return m_model->busName();
107void FlatMenuProxyModel::setObjectPath(const QString &busPath)
109 m_model->setObjectPath(busPath);
112QString FlatMenuProxyModel::objectPath() const
114 return m_model->objectPath();
117int FlatMenuProxyModel::status() const
119 return m_model->status();
122DBusEnums::BusType FlatMenuProxyModel::busType() const
124 return m_model->busType();
127void FlatMenuProxyModel::setIntBusType(int type)
129 m_model->setBusType((DBusEnums::BusType) type);
132void FlatMenuProxyModel::start()
137void FlatMenuProxyModel::stop()
142QModelIndex FlatMenuProxyModel::mapFromSource(const QModelIndex &index) const
144 if (!index.isValid()) {
145 return QModelIndex();
148 QModelIndex result = createIndex(rowOffsetOf(index, index.row()), 0);
152QModelIndex FlatMenuProxyModel::mapToSource(const QModelIndex &index) const
154 if (sourceModel() && m_indexCache.contains(index.row())) {
155 QString key = m_indexCache.value(index.row());
156 QStringList sections = key.split('.', QString::SkipEmptyParts);
157 QModelIndex sourceIndex = QModelIndex();
158 for(int i=0; i < sections.size(); i++) {
159 sourceIndex = sourceModel()->index(sections.at(i).toInt(), 9, sourceIndex);
163 return QModelIndex();
167QModelIndex FlatMenuProxyModel::index(int row, int sectionRow, const QModelIndex &parent, const QString &key) const
169 for (int i = 0, count = 0; i < sourceModel()->rowCount(parent); i++) {
170 QModelIndex sourceIndex = sourceModel()->index(i, 0, parent);
171 int indexCount = recursiveRowCount(sourceIndex);
173 if (count == sectionRow) {
174 QString sectionKey = key + "." + QString::number(i);
175 m_indexCache.insert(row, sectionKey);
176 return createIndex(row, 0);
177 } else if ((count + indexCount) >= sectionRow) {
179 return index(row, sectionRow - count, sourceIndex, key + "." + QString::number(i));
182 count += indexCount + 1;
184 return QModelIndex();
187QModelIndex FlatMenuProxyModel::index(int row, int, const QModelIndex &) const
189 return index(row, row, QModelIndex(), "");
192QModelIndex FlatMenuProxyModel::parent(const QModelIndex &) const
194 return QModelIndex();
197int FlatMenuProxyModel::columnCount(const QModelIndex &) const
202int FlatMenuProxyModel::rowCount(const QModelIndex &) const
207int FlatMenuProxyModel::count() const
209 return rowCount(QModelIndex());
212void FlatMenuProxyModel::onModelAboutToBeReset()
217void FlatMenuProxyModel::onModelReset()
219 // initiliaze rowCount
221 for (int i = 0, iMax = sourceModel()->rowCount(); i < iMax; i++) {
222 QModelIndex index = sourceModel()->index(i, 0);
223 m_rowCount += recursiveRowCount(index) + 1;
225 m_indexCache.clear();
227 Q_EMIT countChanged();
230int FlatMenuProxyModel::recursiveRowCount(const QModelIndex &index) const
232 int size = sourceModel()->rowCount(index);
233 for (int i=0, iMax=size; i < iMax; i++) {
234 QModelIndex currentIndex = sourceModel()->index(i, 0, index);
235 size += recursiveRowCount(currentIndex);
241int FlatMenuProxyModel::rowOffsetOf(const QModelIndex &index, int row, bool inclusive) const
245 QModelIndex parent = index.parent();
247 // Check row 0 offset
248 while(parent.isValid()) {
250 for (int i = 0; i < parent.row(); i++) {
251 QModelIndex currentIndex = sourceModel()->index(i, 0, parent);
252 offset += recursiveRowCount(currentIndex) + 1;
254 parent = parent.parent();
257 // Check index.row() offset
258 for(int i = 0; i < row; i++) {
259 QModelIndex currentIndex = sourceModel()->index(i, 0, index.parent());
260 offset += recursiveRowCount(currentIndex) + 1;
265 QModelIndex currentIndex = sourceModel()->index(row, 0, index.parent());
266 offset += recursiveRowCount(currentIndex);
272QVariant FlatMenuProxyModel::data(int row, int role) const
274 return QAbstractProxyModel::data(index(row, 0), role);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list