Run astyle-kdelibs on the source

This commit is contained in:
Luca Beltrame 2014-10-05 09:53:19 +02:00
commit 39aac8c95b
22 changed files with 1227 additions and 1264 deletions

View file

@ -39,223 +39,221 @@
#include <KIconLoader>
#include <KFormat>
namespace Danbooru
{
namespace Danbooru {
const int DanbooruPostDelegate::MARGIN = 5;
const int DanbooruPostDelegate::MARGIN = 5;
DanbooruPostDelegate::DanbooruPostDelegate(QListView *itemView): QStyledItemDelegate(itemView),
m_itemView(itemView)
{
DanbooruPostDelegate::DanbooruPostDelegate(QListView* itemView):QStyledItemDelegate(itemView),
m_itemView(itemView)
{
// Get the sizes for the buttons
// Get the sizes for the buttons
const int iconSize = KIconLoader::global()->currentSize(
KIconLoader::Toolbar
);
const int iconSize = KIconLoader::global()->currentSize(
KIconLoader::Toolbar
);
const QSize sz = itemView->style()->sizeFromContents(
QStyle::CT_ToolButton, 0,
QSize(iconSize, iconSize));
const QSize sz = itemView->style()->sizeFromContents(
QStyle::CT_ToolButton, 0,
QSize(iconSize, iconSize));
m_buttonSize = qMax(sz.width(), sz.height());
m_buttonSize = qMax(sz.width(), sz.height());
m_downloadButton = new QPushButton(QIcon::fromTheme("download"),
QString(), itemView);
m_viewButton = new QPushButton(QIcon::fromTheme("view-preview"), QString(),
itemView);
m_downloadButton = new QPushButton(QIcon::fromTheme("download"),
QString(), itemView);
m_viewButton = new QPushButton(QIcon::fromTheme("view-preview"), QString(),
itemView);
m_downloadButton->hide();
m_viewButton->hide();
m_downloadButton->resize(m_buttonSize, m_buttonSize);
m_viewButton->resize(m_buttonSize, m_buttonSize);
m_downloadButton->setToolTip(i18n("Download image"));
m_viewButton->setToolTip(i18n("View image"));
// Signal-slot connections
connect(m_viewButton, &QPushButton::clicked, this, &DanbooruPostDelegate::viewButtonClicked);
connect(m_downloadButton, &QPushButton::clicked, this, &DanbooruPostDelegate::downloadButtonClicked);
}
void DanbooruPostDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (!index.isValid()) {
return;
}
QStyleOptionViewItemV4 opt(option);
// Pixmap
QStyle *style = QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, 0);
painter->setRenderHint(QPainter::Antialiasing);
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
if (pixmap.isNull()) {
return;
}
QRect rect = opt.rect;
QFontMetrics metrics = opt.fontMetrics;
QRect textRect(rect.left() + MARGIN, rect.bottom() - 3 * metrics.height(),
rect.width(), 3 * metrics.height());
// Scaling is unavoidable to keep things in the right dimension
QPixmap scaled;
// Reserve enough space for the pixmap + the 3 lines of text:
// this prevents issues with images that have height > width
// (like in yande.re which keeps a lot of scans)
int maxHeight = rect.height() - 3 * metrics.height() - 2 * MARGIN;
scaled = pixmap.scaled(rect.width() - 2 * MARGIN,
maxHeight,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QRect pixRect = scaled.rect();
pixRect.moveCenter(rect.center());
// move the pixmap up to accomodate some lines of text
pixRect.moveTo(pixRect.left(),
pixRect.top() - textRect.height() / 2);
pixRect.moveBottom(textRect.top() - MARGIN);
painter->drawPixmap(pixRect, scaled);
painter->save();
// Show buttons on mouseover
if (option.state & QStyle::State_MouseOver) {
// Get the bottom coordinate for the buttons
// TODO: Perhaps add some transition?
m_downloadButton->move(pixRect.topLeft());
m_viewButton->move(pixRect.bottomLeft() - QPoint(
0, m_viewButton->height() - 1.5 * MARGIN));
m_downloadButton->show();
m_viewButton->show();
} else if (!hoveredIndex().isValid()) {
m_downloadButton->hide();
m_viewButton->hide();
m_downloadButton->resize(m_buttonSize, m_buttonSize);
m_viewButton->resize(m_buttonSize, m_buttonSize);
m_downloadButton->setToolTip(i18n("Download image"));
m_viewButton->setToolTip(i18n("View image"));
// Signal-slot connections
connect(m_viewButton, &QPushButton::clicked, this, &DanbooruPostDelegate::viewButtonClicked);
connect(m_downloadButton, &QPushButton::clicked, this, &DanbooruPostDelegate::downloadButtonClicked);
}
void DanbooruPostDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
painter->restore();
if (!index.isValid()) {
return;
}
// Text
QStyleOptionViewItemV4 opt(option);
DanbooruPost *post = index.data().value<Danbooru::DanbooruPost *>();
// Pixmap
painter->save();
QStyle *style = QApplication::style();
int imageHeight = post->height();
int imageWidth = post->width();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, 0);
painter->setRenderHint(QPainter::Antialiasing);
KFormat format;
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
QString imageText = i18n(
"File size: %1",
format.formatByteSize(post->size()));
if (pixmap.isNull()) {
return;
}
KLocalizedString sizestr = ki18np("1 pixel", "%1 pixels");
QRect rect = opt.rect;
QFontMetrics metrics = opt.fontMetrics;
QRect textRect(rect.left() + MARGIN, rect.bottom() - 3 * metrics.height(),
rect.width(), 3 * metrics.height());
imageText += "\n";
imageText += i18n("Resolution: %1 x %2",
sizestr.subs(imageWidth).toString(),
sizestr.subs(imageHeight).toString());
imageText += "\n";
// Scaling is unavoidable to keep things in the right dimension
QPixmap scaled;
// Reserve enough space for the pixmap + the 3 lines of text:
// this prevents issues with images that have height > width
// (like in yande.re which keeps a lot of scans)
int maxHeight = rect.height() - 3 * metrics.height() - 2 * MARGIN;
scaled = pixmap.scaled(rect.width() - 2 * MARGIN,
maxHeight,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QRect pixRect = scaled.rect();
pixRect.moveCenter(rect.center());
// move the pixmap up to accomodate some lines of text
pixRect.moveTo(pixRect.left(),
pixRect.top() - textRect.height() / 2);
pixRect.moveBottom(textRect.top() - MARGIN);
painter->drawPixmap(pixRect, scaled);
painter->save();
// Show buttons on mouseover
if (option.state & QStyle::State_MouseOver) {
// Get the bottom coordinate for the buttons
// TODO: Perhaps add some transition?
m_downloadButton->move(pixRect.topLeft());
m_viewButton->move(pixRect.bottomLeft() - QPoint(
0, m_viewButton->height() - 1.5 * MARGIN));
m_downloadButton->show();
m_viewButton->show();
} else if (!hoveredIndex().isValid()) {
m_downloadButton->hide();
m_viewButton->hide();
}
painter->restore();
// Text
DanbooruPost* post = index.data().value<Danbooru::DanbooruPost*>();
painter->save();
int imageHeight = post->height();
int imageWidth = post->width();
KFormat format;
QString imageText = i18n(
"File size: %1",
format.formatByteSize(post->size()));
KLocalizedString sizestr = ki18np("1 pixel", "%1 pixels");
imageText += "\n";
imageText += i18n("Resolution: %1 x %2",
sizestr.subs(imageWidth).toString(),
sizestr.subs(imageHeight).toString());
imageText += "\n";
QString ratingString;
switch (post->rating()) {
case DanbooruPost::Safe:
ratingString = i18n("Safe");
break;
case DanbooruPost::Questionable:
ratingString = i18n("Questionable");
break;
case DanbooruPost::Explicit:
ratingString = i18n("Explicit");
break;
default:
ratingString = i18nc("Unknown post rating", "Unknown");
}
imageText += i18n("Rating: %1", ratingString);
painter->drawText(textRect, imageText);
painter->restore();
QString ratingString;
switch (post->rating()) {
case DanbooruPost::Safe:
ratingString = i18n("Safe");
break;
case DanbooruPost::Questionable:
ratingString = i18n("Questionable");
break;
case DanbooruPost::Explicit:
ratingString = i18n("Explicit");
break;
default:
ratingString = i18nc("Unknown post rating", "Unknown");
}
QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
imageText += i18n("Rating: %1", ratingString);
Q_UNUSED(option)
painter->drawText(textRect, imageText);
painter->restore();
if (!index.isValid()) {
return QSize();
}
}
return m_itemView->gridSize();
QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option)
if (!index.isValid()) {
return QSize();
}
QModelIndex DanbooruPostDelegate::hoveredIndex() const
{
const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
return m_itemView->indexAt(pos);
return m_itemView->gridSize();
}
QModelIndex DanbooruPostDelegate::hoveredIndex() const
{
const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
return m_itemView->indexAt(pos);
}
void DanbooruPostDelegate::downloadButtonClicked()
{
QModelIndex index = hoveredIndex();
if (!index.isValid()) {
return;
}
void DanbooruPostDelegate::downloadButtonClicked()
{
QModelIndex index = hoveredIndex();
QVariant data = index.data(Qt::DisplayRole);
const DanbooruPost *post = data.value<DanbooruPost *>();
if (!index.isValid()) {
return;
}
if (post) {
Q_EMIT(postDownloadRequested(post->fileUrl()));
}
}
QVariant data = index.data(Qt::DisplayRole);
const DanbooruPost* post = data.value<DanbooruPost*>();
void DanbooruPostDelegate::viewButtonClicked()
{
if (post) {
Q_EMIT(postDownloadRequested(post->fileUrl()));
}
QModelIndex index = hoveredIndex();
if (!index.isValid()) {
return;
}
void DanbooruPostDelegate::viewButtonClicked()
{
QModelIndex index = hoveredIndex();
if (!index.isValid()) {
return;
}
QVariant data = index.data(Qt::DisplayRole);
const DanbooruPost* post = data.value<DanbooruPost*>();
if (post) {
Q_EMIT(postViewRequested(post->fileUrl()));
}
QVariant data = index.data(Qt::DisplayRole);
const DanbooruPost *post = data.value<DanbooruPost *>();
if (post) {
Q_EMIT(postViewRequested(post->fileUrl()));
}
}
} // namespace Danbooru

View file

@ -37,67 +37,63 @@ class QListView;
class QPainter;
class QPushButton;
namespace Danbooru
{
namespace Danbooru {
/**
* @brief Specific delegate for Danbooru items.
*
*/
/**
* @brief Specific delegate for Danbooru items.
*
*/
class DanbooruPost;
class DanbooruPost;
class DanbooruPostDelegate : public QStyledItemDelegate
{
class DanbooruPostDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
DanbooruPostDelegate(QListView* itemView);
public:
DanbooruPostDelegate(QListView *itemView);
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
QSize sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QModelIndex hoveredIndex() const;
QModelIndex hoveredIndex() const;
private:
private:
QListView* m_itemView;
QPushButton* m_downloadButton;
QPushButton* m_viewButton;
QListView *m_itemView;
QPushButton *m_downloadButton;
QPushButton *m_viewButton;
int m_buttonSize;
static const int MARGIN;
int m_buttonSize;
static const int MARGIN;
Q_SIGNALS:
Q_SIGNALS:
/**
* @brief Emitted when the view button is clicked.
*
* @param postUrl the URL to the full picture.
*
*/
void postViewRequested(QUrl postUrl);
/**
* @brief Emitted when the view button is clicked.
*
* @param postUrl the URL to the full picture.
*
*/
void postViewRequested(QUrl postUrl);
/**
* @brief Emitted when the download button is clicked.
*
* @param postUrl the URL to the full picture.
*
*/
void postDownloadRequested(QUrl postUrl);
/**
* @brief Emitted when the download button is clicked.
*
* @param postUrl the URL to the full picture.
*
*/
void postDownloadRequested(QUrl postUrl);
private Q_SLOTS:
void viewButtonClicked();
void downloadButtonClicked();
private Q_SLOTS:
void viewButtonClicked();
void downloadButtonClicked();
};
};
} // namespace Danbooru

View file

@ -30,88 +30,90 @@
#include <QDebug>
#include <QPixmap>
namespace Danbooru {
namespace Danbooru
{
DanbooruPostModel::DanbooruPostModel(QObject* parent): QAbstractListModel(parent)
{
DanbooruPostModel::DanbooruPostModel(QObject *parent): QAbstractListModel(parent)
{
}
DanbooruPostModel::~DanbooruPostModel()
{
if (!m_items.isEmpty()) {
qDeleteAll(m_items);
m_items.clear();
}
}
DanbooruPostModel::~DanbooruPostModel()
{
int DanbooruPostModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_items.size();
}
if (!m_items.isEmpty()) {
qDeleteAll(m_items);
m_items.clear();
}
}
void DanbooruPostModel::addPost(Danbooru::DanbooruPost *post)
{
int DanbooruPostModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)
return m_items.size();
}
beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
m_items.append(post);
endInsertRows();
}
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();
}
if (role == Qt::DisplayRole) {
QVariant variant;
variant.setValue(post);
return variant;
}
if (role == Qt::DecorationRole) {
const QPixmap pixmap = post->pixmap();
return pixmap;
}
if (role == Qt::ToolTipRole) {
return post->fileUrl().fileName();
}
QVariant DanbooruPostModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
void DanbooruPostModel::clear() {
if (m_items.isEmpty()) {
return;
}
beginRemoveRows(QModelIndex(),0, m_items.size());
qDeleteAll(m_items);
m_items.clear();
endRemoveRows();
reset();
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();
}
if (role == Qt::DisplayRole) {
QVariant variant;
variant.setValue(post);
return variant;
}
if (role == Qt::DecorationRole) {
const QPixmap pixmap = post->pixmap();
return pixmap;
}
if (role == Qt::ToolTipRole) {
return post->fileUrl().fileName();
}
return QVariant();
}
void DanbooruPostModel::clear()
{
if (m_items.isEmpty()) {
return;
}
beginRemoveRows(QModelIndex(), 0, m_items.size());
qDeleteAll(m_items);
m_items.clear();
endRemoveRows();
reset();
}
};

View file

@ -32,49 +32,49 @@
#include <QAbstractListModel>
#include <QVector>
namespace Danbooru {
namespace Danbooru
{
class DanbooruPost;
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 DanbooruPostModel : public QAbstractListModel
{
/**
* @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 DanbooruPostModel : public QAbstractListModel
{
Q_OBJECT
public:
DanbooruPostModel(QObject* parent=0);
~DanbooruPostModel();
public:
DanbooruPostModel(QObject *parent = 0);
~DanbooruPostModel();
int rowCount(const QModelIndex& parent=QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
void clear();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
void clear();
private:
QVector<DanbooruPost*> m_items;
private:
QVector<DanbooruPost *> m_items;
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);
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 // DANBOORUPOSTMODEL_H