Back to index

unity  6.0.0
FilterMultiRangeWidget.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright 2011 Canonical Ltd.
00004  *
00005  * This program is free software: you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser 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, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License version 3 along with this program.  If not, see
00017  * <http://www.gnu.org/licenses/>
00018  *
00019  * Authored by: Gordon Allott <gord.allott@canonical.com>
00020  *
00021  */
00022 
00023 #include <Nux/Nux.h>
00024 
00025 #include "unity-shared/DashStyle.h"
00026 #include "FilterMultiRangeWidget.h"
00027 #include "FilterMultiRangeButton.h"
00028 #include "FilterBasicButton.h"
00029 
00030 #include <glib.h>
00031 #include <glib/gi18n-lib.h>
00032 
00033 namespace unity
00034 {
00035 namespace dash
00036 {
00037 
00038 NUX_IMPLEMENT_OBJECT_TYPE(FilterMultiRange);
00039 
00040 FilterMultiRange::FilterMultiRange(NUX_FILE_LINE_DECL)
00041   : FilterExpanderLabel(_("Multi-range"), NUX_FILE_LINE_PARAM)
00042 {
00043   InitTheme();
00044 
00045   dash::Style& style = dash::Style::Instance();
00046   const int left_padding = 0;
00047   const int right_padding = 0;
00048   const int top_padding = style.GetSpaceBetweenFilterWidgets() - style.GetFilterHighlightPadding() - 2;
00049   const int bottom_padding = style.GetFilterHighlightPadding() - 1;
00050 
00051   all_button_ = new FilterAllButton(NUX_TRACKER_LOCATION);
00052 
00053   layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
00054   layout_->SetLeftAndRightPadding(left_padding, right_padding);
00055   layout_->SetTopAndBottomPadding(top_padding, bottom_padding);
00056 
00057   SetRightHandView(all_button_);
00058   SetContents(layout_);
00059   OnActiveChanged(false);
00060 }
00061 
00062 FilterMultiRange::~FilterMultiRange()
00063 {
00064 }
00065 
00066 void FilterMultiRange::SetFilter(Filter::Ptr const& filter)
00067 {
00068   filter_ = std::static_pointer_cast<MultiRangeFilter>(filter);
00069 
00070   all_button_->SetFilter(filter_);
00071   expanded = !filter_->collapsed();
00072 
00073   filter_->option_added.connect(sigc::mem_fun(this, &FilterMultiRange::OnOptionAdded));
00074   filter_->option_removed.connect(sigc::mem_fun(this, &FilterMultiRange::OnOptionRemoved));
00075 
00076   // finally - make sure we are up-todate with our filter list
00077   for (auto it : filter_->options())
00078     OnOptionAdded(it);
00079 
00080   SetLabel(filter_->name);
00081 }
00082 
00083 void FilterMultiRange::OnActiveChanged(bool value)
00084 {
00085   // go through all the buttons, and set the state :(
00086   int start = 2000;
00087   int end = 0;
00088   int index = 0;
00089   for (auto button : buttons_)
00090   {
00091     FilterOption::Ptr filter = button->GetFilter();
00092     bool tmp_active = filter->active;
00093     button->SetActive(tmp_active);
00094     if (filter != nullptr)
00095     {
00096       if (filter->active)
00097       {
00098         if (index < start)
00099           start = index;
00100         if (index > end)
00101           end = index;
00102       }
00103     }
00104     index++;
00105   }
00106 
00107   index = 0;
00108   for (auto button : buttons_)
00109   {
00110     if (index == start && index == end)
00111       button->SetHasArrow(MultiRangeArrow::BOTH);
00112     else if (index == start)
00113       button->SetHasArrow(MultiRangeArrow::LEFT);
00114     else if (index == end && index != 0)
00115       button->SetHasArrow(MultiRangeArrow::RIGHT);
00116     else
00117       button->SetHasArrow(MultiRangeArrow::NONE);
00118 
00119     if (index == 0)
00120       button->SetVisualSide(MultiRangeSide::LEFT);
00121     else if (index == (int)buttons_.size() - 1)
00122       button->SetVisualSide(MultiRangeSide::RIGHT);
00123     else
00124       button->SetVisualSide(MultiRangeSide::CENTER);
00125 
00126     index++;
00127   }
00128 }
00129 
00130 void FilterMultiRange::OnOptionAdded(FilterOption::Ptr const& new_filter)
00131 {
00132   FilterMultiRangeButton* button = new FilterMultiRangeButton(NUX_TRACKER_LOCATION);
00133   button->SetFilter(new_filter);
00134   layout_->AddView(button);
00135   buttons_.push_back(button);
00136   new_filter->active.changed.connect(sigc::mem_fun(this, &FilterMultiRange::OnActiveChanged));
00137   OnActiveChanged(false);
00138 
00139 }
00140 
00141 void FilterMultiRange::OnOptionRemoved(FilterOption::Ptr const& removed_filter)
00142 {
00143   for (auto it=buttons_.begin() ; it != buttons_.end(); it++)
00144   {
00145     if ((*it)->GetFilter() == removed_filter)
00146     {
00147       layout_->RemoveChildObject(*it);
00148       buttons_.erase(it);
00149       break;
00150     }
00151   }
00152 
00153   OnActiveChanged(false);
00154 }
00155 
00156 std::string FilterMultiRange::GetFilterType()
00157 {
00158   return "FilterMultiRange";
00159 }
00160 
00161 void FilterMultiRange::InitTheme()
00162 {
00163   //FIXME - build theme here - store images, cache them, fun fun fun
00164 }
00165 
00166 } // namespace dash
00167 } // namespace unity