summaryrefslogtreecommitdiffstats
path: root/src/util/units.h
blob: 0ea84bfbbe30cad5eaa2291540900dc6b77d9060 (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
/*
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:
    Glib::ustring  name;
    Glib::ustring  name_plural;
    Glib::ustring  abbr;
    Glib::ustring  description;

    UnitType       type;

    double         factor;

    bool           isAbsolute() const { return type != UNIT_TYPE_DIMENSIONLESS; }
    int            defaultDigits() const;
    bool           compatibleWith(const Unit *u) const;
    
    friend bool operator== (const Unit &u1, const Unit &u2);
    friend bool operator!= (const Unit &u1, const Unit &u2);
    
    // temporary
    int svgUnit() const;
};

class UnitTable {
 public:
    UnitTable();
    virtual ~UnitTable();

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

    void    addUnit(Unit const& u, bool primary);
    Unit    getUnit(Glib::ustring const& name) const;
    bool    deleteUnit(Unit const& u);
    bool    hasUnit(Glib::ustring const &name) const;

    UnitTable::UnitMap units(UnitType type) const;

    Glib::ustring primary(UnitType type) const;

    double  getScale() const;
    void    setScale();

    bool    load(Glib::ustring const &filename);
    bool    loadText(Glib::ustring const &filename);
    bool    save(Glib::ustring 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);

};

class Quantity {
public:
    const Unit *unit;
    double quantity;
    
    Quantity(double q, const Unit *u);   // constructor
    bool compatibleWith(const Unit *u) const;
    double value(Unit *u) const;
    
    static double convert(const double from_dist, const Unit *from, const Unit *to);
};

} // namespace Util
} // namespace Inkscape

#endif // define INKSCAPE_UTIL_UNITS_H