summaryrefslogtreecommitdiffstats
path: root/src/util/units.h
blob: 4b2d782e34456e1694d81129c1293eed5a4bc6e8 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
This is a rough draft of a global 'units' thingee, to allow dialogs and
the ruler to share info about unit systems...  Dunno if this is the
right kind of object though, so we may have to redo this or shift things
around later when it becomes clearer what we need.

This object is used for defining different unit systems.

This is intended to eventually replace inkscape/helper/units.*.

Need to review the Units support that's in Gtkmm already...

*/

#ifndef INKSCAPE_UTIL_UNITS_H
#define INKSCAPE_UTIL_UNITS_H

#include <map>
#include <glibmm/ustring.h>

namespace Inkscape {
namespace Util {

enum UnitType {
    UNIT_TYPE_DIMENSIONLESS,     /* Percentage */
    UNIT_TYPE_LINEAR,
    UNIT_TYPE_LINEAR_SCALED,
    UNIT_TYPE_RADIAL,
    UNIT_TYPE_TIME,
    UNIT_TYPE_FONT_HEIGHT,
    UNIT_TYPE_QTY,
    UNIT_TYPE_NONE = -1
};

const char DEG[] = "°";

class Unit {
 public:
    Unit();
    Unit(UnitType type,
         double factor,
         Glib::ustring const &name,
         Glib::ustring const &name_plural,
         Glib::ustring const &abbr,
         Glib::ustring const &description);

    void clear();

    bool           isAbsolute() const { return type != UNIT_TYPE_DIMENSIONLESS; }

    /**
     * Returns the suggested precision to use for displaying numbers
     * of this unit.
     */
    int            defaultDigits() const;

    UnitType       type;
    double         factor;
    Glib::ustring  name;
    Glib::ustring  name_plural;
    Glib::ustring  abbr;
    Glib::ustring  description;
};

class UnitTable {
 public:
    /**
     * Initializes the unit tables and identifies the primary unit types.
     *
     * The primary unit's conversion factor is required to be 1.00
     */
    UnitTable();
    virtual ~UnitTable();

    typedef std::map<Glib::ustring, Unit*> UnitMap;

    /** Add a new unit to the table */
    void    addUnit(Unit const& u, bool primary);

    /** Retrieve a given unit based on its string identifier */
    Unit    getUnit(Glib::ustring const& name) const;

    /** Remove a unit definition from the given unit type table */
    bool    deleteUnit(Unit const& u);

    /** Returns true if the given string 'name' is a valid unit in the table */
    bool    hasUnit(Glib::ustring const &name) const;

    /** Provides an iteratable list of items in the given unit table */
    UnitTable::UnitMap units(UnitType type) const;

    /** Returns the default unit abbr for the given type */
    Glib::ustring primary(UnitType type) const;

    double  getScale() const;

    void    setScale();

    /** Load units from an XML file.
     *
     * Loads and merges the contents of the given file into the UnitTable,
     * possibly overwriting existing unit definitions.
     *
     * @param filename file to be loaded
     */
    bool    load(std::string const &filename);

    /** Saves the current UnitTable to the given file. */
    bool    save(std::string const &filename);

 protected:
    UnitTable::UnitMap  _unit_map;
    Glib::ustring       _primary_unit[UNIT_TYPE_QTY];

    double              _linear_scale;

 private:
    UnitTable(UnitTable const& t);
    UnitTable operator=(UnitTable const& t);

};

} // namespace Util
} // namespace Inkscape

#endif // define INKSCAPE_UTIL_UNITS_H
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :