No Description

grid.h 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #ifndef GRIDVIEW_H
  26. #define GRIDVIEW_H
  27. #include <QtGui>
  28. #include <QWidget>
  29. #include <QScrollArea>
  30. #include <QVector>
  31. class GridWidget;
  32. ///
  33. /// GridWidget:
  34. /// Shows a GridView without scrolling. If you want to have Scroll option, use
  35. /// GridView instead. GridWidget also supports mouse selection, sends the signal
  36. /// cellClicked with the associated grid coordinates.
  37. ///
  38. class GridWidget : public QWidget
  39. {
  40. Q_OBJECT
  41. public:
  42. /// \fn GridWidget::GridWidget(int rowCount, int columnCount, QWidget *parent):
  43. /// \~English
  44. /// \brief Constructor which receives the number of rows and columns in the grid
  45. /// \param rowCount number of rows in the grid
  46. /// \param columnCount number of columns in the grid
  47. /// \param parent parent window
  48. /// \~English
  49. /// \brief Constructor que recibe el numero de filas y columnas en la cuadricula
  50. /// \param rowCount numero de filas en la cuadricula
  51. /// \param columnCount numero de columnas en la cuadricula
  52. /// \param parent ventana padre
  53. explicit GridWidget(int rowCount = 100, int columnCount =100,QWidget *parent = 0);
  54. /// \fn GridWidget::GridWidget(QWidget *parent)
  55. /// \~English
  56. /// \brief Default constructor. The properties of the grid are set as follows:
  57. ///* mCellSize: Sets the size of the cell to 10px
  58. ///* mRowCount: Sets the number of rows in the grid to 39
  59. ///* mColumnCount: Sets the number of columns in the grid to 27
  60. ///* Tool and ToolSize: Sets the Tool which is going to be used to draw the grid
  61. ///* frontColor: The color which will be used to paint to black
  62. ///* backColor: Current clicked square
  63. ///* background: The background color to white
  64. /// \~English
  65. /// \brief Constructor por defecto. Las propiedades de el grid se ajustan como sigue:
  66. /// * mCellSize: Ajusta el tamano de la celda a 10px
  67. /// * mRowCount: Ajusta el numero de filas en la cuadricula a 39
  68. /// * ColumnCount: Ajusta el numero de columnas en el grid a 27
  69. /// * Tool and ToolSize: Ajusta la herramienta que se va a utilizar para dibujar
  70. /// el grid.
  71. /// * frontColor: El color que se utilizara para pintar en negro
  72. /// * backColor: Cuadrado marcado actualmente
  73. /// * background: El color del fondo en blanco
  74. GridWidget(QWidget *parent);
  75. /// \fn void GridWidget::setGridSize(int rowCount, int columnCount)
  76. /// \~English
  77. /// \brief Sets the number of columns and rows of the grid
  78. /// \param rowCount number of rows
  79. /// \param columnCount number of columns
  80. /// \~Spanish
  81. /// \brief Ajusta el numero de columnas y filas de la cuadricula
  82. /// \param rowCount numero de filas
  83. /// \param columnCount numero de columnas
  84. void setGridSize(int rowCount, int columnCount);
  85. /// \fn void GridWidget::switchOn(int x, int y, const QColor &color)
  86. /// \~English
  87. /// \brief Saves the given color and position in the vector
  88. /// that represents the painted cells of the grid
  89. /// \param x coordinate x of the cell in the grid
  90. /// \param y coordinate y of the cell in the grid
  91. /// \param color color to paint cell
  92. /// \~Spanish
  93. /// \brief Guarda el color y la posicion dados en el vector que
  94. /// representa las celdas pintadas en la cuadricula
  95. /// \param x coordenada x de la celda en el cuadricula
  96. /// \param y coordenada y de la celda en la cuadricula
  97. /// \param color color to paint cell
  98. void switchOn(int x,int y, const QColor& color);
  99. /// \fn QColor GridWidget::getCellColor(int x, int y) ;
  100. /// \~English
  101. /// \brief Returns the color of the cell in position (x,y)
  102. /// \param x coordinate x of the cell in the grid
  103. /// \param y coordinate y of the cell in the grid
  104. /// \return the color of the cell
  105. /// \~Spanish
  106. /// \brief Devuelve el color de la celda en la posicion (x,y)
  107. /// \param x coordenada x de la celda en el cuadricula
  108. /// \param y coordenada y de la celda en la cuadricula
  109. /// \return el color de la celda
  110. QColor getCellColor(int x, int y) ;
  111. /// \fn void GridWidget::switchOff(int x, int y)
  112. /// \~English
  113. /// \brief Removes the given position from the vector that
  114. /// represents the painted cells of the grid
  115. /// \param x coordinate x of the cell in the grid
  116. /// \param y coordinate y of the cell in the grid
  117. /// \~Spanish
  118. /// \brief Elimina la posicion dada del vector que representa
  119. /// las celdas pintadas en la cuadricula
  120. /// \param x coordenada x de la celda en el cuadricula
  121. /// \param y coordenada y de la celda en la cuadricula
  122. void switchOff(int x, int y);
  123. /// \fn int GridWidget::getGridColumns() ;
  124. /// \~English
  125. /// \brief Returns the number of columns in the grid
  126. /// \return number of columns in the grid
  127. /// \~Spanish
  128. /// \brief Devuelve el numero de columnas en la cuadricula
  129. /// \return el numero de columnas en la cuadricula
  130. int getGridColumns() ;
  131. /// \fn int GridWidget::getGridRows() ;
  132. /// \~English
  133. /// \brief Returns the number of rows in the grid
  134. /// \return number of rows in the grid
  135. /// \~Spanish
  136. /// \brief Devuelve el numero de filas en la cuadricula
  137. /// \return el numero de filas en la cuadricula
  138. int getGridRows() ;
  139. /// \fn void GridWidget::clear()
  140. /// \~English
  141. /// \brief Clears the grid and sets it to its initial state
  142. /// \~Spanish
  143. /// \brief Limpia la cuadricula y la pone en su estado inicial.
  144. void clear();
  145. /// \fn void GridWidget::setCellSize(int size)
  146. /// \~English
  147. /// \brief Sets the size of the cells in the grid
  148. /// \param size cell size
  149. /// \~Spanish
  150. /// \brief Ajusta el tamano de las celdas de la cuadricula
  151. /// \param size tamano de la celda
  152. void setCellSize(int size);
  153. /// \fn void GridWidget::setTool(QString tool)
  154. /// \~English
  155. /// \param tool chosen tool
  156. /// \brief Sets the tool
  157. /// \~Spanish
  158. /// \param tool herramienta escogida
  159. /// \brief Ajusta la herramienta
  160. void setTool(QString tool);
  161. /// \fn void GridWidget::setFront(QString front)
  162. /// \~English
  163. /// \brief Sets the color of the brush
  164. /// \param front brush color
  165. /// \~Spanish
  166. /// \brief Ajusta el color de la brocha
  167. /// \param front color de la brocha
  168. void setFront(QString front);
  169. /// \fn void GridWidget::setBack(QString back)
  170. /// \~English
  171. /// \brief Sets the color of the background
  172. /// \param back background color
  173. /// \~Spanish
  174. /// \brief Ajusta el color del fondo
  175. /// \param back color para el fondo
  176. void setBack(QString back);
  177. /// \fn void GridWidget::setToolSize(int size)
  178. /// \~English
  179. /// \brief Sets the size of the tool
  180. /// \param size tool size
  181. /// \~Spanish
  182. /// \brief Ajusta el tamano de la herramienta
  183. /// \param size tamano de la herramienta
  184. void setToolSize(int size);
  185. /// \fn void GridWidget::identifyTool(QString tool, int x, int y)
  186. /// \~English
  187. /// \brief This function is called on each mousePressEvent inside the grid.
  188. /// It identifies the way the grid will be painted calling the function of the
  189. /// tool received in the parameters (dot, rowfill, column fill, diagonal, square,
  190. /// triangles and circles).
  191. /// \param tool the tool to be called by the function
  192. /// \param x coordinate x of the cell in the grid.
  193. /// \param y coordinate y of the cell in the grid.
  194. /// \~Spanish
  195. /// \brief Esta funcion es llamada en cada evento del mouse presionado dentro
  196. /// de la cuadricula. Identifica la forma en que el grid va a ser pintado llamando
  197. /// la funcion del tool recibido en los parametros (dot, rowfill, column fill, diagonal, square,
  198. /// triangles and circles).
  199. /// \param tool la herramienta a llamar por la funcion
  200. /// \param x coordenada x de la celda en la cuadricula
  201. /// \param y coordenada y de la celde en la cuadricula
  202. void identifyTool(QString tool, int x, int y);
  203. /// \fn void GridWidget::Dot(int x, int y, QColor toolColor)
  204. /// \~English
  205. /// \brief Receives the coordinates of the grid where the user clicked
  206. /// and paints that cell with the color of the tool.
  207. /// \param x coordinate x of the cell in the grid
  208. /// \param y coordinate y of the cell in the grid
  209. /// \param toolColor color of the cells painted by the tool
  210. /// \~Spanish
  211. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  212. /// y pinta la celda con el color del tool
  213. /// \param x coordenada x de la celda en la cuadricula
  214. /// \param y coordenada y de la celda en la cuadricula
  215. /// \param toolColor color de las celdas pintadas por el tool
  216. void Dot(int x, int y, QColor colorSelected);
  217. /// \fn void GridWidget::RowMajorFill(int x, int y, QColor colorClicked, QColor toolColor, int cols)
  218. /// \~English
  219. /// \brief Receives the coordinates of the grid where the user clicked
  220. /// and paints (from that point left and right) the longest row of the same color
  221. /// of the cell that was clicked with the color of the tool.
  222. /// \param x coordinate x of the cell in the grid
  223. /// \param y coordinate y of the cell in the grid
  224. /// \param colorClicked current color of the clicked cell
  225. /// \param toolColor color of the cells painted by the tool
  226. /// \~Spanish
  227. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  228. /// y pinta (desde ese punto hacia la izq o derecha) la fila mas larga del mismo color
  229. /// de la celda que fue marcada con el color de la herramienta.
  230. /// \param x coordenada x de la celda en la cuadricula
  231. /// \param y coordenada y de la celda en la cuadricula
  232. /// \param colorClicked el color de la celda marcada
  233. /// \param toolColor color de las celdas pintadas por el tool
  234. void RowMajorFill(int x, int y, QColor colorClicked, QColor toolColor);
  235. /// \fn void GridWidget::ColMajorFill(int x, int y, QColor colorClicked, QColor toolColor)
  236. /// \~English
  237. /// \brief Receives the coordinates of the grid where the user clicked
  238. /// and paints (from that point up and down) the longest column of the same color
  239. /// of the cell that was clicked with the color of the tool.
  240. /// \param x coordinate x of the cell in the grid
  241. /// \param y coordinate y of the cell in the grid
  242. /// \param colorClicked current color of the clicked cell
  243. /// \param toolColor color of the cells painted by the tool
  244. /// \~Spanish
  245. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  246. /// y pinta (desde ese punto hacia arriba o abajo) la columna mas larga del mismo color
  247. /// de la celda que fue marcada con el color de la herramienta.
  248. /// \param x coordenada x de la celda en la cuadricula
  249. /// \param y coordenada y de la celda en la cuadricula
  250. /// \param colorClicked el color de la celda marcada
  251. /// \param toolColor color de las celdas pintadas por el tool
  252. void ColMajorFill(int x, int y, QColor colorClicked, QColor toolColor);
  253. /// \fn void GridWidget::DiagonalLeft(int x, int y, QColor colorClicked, QColor toolColor)
  254. /// \~English
  255. /// \brief Recieves the coordinates of the grid where the user clicked
  256. /// and paints (from that point) the longest left-diagonal of the color
  257. /// of the cell that was clicked with the color of the tool.
  258. /// \param x coordinate x of the cell in the grid
  259. /// \param y coordinate y of the cell in the grid
  260. /// \param colorClicked current color of the clicked cell
  261. /// \param toolColor color of the cells painted by the tool
  262. /// \~Spanish
  263. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  264. /// y pinta (desde ese punto diagonal) la diagonal izquierda mas larga del mismo color
  265. /// de la celda que fue marcada con el color de la herramienta.
  266. /// \param x coordenada x de la celda en la cuadricula
  267. /// \param y coordenada y de la celda en la cuadricula
  268. /// \param colorClicked el color de la celda marcada
  269. /// \param toolColor color de las celdas pintadas por el tool
  270. void DiagonalLeft(int x, int y, QColor colorClicked, QColor toolColor);
  271. /// \fn void GridWidget::DiagonalRight(int x, int y, QColor colorClicked, QColor toolColor)
  272. /// \~English
  273. /// \brief Recieves the coordinates of the grid where the user clicked
  274. /// and paints (from that point) the longest right-diagonal of the color
  275. /// of the cell that was clicked with the color of the tool.
  276. /// \param x coordinate x of the cell in the grid
  277. /// \param y coordinate y of the cell in the grid
  278. /// \param colorClicked current color of the clicked cell
  279. /// \param toolColor color of the cells painted by the tool
  280. /// \~Spanish
  281. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  282. /// y pinta (desde ese punto diagonal) la diagonal derecha mas larga del mismo color
  283. /// de la celda que fue marcada con el color de la herramienta.
  284. /// \param x coordenada x de la celda en la cuadricula
  285. /// \param y coordenada y de la celda en la cuadricula
  286. /// \param colorClicked el color de la celda marcada
  287. /// \param toolColor color de las celdas pintadas por el tool
  288. void DiagonalRight(int x, int y, QColor colorClicked, QColor toolColor);
  289. /// \fn void GridWidget::square(int x, int y, QColor toolColor, int toolSize)
  290. /// \~English
  291. /// \brief Receives the coordinates of the grid where the user clicked
  292. /// and paints a square of the size and with the color of the tool.
  293. /// \param x coordinate x of the cell in the grid
  294. /// \param y coordinate y of the cell in the grid
  295. /// \param toolColor color of the cells painted by the tool
  296. /// \param toolSize size of the tool to be painted
  297. /// \~Spanish
  298. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  299. /// y pinta un cuadrado de el tamano y color de la herramienta.
  300. /// \param x coordenada x de la celda en la cuadricula
  301. /// \param y coordenada y de la celda en la cuadricula
  302. /// \param toolColor color de las celdas pintadas por el tool
  303. /// \param toolSize tamano de la herramienta a ser pintada
  304. void square(int x, int y, QColor toolColor, int toolSize);
  305. /// \fn void GridWidget::triangle(int x, int y, QColor toolColor, int toolSize)
  306. /// \~English
  307. /// \brief Receives the coordinates of the grid where the user clicked
  308. /// and paints a triangle of the size and with the color of the tool.
  309. /// \param x coordinate x of the cell in the grid
  310. /// \param y coordinate y of the cell in the grid
  311. /// \param toolColor color of the cells painted by the tool
  312. /// \param toolSize size of the tool to be painted
  313. /// \~Spanish
  314. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  315. /// y pinta un triangulo de el tamano y color de la herramienta.
  316. /// \param x coordenada x de la celda en la cuadricula
  317. /// \param y coordenada y de la celda en la cuadricula
  318. /// \param toolColor color de las celdas pintadas por el tool
  319. /// \param toolSize tamano de la herramienta a ser pintada
  320. void triangle(int x, int y, QColor toolColor, int toolSize);
  321. /// \fn void GridWidget::circle(int x, int y, QColor toolColor, int toolSize)
  322. /// \~English
  323. /// \brief Receives the coordinates of the grid where the user clicked
  324. /// and paints a circle of the size and with the color of the tool.
  325. /// \param x coordinate x of the cell in the grid
  326. /// \param y coordinate y of the cell in the grid
  327. /// \param toolColor color of the cells painted by the tool
  328. /// \param toolSize size of the tool to be painted
  329. /// \~Spanish
  330. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco
  331. /// y pinta un circulo de el tamano y color de la herramienta.
  332. /// \param x coordenada x de la celda en la cuadricula
  333. /// \param y coordenada y de la celda en la cuadricula
  334. /// \param toolColor color de las celdas pintadas por el tool
  335. /// \param toolSize tamano de la herramienta a ser pintada
  336. void circle(int x, int y, QColor toolColor, int toolSize);
  337. /// \fn void GridWidget::undo()
  338. /// \~English
  339. /// \brief When the undo button is pressed the current state is pushed into
  340. /// the redo vector(newStates) and the last state in the undo vector(oldStates)
  341. /// is painted on the grid.
  342. /// \~Spanish
  343. /// \brief Cuando el boton de deshacer (undo) es presionado el estado actual es empujado
  344. /// al vector(newStates) de rehacer (redo) y el ultimo estado del vector(oldStates) de
  345. /// deshacer (undo) es pintado en la cuadricula
  346. void undo();
  347. /// \fn void GridWidget::redo()
  348. /// \~English
  349. /// \brief When the redo button is pressed the current state is pushed into
  350. /// the undo vector(oldStates) and the last state in the redo vector(newStates)
  351. /// is painted on the grid.
  352. /// \~Spanish Cuando el boton redo es marcado el estado actual es empujado en
  353. /// el vector(oldStates) undo y el ultimo estado en el vector redo(newStates) es
  354. /// pintaod en el grid.
  355. void redo();
  356. /// \fn void GridWidget::flood_fill(int x, int y, QColor toolColor, QColor ColorClicked)
  357. /// \~English
  358. /// \brief Receives the coordinates of the grid where the user clicked
  359. /// and floods the grid with the color selected.
  360. /// \param x coordinate x of the cell in the grid
  361. /// \param y coordinate y of the cell in the grid
  362. /// \param toolColor color of the cell painted by the tool
  363. /// \param ColorClicked current color of the clicked cell
  364. /// \~Spanish
  365. /// \brief Recibe las coordenadas de la cuadricula donde el usuario marco e
  366. /// inunda la cuadricula del color seleccionado
  367. /// \param x coordenada x de la celda en la cuadricula
  368. /// \param y coordenada y de la celda en la cuadricula
  369. /// \param toolColor color de las celdas pintadas por el tool
  370. /// \param ColorClicked el color de la celda marcada
  371. void flood_fill(int x, int y, QColor toolcolor, QColor ColorClicked);
  372. protected:
  373. /// \fn void GridWidget::drawGrid(QPaintDevice *device)
  374. /// \~English
  375. /// \brief Function that first sets the size of the GridWidget, then paints the cells
  376. /// with the color selected for the background and finally paints
  377. /// the lines to form the grid.
  378. /// \param device the panel to paint the grid
  379. /// \~Spanish
  380. /// \brief Funcion que primero ajusta el tamano del widget de la cuadricula, luego pinta
  381. /// las celdas con el color seleccionado en el background y finalmente pinta las lineas
  382. /// para formar el grid.
  383. /// \param device el panel para pintar la cuadricula
  384. void drawGrid(QPaintDevice * device);
  385. /// \fn void GridWidget::paintEvent(QPaintEvent *event)
  386. /// \~English
  387. /// \brief This function is automatically invoked each time the widget or
  388. /// its parent receives a repaint signal.
  389. /// \param event received event reference
  390. /// \~Spanish
  391. /// \brief Esta funcion es invocada automaticmente cada vez que el widget o
  392. /// el padre recibe una senal de repintar.
  393. /// \param event referencia a un evento recibido
  394. virtual void paintEvent(QPaintEvent *);
  395. /// \fn void GridWidget::mousePressEvent(QMouseEvent * event)
  396. /// \~English
  397. /// \brief When the mouse is clicked on a cell of the grid it gets the x,y
  398. /// coordinates of the mouse and uses them to paint the tool at that
  399. /// location.
  400. /// \param event received event reference
  401. /// \~Spanish
  402. /// \brief Cuando el raton (mouse) is marcada en una celda de la cuadricula
  403. /// obtiene las coordenadas x, y del raton y los usa para pintar la herramienta en
  404. /// ese lugar.
  405. virtual void mousePressEvent(QMouseEvent *);
  406. signals:
  407. void cellClicked(QPoint pos);
  408. void canUndo(bool can);
  409. void canRedo(bool can);
  410. private:
  411. int mCellSize; /**< cell size / tamano del la celda*/
  412. int mRowCount; /**< row number / numero de filas*/
  413. int mColumnCount; /**< column number / numero de columnas */
  414. QPixmap mGridPix; /**< to paint the grid / para pintar la cuadricula */
  415. QHash<int, QColor > mColors; /**< Hash of colors with the painted cells and their colors
  416. / Hash de colores con las celdas pintadas y sus colores */
  417. QString Tool; /**< name of the tool selected / nombre de la herramienta seleccionada */
  418. int ToolSize; /**< tool size / tamano del a herramienta */
  419. QColor frontColor; /**< color of the front cell / color de la celda de frente */
  420. QColor backColor; /**< background color of the cell / color del fondo de la celda */
  421. QColor background; /**< cell size / tamano del la celda */
  422. QVector < QHash<int, QColor > > oldStates; /**< vetor to implement undo/redo */
  423. QVector < QHash<int, QColor > > newStates; /**< vector to implement undo/redo */
  424. };
  425. #endif // GRIDVIEW_H