qcross/libqnono/ccrosspackagemodel.cpp

237 lines
6.4 KiB
C++
Raw Normal View History

2010-06-16 12:53:56 +00:00
/***************************************************************************
* 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 <QImage>
#include <QIcon>
#include "ccrosspackage.h"
#include "cnonogram.h"
#include "ccrosspackagemodel.h"
#include "constants.h"
#define COL_NAME 0
#define COL_TIME 1
namespace libqnono {
2010-06-16 12:53:56 +00:00
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) {
2010-06-16 12:53:56 +00:00
if (m_Package)
m_Package->unloadPictures();
2010-06-16 12:53:56 +00:00
m_Package = package;
if (m_Package)
m_Package->loadPictures();
2010-06-16 12:53:56 +00:00
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<void *>(&m_Package->pictures()[row])) :
2010-06-16 12:53:56 +00:00
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<CNonogram *>(index.internalPointer())->timeout() ?
QIcon(LIBQCROSS_ICON_TIMEOUT) :
QIcon(LIBQCROSS_ICON_TIMETRIAL);
case Qt::EditRole:
case Qt::DisplayRole:
return static_cast<CNonogram *>(index.internalPointer())->name();
default:
break;
}
break;
case COL_TIME:
if (role == Qt::DisplayRole)
return formatedTime(static_cast<CNonogram *>(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)
2010-06-16 12:53:56 +00:00
return false;
}
m_Package->pictures()[index.row()].setName(name);
2010-06-16 12:53:56 +00:00
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);
2010-06-16 12:53:56 +00:00
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));
2010-06-16 12:53:56 +00:00
if (!newNonogram.size().isEmpty()) {
2010-06-16 12:53:56 +00:00
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());
2010-06-16 12:53:56 +00:00
}
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);
2010-06-16 12:53:56 +00:00
endRemoveRows();
return true;
}
}