2 * Copyright (C) 2012, 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include "visibleindicatorsmodel.h"
19#include "rootactionstate.h"
20#include "indicators.h"
21#include "cachedunitymenumodel.h"
23#include <unitymenumodel.h>
28VisibleIndicatorsModel::VisibleIndicatorsModel(QObject *parent)
29 : QIdentityProxyModel(parent)
31 connect(this, SIGNAL(modelReset()), SIGNAL(countChanged()));
32 connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
33 connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
36VisibleIndicatorsModel::~VisibleIndicatorsModel()
38 qDeleteAll(m_rootActions);
39 m_rootActions.clear();
41 qDeleteAll(m_cachedModels);
42 m_cachedModels.clear();
47QHash<int, QByteArray> VisibleIndicatorsModel::roleNames() const
49 return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>();
52QString VisibleIndicatorsModel::profile() const
57void VisibleIndicatorsModel::setProfile(const QString& profile)
59 if (m_profile != profile) {
61 Q_EMIT profileChanged();
65void VisibleIndicatorsModel::setModel(QAbstractItemModel *itemModel)
67 if (itemModel != sourceModel()) {
68 if (sourceModel() != NULL) {
69 sourceModel()->disconnect(this);
72 setSourceModel(itemModel);
74 if (sourceModel() != NULL) {
75 // ... and use our own
76 connect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
77 SLOT(sourceRowsInserted(QModelIndex,int,int)));
78 connect(sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
79 SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
80 connect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
81 SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
83 Q_EMIT modelChanged();
87void VisibleIndicatorsModel::sourceRowsInserted(const QModelIndex&, int start, int end)
89 for (int row = start; row <= end; row++) {
90 RootActionState* actionState = new RootActionState();
91 connect(actionState, SIGNAL(updated()), SLOT(onActionStateUpdated()));
92 m_rootActions.insert(row, actionState);
93 m_visible.insert(row, false);
95 m_cachedModels.insert(row, new CachedUnityMenuModel);
96 updateCachedModel(sourceModel()->index(row, 0));
100void VisibleIndicatorsModel::sourceRowsAboutToBeRemoved(const QModelIndex&, int start, int end)
102 for (int row = start; row <= end; row++) {
103 if (m_rootActions.count() > start) {
104 delete m_rootActions[start];
105 m_rootActions.removeAt(start);
107 if (m_visible.count() > start) {
108 m_visible.removeAt(start);
110 if (m_cachedModels.count() > start) {
111 delete m_cachedModels[start];
112 m_cachedModels.removeAt(start);
117void VisibleIndicatorsModel::sourceDataChanged(const QModelIndex& topRight, const QModelIndex& bottomRight)
119 for (int row = topRight.row(); row <= bottomRight.row(); row++) {
120 updateCachedModel(sourceModel()->index(row, 0));
124void VisibleIndicatorsModel::onActionStateUpdated()
126 RootActionState* actionState = qobject_cast<RootActionState*>(sender());
127 int changedRow = m_rootActions.indexOf(actionState);
129 QModelIndex modelIndex = this->index(changedRow, 0);
130 if (!modelIndex.isValid()) {
134 while (m_visible.count() <= changedRow) {
135 m_visible.append(false);
138 if (actionState->isVisible() != m_visible[changedRow]) {
139 m_visible[changedRow] = actionState->isVisible();
141 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << IndicatorsModelRole::IsVisible);
145QVariant VisibleIndicatorsModel::data(const QModelIndex &index, int role) const
147 if (role != IndicatorsModelRole::IsVisible)
148 return QIdentityProxyModel::data(index, role);
150 if (!index.isValid() || m_visible.count() <= index.row())
153 return m_visible[index.row()];
156void VisibleIndicatorsModel::updateCachedModel(const QModelIndex& index)
158 if (!index.isValid()) {
161 QVariant data = sourceModel()->data(index, IndicatorsModelRole::IndicatorProperties);
162 if (!data.isValid()) {
165 int row = index.row();
167 if (m_cachedModels.count() <= row) {
170 CachedUnityMenuModel* cachedModel = m_cachedModels[row];
172 QVariantMap properties = data.toMap();
174 QVariantMap menuObjectPaths = properties["menuObjectPaths"].toMap();
175 if (!menuObjectPaths.contains(m_profile)) {
179 QString menuObjectPath = menuObjectPaths[m_profile].toString();
180 QString busName = properties["busName"].toString();
181 QString actionsObjectPath = properties["actionsObjectPath"].toString();
183 cachedModel->setBusName(busName);
184 cachedModel->setMenuObjectPath(menuObjectPath);
185 QVariantMap actions; actions["indicator"] = actionsObjectPath;
186 cachedModel->setActions(actions);
188 if (m_rootActions.count() > row) {
189 m_rootActions[row]->setMenu(cachedModel->model());
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