diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-29 22:09:30 -0400 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-29 22:09:30 -0400 |
| commit | 58deb5cda212e8a0649f43efafa0b73652930ed7 (patch) | |
| tree | 16ee98a15adf537fb50e43fe87cfd5f55f9ccee4 | |
| parent | 66a3bc958e0f32df7157a7ef7ccca0d945620622 (diff) | |
Add a class to draw a grid on the map
| -rw-r--r-- | sigmodr/widgets/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/MapEditor.cpp | 15 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/MapEditor.h | 6 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/MapGrid.cpp | 68 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/MapGrid.h | 49 |
5 files changed, 132 insertions, 7 deletions
diff --git a/sigmodr/widgets/CMakeLists.txt b/sigmodr/widgets/CMakeLists.txt index da2d2cb0..e04e16f7 100644 --- a/sigmodr/widgets/CMakeLists.txt +++ b/sigmodr/widgets/CMakeLists.txt @@ -79,6 +79,7 @@ set(sigmodrwidgets_SRCS WeatherUI.cpp mapeditor/EffectItem.cpp mapeditor/MapEditor.cpp + mapeditor/MapGrid.cpp mapeditor/MapItem.cpp mapeditor/MapScene.cpp mapeditor/TileItem.cpp diff --git a/sigmodr/widgets/mapeditor/MapEditor.cpp b/sigmodr/widgets/mapeditor/MapEditor.cpp index f215d585..002e29c2 100644 --- a/sigmodr/widgets/mapeditor/MapEditor.cpp +++ b/sigmodr/widgets/mapeditor/MapEditor.cpp @@ -19,6 +19,7 @@ #include "MapEditor.h" // Sigmodr widget includes +#include "MapGrid.h" #include "MapScene.h" // Sigmod includes @@ -35,7 +36,6 @@ #include <QtCore/QFile> #include <QtCore/QSignalMapper> #include <QtGui/QCheckBox> -#include <QtGui/QGraphicsRectItem> #include <QtGui/QGraphicsView> #include <QtGui/QVBoxLayout> #include <QtUiTools/QUiLoader> @@ -97,8 +97,8 @@ void MapEditor::setMap(Map* map) void MapEditor::reset() { m_scene = new MapScene(m_map, this); - m_rect = new QGraphicsRectItem; - m_rect->setRect(0, 0, m_map->width(), m_map->height()); + m_rect = new MapGrid; + m_rect->setSize(m_map->width(), m_map->height()); m_scene->addItem(m_rect); ui_view->setScene(m_scene); ui_width->setValue(m_map->width()); @@ -109,17 +109,22 @@ void MapEditor::reset() void MapEditor::setMapWidth(const int width) { m_map->setWidth(width); - m_rect->setRect(0, 0, m_map->width(), m_map->height()); + m_rect->setSize(m_map->width(), m_map->height()); ui_view->setSceneRect(-10, -10, m_map->width() + 10, m_map->height() + 10); } void MapEditor::setMapHeight(const int height) { m_map->setHeight(height); - m_rect->setRect(0, 0, m_map->width(), m_map->height()); + m_rect->setSize(m_map->width(), m_map->height()); ui_view->setSceneRect(-10, -10, m_map->width() + 10, m_map->height() + 10); } +void MapEditor::setGridSize(const int gridSize) +{ + m_rect->setGridSize(gridSize); +} + void MapEditor::makeConnections() { connect(m_scene, SIGNAL(changed()), this, SIGNAL(changed())); diff --git a/sigmodr/widgets/mapeditor/MapEditor.h b/sigmodr/widgets/mapeditor/MapEditor.h index 2baf4932..c0df096c 100644 --- a/sigmodr/widgets/mapeditor/MapEditor.h +++ b/sigmodr/widgets/mapeditor/MapEditor.h @@ -25,7 +25,6 @@ #include <QtGui/QWidget> // Forward declarations -class QGraphicsRectItem; class QCheckBox; class QGraphicsView; class KComboBox; @@ -40,6 +39,7 @@ namespace Sigmodr { namespace Widgets { +class MapGrid; class MapScene; class SIGMODRWIDGETS_NO_EXPORT MapEditor : public QWidget @@ -62,12 +62,14 @@ class SIGMODRWIDGETS_NO_EXPORT MapEditor : public QWidget protected slots: void setMapWidth(const int width); void setMapHeight(const int height); + + void setGridSize(const int gridSize); protected: void makeConnections(); private: Sigmod::Map* m_map; MapScene* m_scene; - QGraphicsRectItem* m_rect; + MapGrid* m_rect; KIntNumInput* ui_width; KIntNumInput* ui_height; diff --git a/sigmodr/widgets/mapeditor/MapGrid.cpp b/sigmodr/widgets/mapeditor/MapGrid.cpp new file mode 100644 index 00000000..d7b19718 --- /dev/null +++ b/sigmodr/widgets/mapeditor/MapGrid.cpp @@ -0,0 +1,68 @@ +/* + * Copyright 2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "MapGrid.h" + +// Qt includes +#include <QtGui/QPainter> + +using namespace Sigmodr::Widgets; + +MapGrid::MapGrid() : + m_width(0), + m_height(0), + m_gridSize(25) +{ +} + +QRectF MapGrid::boundingRect() const +{ + return QRect(0, 0, m_width, m_height); +} + +void MapGrid::paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget* widget) +{ + Q_UNUSED(options) + Q_UNUSED(widget) + painter->drawRect(boundingRect()); + if (m_gridSize) + { + painter->setOpacity(.5); + for (int i = 0; i < m_width; i += m_gridSize) + painter->drawLine(i, -5, i, m_height + 5); + painter->drawLine(m_width, -5, m_width, m_height + 5); + for (int i = 0; i < m_height; i += m_gridSize) + painter->drawLine(-5, i, m_width + 5, i); + painter->drawLine(-5, m_height, m_width + 5, m_height); + painter->setOpacity(1); + } +} + +void MapGrid::setSize(const int width, const int height) +{ + m_width = width; + m_height = height; + prepareGeometryChange(); + update(); +} + +void MapGrid::setGridSize(const int gridSize) +{ + m_gridSize = gridSize; + update(); +} diff --git a/sigmodr/widgets/mapeditor/MapGrid.h b/sigmodr/widgets/mapeditor/MapGrid.h new file mode 100644 index 00000000..43a79afe --- /dev/null +++ b/sigmodr/widgets/mapeditor/MapGrid.h @@ -0,0 +1,49 @@ +/* + * Copyright 2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGMODRWIDGETS_MAPGRID +#define SIGMODRWIDGETS_MAPGRID + +// Sigmodr widget includes +#include "../Global.h" + +// Qt includes +#include <QtGui/QGraphicsItem> + +namespace Sigmodr +{ +namespace Widgets +{ +class SIGMODRWIDGETS_NO_EXPORT MapGrid : public QGraphicsItem +{ + public: + MapGrid(); + + QRectF boundingRect() const; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget* widget); + + void setSize(const int width, const int height); + void setGridSize(const int gridSize); + private: + int m_width; + int m_height; + int m_gridSize; +}; +} +} + +#endif |
