on 174fe91 help sign in
unity8/plugins/Unity/ratingsfilter.cpp
82 lines 2.4 kB history raw
1/*
2 * Copyright (C) 2011, 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@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 "ratingsfilter.h"
23// local
24#include "genericlistmodel.h"
25#include "ratingfilteroption.h"
27// Qt
28#include <QDebug>
30RatingsFilter::RatingsFilter(QObject *parent) :
31 Filter(parent), m_unityRatingsFilter(nullptr), m_options(nullptr)
35GenericListModel* RatingsFilter::options() const
37 return m_options;
40void RatingsFilter::setUnityFilter(unity::dash::Filter::Ptr filter)
42 if (m_unityFilter != nullptr) {
43 m_signals.disconnectAll();
44 }
46 Filter::setUnityFilter(filter);
47 m_unityRatingsFilter = std::dynamic_pointer_cast<unity::dash::RatingsFilter>(m_unityFilter);
49 if (m_options) {
50 delete m_options;
51 }
53 m_options = new GenericListModel(this);
54 for (int i=1; i<=5; i++) {
55 auto opt = new RatingFilterOption(QString::number(i), i*0.2f, this);
56 connect(opt, SIGNAL(activeChanged(bool)), this, SLOT(onActiveChanged()));
57 m_options->addObject(opt);
58 }
60 Q_EMIT ratingsChanged();
63void RatingsFilter::onActiveChanged()
65 RatingFilterOption *option = dynamic_cast<RatingFilterOption*>(QObject::sender());
66 if (option != nullptr) {
67 if (option->active()) {
68 // disable all other options
69 for (auto it = m_options->optionsBegin(); it != m_options->optionsEnd(); it++) {
70 RatingFilterOption *opt = dynamic_cast<RatingFilterOption *>(*it);
71 if (opt && opt != option && opt->active()) {
72 opt->setActive(false);
73 }
74 }
75 m_unityRatingsFilter->rating = option->value();
76 }
78 Q_EMIT ratingsChanged();
79 } else {
80 qWarning() << "Invalid option";
81 }