Moved models to a separate directory

This commit is contained in:
Luca Beltrame 2013-06-09 21:59:55 +02:00
parent c363891de3
commit ad89dd5db9
4 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,157 @@
/*
* <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 "danboorupostdelegate.h"
#include "libdanbooru/danboorupost.h"
// Qt
#include <QListView>
#include <QPainter>
#include <QDebug>
#include <QApplication>
// KDE
#include <KPushButton>
namespace Danbooru {
const int DanbooruPostDelegate::MARGIN = 5;
DanbooruPostDelegate::DanbooruPostDelegate(QListView* itemView):QStyledItemDelegate(itemView),
m_itemView(itemView)
{
//FIXME: Copied directly from Gwenview without understanding
#define pm(x) itemView->style()->pixelMetric(QStyle::x)
m_margin = pm(PM_ToolBarItemMargin);
m_spacing = pm(PM_ToolBarItemSpacing);
#undef pm
const int iconSize = KIconLoader::global()->currentSize(
KIconLoader::Toolbar
);
const QSize sz = itemView->style()->sizeFromContents(
QStyle::CT_ToolButton, 0,
QSize(iconSize, iconSize));
m_buttonSize = qMax(sz.width(), sz.height());
m_downloadButton = new KPushButton(KIcon("download"),
QString(), itemView);
m_viewButton = new KPushButton(KIcon("view-preview"), QString(),
itemView);
m_downloadButton->hide();
m_viewButton->hide();
m_downloadButton->resize(m_buttonSize, m_buttonSize);
m_viewButton->resize(m_buttonSize, m_buttonSize);
}
void DanbooruPostDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (!index.isValid()) {
return;
}
QStyle *style = QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
painter->setRenderHint(QPainter::Antialiasing);
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
if (pixmap.isNull()) {
return;
}
// Scaling is unavoidable to keep things in the right dimension
//TODO: Replace fixed values by configurable values
QPixmap scaled = pixmap.scaled(256-MARGIN, 256-MARGIN,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QRect rect = option.rect;
// Draw in the center
QPoint centerCoordinate = rect.center() - QPoint(scaled.width() / 2,
scaled.height() / 2);
// Get the bottom coordinate for the buttons, made as center - half
// of the pixmap width (left) then half of the height minus the button
// size and margin (because the move() call later on uses the
// coordinates as TOP coordinates, while we want the bottom of the
// button to align with the bottom of the pixmap)
QPoint scaleFactor = QPoint(
-(scaled.width() / 2) + (MARGIN / 2),
(scaled.height() / 2) - m_buttonSize + (MARGIN * 1.5)
);
QPoint bottomCoordinate = rect.center() + scaleFactor;
painter->drawPixmap(centerCoordinate, scaled);
if (option.state & QStyle::State_MouseOver) {
m_downloadButton->move(centerCoordinate + QPoint(MARGIN / 2, 0));
m_viewButton->move(bottomCoordinate);
m_downloadButton->show();
m_viewButton->show();
} else if (!hoveredIndex().isValid()) {
m_downloadButton->hide();
m_viewButton->hide();
}
}
QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
return m_itemView->gridSize();
}
QModelIndex DanbooruPostDelegate::hoveredIndex() const
{
const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
return m_itemView->indexAt(pos);
}
} // namespace Danbooru

View file

@ -0,0 +1,77 @@
/*
* This file is part of Danbooru Client.
* 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/>.
*
*/
#ifndef DANBOORUPOSTDELEGATE_H
#define DANBOORUPOSTDELEGATE_H
/**
* @brief This file includes custom delegates to represent DanbooruItems.
* @file danboorupostdelegate.h
*
**/
#include <QStyledItemDelegate>
class QListView;
class QPainter;
class KPushButton;
namespace Danbooru {
/**
* @brief Specific delegate for Danbooru items.
*
*/
class DanbooruPostDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
DanbooruPostDelegate(QListView* itemView);
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
QSize sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const;
QModelIndex hoveredIndex() const;
private:
QListView* m_itemView;
KPushButton* m_downloadButton;
KPushButton* m_viewButton;
int m_buttonSize;
int m_margin;
int m_spacing;
static const int MARGIN;
};
} // namespace Danbooru
#endif // DANBOORUPOSTDELEGATE_H

View file

@ -0,0 +1,94 @@
/*
* <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 "danboorutablemodel.h"
#include "libdanbooru/danboorupost.h"
// Qt
#include <QDebug>
#include <QPixmap>
namespace Danbooru {
DanbooruTableModel::DanbooruTableModel(QObject* parent): QAbstractListModel(parent)
{
}
DanbooruTableModel::~DanbooruTableModel()
{
qDeleteAll(m_posts);
}
int DanbooruTableModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return m_posts.length();
}
void DanbooruTableModel::addPost(Danbooru::DanbooruPost* post)
{
beginInsertRows(QModelIndex(), m_posts.length(), m_posts.length() + 1);
m_posts.append(post);
endInsertRows();
}
QVariant DanbooruTableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (index.row() >= m_posts.length() || index.row() < 0) {
return QVariant();
}
if (m_posts.isEmpty()) {
return QVariant();
}
if (role == Qt::DisplayRole) {
Danbooru::DanbooruPost* post = m_posts.at(index.row());
QVariant variant;
variant.setValue(post);
return variant;
}
if (role == Qt::DecorationRole) {
DanbooruPost* post = m_posts.at(index.row());
const QPixmap* pixmap = post->pixmap();
return *pixmap;
}
return QVariant();
}
};

View file

@ -0,0 +1,78 @@
/*
* <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/>.
*
*/
#ifndef DANBOORUTABLEMODEL_H
#define DANBOORUTABLEMODEL_H
/**
* @brief This file contains a specific model to represent Danbooru Items
* @file danboorutablemodel.h
*
**/
#include <QAbstractListModel>
#include <QList>
namespace Danbooru {
class DanbooruPost;
/**
* @brief A model to represent DanbooruItems
*
* Since items from a Danbooru service are sent by the service itself,
* there is no need for sorting or table-like structures: everything is
* represented as a flat list.
*
* Items are added through the addPost() slot.
*
*/
class DanbooruTableModel : public QAbstractListModel
{
Q_OBJECT
public:
DanbooruTableModel(QObject* parent=0);
~DanbooruTableModel();
int rowCount(const QModelIndex& parent=QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
private:
QList<DanbooruPost*> m_posts;
public Q_SLOTS:
/**
* @brief Add a new post to the model
*
* Connect to this slot when you want to add items to the model.
*
* @param post A pointer to a DanbooruPost.
*
*/
void addPost(Danbooru::DanbooruPost* post);
};
}; // namespace Danbooru
#endif // DANBOORUTABLEMODEL_H