on 17ebfaa help sign in
unity8/plugins/Unity/genericoptionsmodel.cpp
152 lines 4.1 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)
31GenericOptionsModel::~GenericOptionsModel()
33 Q_FOREACH(auto opt, m_options)
34 {
35 opt->deleteLater();
36 }
39QHash<int, QByteArray> GenericOptionsModel::roleNames() const
41 QHash<int, QByteArray> roles;
42 roles[GenericOptionsModel::RoleId] = "id";
43 roles[GenericOptionsModel::RoleName] = "name";
44 roles[GenericOptionsModel::RoleIconHint] = "iconHint";
45 roles[GenericOptionsModel::RoleActive] = "active";
46 return roles;
49QVariant GenericOptionsModel::data(const QModelIndex& index, int role) const
51 if (!index.isValid()) {
52 return QVariant();
53 }
55 auto filterOption = m_options[index.row()];
56 switch (role)
57 {
58 case GenericOptionsModel::RoleId:
59 return filterOption->id();
60 case GenericOptionsModel::RoleName:
61 case Qt::DisplayRole:
62 return filterOption->name();
63 case GenericOptionsModel::RoleIconHint:
64 return filterOption->iconHint();
65 case GenericOptionsModel::RoleActive:
66 return filterOption->active();
67 default:
68 break;
69 }
71 return QVariant();
74int GenericOptionsModel::rowCount(const QModelIndex& /* parent */) const
76 return m_options.size ();
79AbstractFilterOption* GenericOptionsModel::getRawOption(QVector<AbstractFilterOption *>::size_type idx) const
81 if (idx < m_options.size())
82 {
83 return m_options[idx];
84 }
85 return nullptr;
88void GenericOptionsModel::ensureTheOnlyActive(AbstractFilterOption *activeOption)
90 if (activeOption->active()) {
91 // disable all other options
92 Q_FOREACH(auto opt, m_options) {
93 if (opt != activeOption && opt->active()) {
94 opt->setActive(false);
95 }
96 }
97 }
100void GenericOptionsModel::addOption(AbstractFilterOption *option, int index)
102 m_options.insert(index, option);
104 connect(option, SIGNAL(idChanged(const QString &)), this, SLOT(onOptionChanged()));
105 connect(option, SIGNAL(nameChanged(const QString &)), this, SLOT(onOptionChanged()));
106 connect(option, SIGNAL(iconHintChanged(const QString &)), this, SLOT(onOptionChanged()));
107 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onOptionChanged()));
108 connect(option, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged()));
111void GenericOptionsModel::removeOption(int index)
113 m_options[index]->deleteLater();
114 m_options.remove(index);
117int GenericOptionsModel::indexOf(const QString &option_id)
119 int row = 0;
120 Q_FOREACH(auto opt, m_options)
121 {
122 if (opt->id() == option_id)
123 {
124 return row;
125 }
126 ++row;
127 }
128 return -1;
131void GenericOptionsModel::onOptionChanged()
133 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
134 if (option)
135 {
136 int idx = indexOf(option->id());
137 if (idx >= 0)
138 {
139 QModelIndex optionIndex = index(idx);
140 Q_EMIT dataChanged(optionIndex, optionIndex);
141 }
142 }
145void GenericOptionsModel::onActiveChanged()
147 AbstractFilterOption *option = dynamic_cast<AbstractFilterOption*>(QObject::sender());
148 if (option)
149 {
150 Q_EMIT activeChanged(option);
151 }