diff --git a/src/model/danboorupoolmodel.cpp b/src/model/danboorupoolmodel.cpp new file mode 100644 index 0000000..9378a13 --- /dev/null +++ b/src/model/danboorupoolmodel.cpp @@ -0,0 +1,122 @@ +/* + * Copyright 2015 Luca Beltrame + * + * This file is part of Danbooru Client. + * + * Danbooru Client 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. + * + * Danbooru Client 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 Danbooru Client. If not, see . + */ + +#include "danboorupoolmodel.h" +#include "libdanbooru/danboorupool.h" + +#include + +#include + +namespace Danbooru { + +DanbooruPoolModel::DanbooruPoolModel(QObject* parent): QAbstractTableModel(parent) +{ +} + +const QStringList DanbooruPoolModel::m_headerNames = { i18n("ID"), + i18n("Name"), i18n("Posts"), i18n("Description") }; + +DanbooruPoolModel::~DanbooruPoolModel() +{ + if (!m_items.isEmpty()) { + qDeleteAll(m_items); + m_items.clear(); + } +} + +void DanbooruPoolModel::addPool(DanbooruPool *pool) +{ + beginInsertRows(QModelIndex(), m_items.size(), m_items.size()); + qDebug() << "POOL NAME" << pool->name(); + m_items.append(pool); + endInsertRows(); +} + +int DanbooruPoolModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_items.size(); +} + +int DanbooruPoolModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return 4; +} + +QVariant DanbooruPoolModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid()) { + return QVariant(); + } + + if (index.row() >= m_items.size() || index.row() < 0) { + return QVariant(); + } + + if (m_items.isEmpty()) { + return QVariant(); + } + + DanbooruPool* pool = m_items.at(index.row()); + + if (role == Qt::DisplayRole) { + + if (index.column() == 0) { + return pool->id(); + } else if (index.column() == 1) { + return pool->name(); + } else if (index.column() == 2) { + return pool->postCount(); + } else if (index.column() == 3) { + return pool->description(); + } + } + + return QVariant(); + +} + +QVariant DanbooruPoolModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + return m_headerNames.at(section); + } + + return QAbstractTableModel::headerData(section, orientation, role); + +} + +void DanbooruPoolModel::clear() +{ + if (m_items.isEmpty()) { + return; + } + + beginRemoveRows(QModelIndex(), 0, m_items.size()); + qDeleteAll(m_items); + m_items.clear(); + endRemoveRows(); + reset(); +} + + + +} // namespace Danbooru \ No newline at end of file diff --git a/src/model/danboorupoolmodel.h b/src/model/danboorupoolmodel.h new file mode 100644 index 0000000..e218fce --- /dev/null +++ b/src/model/danboorupoolmodel.h @@ -0,0 +1,58 @@ +/* + * Copyright 2015 Luca Beltrame + * + * This file is part of Danbooru Client. + * + * Danbooru Client 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. + * + * Danbooru Client 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 Danbooru Client. If not, see . + */ + +#ifndef DANBOORU_POOLMODEL_H +#define DANBOORU_POOLMODEL_H + +#include +#include + +namespace Danbooru { + +class DanbooruPool; + +class DanbooruPoolModel: public QAbstractTableModel +{ + Q_OBJECT + +public: + DanbooruPoolModel(QObject* parent=0); + ~DanbooruPoolModel(); + + int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; + QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE; + void clear(); + +private: + QVector m_items; + static const QStringList m_headerNames; + +public Q_SLOTS: + void addPool(Danbooru::DanbooruPool *pool); + +}; + + +} // namespace Danbooru + + + +#endif \ No newline at end of file