1 /* Dial.hpp
2  * Copyright (C) 2018, 2019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BWIDGETS_DIAL_HPP_
19 #define BWIDGETS_DIAL_HPP_
20 
21 #include "RangeWidget.hpp"
22 #include "Knob.hpp"
23 #include "DrawingSurface.hpp"
24 #include "Label.hpp"
25 #include "Focusable.hpp"
26 
27 #define BWIDGETS_DEFAULT_DIAL_WIDTH 50.0
28 #define BWIDGETS_DEFAULT_DIAL_HEIGHT 50.0
29 #define BWIDGETS_DEFAULT_DIAL_KNOB_SIZE 0.6
30 #define BWIDGETS_DEFAULT_DIAL_DEPTH 1.0
31 #define BWIDGETS_DEFAULT_DIAL_DOT_SIZE 0.1
32 
33 namespace BWidgets
34 {
35 /**
36  * Class BWidgets::Dial
37  *
38  * RangeWidget class dial.
39  * The Widget is clickable by default.
40  */
41 class Dial : public RangeWidget, public Focusable
42 {
43 public:
44 	Dial ();
45 	Dial (const double x, const double y, const double width, const double height, const std::string& name,
46 		const double value, const double min, const double max, const double step);
47 
48 	/**
49 	 * Creates a new (orphan) dial and copies the dial properties from a
50 	 * source dial.
51 	 * @param that Source dial
52 	 */
53 	Dial (const Dial& that);
54 
55 	/**
56 	 * Assignment. Copies the dial properties from a source dial and keeps
57 	 * its name and its position within the widget tree. Emits an expose event
58 	 * if the widget is visible and a value changed event.
59 	 * @param that Source widget
60 	 */
61 	Dial& operator= (const Dial& that);
62 
63 	/**
64 	 * Pattern cloning. Creates a new instance of the widget and copies all
65 	 * its properties.
66 	 */
67 	virtual Widget* clone () const override;
68 
69 	/**
70 	 * Changes the value of the widget and keeps it within the defined range.
71 	 * Passes the value to its predefined child widgets.
72 	 * Emits a value changed event and (if visible) an expose event.
73 	 * @param val Value
74 	 */
75 	virtual void setValue (const double val) override;
76 
77 	/**
78 	 * Calls a redraw of the widget and calls postRedisplay () if the the
79 	 * Widget is visible.
80 	 * This method should be called if the widgets properties are indirectly
81 	 * changed.
82 	 */
83 	virtual void update () override;
84 
85 	/**
86 	 * Scans theme for widget properties and applies these properties.
87 	 * @param theme Theme to be scanned.
88 	 * 				Styles used are:
89 	 * 				BWIDGETS_KEYWORD_BORDER
90 	 * 				BWIDGETS_KEYWORD_BACKGROUND
91 	 * 				BWIDGETS_KEYWORD_FGCOLORS
92 	 * 				BWIDGETS_KEYWORD_BGCOLORS
93 	 * @param name Name of the BStyles::StyleSet within the theme to be
94 	 * 		  	   applied.
95 	 */
96 	virtual void applyTheme (BStyles::Theme& theme) override;
97 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override;
98 
99 	/**
100 	 * Handles the BEvents::BUTTON_PRESS_EVENT to turn the dial.
101 	 * @param event Pointer to a poiter event emitted by the same widget.
102 	 */
103 	virtual void onButtonPressed (BEvents::PointerEvent* event) override;
104 
105 	/**
106 	 * Handles the BEvents::EventType::BUTTON_RELEASE_EVENT to turn the dial.
107 	 * @param event Pointer event
108 	 */
109 	virtual void onButtonReleased (BEvents::PointerEvent* event) override;
110 
111 	/**
112 	 * Handles the BEvents::POINTER_DRAG_EVENT to turn
113 	 * the dial.
114 	 * @param event Pointer to a pointer event emitted by the same widget.
115 	 */
116 	virtual void onPointerDragged (BEvents::PointerEvent* event) override;
117 
118 	/**
119 	 * Handles the BEvents::WHEEL_SCROLL_EVENT to turn
120 	 * the dial.
121 	 * @param event Pointer to a wheel event emitted by the same widget.
122 	 */
123 	virtual void onWheelScrolled (BEvents::WheelEvent* event) override;
124 
125 	/**
126 	 * Predefined empty method to handle a
127 	 * BEvents::EventType::FOCUS_IN_EVENT.
128 	 * @param event Focus event
129 	 */
130 	virtual void onFocusIn (BEvents::FocusEvent* event) override;
131 
132 	/**
133 	 * Predefined empty method to handle a
134 	 * BEvents::EventType::FOCUS_OUT_EVENT.
135 	 * @param event Focus event
136 	 */
137 	virtual void onFocusOut (BEvents::FocusEvent* event) override;
138 
139 
140 protected:
141 	void drawDot ();
142 	virtual void updateCoords ();
143 	virtual void draw (const BUtilities::RectArea& area) override;
144 
145 	BUtilities::Point dialCenter;
146 	double dialRadius;
147 
148 	Knob knob;
149 	DrawingSurface dot;
150 	Label focusLabel;
151 	BColors::ColorSet fgColors;
152 	BColors::ColorSet bgColors;
153 };
154 
155 }
156 
157 
158 #endif /* BWIDGETS_DIAL_HPP_ */
159