-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCairoGauge.hh
74 lines (62 loc) · 2.41 KB
/
CairoGauge.hh
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
/* CairoGauge.hh - Gtk::DrawingArea-based analog gauge widget
*
* Copyright (C) Javier Lopez-Gomez, 2016
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#ifndef CAIROGAUGE_HH
#define CAIROGAUGE_HH
#include <cairomm/context.h>
#include <cmath>
#include <functional>
#include <gtkmm.h>
#include <string>
class CairoGauge : public Gtk::DrawingArea {
protected:
// TODO: make this a configurable parameter
/// The default font size for this widget
static constexpr unsigned CAIROGAUGE_FONT_SIZE = 14;
typedef std::function<double(void *)> sample_fn_t;
std::string _text;
sample_fn_t _sample_fn;
double _value, _value_max, _tick_step;
size_t _label_step;
Cairo::Matrix _transform_matrix;
Cairo::RefPtr<Cairo::Surface> _background;
void draw_background(void);
bool on_draw(const Cairo::RefPtr<Cairo::Context> &context) override;
void on_size_allocate(Gtk::Allocation &allocation);
public:
/** Construct a CairoGauge object
* @param text Text rendered below the gauge
* @param fn std::function<double(void *)> that returns the next value
* @param _M Maximum value of any sample
* @param step Draw ticks using @a step increments
* @param l_step Draw labels each @a l_step ticks
*/
CairoGauge(std::string text, sample_fn_t fn, double _M, double step = 0, size_t l_step = 1)
: _text(text), _sample_fn(fn), _value(0), _value_max(_M), _tick_step(step), _label_step(l_step),
_transform_matrix(Cairo::identity_matrix()) {}
CairoGauge(const CairoGauge &_o)
: CairoGauge(_o._text, _o._sample_fn, _o._value_max, _o._tick_step, _o._label_step) {}
virtual ~CairoGauge() {}
void set_transform_matrix(Cairo::Matrix &_m) {
_transform_matrix = _m;
queue_draw();
}
/** Call the @a fn function (constructor argument) and update gauge with
* the returned value.
*/
void update(void *arg);
protected:
double angle_of(double value) { return 5 * M_PI_4 - (value / _value_max * 3 * M_PI_2); }
};
#endif /* CAIROGAUGE_HH */