~azzar1/unity/fix-1028810

« back to all changes in this revision

Viewing changes to dash/previews/PreviewInfoHintWidget.cpp

  • Committer: Andrea Azzarone
  • Date: 2012-08-22 13:14:18 UTC
  • mfrom: (2516.1.92 unity)
  • Revision ID: azzaronea@gmail.com-20120822131418-mrfwx82k39xnvl9e
Merge trunk.

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 2012 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: Nick Dedekind <nick.dedekind@canonical.com>
 
20
 *
 
21
 */
 
22
 
 
23
#include "PreviewInfoHintWidget.h"
 
24
#include "unity-shared/IntrospectableWrappers.h"
 
25
#include <NuxCore/Logger.h>
 
26
#include <Nux/HLayout.h>
 
27
#include <Nux/VLayout.h>
 
28
#include <unity-shared/StaticCairoText.h>
 
29
#include <unity-shared/IconTexture.h>
 
30
#include <unity-shared/PreviewStyle.h>
 
31
 
 
32
namespace unity
 
33
{
 
34
namespace dash
 
35
{
 
36
namespace previews
 
37
{
 
38
namespace
 
39
{
 
40
nux::logging::Logger logger("unity.dash.previews.previewinfohintwidget");
 
41
 
 
42
const int layout_spacing = 12;
 
43
}
 
44
 
 
45
NUX_IMPLEMENT_OBJECT_TYPE(PreviewInfoHintWidget);
 
46
 
 
47
PreviewInfoHintWidget::PreviewInfoHintWidget(dash::Preview::Ptr preview_model, int icon_size)
 
48
: View(NUX_TRACKER_LOCATION)
 
49
, icon_size_(icon_size)
 
50
, preview_model_(preview_model)
 
51
{
 
52
  SetupViews();
 
53
}
 
54
 
 
55
PreviewInfoHintWidget::~PreviewInfoHintWidget()
 
56
{
 
57
}
 
58
 
 
59
void PreviewInfoHintWidget::Draw(nux::GraphicsEngine& gfx_engine, bool force_draw)
 
60
{
 
61
}
 
62
 
 
63
void PreviewInfoHintWidget::DrawContent(nux::GraphicsEngine& gfx_engine, bool force_draw)
 
64
{
 
65
  nux::Geometry base = GetGeometry();
 
66
  gfx_engine.PushClippingRectangle(base);
 
67
 
 
68
  if (GetCompositionLayout())
 
69
  {
 
70
    unsigned int alpha, src, dest = 0;
 
71
    gfx_engine.GetRenderStates().GetBlend(alpha, src, dest);
 
72
    gfx_engine.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
73
 
 
74
    GetCompositionLayout()->ProcessDraw(gfx_engine, force_draw);
 
75
 
 
76
    gfx_engine.GetRenderStates().SetBlend(alpha, src, dest);
 
77
  }
 
78
 
 
79
  gfx_engine.PopClippingRectangle();
 
80
}
 
81
 
 
82
std::string PreviewInfoHintWidget::GetName() const
 
83
{
 
84
  return "PreviewInfoHintWidget";
 
85
}
 
86
 
 
87
void PreviewInfoHintWidget::AddProperties(GVariantBuilder* builder)
 
88
{
 
89
  variant::BuilderWrapper(builder)
 
90
    .add(GetAbsoluteGeometry());
 
91
}
 
92
 
 
93
std::string StringFromVariant(GVariant* variant)
 
94
{
 
95
    std::stringstream ss;
 
96
    const GVariantType* info_hint_type = g_variant_get_type(variant);
 
97
    
 
98
    if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_BOOLEAN))
 
99
    {
 
100
      ss << g_variant_get_int16(variant);
 
101
    }
 
102
    else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_INT16))
 
103
    {
 
104
      ss << g_variant_get_int16(variant);
 
105
    }
 
106
    else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_UINT16))
 
107
    {
 
108
      ss << g_variant_get_uint16(variant);
 
109
    }
 
110
    else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_INT32))
 
111
    {
 
112
      ss << g_variant_get_int32(variant);
 
113
    }
 
114
    else if (g_variant_type_equal(info_hint_type,  G_VARIANT_TYPE_UINT32))
 
115
    {
 
116
      ss << g_variant_get_uint32(variant);
 
117
    }
 
118
    else if (g_variant_type_equal(info_hint_type,  G_VARIANT_TYPE_INT64))
 
119
    {
 
120
      ss << g_variant_get_int64(variant);
 
121
    }
 
122
    else if (g_variant_type_equal(info_hint_type,   G_VARIANT_TYPE_UINT64))
 
123
    {
 
124
      ss << g_variant_get_uint64(variant);
 
125
    }
 
126
    else if (g_variant_type_equal(info_hint_type,   G_VARIANT_TYPE_DOUBLE))
 
127
    {
 
128
      ss << g_variant_get_double(variant);
 
129
    }
 
130
    else if (g_variant_type_equal(info_hint_type,   G_VARIANT_TYPE_STRING))
 
131
    {
 
132
      std::string str = g_variant_get_string(variant, NULL);
 
133
      ss << str;
 
134
    }
 
135
    else
 
136
    {
 
137
      ss << "unknown value";
 
138
    }
 
139
    return ss.str();
 
140
}
 
141
 
 
142
void PreviewInfoHintWidget::SetupViews()
 
143
{
 
144
  RemoveLayout();
 
145
  info_hints_.clear();
 
146
 
 
147
  previews::Style& style = previews::Style::Instance();
 
148
 
 
149
  nux::VLayout* layout = new nux::VLayout();
 
150
  layout->SetSpaceBetweenChildren(6);
 
151
 
 
152
  for (dash::Preview::InfoHintPtr info_hint : preview_model_->GetInfoHints())
 
153
  {
 
154
    nux::HLayout* hint_layout = new nux::HLayout();
 
155
    hint_layout->SetSpaceBetweenChildren(layout_spacing);
 
156
 
 
157
    StaticCairoTextPtr info_name;
 
158
    if (!info_hint->display_name.empty())
 
159
    {
 
160
      std::string tmp_display_name = info_hint->display_name;
 
161
      tmp_display_name += ":";
 
162
 
 
163
      info_name = new nux::StaticCairoText(tmp_display_name, NUX_TRACKER_LOCATION);
 
164
      info_name->SetFont(style.info_hint_bold_font());
 
165
      info_name->SetLines(-1);
 
166
      info_name->SetTextAlignment(nux::StaticCairoText::NUX_ALIGN_RIGHT);
 
167
      hint_layout->AddView(info_name.GetPointer(), 0, nux::MINOR_POSITION_CENTER);
 
168
    }
 
169
 
 
170
    StaticCairoTextPtr info_value(new nux::StaticCairoText(StringFromVariant(info_hint->value), NUX_TRACKER_LOCATION));
 
171
    info_value->SetFont(style.info_hint_font());
 
172
    info_value->SetLines(-1);
 
173
    hint_layout->AddView(info_value.GetPointer(), 1, nux::MINOR_POSITION_CENTER);
 
174
 
 
175
    InfoHint info_hint_views(info_name, info_value);
 
176
    info_hints_.push_back(info_hint_views);
 
177
 
 
178
    layout->AddLayout(hint_layout, 0);
 
179
  }
 
180
 
 
181
  SetLayout(layout);
 
182
}
 
183
 
 
184
 
 
185
void PreviewInfoHintWidget::PreLayoutManagement()
 
186
{
 
187
  previews::Style& style = previews::Style::Instance();
 
188
  nux::Geometry const& geo = GetGeometry();
 
189
  
 
190
  int info_hint_width = 0;
 
191
  for (InfoHint const& info_hint : info_hints_)
 
192
  {
 
193
    int width = style.GetInfoHintNameMinimumWidth();
 
194
    if (info_hint.first)
 
195
    {
 
196
      width = info_hint.first->GetTextExtents().width;
 
197
 
 
198
      if (width < style.GetInfoHintNameMinimumWidth())
 
199
        width = style.GetInfoHintNameMinimumWidth();
 
200
      else if (width > style.GetInfoHintNameMaximumWidth())
 
201
        width = style.GetInfoHintNameMaximumWidth();
 
202
    }
 
203
 
 
204
    if (info_hint_width < width)
 
205
    {
 
206
      info_hint_width = width;
 
207
    }
 
208
  }
 
209
 
 
210
  int info_value_width = geo.width;
 
211
  info_value_width -= layout_spacing;
 
212
  info_value_width -= info_hint_width;
 
213
 
 
214
  for (InfoHint const& info_hint : info_hints_)
 
215
  {
 
216
    if (info_hint.first)
 
217
    {
 
218
      info_hint.first->SetMinimumWidth(info_hint_width);
 
219
      info_hint.first->SetMaximumWidth(info_hint_width);
 
220
    }
 
221
    if (info_hint.second)
 
222
    {
 
223
      info_hint.second->SetMinimumWidth(info_value_width);
 
224
      info_hint.second->SetMaximumWidth(info_value_width);
 
225
    }
 
226
  }
 
227
 
 
228
  View::PreLayoutManagement();
 
229
}
 
230
 
 
231
 
 
232
} // namespace previews
 
233
} // namespace dash
 
234
} // namespace unity