on 81ef490 help sign in
unity8/plugins/Unity/genericoptionsmodel.cpp
187 lines 5.0 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Pawel Stolowski <pawel.stolowski@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// self
21#include "genericoptionsmodel.h"
23// local
24#include "abstractfilteroption.h"
25#include "showallfilteroption.h"
27GenericOptionsModel::GenericOptionsModel(bool showAllOption, QObject *parent)
28 : QAbstractListModel(parent)
30 if (showAllOption)
31 {
32 auto showAll = new ShowAllFilterOption(this);
33 connect(showAll, SIGNAL(activeChanged(bool)), this, SLOT(onOptionChanged()));
34 connect(showAll, SIGNAL(activeChanged(bool)), this, SLOT(onShowAllClicked(bool)));
35 addOption(showAll);
36 }
39GenericOptionsModel::~GenericOptionsModel()
41 Q_FOREACH(auto opt, m_options)
42 {
43 opt->deleteLater();
44 }
47QHash<int, QByteArray> GenericOptionsModel::roleNames() const
49 QHash<int, QByteArray> roles;
50 roles[GenericOptionsModel::RoleId] = "id";
51 roles[GenericOptionsModel::RoleName] = "name";
52 roles[GenericOptionsModel::RoleIconHint] = "iconHint";
53 roles[GenericOptionsModel::RoleActive] = "active";
54 return roles;
57QVariant GenericOptionsModel::data(const QModelIndex& index, int role) const
59 if (!index.isValid()) {
60 return QVariant();
61 }
63 auto filterOption = m_options[index.row()];
64 switch (role)
65 {
66 case GenericOptionsModel::RoleId:
67 return filterOption->id();
68 case GenericOptionsModel::RoleName:
69 case Qt::DisplayRole:
70 return filterOption->name();
71 case GenericOptionsModel::RoleIconHint:
72 return filterOption->iconHint();
73 case GenericOptionsModel::RoleActive:
74 return filterOption->active();
75 default:
76 break;
77 }
79 return QVariant();
82int GenericOptionsModel::rowCount(const QModelIndex& /* parent */) const
84 return m_options.size();
87void GenericOptionsModel::setActive(int idx, bool value)
89 if (idx >= 0 && idx < m_options.size())
90 {
91 m_options[idx]->setActive(value);
92 }
95void GenericOptionsModel::setActive(const QString &id, bool value)
97 Q_FOREACH(auto opt, m_options) {
98 if (opt->id() == id) {
99 opt->setActive(value);
100 break;
101 }
102 }
105AbstractFilterOption* GenericOptionsModel::getRawOption(QVector<AbstractFilterOption *>::size_type idx) const
107 if (idx < m_options.size())
108 {
109 return m_options[idx];
110 }
111 return nullptr;
114void GenericOptionsModel::ensureTheOnlyActive(AbstractFilterOption *activeOption)
116 if (activeOption->active()) {
117 // disable all other options
118 Q_FOREACH(auto opt, m_options) {
119 if (opt != activeOption && opt->active()) {
120 opt->setActive(false);
121 }
122 }
123 }
126void GenericOptionsModel::addOption(AbstractFilterOption *option, int index)
128 if (index >= 0)
129 m_options.insert(index, option);
130 else
131 m_options.append(option);
133 connect(option, SIGNAL(idChanged(const QString &)), this, SLOT(onOptionChanged()));
134 connect(option, SIGNAL(nameChanged(const QString &)), this, SLOT(onOptionChanged()));
135 connect(option, SIGNAL(iconHintChanged(const QString &)), this, SLOT(onOptionChanged()));
136 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onOptionChanged()));
137 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged()));
140void GenericOptionsModel::removeOption(int index)
142 m_options[index]->deleteLater();
143 m_options.remove(index);
146int GenericOptionsModel::indexOf(const QString &option_id)
148 int row = 0;
149 Q_FOREACH(auto opt, m_options)
150 {
151 if (opt->id() == option_id)
152 {
153 return row;
154 }
155 ++row;
156 }
157 return -1;
160void GenericOptionsModel::onOptionChanged()
162 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
163 if (option)
164 {
165 int idx = indexOf(option->id());
166 if (idx >= 0)
167 {
168 QModelIndex optionIndex = index(idx);
169 Q_EMIT dataChanged(optionIndex, optionIndex);
170 }
171 }
174void GenericOptionsModel::onActiveChanged()
176 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
177 if (option)
178 {
179 Q_EMIT activeChanged(option);
180 }
183void GenericOptionsModel::onShowAllClicked(bool active)
185 if (active)
186 Q_EMIT showAllActivated();