28 #include "gdrawingsurface.h" 30 #include "ginteractor.h" 35 #define GCANVAS_DEFAULT_DIFF_PIXEL_COLOR 0xdd00dd 37 class _Internal_QCanvas;
112 static int getRed(
int rgb);
124 GCanvas(QWidget* parent =
nullptr);
132 GCanvas(
const string& filename, QWidget* parent =
nullptr);
139 GCanvas(std::istream& filename, QWidget* parent =
nullptr);
147 GCanvas(
double width,
double height,
int rgbBackground, QWidget* parent =
nullptr);
155 GCanvas(
double width,
double height,
const string& rgbBackground =
"#00000000", QWidget* parent =
nullptr);
173 virtual void add(
GObject* gobj,
double x,
double y);
184 virtual void add(
GObject& gobj,
double x,
double y);
190 void clear()
override;
217 virtual bool contains(
double x,
double y)
const;
262 virtual GCanvas*
diff(
const GCanvas& image,
int diffPixelColor = GCANVAS_DEFAULT_DIFF_PIXEL_COLOR)
const;
270 virtual GCanvas*
diff(
const GCanvas* image,
int diffPixelColor = GCANVAS_DEFAULT_DIFF_PIXEL_COLOR)
const;
285 void draw(QPainter* painter)
override;
297 virtual void fill(
int rgb);
303 virtual void fill(
const string& rgb);
314 virtual void fillRegion(
double x,
double y,
double width,
double height,
int rgb);
324 virtual void fillRegion(
double x,
double y,
double width,
double height,
341 virtual void fromGrid(
const Grid<int>& grid);
378 string
getFont()
const override;
394 int getPixel(
double x,
double y)
const override;
430 string
getType()
const override;
443 virtual void load(
const string& filename);
456 virtual void remove(
GObject* gobj);
462 virtual void remove(
GObject& gobj);
483 void repaintRegion(
int x,
int y,
int width,
int height)
override;
495 void resize(
double width,
double height,
bool retain =
true);
501 virtual void save(
const string& filename);
523 void setColor(
const string& color)
override;
526 void setFont(
const QFont& font)
override;
529 void setFont(
const string& font)
override;
562 void setPixel(
double x,
double y,
int rgb)
override;
575 void setPixel(
double x,
double y,
int r,
int g,
int b)
override;
588 void setPixelARGB(
double x,
double y,
int argb)
override;
601 void setPixelARGB(
double x,
double y,
int a,
int r,
int g,
int b)
override;
611 void setPixels(
const Grid<int>& pixels)
override;
635 virtual Grid<int>
toGrid()
const;
645 virtual void toGrid(Grid<int>& grid)
const;
650 _Internal_QCanvas* _iqcanvas;
652 QImage* _backgroundImage;
655 friend class _Internal_QCanvas;
657 void ensureBackgroundImage();
659 void ensureBackgroundImageConstHack()
const;
661 void init(
double width,
double height,
int rgbBackground, QWidget* parent);
667 virtual bool loadFromStream(std::istream& input);
669 void notifyOfResize(
double width,
double height);
677 class _Internal_QCanvas :
public QWidget,
public _Internal_QWidget {
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;
697 void doubleClicked();
700 void mouseDoubleClickEvent(QMouseEvent* e)
override;
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'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'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'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'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'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'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