~paullo612/unity/unityshell-rotated

« back to all changes in this revision

Viewing changes to plugins/unityshell-rotated/src/FilterMultiRangeWidget.cpp

  • Committer: Pavel Golikov
  • Date: 2011-11-23 12:54:52 UTC
  • Revision ID: paullo612@gmail.com-20111123125452-y9d0k2shq5qfr8oh
Changed unityshell-rotated directory name to unityshell to better merging

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
2
 
/*
3
 
 * Copyright 2011 Canonical Ltd.
4
 
 *
5
 
 * This program is free software: you can redistribute it and/or modify it
6
 
 * under the terms of the GNU Lesser General Public License version 3, as
7
 
 * published by the  Free Software Foundation.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful, but
10
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
11
 
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
 
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
13
 
 * License for more details.
14
 
 *
15
 
 * You should have received a copy of both the GNU Lesser General Public
16
 
 * License version 3 along with this program.  If not, see
17
 
 * <http://www.gnu.org/licenses/>
18
 
 *
19
 
 * Authored by: Gordon Allott <gord.allott@canonical.com>
20
 
 *
21
 
 */
22
 
 
23
 
#include "config.h"
24
 
 
25
 
#include <Nux/Nux.h>
26
 
 
27
 
#include "FilterMultiRangeWidget.h"
28
 
#include "FilterMultiRangeButton.h"
29
 
#include "FilterBasicButton.h"
30
 
 
31
 
#include <glib.h>
32
 
#include <glib/gi18n-lib.h>
33
 
 
34
 
namespace unity {
35
 
 
36
 
NUX_IMPLEMENT_OBJECT_TYPE(FilterMultiRange);
37
 
 
38
 
  FilterMultiRange::FilterMultiRange (NUX_FILE_LINE_DECL)
39
 
      : FilterExpanderLabel (_("Multi-range"), NUX_FILE_LINE_PARAM)
40
 
      , all_selected (false) {
41
 
    InitTheme();
42
 
 
43
 
    all_button_ = new FilterBasicButton(_("All"), NUX_TRACKER_LOCATION);
44
 
    all_button_->activated.connect(sigc::mem_fun(this, &FilterMultiRange::OnAllActivated));
45
 
    all_button_->label = _("All");
46
 
 
47
 
    layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
48
 
    layout_->SetVerticalExternalMargin (12);
49
 
 
50
 
    SetRightHandView(all_button_);
51
 
    SetContents(layout_);
52
 
    OnActiveChanged(false);
53
 
  }
54
 
 
55
 
  FilterMultiRange::~FilterMultiRange()
56
 
  {
57
 
  }
58
 
 
59
 
  void FilterMultiRange::SetFilter(dash::Filter::Ptr filter)
60
 
  {
61
 
    filter_ = std::static_pointer_cast<dash::MultiRangeFilter>(filter);
62
 
 
63
 
    filter_->option_added.connect (sigc::mem_fun (this, &FilterMultiRange::OnOptionAdded));
64
 
    filter_->option_removed.connect(sigc::mem_fun (this, &FilterMultiRange::OnOptionRemoved));
65
 
 
66
 
    // finally - make sure we are up-todate with our filter list
67
 
    dash::MultiRangeFilter::Options::iterator it;
68
 
    dash::MultiRangeFilter::Options options = filter_->options;
69
 
    for (it = options.begin(); it < options.end(); it++)
70
 
      OnOptionAdded(*it);
71
 
 
72
 
    SetLabel(filter_->name);
73
 
  }
74
 
 
75
 
  void FilterMultiRange::OnActiveChanged(bool value)
76
 
  {
77
 
    // go through all the buttons, and set the state :(
78
 
 
79
 
    std::vector<FilterMultiRangeButton*>::iterator it;
80
 
    int start = 2000;
81
 
    int end = 0;
82
 
    int index = 0;
83
 
    for ( it=buttons_.begin() ; it < buttons_.end(); it++ )
84
 
    {
85
 
      FilterMultiRangeButton* button = (*it);
86
 
      dash::FilterOption::Ptr filter = button->GetFilter();
87
 
      bool tmp_active = filter->active;
88
 
      button->active = tmp_active;
89
 
      if (filter != NULL)
90
 
      {
91
 
        if (filter->active)
92
 
        {
93
 
          if (index < start)
94
 
            start = index;
95
 
          if (index > end)
96
 
            end = index;
97
 
        }
98
 
      }
99
 
      index++;
100
 
    }
101
 
 
102
 
    index = 0;
103
 
    for ( it=buttons_.begin() ; it < buttons_.end(); it++ )
104
 
    {
105
 
      FilterMultiRangeButton* button = (*it);
106
 
 
107
 
      if (index == start && index == end)
108
 
        button->SetHasArrow(MultiRangeArrow::MULTI_RANGE_ARROW_BOTH);
109
 
      else if (index == start)
110
 
        button->SetHasArrow(MultiRangeArrow::MULTI_RANGE_ARROW_LEFT);
111
 
      else if (index == end)
112
 
        button->SetHasArrow(MultiRangeArrow::MULTI_RANGE_ARROW_RIGHT);
113
 
      else
114
 
        button->SetHasArrow(MultiRangeArrow::MULTI_RANGE_ARROW_NONE);
115
 
 
116
 
      if (index == 0)
117
 
        button->SetVisualSide(MULTI_RANGE_SIDE_LEFT);
118
 
      else if (index == (int)buttons_.size() - 1)
119
 
        button->SetVisualSide(MULTI_RANGE_SIDE_RIGHT);
120
 
      else
121
 
        button->SetVisualSide(MULTI_RANGE_CENTER);
122
 
 
123
 
      index++;
124
 
    }
125
 
  }
126
 
 
127
 
  void FilterMultiRange::OnOptionAdded(dash::FilterOption::Ptr new_filter)
128
 
  {
129
 
    FilterMultiRangeButton* button = new FilterMultiRangeButton (NUX_TRACKER_LOCATION);
130
 
    button->SetFilter (new_filter);
131
 
    layout_->AddView (button, 1);
132
 
    buttons_.push_back (button);
133
 
    new_filter->active.changed.connect(sigc::mem_fun (this, &FilterMultiRange::OnActiveChanged));
134
 
    OnActiveChanged(false);
135
 
 
136
 
  }
137
 
 
138
 
  void FilterMultiRange::OnOptionRemoved(dash::FilterOption::Ptr removed_filter)
139
 
  {
140
 
    std::vector<FilterMultiRangeButton*>::iterator it;
141
 
    FilterMultiRangeButton* found_filter = NULL;
142
 
    for ( it=buttons_.begin() ; it < buttons_.end(); it++ )
143
 
    {
144
 
      if ((*it)->GetFilter() == removed_filter)
145
 
      {
146
 
        found_filter = *it;
147
 
        break;
148
 
      }
149
 
    }
150
 
 
151
 
    if (found_filter)
152
 
    {
153
 
      layout_->RemoveChildObject(*it);
154
 
      buttons_.erase(it);
155
 
    }
156
 
 
157
 
    OnActiveChanged(false);
158
 
  }
159
 
 
160
 
  std::string FilterMultiRange::GetFilterType ()
161
 
  {
162
 
    return "FilterMultiRange";
163
 
  }
164
 
 
165
 
  void FilterMultiRange::InitTheme()
166
 
  {
167
 
    //FIXME - build theme here - store images, cache them, fun fun fun
168
 
  }
169
 
 
170
 
  void FilterMultiRange::OnAllActivated(nux::View *view)
171
 
  {
172
 
    if (filter_)
173
 
      filter_->Clear();
174
 
  }
175
 
 
176
 
  long int FilterMultiRange::ProcessEvent(nux::IEvent& ievent, long int TraverseInfo, long int ProcessEventInfo) {
177
 
    return GetLayout()->ProcessEvent(ievent, TraverseInfo, ProcessEventInfo);
178
 
  }
179
 
 
180
 
  void FilterMultiRange::Draw(nux::GraphicsEngine& GfxContext, bool force_draw) {
181
 
    nux::Geometry geo = GetGeometry();
182
 
    nux::Color col(0.2f, 0.2f, 0.2f, 0.2f);
183
 
 
184
 
    GfxContext.PushClippingRectangle(geo);
185
 
    nux::GetPainter().PaintBackground(GfxContext, geo);
186
 
 
187
 
    nux::GetPainter().Draw2DLine(GfxContext,
188
 
                                 geo.x, geo.y + geo.height - 1,
189
 
                                 geo.x + geo.width, geo.y + geo.height - 1,
190
 
                                 col,
191
 
                                 col);
192
 
 
193
 
    GfxContext.PopClippingRectangle();
194
 
  }
195
 
 
196
 
  void FilterMultiRange::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw) {
197
 
    GfxContext.PushClippingRectangle(GetGeometry());
198
 
 
199
 
    GetLayout()->ProcessDraw(GfxContext, force_draw);
200
 
 
201
 
    GfxContext.PopClippingRectangle();
202
 
  }
203
 
 
204
 
  void FilterMultiRange::PostDraw(nux::GraphicsEngine& GfxContext, bool force_draw) {
205
 
    nux::View::PostDraw(GfxContext, force_draw);
206
 
  }
207
 
 
208
 
};