File: badge_figure.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (129 lines) | stat: -rw-r--r-- 2,982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "badge_figure.h"
#include "figure_common.h"

using namespace base;

BadgeFigure::BadgeFigure(mdc::Layer *layer)
: mdc::Figure(layer)
{
  _font= mdc::FontSpec::from_string("Helvetica Bold 11");
  _xpadding= 8;
  _ypadding= 3;
  _line_width= 2;
  _pen_color= Color(0.9, 0.9, 0.9);
  _text_color= Color(1, 1, 1);
  
  set_cache_toplevel_contents(false);
  
  _gradient= 0;
}


BadgeFigure::~BadgeFigure()
{
  cairo_pattern_destroy(_gradient);
}


void BadgeFigure::set_badge_id(const std::string &bid)
{
  _badge_id= bid;
}


void BadgeFigure::set_text(const std::string &text)
{
  _text= text;
  set_needs_relayout();
}


void BadgeFigure::set_gradient_from_color(const Color &color)
{
  HSVColor hsv(color);
  
  hsv.v/= 1.4;
  
  set_fill_color(Color(hsv));
  set_fill_color2(color);
  set_needs_render();
}


void BadgeFigure::set_fill_color2(const Color &color)
{
  _fill_color2= color;
  if (_gradient)
    cairo_pattern_destroy(_gradient);
  _gradient= 0;
}


void BadgeFigure::set_text_color(const Color &color)
{
  _text_color= color;
}



Size BadgeFigure::calc_min_size()
{
  Size size;
  cairo_text_extents_t extents;

  get_view()->cairoctx()->get_text_extents(_font, _text.c_str(), extents);
  
  size.width= extents.x_advance;
  size.height= extents.height;
  _text_size= size;
//  size.width+= 2*_xpadding;
//  size.height+= 2*_ypadding;
  
  return size;
}


void BadgeFigure::draw_contents(mdc::CairoCtx *cr)
{
  if (!_gradient)
  {
    _gradient= cairo_pattern_create_linear(0.0, 0.0, 0.0, get_size().height);
    cairo_pattern_add_color_stop_rgba(_gradient, 1, _fill_color.red, _fill_color.green, _fill_color.blue, _fill_color.alpha);
    cairo_pattern_add_color_stop_rgba(_gradient, 0, _fill_color2.red, _fill_color2.green, _fill_color2.blue, _fill_color2.alpha);    
  }
  
  cr->save();
  
  stroke_rounded_rectangle(cr, get_bounds(), mdc::CAll, 4.0, 0.0);
  cairo_set_source(cr->get_cr(), _gradient);
  cr->fill_preserve();
  cr->set_line_width(_line_width);
  cr->set_color(_pen_color);
  cr->stroke();

  cr->move_to(get_position().x + _xpadding, get_position().y + (get_size().height + _text_size.height)/2);
  cr->set_color(_text_color);
  cr->show_text(_text);
  
  cr->restore();
}