/*************************************************************************** * 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 "ccrosspackagemodel.h" #include "nonogramproblem.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) { result = QString("%1:").arg(hours, 2, 10, QLatin1Char('0')); } minutes %= 60; result += QString("%1").arg(minutes, 2, 10, QLatin1Char('0')); if (showSeconds) { result += QString(":%1").arg(strippedSeconds, 2, 10, QLatin1Char('0')); } 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->unloadPictures(); m_Package = package; if (m_Package) m_Package->loadPictures(); 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 (const NonogramProblem & 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); NonogramProblem newNonogram(name, 60*60, NonogramImage(size)); newNonogram.fill(fillValue); m_Package->pictures() << newNonogram; endInsertRows(); return true; } bool CCrossPackageModel::appendImage(QString name, QImage image) { if (!m_Package) return false; NonogramProblem newNonogram(name, 60*60, NonogramImage(image)); if (!newNonogram.size().isEmpty()) { beginInsertRows(QModelIndex(), m_Package->pictures().size(), 1); m_Package->pictures() << newNonogram; endInsertRows(); return true; } else { return false; } } bool CCrossPackageModel::insertRows(int row, int count, const QModelIndex & parent) { if (!m_Package) return false; beginInsertRows(parent, row, count); for (int i = row; i < row+count; ++i) { m_Package->pictures().push_back(NonogramProblem()); } 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) m_Package->pictures().removeAt(i); endRemoveRows(); return true; } }