Back to index

unity  6.0.0
RatingsFilter.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  */
00019 
00020 #include "RatingsFilter.h"
00021 
00022 #include <NuxCore/Logger.h>
00023 
00024 namespace unity
00025 {
00026 namespace dash
00027 {
00028 
00029 namespace
00030 {
00031 nux::logging::Logger logger("unity.dash.ratingsfilter");
00032 }
00033 
00034 RatingsFilter::RatingsFilter(DeeModel* model, DeeModelIter* iter)
00035   : Filter(model, iter)
00036   , rating(0.0f)
00037 {
00038   rating.changed.connect(sigc::mem_fun(this, &RatingsFilter::OnRatingChanged));
00039   Refresh();
00040 }
00041 
00042 void RatingsFilter::Clear()
00043 {
00044   rating = 0.0f;
00045 }
00046 
00047 void RatingsFilter::Update(Filter::Hints& hints)
00048 {
00049   GVariant* rating_variant = hints["rating"];
00050 
00051   if (rating_variant)
00052   {
00053     rating = (float)g_variant_get_double(rating_variant);
00054   }
00055   else
00056   {
00057     LOG_WARN(logger) << "Hints do not contain a 'rating' key";
00058     rating = 0.0f;
00059   }
00060 }
00061 
00062 void RatingsFilter::OnRatingChanged(float new_value)
00063 {
00064   UpdateState(new_value);
00065 }
00066 
00067 void RatingsFilter::UpdateState(float raw_rating)
00068 {
00069   if (!IsValid())
00070     return;
00071     
00072   bool new_filtering = raw_rating > 0.0f;
00073 
00074   GVariantBuilder b;
00075   g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
00076   g_variant_builder_add(&b, "{sv}", "rating", g_variant_new("d", raw_rating));
00077 
00078   IgnoreChanges(true);
00079   dee_model_set_value(model_,iter_,
00080                       FilterColumn::RENDERER_STATE,
00081                       g_variant_builder_end(&b));
00082   dee_model_set_value(model_, iter_,
00083                       FilterColumn::FILTERING,
00084                       g_variant_new("b", new_filtering ? TRUE : FALSE));
00085   IgnoreChanges(false);
00086 
00087   filtering.EmitChanged(filtering);
00088 }
00089 
00090 }
00091 }