Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
scalar.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Carl Hetherington <inkscape@carlh.net>
5 * Derek P. Moore <derekm@hackunix.org>
6 * Bryce Harrington <bryce@bryceharrington.org>
7 * Johan Engelen <j.b.c.engelen@alumnus.utwente.nl>
8 *
9 * Copyright (C) 2004-2011 authors
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include "scalar.h"
15
16#include <cmath>
17#include <gtkmm/label.h>
18#include <gtkmm/scale.h>
19
20#include "spinbutton.h"
21#include "ui/pack.h"
22
23namespace Inkscape::UI::Widget {
24
25Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
26 Glib::ustring const &icon,
27 bool mnemonic)
28 : Scalar{label, tooltip, {}, 0u, icon, mnemonic}
29{
30}
31
32Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
33 unsigned digits,
34 Glib::ustring const &icon,
35 bool mnemonic)
36 : Scalar{label, tooltip, {}, digits, icon, mnemonic}
37{
38}
39
40Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
41 Glib::RefPtr<Gtk::Adjustment> const &adjust,
42 unsigned digits,
43 Glib::ustring const &icon,
44 bool mnemonic)
45 : Labelled(label, tooltip, new SpinButton(adjust, 0.0, digits), icon, mnemonic),
46 setProgrammatically(false)
47{
48}
49
50unsigned Scalar::getDigits() const
51{
52 return getSpinButton().get_digits();
53}
54
55double Scalar::getStep() const
56{
57 double step, page;
58 getSpinButton().get_increments(step, page);
59 return step;
60}
61
62double Scalar::getPage() const
63{
64 double step, page;
65 getSpinButton().get_increments(step, page);
66 return page;
67}
68
69double Scalar::getRangeMin() const
70{
71 double min, max;
72 getSpinButton().get_range(min, max);
73 return min;
74}
75
76double Scalar::getRangeMax() const
77{
78 double min, max;
79 getSpinButton().get_range(min, max);
80 return max;
81}
82
83double Scalar::getValue() const
84{
85 return getSpinButton().get_value();
86}
87
89{
90 return getSpinButton().get_value_as_int();
91}
92
93
94void Scalar::setDigits(unsigned digits)
95{
96 return getSpinButton().set_digits(digits);
97}
98
100{
101 if (getDigits()) {
102 auto &spin_button = getSpinButton();
103 spin_button.set_numeric(false);
104 spin_button.signal_output().connect(sigc::mem_fun(*this, &Scalar::setNoLeadingZerosOutput), false);
105 }
106}
107
108bool
110{
111 auto &spin_button = getSpinButton();
112 double digits = std::pow(10.0, spin_button.get_digits());
113 double val = std::round(spin_button.get_value() * digits) / digits;
114 spin_button.set_text(Glib::ustring::format(val));
115 return true;
116}
117
118void
119Scalar::setWidthChars(gint width_chars) {
120 getSpinButton().property_width_chars() = width_chars;
121}
122
123void Scalar::setIncrements(double step, double /*page*/)
124{
125 getSpinButton().set_increments(step, 0);
126}
127
128void Scalar::setRange(double min, double max)
129{
130 getSpinButton().set_range(min, max);
131}
132
133void Scalar::setValue(double value, bool setProg)
134{
135 if (setProg) {
136 setProgrammatically = true; // callback is supposed to reset back, if it cares
137 }
138 getSpinButton().set_value(value);
139 setProgrammatically = false;
140}
141
142void Scalar::setWidthChars(unsigned chars)
143{
144 getSpinButton().set_width_chars(chars);
145}
146
148{
149 getSpinButton().update();
150}
151
153{
154 auto const scale = Gtk::make_managed<Gtk::Scale>(getSpinButton().get_adjustment());
155 scale->set_draw_value(false);
156 UI::pack_start(*this, *scale);
157}
158
159Glib::SignalProxy<void()> Scalar::signal_value_changed()
160{
161 return getSpinButton().signal_value_changed();
162}
163
165 if (auto const label = getLabel()) {
166 label->set_visible(false);
167 }
168
169 if (auto const widget = getWidget()) {
170 widget->reference();
171 remove(*widget);
172 widget->set_hexpand();
173 UI::pack_end(*this, *widget);
174 widget->unreference();
175 }
176}
177
179{
180 return dynamic_cast<SpinButton const &>(*getWidget());
181}
182
183} // namespace Inkscape::UI::Widget
184
185/*
186 Local Variables:
187 mode:c++
188 c-file-style:"stroustrup"
189 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
190 indent-tabs-mode:nil
191 fill-column:99
192 End:
193*/
194// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
double scale
Definition aa.cpp:228
uint64_t page
Definition canvas.cpp:171
Adds a label with optional icon to another widget.
Definition labelled.h:31
Gtk::Label const * getLabel() const
Definition labelled.h:51
Gtk::Widget const * getWidget() const
Definition labelled.h:48
A labelled text box, with spin buttons and optional icon, for entering arbitrary number values.
Definition scalar.h:34
void setIncrements(double step, double page)
Sets the step and page increments for the spin button.
Definition scalar.cpp:123
unsigned getDigits() const
Fetches the precision of the spin button.
Definition scalar.cpp:50
void setWidthChars(unsigned chars)
Sets the width of the spin button by number of characters.
Definition scalar.cpp:142
int getValueAsInt() const
Get the value spin_button represented as an integer.
Definition scalar.cpp:88
Glib::SignalProxy< void()> signal_value_changed()
Signal raised when the spin button's value changes.
Definition scalar.cpp:159
double getStep() const
Gets the current step increment used by the spin button.
Definition scalar.cpp:55
double getRangeMax() const
Gets the maximum range value allowed for the spin button.
Definition scalar.cpp:76
void setDigits(unsigned digits)
Sets the precision to be displayed by the spin button.
Definition scalar.cpp:94
void addSlider()
Adds a slider (HScale) to the left of the spinbox.
Definition scalar.cpp:152
void setRange(double min, double max)
Sets the minimum and maximum range allowed for the spin button.
Definition scalar.cpp:128
double getRangeMin() const
Gets the minimum range value allowed for the spin button.
Definition scalar.cpp:69
SpinButton const & getSpinButton() const
Definition scalar.cpp:178
void setValue(double value, bool setProg=true)
Sets the value of the spin button.
Definition scalar.cpp:133
bool setProgrammatically
true if the value was set by setValue, not changed by the user; if a callback checks it,...
Definition scalar.h:180
double getValue() const
Get the value in the spin_button.
Definition scalar.cpp:83
double getPage() const
Gets the current page increment used by the spin button.
Definition scalar.cpp:62
void setNoLeadingZeros()
remove leading zeros fron widget.
Definition scalar.cpp:99
Scalar(Glib::ustring const &label, Glib::ustring const &tooltip, Glib::ustring const &icon={}, bool mnemonic=true)
Construct a Scalar Widget.
Definition scalar.cpp:25
void update()
Manually forces an update of the spin button.
Definition scalar.cpp:147
SpinButton widget, that allows entry of simple math expressions (also units, when linked with UnitMen...
Definition spinbutton.h:52
Glib::ustring label
Custom widgets.
Definition desktop.h:126
void pack_end(Gtk::Box &box, Gtk::Widget &child, bool const expand, bool const fill, unsigned const padding)
Adds child to box, packed with reference to the end of box.
Definition pack.cpp:153
void pack_start(Gtk::Box &box, Gtk::Widget &child, bool const expand, bool const fill, unsigned const padding)
Adds child to box, packed with reference to the start of box.
Definition pack.cpp:141
Helpers for using Gtk::Boxes, encapsulating large changes between GTK3 & GTK4.
void remove(std::vector< T > &vec, T const &val)
Definition sanitize.cpp:94