danbooru-client/src/model/danboorupostmodel.cpp

151 lines
3.5 KiB
C++

/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 Luca Beltrame <lbeltrame@kde.org>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
// Own
#include "danboorupostmodel.h"
#include "libdanbooru/danboorupost.h"
// Qt
#include <QPixmap>
namespace Danbooru
{
DanbooruPostModel::DanbooruPostModel(QObject *parent): QAbstractListModel(parent)
{
}
DanbooruPostModel::~DanbooruPostModel()
{
if (!m_items.isEmpty()) {
qDeleteAll(m_items);
m_items.clear();
}
}
int DanbooruPostModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_items.size();
}
void DanbooruPostModel::addPost(Danbooru::DanbooruPost *post)
{
beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
m_items.append(post);
endInsertRows();
}
QVariant DanbooruPostModel::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();
}
DanbooruPost *post = m_items.at(index.row());
if (!post) {
return QVariant();
}
switch(role) {
case Qt::DisplayRole:
{
QVariant variant;
variant.setValue(post);
return variant;
}
case Qt::DecorationRole:
{
const QPixmap pixmap = post->pixmap();
return pixmap;
}
case Qt::ToolTipRole:
return post->fileUrl().fileName();
case FileUrlRole:
return post->fileUrl();
case ThumbUrlRole:
return post->thumbnailUrl();
case SizeRole:
return post->size();
case ResolutionRole:
return QSize(post->width(), post->height());
case RatingRole:
return post->rating();
case TagRole:
{
QVariant variant;
variant.setValue(post->tags());
return variant;
}
default:
return QVariant();
}
return QVariant();
}
void DanbooruPostModel::clear()
{
if (m_items.isEmpty()) {
return;
}
beginRemoveRows(QModelIndex(), 0, m_items.size());
qDeleteAll(m_items);
m_items.clear();
endRemoveRows();
reset();
}
QHash< int, QByteArray > DanbooruPostModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::DecorationRole] = "thumbPix";
roles[FileUrlRole] = "fileUrl";
roles[ThumbUrlRole] = "thumbnailUrl";
roles[RatingRole] = "rating";
roles[TagRole] = "tags";
roles[SizeRole] = "fileSize";
roles[ResolutionRole] = "resolution";
return roles;
}
};