SGL
gdrawingsurface.h
1 /*
2  * File: gdrawingsurface.h
3  * -----------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * - converted Grid functionality to 2D array/vector
9  * @version 2018/09/10
10  * - added doc comments for new documentation generation
11  * @version 2018/08/23
12  * - renamed to gdrawingsurface.h to replace Java version
13  * @version 2018/07/11
14  * - initial version
15  */
16 
17 
18 #ifndef _gdrawingsurface_h
19 #define _gdrawingsurface_h
20 
21 #include <string>
22 #include <vector>
23 #include <QFont>
24 #include <QWidget>
25 #include "gobjects.h"
26 #include "gtypes.h"
27 
28 namespace sgl {
29 
36 public:
40  virtual void clear() = 0;
41 
45  virtual void conditionalRepaint();
46 
50  virtual void conditionalRepaintRegion(int x, int y, int width, int height);
51 
55  virtual void conditionalRepaintRegion(const GRectangle& bounds);
56 
62  virtual void draw(GObject* gobj) = 0;
63 
69  virtual void draw(GObject* gobj, double x, double y);
70 
75  virtual void draw(GObject& gobj);
76 
81  virtual void draw(GObject& gobj, double x, double y);
82 
87  virtual void draw(QPainter* painter) = 0;
88 
94  virtual void drawArc(double x, double y, double width, double height, double start, double sweep);
95 
103  virtual void drawImage(const string& filename, double x = 0, double y = 0);
104 
110  virtual void drawLine(const GPoint& p0, const GPoint& p1);
111 
117  virtual void drawLine(double x0, double y0, double x1, double y1);
118 
124  virtual void drawOval(const GRectangle& bounds);
125 
131  virtual void drawOval(double x, double y, double width, double height);
132 
141  virtual GPoint drawPolarLine(const GPoint& p0, double r, double theta);
142 
151  virtual GPoint drawPolarLine(double x0, double y0, double r, double theta);
152 
157  virtual void drawPixel(double x, double y);
158 
163  virtual void drawPixel(double x, double y, int color);
164 
169  virtual void drawPixel(double x, double y, const string& color);
170 
176  virtual void drawPolygon(std::initializer_list<double> coords);
177 
183  virtual void drawPolygon(std::initializer_list<GPoint> points);
184 
190  virtual void drawRect(const GRectangle& bounds);
191 
197  virtual void drawRect(double x, double y, double width, double height);
198 
204  virtual void drawString(const string& text, double x, double y);
205 
211  virtual void fillArc(double x, double y, double width, double height, double start, double sweep);
212 
219  virtual void fillOval(const GRectangle& bounds);
220 
227  virtual void fillOval(double x, double y, double width, double height);
228 
234  virtual void fillPolygon(std::initializer_list<double> coords);
235 
241  virtual void fillPolygon(std::initializer_list<GPoint> coords);
242 
248  virtual void fillRect(const GRectangle& bounds);
249 
255  virtual void fillRect(double x, double y, double width, double height);
256 
261  virtual int getARGB(double x, double y) const;
262 
267  virtual string getBackground() const;
268 
273  virtual int getBackgroundInt() const;
274 
283  virtual string getColor() const;
284 
293  virtual int getColorInt() const;
294 
300  virtual string getFillColor() const;
301 
307  virtual int getFillColorInt() const;
308 
314  virtual string getFont() const;
315 
324  virtual string getForeground() const;
325 
334  virtual int getForegroundInt() const;
335 
341  virtual GObject::LineStyle getLineStyle() const;
342 
347  virtual double getLineWidth() const;
348 
362  virtual int getPixel(double x, double y) const = 0;
363 
378  virtual int getPixelARGB(double x, double y) const = 0;
379 
385  virtual std::vector<std::vector<int>> getPixels() const = 0;
386 
393  virtual int** getPixelsArray() const = 0;
394 
403  virtual std::vector<std::vector<int>> getPixelsARGB() const = 0;
404 
414  virtual int** getPixelsArrayARGB() const = 0;
415 
423  virtual string getPixelString(double x, double y) const;
424 
438  virtual int getRGB(double x, double y) const;
439 
451  virtual string getRGBString(double x, double y) const;
452 
461  virtual bool isAutoRepaint() const;
462 
471  virtual bool isRepaintImmediately() const;
472 
481  virtual void repaint() = 0;
482 
489  virtual void repaintRegion(int x, int y, int width, int height) = 0;
490 
497  virtual void repaintRegion(const GRectangle& bounds);
498 
508  virtual void setAutoRepaint(bool autoRepaint);
509 
514  virtual void setBackground(int color);
515 
520  virtual void setBackground(const string& color);
521 
530  virtual void setColor(int color);
531 
540  virtual void setColor(const string& color);
541 
547  virtual void setFillColor(int color);
548 
554  virtual void setFillColor(const string& color);
555 
561  virtual void setFont(const QFont& font);
562 
568  virtual void setFont(const string& font);
569 
578  virtual void setForeground(int color);
579 
588  virtual void setForeground(const string& color);
589 
594  virtual void setLineWidth(double lineWidth);
595 
601  virtual void setLineStyle(GObject::LineStyle lineStyle);
602 
615  virtual void setPixel(double x, double y, int rgb) = 0;
616 
629  virtual void setPixel(double x, double y, int r, int g, int b);
630 
643  virtual void setPixel(double x, double y, const string& color);
644 
656  virtual void setPixelARGB(double x, double y, int argb) = 0;
657 
669  virtual void setPixelARGB(double x, double y, int a, int r, int g, int b);
670 
679  virtual void setPixels(int** pixels, int width = -1, int height = -1) = 0;
680 
689  virtual void setPixels(const std::vector<std::vector<int>>& pixels) = 0;
690 
699  virtual void setPixelsARGB(int** pixelsARGB, int width = -1, int height = -1) = 0;
700 
709  virtual void setPixelsARGB(const std::vector<std::vector<int>>& pixelsARGB) = 0;
710 
720  virtual void setRepaintImmediately(bool autoRepaint);
721 
734  virtual void setRGB(double x, double y, int rgb);
735 
748  virtual void setRGB(double x, double y, int r, int g, int b);
749 
762  virtual void setRGB(double x, double y, const string& color);
763 
764 protected:
765  GDrawingSurface();
766  virtual ~GDrawingSurface();
767 
770  string _color;
771  string _fillColor;
772  string _font;
777  double _lineWidth;
779 
783  void checkBounds(const string& member, double x, double y, double width, double height) const;
784 
788  void checkColor(const string& member, int rgb) const;
789 
793  void checkSize(const string& member, double width, double height) const;
794 
801  virtual void initializeGObject(GObject& obj, bool filled = false);
802 
809  virtual void initializeGObject(GObject* obj, bool filled = false);
810 
816  virtual void setDrawingForwardTarget(GDrawingSurface* forwardTarget);
817 };
818 
826 class GForwardDrawingSurface : public virtual GDrawingSurface {
827 public:
828  void clear() override;
829  void draw(GObject* gobj) override;
830  void draw(GObject* gobj, double x, double y) override;
831  void draw(GObject& gobj) override;
832  void draw(GObject& gobj, double x, double y) override;
833  void draw(QPainter* painter) override;
834  int getPixel(double x, double y) const override;
835  int getPixelARGB(double x, double y) const override;
836  std::vector<std::vector<int>> getPixels() const override;
837  int** getPixelsArray() const override;
838  std::vector<std::vector<int>> getPixelsARGB() const override;
839  int** getPixelsArrayARGB() const override;
840  bool isAutoRepaint() const override;
841  void repaint() override;
842  void repaintRegion(int x, int y, int width, int height) override;
843  void setAutoRepaint(bool autoRepaint) override;
844  void setBackground(int color) override;
845  void setBackground(const string& color) override;
846  void setColor(int color) override;
847  void setColor(const string& color) override;
848  void setFillColor(int color) override;
849  void setFillColor(const string& color) override;
850  void setFont(const QFont& font) override;
851  void setFont(const string& font) override;
852  void setLineWidth(double lineWidth) override;
853  void setPixel(double x, double y, int rgb) override;
854  void setPixel(double x, double y, int r, int g, int b) override;
855  void setPixelARGB(double x, double y, int argb) override;
856  void setPixelARGB(double x, double y, int a, int r, int g, int b) override;
857  void setPixels(int** pixels, int width = -1, int height = -1) override;
858  void setPixels(const std::vector<std::vector<int>>& pixels) override;
859  void setPixelsARGB(int** pixelsARGB, int width = -1, int height = -1) override;
860  void setPixelsARGB(const std::vector<std::vector<int>>& pixelsARGB) override;
861  void setRepaintImmediately(bool repaintImmediately) override;
862 
863 protected:
864  virtual void ensureForwardTarget() = 0;
865  virtual void ensureForwardTargetConstHack() const;
866 };
867 
868 } // namespace sgl
869 
870 #endif // _gcanvas_h
string _font
Definition: gdrawingsurface.h:772
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:293
virtual int getForegroundInt() const
Returns the current foreground outline color of the interactor as an RGB integer. ...
Definition: gdrawingsurface.cpp:287
virtual string getRGBString(double x, double y) const
Returns the color of the pixel at the given x/y coordinates of the background layer of the interactor...
Definition: gdrawingsurface.cpp:315
virtual void setPixelARGB(double x, double y, int argb)=0
Sets the color of the given x/y pixel in the background layer of the interactor to the given ARGB val...
virtual string getFillColor() const
Returns the current fill color of the interactor as a string.
Definition: gdrawingsurface.cpp:259
virtual double getLineWidth() const
Returns the thickness used when drawing outlines of shapes and lines.
Definition: gdrawingsurface.cpp:299
virtual void conditionalRepaint()
Repaints the interactor only if its contents have changed.
Definition: gdrawingsurface.cpp:66
virtual int getFillColorInt() const
Returns the current fill color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:267
string _fillColor
Definition: gdrawingsurface.h:771
virtual void drawImage(string filename, double x=0, double y=0)
Draws an image loaded from the given file name onto the background pixel layer of this interactor at ...
Definition: gdrawingsurface.cpp:109
virtual int getRGB(double x, double y) const
Returns the color of the pixel at the given x/y coordinates of the background layer of the interactor...
Definition: gdrawingsurface.cpp:311
virtual string getFont() const
Returns the current text font of the interactor as a font string.
Definition: gdrawingsurface.cpp:275
virtual void repaint()=0
Instructs the interactor to redraw itself on the screen.
bool _autoRepaint
Definition: gdrawingsurface.h:778
string _backgroundColor
Definition: gdrawingsurface.h:769
GDrawingSurface * _forwardTarget
Definition: gdrawingsurface.h:768
virtual int getPixel(double x, double y) const =0
Returns the color of the pixel at the given x/y coordinates of the background layer of the interactor...
virtual ~GDrawingSurface()
Definition: gdrawingsurface.cpp:39
LineStyle
Styles that can be used for the outline around various shapes.
Definition: gobjects.h:72
virtual void fillRect(const GRectangle &bounds)
Draws a filled rectangle of the given dimensions onto the background pixel layer of this interactor i...
Definition: gdrawingsurface.cpp:213
GDrawingSurface is an abstract superclass for types that allow drawing shapes and pixels onto themsel...
Definition: gdrawingsurface.h:35
virtual bool isAutoRepaint() const
Returns true if the interactor should repaint itself automatically whenever any change is made to its...
Definition: gdrawingsurface.cpp:337
virtual string getBackground() const
Returns the current background color of the interactor as a string.
Definition: gdrawingsurface.cpp:227
void repaint()
Issues a request to update the most recently created graphics window.
Definition: gwindow.cpp:1360
This class is the common superclass of all graphical objects that can be displayed on a graphical win...
Definition: gobjects.h:66
virtual int ** getPixelsArray() const =0
Returns all pixels of the surface as a heap-allocated 2D array, where the first index represents the ...
int _colorInt
Definition: gdrawingsurface.h:774
virtual void setLineWidth(double lineWidth)
Sets the thickness used when drawing outlines of shapes and lines.
Definition: gdrawingsurface.cpp:460
virtual string getPixelString(double x, double y) const
Returns the color of the pixel at the given x/y coordinates of the image as a string such as "#ff00cc...
Definition: gdrawingsurface.cpp:307
virtual void fillArc(double x, double y, double width, double height, double start, double sweep)
Draws a filled arc with the given attributes onto the background pixel layer of this interactor in th...
Definition: gdrawingsurface.cpp:185
void checkColor(string member, int rgb) const
Throws an error if the given rgb value is not a valid color.
Definition: gdrawingsurface.cpp:47
virtual bool isRepaintImmediately() const
Returns true if the interactor should repaint itself automatically whenever any change is made to its...
Definition: gdrawingsurface.cpp:345
virtual void setAutoRepaint(bool autoRepaint)
Sets whether the interactor should repaint itself automatically whenever any change is made to its gr...
Definition: gdrawingsurface.cpp:354
virtual void setColor(int color)
Sets the current foreground outline color of the interactor as as RGB integer.
Definition: gdrawingsurface.cpp:380
Definition: console.h:45
virtual GPoint drawPolarLine(const GPoint &p0, double r, double theta)
Draws a line using polar coordinates onto the background pixel layer of this interactor in the curren...
Definition: gdrawingsurface.cpp:134
virtual void drawOval(const GRectangle &bounds)
Draws an unfilled oval with the given bounding box onto the background pixel layer of this interactor...
Definition: gdrawingsurface.cpp:124
GDrawingSurface()
Definition: gdrawingsurface.cpp:24
virtual void drawRect(const GRectangle &bounds)
Draws an unfilled rectangle of the given dimensions onto the background pixel layer of this interacto...
Definition: gdrawingsurface.cpp:169
virtual std::vector< std::vector< int > > getPixels() const =0
Returns all pixels of the surface as a nested STL vector of vectors, where the first index represents...
virtual void draw(GObject *gobj)=0
Draws the given graphical object onto the background pixel layer of this interactor.
virtual int getColorInt() const
Returns the current foreground outline color of the interactor as an RGB integer. ...
Definition: gdrawingsurface.cpp:251
virtual int ** getPixelsArrayARGB() const =0
Returns all pixels of the background layer of the surface as a heap-allocated 2D array, where the first index represents the x value and the second index represents the y value.
virtual void drawLine(const GPoint &p0, const GPoint &p1)
Draws a line between the given two points onto the background pixel layer of this interactor at the g...
Definition: gdrawingsurface.cpp:114
virtual void setPixelsARGB(int **pixelsARGB, int width=-1, int height=-1)=0
Sets the color of the all pixels in the background layer of the interactor to the given ARGB values...
virtual void conditionalRepaintRegion(int x, int y, int width, int height)
Repaints the given region of the interactor only if its contents have changed.
Definition: gdrawingsurface.cpp:76
virtual void setRGB(double x, double y, int rgb)
Sets the color of the given x/y pixel in the background layer of the interactor to the given RGB valu...
Definition: gdrawingsurface.cpp:484
string _color
Definition: gdrawingsurface.h:770
virtual GObject::LineStyle getLineStyle() const
Returns the current line style which will be used to draw outlines of shapes and lines.
Definition: gdrawingsurface.cpp:291
virtual void fillPolygon(std::initializer_list< double > coords)
Draws a filled polygon containing the given points onto the background pixel layer of this interactor...
Definition: gdrawingsurface.cpp:201
virtual int getPixelARGB(double x, double y) const =0
Returns the color of the pixel at the given x/y coordinates of the background layer of the interactor...
virtual void clear()=0
Erases any pixel data from the drawing surface.
virtual void setForeground(int color)
Sets the current foreground outline color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:444
virtual void fillOval(const GRectangle &bounds)
Draws a filled oval with the given bounding box onto the background pixel layer of this interactor at...
Definition: gdrawingsurface.cpp:191
void checkBounds(string member, double x, double y, double width, double height) const
Throws an error if the given x/y values are out of bounds.
Definition: gdrawingsurface.cpp:43
void checkSize(string member, double width, double height) const
Throws an error if the given width/height values are out of bounds.
Definition: gdrawingsurface.cpp:55
virtual void drawPixel(double x, double y)
Colors the given x/y pixel of the background layer of this interactor using the interactor&#39;s current ...
Definition: gdrawingsurface.cpp:145
virtual string getColor() const
Returns the current foreground outline color of the interactor as a string.
Definition: gdrawingsurface.cpp:243
GObject::LineStyle _lineStyle
Definition: gdrawingsurface.h:776
virtual void setBackground(int color)
Sets the current background color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:362
virtual void drawString(string text, double x, double y)
Draws a text string onto the background pixel layer of this interactor at the given x/y location in t...
Definition: gdrawingsurface.cpp:179
double _lineWidth
Definition: gdrawingsurface.h:777
virtual int getARGB(double x, double y) const
Returns the pixel color data at the given x/y location, retaining alpha-channel transparency in the t...
Definition: gdrawingsurface.cpp:223
virtual string getForeground() const
Returns the current foreground outline color of the interactor as a string.
Definition: gdrawingsurface.cpp:283
virtual void setRepaintImmediately(bool autoRepaint)
Sets whether the interactor should repaint itself automatically whenever any change is made to its gr...
Definition: gdrawingsurface.cpp:480
virtual void setPixel(double x, double y, int rgb)=0
Sets the color of the given x/y pixel in the background layer of the interactor to the given RGB valu...
virtual void setLineStyle(GObject::LineStyle lineStyle)
Sets the current line style which will be used to draw outlines of shapes and lines.
Definition: gdrawingsurface.cpp:452
int _backgroundColorInt
Definition: gdrawingsurface.h:773
virtual std::vector< std::vector< int > > getPixelsARGB() const =0
Returns all pixels of the background layer of the surface as a nested STL vector of vectors...
This struct contains real-valued x and y fields.
Definition: gtypes.h:202
virtual void drawArc(double x, double y, double width, double height, double start, double sweep)
Draws an unfilled arc with the given attributes onto the background pixel layer of this interactor in...
Definition: gdrawingsurface.cpp:103
int _fillColorInt
Definition: gdrawingsurface.h:775
virtual void initializeGObject(GObject &obj, bool filled=false)
Initializes a new graphical object to be drawn.
Definition: gdrawingsurface.cpp:319
virtual void repaintRegion(int x, int y, int width, int height)=0
Instructs the interactor to repaint the given region of pixel data.
virtual void setPixels(int **pixels, int width=-1, int height=-1)=0
Sets the color of the all pixels in the background layer of the interactor to the given RGB values...
virtual int getBackgroundInt() const
Returns the current background color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:235
virtual void drawPolygon(std::initializer_list< double > coords)
Draws an unfilled polygon containing the given points onto the background pixel layer of this interac...
Definition: gdrawingsurface.cpp:157
virtual void setFillColor(int color)
Sets the current fill color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:410