File indexing completed on 2024-05-05 17:09:09

0001 /*
0002  * This file is part of the KDE project
0003  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "CQThumbnailItem.h"
0021 
0022 #include <QPainter>
0023 
0024 class CQThumbnailItem::Private
0025 {
0026 public:
0027     Private() : contentWidth(-1), contentHeight(-1) { }
0028 
0029     QPixmap content;
0030     QString source;
0031     int contentWidth;
0032     int contentHeight;
0033 };
0034 
0035 CQThumbnailItem::CQThumbnailItem(QDeclarativeItem* parent)
0036     : QDeclarativeItem(parent), d(new Private)
0037 {
0038     setFlag(QGraphicsItem::ItemHasNoContents, false);
0039 }
0040 
0041 CQThumbnailItem::~CQThumbnailItem()
0042 {
0043     delete d;
0044 }
0045 
0046 void CQThumbnailItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* )
0047 {
0048     if (!d->content.isNull()) {
0049         QPixmap pixmap = d->content.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
0050         int xpos = (width() - pixmap.width()) / 2;
0051         int ypos = (height() - pixmap.height()) / 2;
0052         painter->drawPixmap(xpos, ypos, pixmap);
0053     }
0054 }
0055 
0056 QPixmap CQThumbnailItem::content() const
0057 {
0058     return d->content;
0059 }
0060 
0061 void CQThumbnailItem::setContent(const QPixmap& content)
0062 {
0063     d->content = content;
0064     emit contentChanged();
0065     update();
0066 }