scantools  1.0.4
Graphics manipulation with a view towards scanned documents
length.h
1 /*
2  * Copyright © 2017 - 2019 Stefan Kebekus <stefan.kebekus@math.uni-freiburg.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation, either version 3 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef LENGTH
20 #define LENGTH
21 
22 #include <QMetaType>
23 #include <QString>
24 #include <QtGlobal>
25 
26 
37 class length
38 {
39 public:
41  enum unit {
42  cm,
43  in,
44  mm,
45  pt
46  };
47 
49  length() : _length(0) {
50 
51  }
52 
59  length(qreal l, unit u) {
60  set(l, u);
61  }
62 
72  explicit length(QString lstring, bool *ok=0) {
73  bool myOk = set(lstring);
74  if (ok)
75  *ok = myOk;
76  }
77 
84  qreal get(unit u) const;
85 
92  void set(qreal l, unit u);
93 
101  bool set(QString lstring);
102 
106  bool isNonPositive() const {
107  return (_length <= 0);
108  }
109 
114  operator QString() const {
115  return QString("%1mm").arg(get(mm));
116  }
117 
124  bool operator==(const length other) const
125  {
126  return _length == other._length;
127  }
128 
135  const length operator+(const length rhs) const
136  {
137  length result;
138  result._length = _length + rhs._length;
139  return result;
140  }
141 
148  const length operator-(const length rhs) const
149  {
150  length result;
151  result._length = _length - rhs._length;
152  return result;
153  }
154 
161  const length operator/(const qreal div) const
162  {
163  length result;
164  result._length = _length/div;
165  return result;
166  }
167 
168 private:
169  // Length in 1/360 mm
170  qreal _length;
171 };
172 
173 // Makes "length" known to QMetaType
174 Q_DECLARE_METATYPE(length)
175 
176 #endif
The length stores a length and converts between units.
Definition: length.h:38
length()
Constructs a zero length.
Definition: length.h:49
const length operator-(const length rhs) const
Difference of two lengths.
Definition: length.h:148
void set(qreal l, unit u)
Sets length.
unit
List of supported units.
Definition: length.h:41
@ in
Inch.
Definition: length.h:43
@ cm
Centimeter.
Definition: length.h:42
@ mm
Millimeter.
Definition: length.h:44
@ pt
Points (=1/72 inch)
Definition: length.h:45
qreal get(unit u) const
Returns length in given unit.
bool set(QString lstring)
Constructs length from a string.
const length operator+(const length rhs) const
Sum of the lengths.
Definition: length.h:135
bool isNonPositive() const
Check if length is positive.
Definition: length.h:106
const length operator/(const qreal div) const
Divide a length by a number.
Definition: length.h:161
bool operator==(const length other) const
Checks for equality.
Definition: length.h:124
length(qreal l, unit u)
Constructs length of given value and unit.
Definition: length.h:59
length(QString lstring, bool *ok=0)
Constructs length from a string.
Definition: length.h:72