Inkscape
Vector Graphics Editor
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
18{
19 set_active(0);
20}
21
22UnitMenu::UnitMenu(BaseObjectType * const cobject, Glib::RefPtr<Gtk::Builder> const &builder)
23 : Gtk::ComboBoxText{cobject}
24{
25 // We assume the UI file sets the active item & thus we do not do that here.
26}
27
28UnitMenu::~UnitMenu() = default;
29
31{
32 // Expand the unit widget with unit entries from the unit table
33 auto const &unit_table = Util::UnitTable::get();
34 auto const &m = unit_table.units(unit_type);
35
36 for (auto & i : m) {
37 append(i.first);
38 }
39 _type = unit_type;
40 set_active_text(unit_table.primary(unit_type));
41
42 return true;
43}
44
46{
47 remove_all();
48
49 return setUnitType(unit_type);
50}
51
52void UnitMenu::addUnit(Unit const& u)
53{
54 Util::UnitTable::get().addUnit(u, false);
55 append(u.abbr);
56}
57
58Unit const * UnitMenu::getUnit() const
59{
60 auto const &unit_table = Util::UnitTable::get();
61 if (get_active_text() == "") {
62 g_assert(_type != UNIT_TYPE_NONE);
63 return unit_table.getUnit(unit_table.primary(_type));
64 }
65 return unit_table.getUnit(get_active_text());
66}
67
68bool UnitMenu::setUnit(Glib::ustring const & unit)
69{
70 // TODO: Determine if 'unit' is available in the dropdown.
71 // If not, return false
72
73 set_active_text(unit);
74 return true;
75}
76
77Glib::ustring UnitMenu::getUnitAbbr() const
78{
79 if (get_active_text() == "") {
80 return "";
81 }
82 return getUnit()->abbr;
83}
84
86{
87 return getUnit()->type;
88}
89
91{
92 return getUnit()->factor;
93}
94
96{
97 return getUnit()->defaultDigits();
98}
99
101{
102 int factor_digits = -1 * static_cast<int>(std::log10(getUnit()->factor));
103 return std::pow(10.0, factor_digits);
104}
105
107{
108 return 10 * getDefaultStep();
109}
110
111double UnitMenu::getConversion(Glib::ustring const &new_unit_abbr, Glib::ustring const &old_unit_abbr) const
112{
113 auto const &unit_table = Util::UnitTable::get();
114
115 double old_factor = getUnit()->factor;
116 if (old_unit_abbr != "no_unit") {
117 old_factor = unit_table.getUnit(old_unit_abbr)->factor;
118 }
119 Unit const * new_unit = unit_table.getUnit(new_unit_abbr);
120
121 // Catch the case of zero or negative unit factors (error!)
122 if (old_factor < 0.0000001 ||
123 new_unit->factor < 0.0000001) {
124 // TODO: Should we assert here?
125 return 0.00;
126 }
127
128 return old_factor / new_unit->factor;
129}
130
132{
134}
135
137{
138 return getUnitType() == UNIT_TYPE_RADIAL;
139}
140
141} // namespace Inkscape::UI::Widget
142
143/*
144 Local Variables:
145 mode:c++
146 c-file-style:"stroustrup"
147 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
148 indent-tabs-mode:nil
149 fill-column:99
150 End:
151*/
152// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Unit const * getUnit() const
Returns the Unit object corresponding to the current selection in the dropdown widget.
Definition: unit-menu.cpp:58
bool isRadial() const
Returns true if the selected unit is radial (deg or rad).
Definition: unit-menu.cpp:136
double getDefaultStep() const
Returns the recommended step size in spin buttons displaying units of this type.
Definition: unit-menu.cpp:100
double getUnitFactor() const
Returns the unit factor for the selected unit.
Definition: unit-menu.cpp:90
void addUnit(Unit const &u)
Adds a unit, possibly user-defined, to the menu.
Definition: unit-menu.cpp:52
UnitType getUnitType() const
Returns the UnitType of the selected unit.
Definition: unit-menu.cpp:85
int getDefaultDigits() const
Returns the recommended number of digits for displaying numbers of this unit type.
Definition: unit-menu.cpp:95
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...
Definition: unit-menu.cpp:111
bool isAbsolute() const
Returns true if the selected unit is not dimensionless (false for %, true for px, pt,...
Definition: unit-menu.cpp:131
bool setUnit(Glib::ustring const &unit)
Sets the dropdown widget to the given unit abbreviation.
Definition: unit-menu.cpp:68
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...
Definition: unit-menu.cpp:106
Glib::ustring getUnitAbbr() const
Returns the abbreviated unit name of the selected unit.
Definition: unit-menu.cpp:77
bool resetUnitType(UnitType unit_type)
Removes all unit entries, then adds the unit type to the widget.
Definition: unit-menu.cpp:45
bool setUnitType(UnitType unit_type)
Adds the unit type to the widget.
Definition: unit-menu.cpp:30
void addUnit(Unit const &u, bool primary)
Add a new unit to the table.
Definition: units.cpp:268
static UnitTable & get()
Definition: units.cpp:410
T pow(T const &t, int n)
Integer exponentiation for transforms.
Definition: transforms.h:98
Definition: desktop.h:51
static const Triplet m[3]
Definition: hsluv.cpp:44
Custom widgets.
Definition: desktop.h:127
@ UNIT_TYPE_DIMENSIONLESS
Definition: units.h:33
@ UNIT_TYPE_RADIAL
Definition: units.h:36
@ UNIT_TYPE_NONE
Definition: units.h:40
static void append(std::vector< T > &target, std::vector< T > &&source)
Definition: shortcuts.cpp:515
Glib::RefPtr< Gtk::Builder > builder