1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
3
* Copyright 2012 Canonical Ltd.
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.
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.
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/>
19
* Authored by: Nick Dedekind <nick.dedekind@canonical.com>
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>
40
nux::logging::Logger logger("unity.dash.previews.previewinfohintwidget");
42
const int layout_spacing = 12;
45
NUX_IMPLEMENT_OBJECT_TYPE(PreviewInfoHintWidget);
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)
55
PreviewInfoHintWidget::~PreviewInfoHintWidget()
59
void PreviewInfoHintWidget::Draw(nux::GraphicsEngine& gfx_engine, bool force_draw)
63
void PreviewInfoHintWidget::DrawContent(nux::GraphicsEngine& gfx_engine, bool force_draw)
65
nux::Geometry base = GetGeometry();
66
gfx_engine.PushClippingRectangle(base);
68
if (GetCompositionLayout())
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);
74
GetCompositionLayout()->ProcessDraw(gfx_engine, force_draw);
76
gfx_engine.GetRenderStates().SetBlend(alpha, src, dest);
79
gfx_engine.PopClippingRectangle();
82
std::string PreviewInfoHintWidget::GetName() const
84
return "PreviewInfoHintWidget";
87
void PreviewInfoHintWidget::AddProperties(GVariantBuilder* builder)
89
variant::BuilderWrapper(builder)
90
.add(GetAbsoluteGeometry());
93
std::string StringFromVariant(GVariant* variant)
96
const GVariantType* info_hint_type = g_variant_get_type(variant);
98
if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_BOOLEAN))
100
ss << g_variant_get_int16(variant);
102
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_INT16))
104
ss << g_variant_get_int16(variant);
106
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_UINT16))
108
ss << g_variant_get_uint16(variant);
110
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_INT32))
112
ss << g_variant_get_int32(variant);
114
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_UINT32))
116
ss << g_variant_get_uint32(variant);
118
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_INT64))
120
ss << g_variant_get_int64(variant);
122
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_UINT64))
124
ss << g_variant_get_uint64(variant);
126
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_DOUBLE))
128
ss << g_variant_get_double(variant);
130
else if (g_variant_type_equal(info_hint_type, G_VARIANT_TYPE_STRING))
132
std::string str = g_variant_get_string(variant, NULL);
137
ss << "unknown value";
142
void PreviewInfoHintWidget::SetupViews()
147
previews::Style& style = previews::Style::Instance();
149
nux::VLayout* layout = new nux::VLayout();
150
layout->SetSpaceBetweenChildren(6);
152
for (dash::Preview::InfoHintPtr info_hint : preview_model_->GetInfoHints())
154
nux::HLayout* hint_layout = new nux::HLayout();
155
hint_layout->SetSpaceBetweenChildren(layout_spacing);
157
StaticCairoTextPtr info_name;
158
if (!info_hint->display_name.empty())
160
std::string tmp_display_name = info_hint->display_name;
161
tmp_display_name += ":";
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);
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);
175
InfoHint info_hint_views(info_name, info_value);
176
info_hints_.push_back(info_hint_views);
178
layout->AddLayout(hint_layout, 0);
185
void PreviewInfoHintWidget::PreLayoutManagement()
187
previews::Style& style = previews::Style::Instance();
188
nux::Geometry const& geo = GetGeometry();
190
int info_hint_width = 0;
191
for (InfoHint const& info_hint : info_hints_)
193
int width = style.GetInfoHintNameMinimumWidth();
196
width = info_hint.first->GetTextExtents().width;
198
if (width < style.GetInfoHintNameMinimumWidth())
199
width = style.GetInfoHintNameMinimumWidth();
200
else if (width > style.GetInfoHintNameMaximumWidth())
201
width = style.GetInfoHintNameMaximumWidth();
204
if (info_hint_width < width)
206
info_hint_width = width;
210
int info_value_width = geo.width;
211
info_value_width -= layout_spacing;
212
info_value_width -= info_hint_width;
214
for (InfoHint const& info_hint : info_hints_)
218
info_hint.first->SetMinimumWidth(info_hint_width);
219
info_hint.first->SetMaximumWidth(info_hint_width);
221
if (info_hint.second)
223
info_hint.second->SetMinimumWidth(info_value_width);
224
info_hint.second->SetMaximumWidth(info_value_width);
228
View::PreLayoutManagement();
232
} // namespace previews