qcross/libqnono/ccrosspackagelistmodel.cpp

98 lines
3.6 KiB
C++

/***************************************************************************
* 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 <QDir>
#include "ccrosspackage.h"
#include "cnonogram.h"
#include "ccrosspackagelistmodel.h"
namespace libqcross {
//public:
CCrossPackageListModel::CCrossPackageListModel(QString dataPath, QObject * parent) : QAbstractItemModel(parent),
m_DataPath(dataPath) {
update();
}
CCrossPackageListModel::~CCrossPackageListModel() {
foreach (CCrossPackage * i, m_PackageList)
delete i;
}
void CCrossPackageListModel::update() {
foreach (CCrossPackage * i, m_PackageList)
delete i;
m_PackageList.clear();
CCrossPackage * newPackage;
QDir workDir(m_DataPath);
QStringList fileNames = workDir.entryList(QStringList("*.cpk"), QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
foreach (QString fileName, fileNames) {
newPackage = new CCrossPackage(workDir.filePath(fileName));
if (newPackage->open())
m_PackageList << newPackage;
else
delete newPackage;
}
reset();
}
QModelIndex CCrossPackageListModel::index(int row, int column, const QModelIndex & /*parent*/) const {
return (column > 0) || (row < 0) || (row >= m_PackageList.size()) ?
QModelIndex() :
createIndex(row, column, static_cast<void *>(m_PackageList[row]));
}
QVariant CCrossPackageListModel::data(const QModelIndex & index, int role) const {
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole)
return static_cast<CCrossPackage *>(index.internalPointer())->name();
else
return QVariant();
}
int CCrossPackageListModel::rowCount(const QModelIndex & /*parent*/) const {
return m_PackageList.size();
}
int CCrossPackageListModel::columnCount(const QModelIndex & /*parent*/) const {
return 1;
}
QVariant CCrossPackageListModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const {
return QVariant();
}
// Qt::ItemFlags CCrossPackageListModel::flags(const QModelIndex & index) const {
// return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
// }
QModelIndex CCrossPackageListModel::parent(const QModelIndex &) const {
return QModelIndex();
}
bool CCrossPackageListModel::hasChildren(const QModelIndex & parent) const {
return !parent.isValid();
}
}