My Project
grid.h
1 /***************************************************************************
2 ** **
3 ** GridView, a simple GridView made with Qt4 **
4 ** Copyright (C) 2013 Sacha Schutz **
5 ** **
6 ** This program is free software: you can redistribute it and/or modify **
7 ** it under the terms of the GNU General Public License as published by **
8 ** the Free Software Foundation, either version 3 of the License, or **
9 ** (at your option) any later version. **
10 ** **
11 ** This program is distributed in the hope that it will be useful, **
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14 ** GNU General Public License for more details. **
15 ** **
16 ** You should have received a copy of the GNU General Public License **
17 ** along with this program. If not, see http://www.gnu.org/licenses/. **
18 ** **
19 ****************************************************************************
20 ** Author : Sacha Schutz **
21 ** Website: http://www.labsquare.org **
22 ** Email : sacha@labsquare.org **
23 ** Date : 12.03.12 **
24 ****************************************************************************/
25 
26 #ifndef GRIDVIEW_H
27 #define GRIDVIEW_H
28 
29 #include <QtGui>
30 #include <QWidget>
31 #include <QScrollArea>
32 #include <QVector>
33 
34 class GridWidget;
35 
42 class GridWidget : public QWidget
43 {
44  Q_OBJECT
45 public:
46 
58  explicit GridWidget(int rowCount = 100, int columnCount =100,QWidget *parent = 0);
59 
80  GridWidget(QWidget *parent);
81 
91  void setGridSize(int rowCount, int columnCount);
92 
106  void switchOn(int x,int y, const QColor& color);
107 
119  QColor getCellColor(int x, int y) ;
120 
121 
133  void switchOff(int x, int y);
134 
135 
143  int getGridColumns() ;
144 
152  int getGridRows() ;
153 
159  void clear();
160 
168  void setCellSize(int size);
169 
177  void setTool(QString tool);
178 
186  void setFront(QString front);
187 
195  void setBack(QString back);
196 
204  void setToolSize(int size);
205 
223  void identifyTool(QString tool, int x, int y);
224 
238  void Dot(int x, int y, QColor colorSelected);
239 
257  void RowMajorFill(int x, int y, QColor colorClicked, QColor toolColor);
258 
276  void ColMajorFill(int x, int y, QColor colorClicked, QColor toolColor);
277 
295  void DiagonalLeft(int x, int y, QColor colorClicked, QColor toolColor);
296 
314  void DiagonalRight(int x, int y, QColor colorClicked, QColor toolColor);
315 
331  void square(int x, int y, QColor toolColor, int toolSize);
332 
348  void triangle(int x, int y, QColor toolColor, int toolSize);
349 
365  void circle(int x, int y, QColor toolColor, int toolSize);
366 
376  void undo();
377 
378 
387  void redo();
388 
389 protected:
390 
402  void drawGrid(QPaintDevice * device);
403 
413  virtual void paintEvent(QPaintEvent *);
414 
425  virtual void mousePressEvent(QMouseEvent *);
426 
427 signals:
428 
429  void cellClicked(QPoint pos);
430 
431  void canUndo(bool can);
432 
433  void canRedo(bool can);
434 
435 private:
436 
437  int mCellSize;
439  int mRowCount;
443  QPixmap mGridPix;
445  QHash<int, QColor > mColors;
448  QString Tool;
450  int ToolSize;
452  QColor frontColor;
454  QColor backColor;
456  QColor background;
458  QVector < QHash<int, QColor > > oldStates;
460  QVector < QHash<int, QColor > > newStates;
462 };
463 #endif // GRIDVIEW_H
QColor frontColor
Definition: grid.h:452
void Dot(int x, int y, QColor colorSelected)
Receives the coordinates of the grid where the user clicked and paints that cell with the color of th...
Definition: tools.cpp:17
int mCellSize
Definition: grid.h:437
void circle(int x, int y, QColor toolColor, int toolSize)
Receives the coordinates of the grid where the user clicked and paints a circle of the size and with ...
Definition: tools.cpp:243
int mColumnCount
Definition: grid.h:441
int getGridColumns()
Returns the number of columns in the grid.
Definition: grid.cpp:302
QHash< int, QColor > mColors
Definition: grid.h:445
void setGridSize(int rowCount, int columnCount)
Sets the number of columns and rows of the grid.
Definition: grid.cpp:155
int ToolSize
Definition: grid.h:450
void DiagonalLeft(int x, int y, QColor colorClicked, QColor toolColor)
Recieves the coordinates of the grid where the user clicked and paints (from that point) the longest ...
Definition: tools.cpp:111
void triangle(int x, int y, QColor toolColor, int toolSize)
Receives the coordinates of the grid where the user clicked and paints a triangle of the size and wit...
Definition: tools.cpp:213
int getGridRows()
Returns the number of rows in the grid.
Definition: grid.cpp:313
void DiagonalRight(int x, int y, QColor colorClicked, QColor toolColor)
Recieves the coordinates of the grid where the user clicked and paints (from that point) the longest ...
Definition: tools.cpp:147
QVector< QHash< int, QColor > > oldStates
Definition: grid.h:458
int mRowCount
Definition: grid.h:439
QPixmap mGridPix
Definition: grid.h:443
void switchOn(int x, int y, const QColor &color)
Saves the given color and position in the vector that represents the painted cells of the grid...
Definition: grid.cpp:271
void ColMajorFill(int x, int y, QColor colorClicked, QColor toolColor)
Receives the coordinates of the grid where the user clicked and paints (from that point up and down) ...
Definition: tools.cpp:75
void drawGrid(QPaintDevice *device)
Function that first sets the size of the GridWidget, then paints the cells with the color selected fo...
Definition: grid.cpp:370
QColor backColor
Definition: grid.h:454
void switchOff(int x, int y)
Removes the given position from the vector that represents the painted cells of the grid...
Definition: grid.cpp:328
void redo()
When the redo button is pressed the current state is pushed into the undo vector(oldStates) and the l...
Definition: grid.cpp:245
QColor getCellColor(int x, int y)
Returns the color of the cell in position (x,y)
Definition: grid.cpp:289
QString Tool
Definition: grid.h:448
void setTool(QString tool)
Sets the tool.
Definition: grid.cpp:109
void clear()
Clears the grid and sets it to its initial state.
Definition: grid.cpp:339
virtual void mousePressEvent(QMouseEvent *)
When the mouse is clicked on a cell of the grid it gets the x,y coordinates of the mouse and uses the...
Definition: grid.cpp:186
QVector< QHash< int, QColor > > newStates
Definition: grid.h:460
void setFront(QString front)
Sets the color of the brush.
Definition: grid.cpp:120
void square(int x, int y, QColor toolColor, int toolSize)
Receives the coordinates of the grid where the user clicked and paints a square of the size and with ...
Definition: tools.cpp:181
void setToolSize(int size)
Sets the size of the tool.
Definition: grid.cpp:142
void setBack(QString back)
Sets the color of the background.
Definition: grid.cpp:131
GridWidget(int rowCount=100, int columnCount=100, QWidget *parent=0)
Constructor which receives the number of rows and columns in the grid.
Definition: grid.cpp:85
void setCellSize(int size)
Sets the size of the cells in the grid.
Definition: grid.cpp:354
void RowMajorFill(int x, int y, QColor colorClicked, QColor toolColor)
Receives the coordinates of the grid where the user clicked and paints (from that point left and righ...
Definition: tools.cpp:40
Definition: grid.h:42
QColor background
Definition: grid.h:456
void identifyTool(QString tool, int x, int y)
This function is called on each mousePressEvent inside the grid. It identifies the way the grid will ...
Definition: grid.cpp:424
void undo()
When the undo button is pressed the current state is pushed into the redo vector(newStates) and the l...
Definition: grid.cpp:214
virtual void paintEvent(QPaintEvent *)
This function is automatically invoked each time the widget or its parent receives a repaint signal...
Definition: grid.cpp:170