Back to index

unity  6.0.0
FilterBar.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License version 3, as
00006  * published by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00012  * License for more details.
00013  *
00014  * You should have received a copy of both the GNU Lesser General Public
00015  * License version 3 along with this program.  If not, see
00016  * <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Gordon Allott <gord.allott@canonical.com>
00019  *
00020  */
00021 
00022 #include <Nux/Nux.h>
00023 #include <Nux/VLayout.h>
00024 #include <NuxCore/Logger.h>
00025 
00026 #include "unity-shared/DashStyle.h"
00027 #include "FilterBar.h"
00028 #include "FilterExpanderLabel.h"
00029 #include "FilterFactory.h"
00030 
00031 namespace unity
00032 {
00033 namespace dash
00034 {
00035 namespace
00036 {
00037 
00038 nux::logging::Logger logger("unity.dash.filterbar");
00039 
00040 }
00041 
00042 NUX_IMPLEMENT_OBJECT_TYPE(FilterBar);
00043 
00044 FilterBar::FilterBar(NUX_FILE_LINE_DECL)
00045   : View(NUX_FILE_LINE_PARAM)
00046 {
00047   // TODO - does the filterbar associate itself with a model of some sort?
00048   // does libunity provide a Lens.Filters model or something that we can update on?
00049   // don't want to associate a Filterbar with just a lens model, its a filter bar not a
00050   // lens parser
00051   Init();
00052 }
00053 
00054 FilterBar::~FilterBar()
00055 {
00056 }
00057 
00058 void FilterBar::Init()
00059 {
00060   dash::Style& style = dash::Style::Instance();
00061 
00062   nux::LinearLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00063   layout->SetTopAndBottomPadding(style.GetFilterBarTopPadding() - style.GetFilterHighlightPadding());
00064   layout->SetSpaceBetweenChildren(style.GetSpaceBetweenFilterWidgets() - style.GetFilterHighlightPadding());
00065   SetLayout(layout);
00066 }
00067 
00068 void FilterBar::SetFilters(Filters::Ptr const& filters)
00069 {
00070   filters_ = filters;
00071 }
00072 
00073 void FilterBar::AddFilter(Filter::Ptr const& filter)
00074 {
00075   if (filter_map_.count(filter) > 0)
00076   {
00077     LOG_WARN(logger) << "Attempting to add a filter that has already been added";
00078     return;
00079   }
00080 
00081   FilterExpanderLabel* filter_view = factory_.WidgetForFilter(filter);
00082   AddChild(filter_view);
00083   filter_map_[filter] = filter_view;
00084   GetLayout()->AddView(filter_view, 0, nux::MINOR_POSITION_LEFT, nux::MINOR_SIZE_FULL);
00085 
00086   UpdateDrawSeparators();
00087 }
00088 
00089 void FilterBar::RemoveFilter(Filter::Ptr const& filter)
00090 {
00091   for (auto iter: filter_map_)
00092   {
00093     if (iter.first->id == filter->id)
00094     {
00095       FilterExpanderLabel* filter_view = iter.second;
00096       RemoveChild(filter_view);
00097       filter_map_.erase(filter_map_.find(iter.first));
00098       GetLayout()->RemoveChildObject(filter_view);
00099       break;
00100     }
00101   }
00102 
00103   UpdateDrawSeparators();
00104 }
00105 
00106 void FilterBar::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
00107 {
00108   nux::Geometry const& geo = GetGeometry();
00109 
00110   GfxContext.PushClippingRectangle(geo);
00111   nux::GetPainter().PaintBackground(GfxContext, geo);
00112   GfxContext.PopClippingRectangle();
00113 }
00114 
00115 void FilterBar::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
00116 {
00117   GfxContext.PushClippingRectangle(GetGeometry());
00118   GetLayout()->ProcessDraw(GfxContext, force_draw);
00119 
00120   GfxContext.PopClippingRectangle();
00121 }
00122 
00123 void FilterBar::UpdateDrawSeparators()
00124 {
00125   std::list<Area*> children = GetLayout()->GetChildren();
00126   std::list<Area*>::reverse_iterator rit;
00127   bool found_one = false;
00128 
00129   for (rit = children.rbegin(); rit != children.rend(); ++rit)
00130   {
00131     FilterExpanderLabel* widget = dynamic_cast<FilterExpanderLabel*>(*rit);
00132 
00133     if (!widget)
00134       continue;
00135 
00136     widget->draw_separator = found_one;
00137     found_one = true;
00138   }
00139 }
00140 
00141 //
00142 // Key navigation
00143 //
00144 
00145 bool FilterBar::AcceptKeyNavFocus()
00146 {
00147   return false;
00148 }
00149 
00150 //
00151 // Introspection
00152 //
00153 std::string FilterBar::GetName() const
00154 {
00155   return "FilterBar";
00156 }
00157 
00158 void FilterBar::AddProperties(GVariantBuilder* builder)
00159 {
00160   variant::BuilderWrapper(builder)
00161     .add("x", GetAbsoluteX())
00162     .add("y", GetAbsoluteY())
00163     .add("width", GetAbsoluteWidth())
00164     .add("height", GetAbsoluteHeight());
00165 }
00166 
00167 } // namespace dash
00168 } // namespace unity