Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
unit-menu.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author:
4 * Bryce Harrington <bryce@bryceharrington.org>
5 *
6 * Copyright (C) 2004 Bryce Harrington
7 *
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#include <cmath>
12
13#include "unit-menu.h"
14
15namespace Inkscape::UI::Widget {
16
17UnitMenu::UnitMenu() : _type(UNIT_TYPE_NONE)
18{
19 set_selected(0);
20 property_selected().signal_changed().connect([this](){ on_changed(); });
21}
22
23UnitMenu::UnitMenu(BaseObjectType * const cobject, Glib::RefPtr<Gtk::Builder> const &builder)
24 : DropDownList{cobject, builder}
25{
26 // We assume the UI file sets the active item & thus we do not do that here.
27
28 property_selected().signal_changed().connect([this](){ on_changed(); });
29}
30
31UnitMenu::~UnitMenu() = default;
32
33bool UnitMenu::setUnitType(UnitType unit_type, bool svg_length)
34{
35 // Expand the unit widget with unit entries from the unit table
36 auto const &unit_table = Util::UnitTable::get();
37 auto const &m = unit_table.units(unit_type);
38
39 for (auto & i : m) {
40 // We block the use of non SVG units if requested
41 if (!svg_length || i.second.svgUnit() > 0) {
42 append(i.first);
43 }
44 }
45 _type = unit_type;
46 setUnit(unit_table.primary(unit_type));
47
48 return true;
49}
50
51bool UnitMenu::resetUnitType(UnitType unit_type, bool svg_length)
52{
53 remove_all();
54
55 return setUnitType(unit_type, svg_length);
56}
57
58void UnitMenu::addUnit(Unit const& u)
59{
60 Util::UnitTable::get().addUnit(u, false);
61 append(u.abbr);
62}
63
64Unit const * UnitMenu::getUnit() const
65{
66 auto const &unit_table = Util::UnitTable::get();
68 if (current.empty()) {
69 g_assert(_type != UNIT_TYPE_NONE);
70 return unit_table.getUnit(unit_table.primary(_type));
71 }
72 return unit_table.getUnit(current);
73}
74
75bool UnitMenu::setUnit(Glib::ustring const & unit)
76{
77 // TODO: Determine if 'unit' is available in the dropdown.
78 // If not, return false
79
80 // search for "unit" string
81 // Note: find() method is not yet available (https://docs.gtk.org/gtk4/method.StringList.find.html), so use the loop
82 // TODO: replace with "find" when it becomes practical to do so
83 for (int i = 0; i < get_item_count(); ++i) {
84 if (get_string(i) == unit) {
85 set_selected(i);
86 break;
87 }
88 }
89 return true;
90}
91
92Glib::ustring UnitMenu::getUnitAbbr() const
93{
94 if (get_selected_string().empty()) {
95 return "";
96 }
97 return getUnit()->abbr;
98}
99
101{
102 return getUnit()->type;
103}
104
106{
107 return getUnit()->factor;
108}
109
111{
112 return getUnit()->defaultDigits();
113}
114
116{
117 int factor_digits = -1 * static_cast<int>(std::log10(getUnit()->factor));
118 return std::pow(10.0, factor_digits);
119}
120
122{
123 return 10 * getDefaultStep();
124}
125
126double UnitMenu::getConversion(Glib::ustring const &new_unit_abbr, Glib::ustring const &old_unit_abbr) const
127{
128 auto const &unit_table = Util::UnitTable::get();
129
130 double old_factor = getUnit()->factor;
131 if (old_unit_abbr != "no_unit") {
132 old_factor = unit_table.getUnit(old_unit_abbr)->factor;
133 }
134 Unit const * new_unit = unit_table.getUnit(new_unit_abbr);
135
136 // Catch the case of zero or negative unit factors (error!)
137 if (old_factor < 0.0000001 ||
138 new_unit->factor < 0.0000001) {
139 // TODO: Should we assert here?
140 return 0.00;
141 }
142
143 return old_factor / new_unit->factor;
144}
145
147{
148 return getUnitType() != UNIT_TYPE_DIMENSIONLESS;
149}
150
152{
153 return getUnitType() == UNIT_TYPE_RADIAL;
154}
155
156Glib::SignalProxyProperty UnitMenu::signal_changed() {
157 return property_selected().signal_changed();
158}
159
161 // no op
162}
163
164Glib::ustring UnitMenu::get_selected_string() const {
165 auto n = get_selected();
166 return get_string(n);
167}
168
169} // namespace Inkscape::UI::Widget
170
171/*
172 Local Variables:
173 mode:c++
174 c-file-style:"stroustrup"
175 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
176 indent-tabs-mode:nil
177 fill-column:99
178 End:
179*/
180// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
unsigned int append(const Glib::ustring &item)
unsigned int get_item_count() const
Glib::SignalProxyProperty signal_changed()
Glib::ustring get_string(unsigned int position) const
Unit const * getUnit() const
Returns the Unit object corresponding to the current selection in the dropdown widget.
Definition unit-menu.cpp:64
bool isRadial() const
Returns true if the selected unit is radial (deg or rad).
bool resetUnitType(UnitType unit_type, bool svg_length=false)
Removes all unit entries, then adds the unit type to the widget.
Definition unit-menu.cpp:51
Glib::SignalProxyProperty signal_changed()
Glib::ustring get_selected_string() const
double getDefaultStep() const
Returns the recommended step size in spin buttons displaying units of this type.
double getUnitFactor() const
Returns the unit factor for the selected unit.
void addUnit(Unit const &u)
Adds a unit, possibly user-defined, to the menu.
Definition unit-menu.cpp:58
UnitType getUnitType() const
Returns the UnitType of the selected unit.
int getDefaultDigits() const
Returns the recommended number of digits for displaying numbers of this unit type.
double getConversion(Glib::ustring const &new_unit_abbr, Glib::ustring const &old_unit_abbr="no_unit") const
Returns the conversion factor required to convert values of the currently selected unit into units of...
bool isAbsolute() const
Returns true if the selected unit is not dimensionless (false for %, true for px, pt,...
bool setUnit(Glib::ustring const &unit)
Sets the dropdown widget to the given unit abbreviation.
Definition unit-menu.cpp:75
bool setUnitType(UnitType unit_type, bool svg_length=false)
Adds the unit type to the widget.
Definition unit-menu.cpp:33
UnitMenu()
Construct a UnitMenu.
Definition unit-menu.cpp:17
double getDefaultPage() const
Returns the recommended page size (when hitting pgup/pgdn) in spin buttons displaying units of this t...
Glib::ustring getUnitAbbr() const
Returns the abbreviated unit name of the selected unit.
Definition unit-menu.cpp:92
void addUnit(Unit const &u, bool primary)
Add a new unit to the table.
Definition units.cpp:299
static UnitTable & get()
Definition units.cpp:441
static char const *const current
Definition dir-util.cpp:71
Custom widgets.
Definition desktop.h:126
Glib::RefPtr< Gtk::Builder > builder