/* * Copyright 2008 Ben Boeckel * * 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 . */ // Header include #include "TilemapModel.h" // Sigmod includes #include "../sigmod/Sigmod.h" #include "../sigmod/Sprite.h" #include "../sigmod/Tile.h" // Qt includes #include Sigmodr::TilemapModel::TilemapModel(Sigmod::Matrix* tilemap, const Sigmod::Sigmod* sigmod) : QAbstractTableModel(), m_tilemap(*tilemap), m_sigmod(sigmod) { } QVariant Sigmodr::TilemapModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::DisplayRole) { const int tileIndex = m_sigmod->tileIndex(m_tilemap(index.row(), index.column())); if (tileIndex != INT_MAX) { const Sigmod::Tile* tile = m_sigmod->tile(tileIndex); if (m_sigmod->spriteIndex(tile->sprite()) == INT_MAX) return QPixmap(); else { QPixmap icon; icon.loadFromData(m_sigmod->spriteById(tile->sprite())->sprite()); return icon; } } } else if (role == Qt::EditRole) return m_tilemap(index.row(), index.column()); return QVariant(); } QVariant Sigmodr::TilemapModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const { if (role == Qt::DisplayRole) return section; return QVariant(); } int Sigmodr::TilemapModel::rowCount(const QModelIndex& /*parent*/) const { return m_tilemap.height(); } int Sigmodr::TilemapModel::columnCount(const QModelIndex& /*parent*/) const { return m_tilemap.width(); } bool Sigmodr::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; } bool Sigmodr::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; } bool Sigmodr::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; } bool Sigmodr::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; } Qt::ItemFlags Sigmodr::TilemapModel::flags(const QModelIndex& index) const { if (!index.isValid()) return 0; return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } bool Sigmodr::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; }