on 27c40fc help sign in
unity8/plugins/Unity/combinedfilteroptions.cpp
85 lines 2.7 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#include "combinedfilteroptions.h"
22CombinedFilterOptions::CombinedFilterOptions(const std::vector<unity::dash::FilterOption::Ptr>& list, QObject *parent)
23 : QAbstractListModel(parent)
25 initList(list);
29QVariant CombinedFilterOptions::data(const QModelIndex& index, int /* role */) const
31 if (!index.isValid()) {
32 return QVariant();
33 }
35 CombinedFilterOption* item = m_list.at(index.row());
36 return QVariant::fromValue(item);
39int CombinedFilterOptions::rowCount(const QModelIndex& /* parent */) const
41 return m_list.count();
44void CombinedFilterOptions::initList(const std::vector<unity::dash::FilterOption::Ptr>& list)
46 // combine options, e.g. A, B, C becomes A-B, B-C, C
47 unsigned int i = 0;
48 while (i < list.size() - 1) {
49 unity::dash::FilterOption::Ptr option1 = list[i];
50 unity::dash::FilterOption::Ptr option2 = list[i+1];
51 auto co = new CombinedFilterOption(option1, option2);
52 connect(co, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged(bool)));
53 addOption(co);
54 ++i;
55 }
56 if (list.size() > 0) {
57 auto co = new CombinedFilterOption(list[i], NULL);
58 addOption(co);
59 }
62void CombinedFilterOptions::addOption(CombinedFilterOption *option)
64 int index = m_list.count();
65 beginInsertRows(QModelIndex(), index, index);
66 m_list.insert(index, option);
67 endInsertRows();
70void CombinedFilterOptions::onActiveChanged(bool state)
72 // if option became active, need to disable all other options
73 if (state) {
74 CombinedFilterOption *option = dynamic_cast<CombinedFilterOption*>(QObject::sender());
75 if (option) {
76 Q_FOREACH (auto fo, m_list) {
77 if (option != fo) {
78 // note that changing state will result in onActiveChanged signal for that option, but
79 // since it will be 'false', we won't end up in an infinite loop
80 fo->setInactive(*option);
81 }
82 }
83 }
84 }