SGL
gtypes.h
1 /*
2  * File: gtypes.h
3  * --------------
4  * This file defines classes for representing points, dimensions, and
5  * rectangles.
6  *
7  * @version 2021/04/09
8  * - added sgl namespace
9  * @version 2018/09/09
10  * - added doc comments for new documentation generation
11  * @version 2018/07/14
12  * - initial version, based on gtypes.h
13  */
14 
15 
16 #ifndef _gtypes_h
17 #define _gtypes_h
18 
19 #include <functional>
20 #include <iostream>
21 #include <Qt>
22 #include <string>
23 
24 namespace sgl {
25 
30 typedef std::function<void()> GThunk;
31 
36 typedef std::function<int()> GThunkInt;
37 
38 
43 struct GDimension {
44 public:
49  GDimension(double width, double height);
50 
51  /*
52  * Constructs a default dimension 0, 0.
53  */
54  GDimension();
55 
56 
61  string toString() const;
62 
63  /* width and height - may be directly accessed or modified */
64  double width;
65  double height;
66 
67  /* Private section */
68 
69  /**********************************************************************/
70  /* Note: Everything below this point in this class is logically part */
71  /* of the implementation and should not be of interest to clients. */
72  /**********************************************************************/
73 private:
74  /* Instance variables */
75 
76  /* Friend declarations */
77  friend std::ostream& operator <<(std::ostream& os, const GDimension& dim);
78  friend bool operator ==(const GDimension& d1, const GDimension& d2);
79  friend bool operator !=(const GDimension& d1, const GDimension& d2);
80  friend bool operator <(const GDimension& d1, const GDimension& d2);
81  friend bool operator <=(const GDimension& d1, const GDimension& d2);
82  friend bool operator >(const GDimension& d1, const GDimension& d2);
83  friend bool operator >=(const GDimension& d1, const GDimension& d2);
84  friend GDimension operator *(const GDimension& d, double scale);
85 };
86 
90 std::ostream& operator <<(std::ostream& os, const GDimension& dim);
91 
95 bool operator ==(const GDimension& d1, const GDimension& d2);
96 
100 bool operator !=(const GDimension& d1, const GDimension& d2);
101 
106 bool operator <(const GDimension& d1, const GDimension& d2);
107 
112 bool operator <=(const GDimension& d1, const GDimension& d2);
113 
118 bool operator >(const GDimension& d1, const GDimension& d2);
119 
124 bool operator >=(const GDimension& d1, const GDimension& d2);
125 
130 GDimension operator *(const GDimension& d, double scale);
131 
141 };
142 
152 };
153 
165 };
166 // Note: Must keep in sync with GInteractor::TextPosition.
167 
171 string toString(HorizontalAlignment alignment);
172 
176 string toString(VerticalAlignment alignment);
177 
181 HorizontalAlignment toHorizontalAlignment(const string& alignmentStr);
182 
186 Qt::Alignment toQtAlignment(HorizontalAlignment alignment);
187 
191 Qt::Alignment toQtAlignment(VerticalAlignment alignment);
192 
196 VerticalAlignment toVerticalAlignment(const string& alignmentStr);
197 
202 struct GPoint {
203 public:
208  GPoint(double x, double y);
209 
210  /*
211  * Constructs a default GPoint 0, 0.
212  */
213  GPoint();
214 
219  string toString() const;
220 
221  /* x and y coordinates - may be directly accessed or modified */
222  double x;
223  double y;
224 
225  /* Private section */
226 
227  /**********************************************************************/
228  /* Note: Everything below this point in this class is logically part */
229  /* of the implementation and should not be of interest to clients. */
230  /**********************************************************************/
231 private:
232 
233  /* Friend declarations */
234  friend std::ostream& operator <<(std::ostream& out, const GPoint& p);
235  friend bool operator ==(const GPoint& p1, const GPoint& p2);
236  friend bool operator !=(const GPoint& p1, const GPoint& p2);
237  friend bool operator <(const GPoint& p1, const GPoint& p2);
238  friend bool operator <=(const GPoint& p1, const GPoint& p2);
239  friend bool operator >(const GPoint& p1, const GPoint& p2);
240  friend bool operator >=(const GPoint& p1, const GPoint& p2);
241  friend GPoint operator *(const GPoint& p, double scale);
242 };
243 
247 std::ostream& operator <<(std::ostream& os, const GPoint& pt);
248 
252 bool operator ==(const GPoint& p1, const GPoint& p2);
253 
257 bool operator !=(const GPoint& p1, const GPoint& p2);
258 
263 bool operator <(const GPoint& p1, const GPoint& p2);
264 
269 bool operator <=(const GPoint& p1, const GPoint& p2);
270 
275 bool operator >(const GPoint& p1, const GPoint& p2);
276 
281 bool operator >=(const GPoint& p1, const GPoint& p2);
282 
287 GPoint operator *(const GPoint& p, double scale);
288 
293 struct GRectangle {
294 public:
300  GRectangle(double x = 0, double y = 0, double width = 0, double height = 0);
301 
307  GRectangle(double x, double y, const GDimension& size);
308 
314  GRectangle(const GPoint& p, double width = 0, double height = 0);
315 
321  GRectangle(const GPoint& p, const GDimension& size);
322 
326  bool contains(double x, double y) const;
327 
331  bool contains(const GPoint& pt) const;
332 
337  bool contains(const GRectangle& rect) const;
338 
345  GRectangle enlargedBy(double amount);
346 
350  bool intersects(const GRectangle& other) const;
351 
356  bool isEmpty() const;
357 
363  string toString() const;
364 
365  /* coordinates and dimension - may be directly accessed or modified */
366  double x; /* The x-coordinate of the rectangle */
367  double y; /* The y-coordinate of the rectangle */
368  double width; /* The width of the rectangle */
369  double height; /* The height of the rectangle */
370 
371  /* Private section */
372 
373  /**********************************************************************/
374  /* Note: Everything below this point in this class is logically part */
375  /* of the implementation and should not be of interest to clients. */
376  /**********************************************************************/
377 private:
378  /* Instance variables */
379 
380 
381  /* Friend declarations */
382  friend std::ostream& operator <<(std::ostream& os, const GRectangle& rect);
383  friend bool operator ==(const GRectangle& r1, const GRectangle& r2);
384  friend bool operator !=(const GRectangle& r1, const GRectangle& r2);
385  friend bool operator <(const GRectangle& r1, const GRectangle& r2);
386  friend bool operator <=(const GRectangle& r1, const GRectangle& r2);
387  friend bool operator >(const GRectangle& r1, const GRectangle& r2);
388  friend bool operator >=(const GRectangle& r1, const GRectangle& r2);
389 };
390 
394 std::ostream& operator <<(std::ostream& os, const GRectangle& rect);
395 
399 bool operator ==(const GRectangle& r1, const GRectangle& r2);
400 
404 bool operator !=(const GRectangle& r1, const GRectangle& r2);
405 
409 bool operator <(const GRectangle& r1, const GRectangle& r2);
410 
414 bool operator <=(const GRectangle& r1, const GRectangle& r2);
415 
419 bool operator >(const GRectangle& r1, const GRectangle& r2);
420 
424 bool operator >=(const GRectangle& r1, const GRectangle& r2);
425 
426 } // namespace sgl
427 
428 #endif // _gtypes_h
SwingConstants
Constants for alignments and icon positions.
Definition: gtypes.h:159
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:293
string toString() const
Converts the GDimension to a string in the form "(</code><i>width</i><code>,</code>&nbsp;<i>height</i><cod...
Definition: gtypes.cpp:40
Definition: gtypes.h:137
friend std::ostream & operator<<(std::ostream &os, const GDimension &dim)
Writes the GDimension to the given output stream.
Definition: gtypes.cpp:46
double height
Definition: gtypes.h:65
HorizontalAlignment
The supported kinds of horizontal alignment of a widget or onscreen object.
Definition: gtypes.h:136
bool operator>=(const GTableIndex &loc1, const GTableIndex &loc2)
Definition: gtable.cpp:1089
Definition: gtypes.h:139
bool intersects(const GRectangle &other) const
Returns true if this rectangle and the given other rectangle overlap.
Definition: gtypes.cpp:253
std::function< int()> GThunkInt
An alias for a function wrapper around a function with no parameters and an int return (such as main(...
Definition: gtypes.h:36
friend bool operator>=(const GRectangle &r1, const GRectangle &r2)
Relational operators that compare rectangles by x, y, then width, then height.
Definition: gtypes.cpp:302
friend bool operator<=(const GRectangle &r1, const GRectangle &r2)
Relational operators that compare rectangles by x, y, then width, then height.
Definition: gtypes.cpp:294
Definition: gtypes.h:149
string toString() const
Converts the GRectangle to a string in the form "(</code><i>x</i><code>,</code>&nbsp;<i>y</i><code>,</code> <i>width</i><code>,</code>&nbsp;<i>height</i><code>)".
Definition: gtypes.cpp:265
Definition: gtypes.h:162
GDimension()
Definition: gtypes.cpp:38
Definition: gtypes.h:160
double x
Definition: gtypes.h:222
friend bool operator>(const GPoint &p1, const GPoint &p2)
Relational operators that compare points by x-coordinate and then by y-coordinate.
Definition: gtypes.cpp:182
friend std::ostream & operator<<(std::ostream &out, const GPoint &p)
Writes the given point to the given output stream.
Definition: gtypes.cpp:161
std::ostream & operator<<(std::ostream &out, const GEvent &event)
Writes the given event to the given output stream.
Definition: gevent.cpp:494
Definition: gtypes.h:161
bool operator>(const GTableIndex &loc1, const GTableIndex &loc2)
Definition: gtable.cpp:1085
HorizontalAlignment toHorizontalAlignment(string alignmentStr)
Converts a string such as "Center" or "Left" into an alignment value.
Definition: gtypes.cpp:100
double y
Definition: gtypes.h:223
friend bool operator>=(const GPoint &p1, const GPoint &p2)
Relational operators that compare points by x-coordinate and then by y-coordinate.
Definition: gtypes.cpp:186
friend bool operator<(const GRectangle &r1, const GRectangle &r2)
Relational operators that compare rectangles by x, y, then width, then height.
Definition: gtypes.cpp:287
string toString() const
Converts the GPoint to a string in the form "(</code><i>x</i><code>,</code>&nbsp;<i>y</i><code>)".
Definition: gtypes.cpp:155
string toString(HorizontalAlignment alignment)
Converts an alignment value into a string such as "Center" or "Left".
Definition: gtypes.cpp:80
GDimension operator*(const GDimension &d, double scale)
Multiplies the width and height of the given GDimension object by the given scale factor and returns ...
Definition: gtypes.cpp:76
Definition: console.h:45
friend bool operator<=(const GPoint &p1, const GPoint &p2)
Relational operators that compare points by x-coordinate and then by y-coordinate.
Definition: gtypes.cpp:178
friend bool operator<(const GPoint &p1, const GPoint &p2)
Relational operators that compare points by x-coordinate and then by y-coordinate.
Definition: gtypes.cpp:174
bool operator==(const GTableIndex &loc1, const GTableIndex &loc2)
Definition: gtable.cpp:1077
friend bool operator==(const GPoint &p1, const GPoint &p2)
Compares two GPoint objects for equality.
Definition: gtypes.cpp:165
friend bool operator>=(const GDimension &d1, const GDimension &d2)
Relational operators that compare two GDimension objects by width and then by height.
Definition: gtypes.cpp:72
double width
Definition: gtypes.h:64
friend bool operator>(const GRectangle &r1, const GRectangle &r2)
Relational operators that compare rectangles by x, y, then width, then height.
Definition: gtypes.cpp:298
double height
Definition: gtypes.h:369
friend bool operator==(const GDimension &d1, const GDimension &d2)
Compares two GDimension objects for equality.
Definition: gtypes.cpp:50
friend bool operator!=(const GRectangle &r1, const GRectangle &r2)
Compares two rectangles for inequality.
Definition: gtypes.cpp:283
Definition: gtypes.h:164
bool operator<=(const GTableIndex &loc1, const GTableIndex &loc2)
Definition: gtable.cpp:1073
Qt::Alignment toQtAlignment(HorizontalAlignment alignment)
Converts our alignment values into Qt alignment constants.
Definition: gtypes.cpp:113
friend bool operator!=(const GPoint &p1, const GPoint &p2)
Compares two GPoint objects for inequality.
Definition: gtypes.cpp:170
VerticalAlignment toVerticalAlignment(string alignmentStr)
Converts a string such as "Middle" or "Top" into an alignment value.
Definition: gtypes.cpp:127
bool operator<(const GTableIndex &loc1, const GTableIndex &loc2)
Relational operators for comparing table locations.
Definition: gtable.cpp:1068
This struct contains real-valued width and height fields.
Definition: gtypes.h:43
double width
Definition: gtypes.h:368
GPoint()
Definition: gtypes.cpp:153
Definition: gtypes.h:150
friend std::ostream & operator<<(std::ostream &os, const GRectangle &rect)
Writes the given rectangle to the given output stream.
Definition: gtypes.cpp:271
friend bool operator>(const GDimension &d1, const GDimension &d2)
Relational operators that compare two GDimension objects by width and then by height.
Definition: gtypes.cpp:68
Definition: gtypes.h:151
GRectangle enlargedBy(double amount)
Returns a new rectangle with its boundaries shifted outward by the given amount on all 4 sides...
Definition: gtypes.cpp:249
GRectangle(double x=0, double y=0, double width=0, double height=0)
Creates a GRectangle object with the specified position and size.
Definition: gtypes.cpp:202
friend GDimension operator*(const GDimension &d, double scale)
Multiplies the width and height of the given GDimension object by the given scale factor and returns ...
Definition: gtypes.cpp:76
This struct contains real-valued x and y fields.
Definition: gtypes.h:202
bool isEmpty() const
Returns true if the rectangle is empty, meaning that it has a width and height that are both 0 or neg...
Definition: gtypes.cpp:261
bool contains(double x, double y) const
Returns true if the rectangle contains the given point.
Definition: gtypes.cpp:234
Definition: gtypes.h:163
friend GPoint operator*(const GPoint &p, double scale)
Multiplies the x and y coordinates of the given point by the given scale factor and returns the scale...
Definition: gtypes.cpp:190
friend bool operator!=(const GDimension &d1, const GDimension &d2)
Compares two GDimension objects for inequality.
Definition: gtypes.cpp:55
double x
Definition: gtypes.h:366
friend bool operator<=(const GDimension &d1, const GDimension &d2)
Relational operators that compare two GDimension objects by width and then by height.
Definition: gtypes.cpp:64
Definition: gtypes.h:138
double y
Definition: gtypes.h:367
Definition: gtypes.h:148
friend bool operator==(const GRectangle &r1, const GRectangle &r2)
Compares two rectangles for equality.
Definition: gtypes.cpp:276
bool operator!=(const GTableIndex &loc1, const GTableIndex &loc2)
Definition: gtable.cpp:1081
std::function< void()> GThunk
An alias for a function wrapper around a void function with no parameters and no return.
Definition: gtypes.h:30
friend bool operator<(const GDimension &d1, const GDimension &d2)
Relational operators that compare two GDimension objects by width and then by height.
Definition: gtypes.cpp:59
VerticalAlignment
The supported kinds of vertical alignment of a widget or onscreen object.
Definition: gtypes.h:147
Definition: gtypes.h:140