summaryrefslogtreecommitdiffstats
path: root/sigmodr/TilemapModel.h
diff options
context:
space:
mode:
Diffstat (limited to 'sigmodr/TilemapModel.h')
-rw-r--r--sigmodr/TilemapModel.h171
1 files changed, 171 insertions, 0 deletions
diff --git a/sigmodr/TilemapModel.h b/sigmodr/TilemapModel.h
new file mode 100644
index 00000000..64313ae5
--- /dev/null
+++ b/sigmodr/TilemapModel.h
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2008 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 __POKEMODR_TILEMAPMODEL__
+#define __POKEMODR_TILEMAPMODEL__
+
+// Pokemod includes
+#include "../pokemod/Matrix.h"
+#include "../pokemod/Pokemod.h"
+#include "../pokemod/Sprite.h"
+#include "../pokemod/Tile.h"
+
+// Qt includes
+#include <QtCore/QAbstractTableModel>
+#include <QtCore/QVariant>
+#include <QtGui/QPixmap>
+
+namespace Pokemodr
+{
+class TilemapModel : public QAbstractTableModel
+{
+ Q_OBJECT
+
+ public:
+ TilemapModel(Pokemod::Matrix<int>* tilemap, const Pokemod::Pokemod* pokemod);
+
+ QVariant data(const QModelIndex& index, int role) const;
+
+ QVariant headerData(int section, Qt::Orientation /*orientation*/, int role = Qt::DisplayRole) const;
+
+ int rowCount(const QModelIndex& /*parent*/) const;
+ int columnCount(const QModelIndex& /*parent*/) const;
+
+ bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
+ bool insertColumns(int column, int count, const QModelIndex& parent = QModelIndex());
+ bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
+ bool removeColumns(int column, int count, const QModelIndex& parent = QModelIndex());
+
+ Qt::ItemFlags flags(const QModelIndex& index) const;
+
+ bool setData(const QModelIndex& index, const QVariant& value, int role);
+ private:
+ Pokemod::Matrix<int>& m_tilemap;
+ const Pokemod::Pokemod* m_pokemod;
+};
+
+inline TilemapModel::TilemapModel(Pokemod::Matrix<int>* tilemap, const Pokemod::Pokemod* pokemod) :
+ QAbstractTableModel(),
+ m_tilemap(*tilemap),
+ m_pokemod(pokemod)
+{
+}
+
+inline QVariant TilemapModel::data(const QModelIndex& index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+ if (role == Qt::DisplayRole)
+ {
+ const int tileIndex = m_pokemod->tileIndex(m_tilemap(index.row(), index.column()));
+ if (tileIndex != INT_MAX)
+ {
+ const Pokemod::Tile* tile = m_pokemod->tile(tileIndex);
+ if (m_pokemod->spriteIndex(tile->sprite()) == INT_MAX)
+ return QPixmap(64, 64);
+ else
+ return QPixmap::fromImage(m_pokemod->spriteById(tile->sprite())->sprite());
+ }
+ }
+ else if (role == Qt::EditRole)
+ return m_tilemap.at(index.row(), index.column());
+ return QVariant();
+}
+
+inline QVariant TilemapModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
+{
+ if (role == Qt::DisplayRole)
+ return section;
+ return QVariant();
+}
+
+inline int TilemapModel::rowCount(const QModelIndex& /*parent*/) const
+{
+ return m_tilemap.height();
+}
+
+inline int TilemapModel::columnCount(const QModelIndex& /*parent*/) const
+{
+ return m_tilemap.width();
+}
+
+inline bool TilemapModel::insertRows(int row, int count, const QModelIndex& parent)
+{
+ emit(beginInsertRows(parent, row, row + count - 1));
+ for (int i = 0; i < count; ++i)
+ m_tilemap.insertRow(row);
+ emit(endInsertRows());
+ if (m_tilemap.height() == 1)
+ reset();
+ return true;
+}
+
+inline bool TilemapModel::insertColumns(int column, int count, const QModelIndex& parent)
+{
+ emit(beginInsertColumns(parent, column, column + count - 1));
+ for (int i = 0; i < count; ++i)
+ m_tilemap.insertColumn(column);
+ emit(endInsertColumns());
+ if (m_tilemap.width() == 1)
+ reset();
+ return true;
+}
+
+inline bool TilemapModel::removeRows(int row, int count, const QModelIndex& parent)
+{
+ emit(beginRemoveRows(parent, row, row + count - 1));
+ for (int i = 0; i < count; ++i)
+ m_tilemap.deleteRow(row);
+ emit(endRemoveRows());
+ if (!m_tilemap.height())
+ reset();
+ return true;
+}
+
+inline bool TilemapModel::removeColumns(int column, int count, const QModelIndex& parent)
+{
+ emit(beginRemoveColumns(parent, column, column + count - 1));
+ for (int i = 0; i < count; ++i)
+ m_tilemap.deleteColumn(column);
+ emit(endRemoveColumns());
+ if (!m_tilemap.width())
+ reset();
+ return true;
+}
+
+inline Qt::ItemFlags TilemapModel::flags(const QModelIndex& index) const
+{
+ if (!index.isValid())
+ return 0;
+ return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+inline bool TilemapModel::setData(const QModelIndex& index, const QVariant& value, int role)
+{
+ if (!index.isValid())
+ return false;
+ if (role == Qt::EditRole)
+ {
+ m_tilemap(index.row(), index.column()) = value.toInt();
+ emit(dataChanged(index, index));
+ return true;
+ }
+ return false;
+}
+}
+
+#endif