/*************************************************************************** * Copyright (C) 2008 by Oliver Groß * * z.o.gross@gmx.de * * * * 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 2 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, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include "ccrosspackage.h" #include "cnonogram.h" #include "ccrosspackagemodel.h" #include "constants.h" #define COL_NAME 0 #define COL_TIME 1 namespace libqnono { QString formatedTime(quint32 seconds, bool showSeconds = false) { quint32 strippedSeconds = seconds % 60; quint8 minutes = seconds / 60; quint8 hours = minutes / 60; QString result; if (hours) { if (hours < 10) result += '0'; result += QString::number(hours); result += ':'; } minutes %= 60; if (minutes < 10) result += '0'; result += QString::number(minutes); if (showSeconds) { result += ':'; if (strippedSeconds < 10) result += '0'; result += QString::number(strippedSeconds); } return result; } //public: CCrossPackageModel::CCrossPackageModel(QObject * parent) : QAbstractItemModel(parent), m_Package(NULL) { } CCrossPackageModel::~CCrossPackageModel() { setPackage(NULL); } void CCrossPackageModel::setPackage(libqnono::CCrossPackage * package) { if (m_Package) m_Package->pictures().clear(); m_Package = package; if (m_Package && m_Package->pictures().isEmpty()) m_Package->readAll(); reset(); } QModelIndex CCrossPackageModel::index(int row, int column, const QModelIndex & /*parent*/) const { return (m_Package) && (column >= 0) && (row >= 0) && (row < m_Package->pictures().size()) ? createIndex(row, column, static_cast(m_Package->pictures()[row])) : QModelIndex(); } QVariant CCrossPackageModel::data(const QModelIndex & index, int role) const { if (!index.isValid()) return QVariant(); switch (index.column()) { case COL_NAME: switch (role) { case Qt::DecorationRole: return static_cast(index.internalPointer())->timeout() ? QIcon(LIBQCROSS_ICON_TIMEOUT) : QIcon(LIBQCROSS_ICON_TIMETRIAL); case Qt::EditRole: case Qt::DisplayRole: return static_cast(index.internalPointer())->name(); default: break; } break; case COL_TIME: if (role == Qt::DisplayRole) return formatedTime(static_cast(index.internalPointer())->timeout()); break; default: break; } return QVariant(); } int CCrossPackageModel::rowCount(const QModelIndex & parent) const { if (m_Package && !parent.isValid()) return m_Package->pictures().size(); else return 0; } int CCrossPackageModel::columnCount(const QModelIndex & /*parent*/) const { return 2; } QVariant CCrossPackageModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const { if (role == Qt::DisplayRole) { switch (section) { case COL_NAME: return tr("Nonogram"); case COL_TIME: return tr("Time"); default: break; } } return QVariant(); } Qt::ItemFlags CCrossPackageModel::flags(const QModelIndex & index) const { if (index.column() == COL_NAME) return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; else return QAbstractItemModel::flags(index); } QModelIndex CCrossPackageModel::parent(const QModelIndex &) const { return QModelIndex(); } bool CCrossPackageModel::hasChildren(const QModelIndex & parent) const { return m_Package && !parent.isValid(); } bool CCrossPackageModel::setData(const QModelIndex & index, const QVariant & value, int role) { if (index.isValid() && role == Qt::EditRole) { QString name = value.toString(); if (name.isEmpty()) return false; foreach (CNonogram * i, m_Package->pictures()) { if (i->name() == name) return false; } m_Package->pictures()[index.row()]->setName(name); emit dataChanged(index, index); return true; } return false; } bool CCrossPackageModel::appendEmpty(QString name, QSize size, bool fillValue) { if (!m_Package) return false; beginInsertRows(QModelIndex(), m_Package->pictures().size(), 1); CNonogram * newNonogram = new CNonogram(size); newNonogram->fill(fillValue); newNonogram->setName(name); m_Package->pictures() << newNonogram; endInsertRows(); return true; } bool CCrossPackageModel::appendImage(QString name, QImage image) { if (!m_Package) return false; CNonogram * newNonogram = new CNonogram(); newNonogram->loadFromImage(image); newNonogram->setName(name); newNonogram->setTimeout(60*60); // set an hour as default timeout if (newNonogram->isValid()) { beginInsertRows(QModelIndex(), m_Package->pictures().size(), 1); m_Package->pictures() << newNonogram; endInsertRows(); return true; } else { delete newNonogram; return false; } } bool CCrossPackageModel::insertRows(int row, int count, const QModelIndex & parent) { if (!m_Package) return false; beginInsertRows(parent, row, count); CNonogram * newNonogram; for (int i = row; i < row+count; ++i) { newNonogram = new CNonogram(); newNonogram->fill(false); m_Package->pictures() << newNonogram; } endInsertRows(); return true; } bool CCrossPackageModel::removeRows(int row, int count, const QModelIndex & parent) { if (!m_Package) return false; beginRemoveRows(parent, row, row+count-1); for (int i = row; i < row+count; ++i) delete m_Package->pictures().takeAt(i); endRemoveRows(); return true; } }