on 76f5c02 help sign in
unity8/plugins/Unity/genericoptionsmodel.cpp
156 lines 4.2 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"
26GenericOptionsModel::GenericOptionsModel(QObject *parent)
27 : QAbstractListModel(parent)
29 setupRoles();
32GenericOptionsModel::~GenericOptionsModel()
34 Q_FOREACH(auto opt, m_options)
35 {
36 opt->deleteLater();
37 }
40void GenericOptionsModel::setupRoles()
42 m_roles[GenericOptionsModel::RoleId] = "id";
43 m_roles[GenericOptionsModel::RoleName] = "name";
44 m_roles[GenericOptionsModel::RoleIconHint] = "iconHint";
45 m_roles[GenericOptionsModel::RoleActive] = "active";
48QHash<int, QByteArray> GenericOptionsModel::roleNames() const
50 return m_roles;
53QVariant GenericOptionsModel::data(const QModelIndex& index, int role) const
55 if (!index.isValid()) {
56 return QVariant();
57 }
59 auto filterOption = m_options[index.row()];
60 switch (role)
61 {
62 case GenericOptionsModel::RoleId:
63 return filterOption->id();
64 case GenericOptionsModel::RoleName:
65 case Qt::DisplayRole:
66 return filterOption->name();
67 case GenericOptionsModel::RoleIconHint:
68 return filterOption->iconHint();
69 case GenericOptionsModel::RoleActive:
70 return filterOption->active();
71 default:
72 break;
73 }
75 return QVariant();
78int GenericOptionsModel::rowCount(const QModelIndex& /* parent */) const
80 return m_options.size ();
83AbstractFilterOption* GenericOptionsModel::getRawOption(QVector<AbstractFilterOption *>::size_type idx) const
85 if (idx < m_options.size())
86 {
87 return m_options[idx];
88 }
89 return nullptr;
92void GenericOptionsModel::ensureTheOnlyActive(AbstractFilterOption *activeOption)
94 if (activeOption->active()) {
95 // disable all other options
96 Q_FOREACH(auto opt, m_options) {
97 if (opt != activeOption && opt->active()) {
98 opt->setActive(false);
99 }
100 }
101 }
104void GenericOptionsModel::addOption(AbstractFilterOption *option, int index)
106 m_options.insert(index, option);
108 connect(option, SIGNAL(idChanged(const QString &)), this, SLOT(onOptionChanged()));
109 connect(option, SIGNAL(nameChanged(const QString &)), this, SLOT(onOptionChanged()));
110 connect(option, SIGNAL(iconHintChanged(const QString &)), this, SLOT(onOptionChanged()));
111 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onOptionChanged()));
112 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged()));
115void GenericOptionsModel::removeOption(int index)
117 m_options[index]->deleteLater();
118 m_options.remove(index);
121int GenericOptionsModel::indexOf(const QString &option_id)
123 int row = 0;
124 Q_FOREACH(auto opt, m_options)
125 {
126 if (opt->id() == option_id)
127 {
128 return row;
129 }
130 ++row;
131 }
132 return -1;
135void GenericOptionsModel::onOptionChanged()
137 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
138 if (option)
139 {
140 int idx = indexOf(option->id());
141 if (idx >= 0)
142 {
143 QModelIndex optionIndex = index(idx);
144 Q_EMIT dataChanged(optionIndex, optionIndex);
145 }
146 }
149void GenericOptionsModel::onActiveChanged()
151 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
152 if (option)
153 {
154 Q_EMIT activeChanged(option);
155 }