SGL
gcanvas.h
1 /*
2  * File: gcanvas.h
3  * ---------------
4  *
5  * @author Marty Stepp
6  * @version 2019/05/01
7  * - added createArgbPixel
8  * - bug fixes related to save / setPixels with alpha transparency
9  * @version 2019/03/07
10  * - added support for loading canvas directly from istream (htiek)
11  * @version 2018/09/10
12  * - added doc comments for new documentation generation
13  * @version 2018/09/04
14  * - added double-click event support
15  * @version 2018/08/23
16  * - renamed to gcanvas.h to replace Java version
17  * @version 2018/06/30
18  * - initial version
19  */
20 
21 
22 #ifndef _gcanvas_h
23 #define _gcanvas_h
24 
25 #include <string>
26 #include <QtEvents>
27 #include <QPainter>
28 #include "gdrawingsurface.h"
29 #include "gevent.h"
30 #include "ginteractor.h"
31 #include "gobjects.h"
32 #include "grid.h"
33 
34 // default color used to highlight pixels that do not match between two images
35 #define GCANVAS_DEFAULT_DIFF_PIXEL_COLOR 0xdd00dd
36 
37 class _Internal_QCanvas;
38 
70 class GCanvas : public virtual GInteractor, public virtual GDrawingSurface {
71 public:
76  static const int WIDTH_HEIGHT_MAX;
77 
82  static int createArgbPixel(int alpha, int red, int green, int blue);
83 
88  static int createRgbPixel(int red, int green, int blue);
89 
94  static int getAlpha(int argb);
95 
100  static int getBlue(int rgb);
101 
106  static int getGreen(int rgb);
107 
112  static int getRed(int rgb);
113 
118  static void getRedGreenBlue(int rgb, int& red, int& green, int& blue);
119 
124  GCanvas(QWidget* parent = nullptr);
125 
132  GCanvas(const string& filename, QWidget* parent = nullptr);
133 
139  GCanvas(std::istream& filename, QWidget* parent = nullptr);
140 
147  GCanvas(double width, double height, int rgbBackground, QWidget* parent = nullptr);
148 
155  GCanvas(double width, double height, const string& rgbBackground = "#00000000", QWidget* parent = nullptr);
156 
160  ~GCanvas() override;
161 
166  virtual void add(GObject* gobj);
167 
173  virtual void add(GObject* gobj, double x, double y);
174 
178  virtual void add(GObject& gobj);
179 
184  virtual void add(GObject& gobj, double x, double y);
185 
190  void clear() override;
191 
200  virtual void clearObjects();
201 
211  virtual void clearPixels();
212 
217  virtual bool contains(double x, double y) const;
218 
226  virtual int countDiffPixels(const GCanvas& image) const;
227 
235  virtual int countDiffPixels(const GCanvas& image, int xmin, int ymin, int xmax, int ymax) const;
236 
245  virtual int countDiffPixels(const GCanvas* image) const;
246 
255  virtual int countDiffPixels(const GCanvas* image, int xmin, int ymin, int xmax, int ymax) const;
256 
262  virtual GCanvas* diff(const GCanvas& image, int diffPixelColor = GCANVAS_DEFAULT_DIFF_PIXEL_COLOR) const;
263 
270  virtual GCanvas* diff(const GCanvas* image, int diffPixelColor = GCANVAS_DEFAULT_DIFF_PIXEL_COLOR) const;
271 
281  void draw(GObject* gobj) override;
282 
283  /* @private */
284  /* @inherit */
285  void draw(QPainter* painter) override;
286 
290  virtual bool equals(const GCanvas& other) const;
291 
297  virtual void fill(int rgb);
298 
303  virtual void fill(const string& rgb);
304 
314  virtual void fillRegion(double x, double y, double width, double height, int rgb);
315 
324  virtual void fillRegion(double x, double y, double width, double height,
325  const string& rgb);
326 
332  virtual void flatten();
333 
341  virtual void fromGrid(const Grid<int>& grid);
342 
343  /* @inherit */
344  string getBackground() const override;
345 
346  /* @inherit */
347  int getBackgroundInt() const override;
348 
355  virtual GObject* getElement(int index) const;
356 
362  virtual GObject* getElementAt(double x, double y) const;
363 
368  virtual int getElementCount() const;
369 
375  virtual string getFilename() const;
376 
377  /* @inherit */
378  string getFont() const override;
379 
380  /* @inherit */
381  _Internal_QWidget* getInternalWidget() const override;
382 
394  int getPixel(double x, double y) const override;
395 
410  int getPixelARGB(double x, double y) const override;
411 
418  Grid<int> getPixels() const override;
419 
427  Grid<int> getPixelsARGB() const override;
428 
429  /* @inherit */
430  string getType() const override;
431 
432  /* @inherit */
433  QWidget* getWidget() const override;
434 
435  /* @inherit */
436  bool isAutoRepaint() const override;
437 
443  virtual void load(const string& filename);
444 
456  virtual void remove(GObject* gobj);
457 
462  virtual void remove(GObject& gobj);
463 
467  virtual void removeAll();
468 
477  void repaint() override;
478 
483  void repaintRegion(int x, int y, int width, int height) override;
484 
495  void resize(double width, double height, bool retain = true);
496 
501  virtual void save(const string& filename);
502 
511  void setAutoRepaint(bool autoRepaint) override;
512 
513  /* @inherit */
514  void setBackground(int color) override;
515 
516  /* @inherit */
517  void setBackground(const string& color) override;
518 
519  /* @inherit */
520  void setColor(int color) override;
521 
522  /* @inherit */
523  void setColor(const string& color) override;
524 
525  /* @inherit */
526  void setFont(const QFont& font) override;
527 
528  /* @inherit */
529  void setFont(const string& font) override;
530 
531  /* @inherit */
532  void setForeground(int rgb) override;
533 
534  /* @inherit */
535  void setForeground(const string& color) override;
536 
542  void setKeyListener(GEventListener func) override;
543 
549  void setKeyListener(GEventListenerVoid func) override;
550 
562  void setPixel(double x, double y, int rgb) override;
563 
575  void setPixel(double x, double y, int r, int g, int b) override;
576 
588  void setPixelARGB(double x, double y, int argb) override;
589 
601  void setPixelARGB(double x, double y, int a, int r, int g, int b) override;
602 
611  void setPixels(const Grid<int>& pixels) override;
612 
620  void setPixelsARGB(const Grid<int>& pixelsARGB) override;
621 
625  virtual GImage* toGImage() const;
626 
635  virtual Grid<int> toGrid() const;
636 
645  virtual void toGrid(Grid<int>& grid) const;
646 
647 private:
648  Q_DISABLE_COPY(GCanvas)
649 
650  _Internal_QCanvas* _iqcanvas;
651  GCompound _gcompound;
652  QImage* _backgroundImage;
653  string _filename; // file canvas was loaded from; "" if not loaded from a file
654 
655  friend class _Internal_QCanvas;
656 
657  void ensureBackgroundImage();
658 
659  void ensureBackgroundImageConstHack() const;
660 
661  void init(double width, double height, int rgbBackground, QWidget* parent);
662 
667  virtual bool loadFromStream(std::istream& input);
668 
669  void notifyOfResize(double width, double height);
670 };
671 
672 
677 class _Internal_QCanvas : public QWidget, public _Internal_QWidget {
678  Q_OBJECT
679 
680 public:
681  _Internal_QCanvas(GCanvas* gcanvas, QWidget* parent = nullptr);
682  void detach() override;
683  void enterEvent(QEvent* event) override;
684  void keyPressEvent(QKeyEvent* event) override;
685  void keyReleaseEvent(QKeyEvent* event) override;
686  void leaveEvent(QEvent* event) override;
687  void mouseMoveEvent(QMouseEvent* event) override;
688  void mousePressEvent(QMouseEvent* event) override;
689  void mouseReleaseEvent(QMouseEvent* event) override;
690  void paintEvent(QPaintEvent *event) override;
691  void resizeEvent(QResizeEvent* event) override;
692  virtual void setCanvasSize(double width, double height);
693  QSize sizeHint() const override;
694  void wheelEvent(QWheelEvent* event) override;
695 
696 signals:
697  void doubleClicked();
698 
699 protected:
700  void mouseDoubleClickEvent(QMouseEvent* e) override;
701 
702 private:
703  GCanvas* _gcanvas;
704 
705  friend class GCanvas;
706 };
707 
708 #endif // _gcanvas_h
virtual void fill(int rgb)
Sets the color of every pixel in the canvas to the given color value.
Definition: gcanvas.cpp:389
virtual void save(string filename)
Saves the canvas&#39;s contents to the given image file.
Definition: gcanvas.cpp:699
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gcanvas.cpp:553
Grid< int > getPixelsARGB() const override
Returns all pixels of the background layer of the canvas as a Grid, where rows represent y values and...
Definition: gcanvas.cpp:540
virtual bool contains(double x, double y) const
Returns true if any of the graphical objects in the foreground layer of the canvas touch the given x/...
Definition: gcanvas.cpp:216
A GCanvas is a graphical drawing surface on which you can draw shapes, lines, and colors...
Definition: gcanvas.h:70
string getBackground() const override
Returns the current background color of the interactor as a string.
Definition: gcanvas.cpp:466
virtual void clearPixels()
Resets the background layer of pixels in the canvas to the current background color.
Definition: gcanvas.cpp:196
void setForeground(int rgb) override
Sets the current foreground outline color of the interactor as an RGB integer.
Definition: gcanvas.cpp:776
GDrawingSurface is an abstract superclass for types that allow drawing shapes and pixels onto themsel...
Definition: gdrawingsurface.h:31
string getFont() const override
Returns the current text font of the interactor as a font string.
Definition: gcanvas.cpp:499
~GCanvas() override
Frees memory allocated internally by the canvas.
Definition: gcanvas.cpp:145
This graphical object subclass represents an image from a file.
Definition: gobjects.h:953
static int getBlue(int rgb)
Extracts the blue component from 0-255 of the given RGB integer.
Definition: gcanvas.cpp:59
int getPixelARGB(double x, double y) const override
Returns the color of the pixel at the given x/y coordinates of the background layer of the canvas as ...
Definition: gcanvas.cpp:516
static int createRgbPixel(int red, int green, int blue)
Creates a single RGB integer from the given R-G-B components from 0-255.
Definition: gcanvas.cpp:47
virtual void removeAll()
Removes all graphical objects from the foreground layer of the canvas.
Definition: gcanvas.cpp:665
virtual void flatten()
Moves all graphical objects from the foreground layer to the background layer.
Definition: gcanvas.cpp:427
void setColor(int color) override
Sets the current foreground outline color of the interactor as as RGB integer.
Definition: gcanvas.cpp:760
virtual GImage * toGImage() const
Converts the pixels of the canvas into a GImage object.
Definition: gcanvas.cpp:885
int getPixel(double x, double y) const override
Returns the color of the pixel at the given x/y coordinates of the background layer of the canvas as ...
Definition: gcanvas.cpp:507
int getBackgroundInt() const override
Returns the current background color of the interactor as an RGB integer.
Definition: gcanvas.cpp:470
void setPixelsARGB(const Grid< int > &pixelsARGB) override
Sets the color of the all pixels in the background layer of the canvas to the given ARGB values...
Definition: gcanvas.cpp:865
virtual GObject * getElement(int index) const
Returns a pointer to the graphical object in the foreground layer of the canvas at the specified inde...
Definition: gcanvas.cpp:474
virtual void add(GObject *gobj)
Adds the given interactor to canvas.
Definition: gcanvas.cpp:151
virtual GObject * getElementAt(double x, double y) const
Returns a pointer to the first graphical object in the foreground layer of the canvas that contains t...
Definition: gcanvas.cpp:481
virtual int countDiffPixels(const GCanvas &image) const
Returns the total number of pixels that are not the same color between this image and the given other...
Definition: gcanvas.cpp:223
bool isAutoRepaint() const override
Returns true if the interactor should repaint itself automatically whenever any change is made to its...
Definition: gcanvas.cpp:561
static int getAlpha(int argb)
Extracts the alpha component from 0-255 of the given ARGB integer.
Definition: gcanvas.cpp:54
void setPixels(const Grid< int > &pixels) override
Sets the color of the all pixels in the background layer of the canvas to the given RGB values...
Definition: gcanvas.cpp:844
virtual string getFilename() const
Returns the name of the image file from which this canvas was loaded or to which it was saved most re...
Definition: gcanvas.cpp:495
static int getRed(int rgb)
Extracts the red component from 0-255 of the given RGB integer.
Definition: gcanvas.cpp:67
void setFont(const QFont &font) override
Returns the current text font of the interactor using a Qt font object.
Definition: gcanvas.cpp:768
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gcanvas.cpp:557
virtual void load(string filename)
Reads the canvas&#39;s pixel contents from the given image file.
Definition: gcanvas.cpp:565
static int getGreen(int rgb)
Extracts the green component from 0-255 of the given RGB integer.
Definition: gcanvas.cpp:63
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual void clearObjects()
Removes all graphical objects from the foreground layer of the canvas.
Definition: gcanvas.cpp:188
void draw(GObject *gobj) override
Draws the given graphical object onto the background layer of the canvas.
Definition: gcanvas.cpp:333
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gcanvas.cpp:503
void clear() override
Removes all graphical objects from the canvas foreground layer and wipes the background layer to show...
Definition: gcanvas.cpp:183
virtual int getElementCount() const
Returns the number of graphical objects stored in the foreground layer of the canvas.
Definition: gcanvas.cpp:488
This class is the common superclass of all graphical objects that can be displayed on a graphical win...
Definition: gobjects.h:62
void setBackground(int color) override
Sets the current background color of the interactor as an RGB integer.
Definition: gcanvas.cpp:738
This graphical object subclass consists of a collection of other graphical objects.
Definition: gobjects.h:765
void repaint() override
Instructs the canvas to redraw its layers.
Definition: gcanvas.cpp:673
virtual GCanvas * diff(const GCanvas &image, int diffPixelColor=GCANVAS_DEFAULT_DIFF_PIXEL_COLOR) const
Generates a new canvas whose content is equal to that of this canvas but with any pixels that don&#39;t m...
Definition: gcanvas.cpp:282
void repaintRegion(int x, int y, int width, int height) override
Instructs the canvas to redraw the given region of pixels within both of its layers.
Definition: gcanvas.cpp:682
virtual bool equals(const GCanvas &other) const
Returns true if the two given canvases contain exactly the same pixel data.
Definition: gcanvas.cpp:381
void resize(double width, double height, bool retain=true)
Changes this image&#39;s bounds to be the given size.
Definition: gcanvas.cpp:690
virtual void fillRegion(double x, double y, double width, double height, int rgb)
Sets the color of every pixel in the given rectangular range of the canvas pixel data to the given co...
Definition: gcanvas.cpp:402
void setAutoRepaint(bool autoRepaint) override
Sets whether the canvas will automatically repaint itself whenever you make a change to either the ba...
Definition: gcanvas.cpp:730
virtual Grid< int > toGrid() const
Converts this canvas&#39;s pixel data into a grid of RGB pixels.
Definition: gcanvas.cpp:909
void setKeyListener(GEventListener func) override
Sets a key listener on this canvas so that it will be called when any key is pressed or released on t...
Definition: gcanvas.cpp:784
static int createArgbPixel(int alpha, int red, int green, int blue)
Creates a single ARGB integer from the given A-R-G-B components from 0-255.
Definition: gcanvas.cpp:40
static void getRedGreenBlue(int rgb, int &red, int &green, int &blue)
Extracts the red, green, and blue components from 0-255 of the given RGB integer, filling by referenc...
Definition: gcanvas.cpp:71
GCanvas(QWidget* parent=nullptr)
Creates an empty canvas with a default size of 0x0 pixels and a default background and foreground col...
Definition: gcanvas.cpp:78
static const int WIDTH_HEIGHT_MAX
Largest value that an image&#39;s width and/or height can have.
Definition: gcanvas.h:76
void setPixelARGB(double x, double y, int argb) override
Sets the color of the given x/y pixel in the background layer of the canvas to the given ARGB value...
Definition: gcanvas.cpp:828
virtual void fromGrid(const Grid< int > &grid)
Replaces the entire contents of the background layer of the canvas with the contents of the given gri...
Definition: gcanvas.cpp:441
void setPixel(double x, double y, int rgb) override
Sets the color of the given x/y pixel in the background layer of the canvas to the given RGB value...
Definition: gcanvas.cpp:802
Grid< int > getPixels() const override
Returns all pixels of the background layer of the canvas as a Grid, where rows represent y values and...
Definition: gcanvas.cpp:525