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
60 SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
61 SLOT(onModelAboutToBeReset()));
63 SIGNAL(rowsInserted(QModelIndex,int,int)),
64 SLOT(onModelReset()));
67 SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
68 SLOT(onModelAboutToBeReset()));
70 SIGNAL(rowsRemoved(QModelIndex,int,int)),
71 SLOT(onModelReset()));
74 SIGNAL(modelAboutToBeReset()),
75 SLOT(onModelAboutToBeReset()));
78 SLOT(onModelReset()));
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);
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()
134void FlatMenuProxyModel::stop()
139QModelIndex FlatMenuProxyModel::mapFromSource(const QModelIndex &index) const
141 if (!index.isValid()) {
142 return QModelIndex();
145 QModelIndex result = createIndex(rowOffsetOf(index, index.row()), 0);
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);
160 return QModelIndex();
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) {
176 return index(row, sectionRow - count, sourceIndex, key + "." + QString::number(i));
179 count += indexCount + 1;
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
199int FlatMenuProxyModel::rowCount(const QModelIndex &) const
204int FlatMenuProxyModel::count() const
206 return rowCount(QModelIndex());
209void FlatMenuProxyModel::onModelAboutToBeReset()
214void FlatMenuProxyModel::onModelReset()
216 // initiliaze rowCount
218 for (int i = 0, iMax = sourceModel()->rowCount(); i < iMax; i++) {
219 QModelIndex index = sourceModel()->index(i, 0);
220 m_rowCount += recursiveRowCount(index) + 1;
222 m_indexCache.clear();
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);
238int FlatMenuProxyModel::rowOffsetOf(const QModelIndex &index, int row, bool inclusive) const
242 QModelIndex parent = index.parent();
244 // Check row 0 offset
245 while(parent.isValid()) {
247 for (int i = 0; i < parent.row(); i++) {
248 QModelIndex currentIndex = sourceModel()->index(i, 0, parent);
249 offset += recursiveRowCount(currentIndex) + 1;
251 parent = parent.parent();
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;
262 QModelIndex currentIndex = sourceModel()->index(row, 0, index.parent());
263 offset += recursiveRowCount(currentIndex);
269QVariant FlatMenuProxyModel::data(int row, int role) const
271 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