on f2191cb help sign in
unity8/plugins/Unity/combinedfilteroptions.cpp
104 lines 3.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,
23 sigc::signal<void, unity::dash::FilterOption::Ptr> itemAddedSignal,
24 sigc::signal<void, unity::dash::FilterOption::Ptr> itemRemovedSignal,
25 QObject *parent)
26 : QAbstractListModel(parent)
28 initList(list, itemAddedSignal, itemRemovedSignal);
32QVariant CombinedFilterOptions::data(const QModelIndex& index, int /* role */) const
34 if (!index.isValid()) {
35 return QVariant();
36 }
38 CombinedFilterOption* item = m_list.at(index.row());
39 return QVariant::fromValue(item);
42int CombinedFilterOptions::rowCount(const QModelIndex& /* parent */) const
44 return m_list.count();
47void CombinedFilterOptions::initList(const std::vector<unity::dash::FilterOption::Ptr>& list,
48 sigc::signal<void, unity::dash::FilterOption::Ptr> itemAddedSignal,
49 sigc::signal<void, unity::dash::FilterOption::Ptr> itemRemovedSignal)
51 // combine options, e.g. A, B, C becomes A-B, B-C, C
52 unsigned int i = 0;
53 while (i < list.size() - 1) {
54 unity::dash::FilterOption::Ptr option1 = list[i];
55 unity::dash::FilterOption::Ptr option2 = NULL;
56 //if (i < list.size()-1) {
57 option2 = list[i+1];
58 //}
59 auto co = new CombinedFilterOption(option1, option2);
60 connect(co, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged(bool)));
61 addOption(co);
62 ++i;
63 }
64 if (list.size() > 0) {
65 auto co = new CombinedFilterOption(list[i], NULL);
66 addOption(co);
67 }
69 itemAddedSignal.connect(sigc::mem_fun(this, &CombinedFilterOptions::onItemAdded));
70 itemRemovedSignal.connect(sigc::mem_fun(this, &CombinedFilterOptions::onItemRemoved));
73void CombinedFilterOptions::addOption(CombinedFilterOption *option)
75 int index = m_list.count();
76 beginInsertRows(QModelIndex(), index, index);
77 m_list.insert(index, option);
78 endInsertRows();
81void CombinedFilterOptions::onActiveChanged(bool state)
83 // if option became active, need to disable all other options
84 if (state) {
85 CombinedFilterOption *option = dynamic_cast<CombinedFilterOption*>(QObject::sender());
86 if (option) {
87 Q_FOREACH (auto fo, m_list) {
88 if (option != fo) {
89 // note that changing state will result in onActiveChanged signal for that option, but
90 // since it will be 'false', we won't end up in an infinite loop
91 fo->setInactive(*option);
92 }
93 }
94 }
95 }
98void CombinedFilterOptions::onItemAdded(unity::dash::FilterOption::Ptr item)
102void CombinedFilterOptions::onItemRemoved(unity::dash::FilterOption::Ptr item)