2011-02-10 18:41:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Marco Martin <mart@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qpixmapitem.h"
|
|
|
|
|
2012-04-10 00:22:13 +02:00
|
|
|
#include <QPainter>
|
2011-02-10 18:41:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
QPixmapItem::QPixmapItem(QDeclarativeItem *parent)
|
|
|
|
: QDeclarativeItem(parent),
|
2011-11-14 14:36:36 +01:00
|
|
|
m_smooth(false),
|
|
|
|
m_fillMode(QPixmapItem::Stretch)
|
2011-02-10 18:41:47 +01:00
|
|
|
{
|
|
|
|
setFlag(QGraphicsItem::ItemHasNoContents, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QPixmapItem::~QPixmapItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPixmapItem::setPixmap(const QPixmap &pixmap)
|
|
|
|
{
|
2012-08-14 19:58:21 +02:00
|
|
|
bool oldPixmapNull = m_pixmap.isNull();
|
2011-02-10 18:41:47 +01:00
|
|
|
m_pixmap = pixmap;
|
|
|
|
update();
|
|
|
|
emit nativeWidthChanged();
|
|
|
|
emit nativeHeightChanged();
|
2012-08-14 19:58:21 +02:00
|
|
|
emit pixmapChanged();
|
|
|
|
if (oldPixmapNull != m_pixmap.isNull()) {
|
|
|
|
emit nullChanged();
|
|
|
|
}
|
2011-02-10 18:41:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap QPixmapItem::pixmap() const
|
|
|
|
{
|
|
|
|
return m_pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPixmapItem::setSmooth(const bool smooth)
|
|
|
|
{
|
|
|
|
if (smooth == m_smooth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_smooth = smooth;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QPixmapItem::smooth() const
|
|
|
|
{
|
|
|
|
return m_smooth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QPixmapItem::nativeWidth() const
|
|
|
|
{
|
|
|
|
return m_pixmap.size().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QPixmapItem::nativeHeight() const
|
|
|
|
{
|
|
|
|
return m_pixmap.size().height();
|
|
|
|
}
|
|
|
|
|
2011-11-14 14:36:36 +01:00
|
|
|
QPixmapItem::FillMode QPixmapItem::fillMode() const
|
|
|
|
{
|
|
|
|
return m_fillMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPixmapItem::setFillMode(QPixmapItem::FillMode mode)
|
|
|
|
{
|
|
|
|
if (mode == m_fillMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_fillMode = mode;
|
|
|
|
update();
|
|
|
|
emit fillModeChanged();
|
|
|
|
}
|
|
|
|
|
2011-02-10 18:41:47 +01:00
|
|
|
void QPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
if (m_pixmap.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-14 16:34:46 +01:00
|
|
|
painter->save();
|
2011-02-10 18:41:47 +01:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, m_smooth);
|
|
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
|
|
|
|
|
2012-08-17 18:19:14 +02:00
|
|
|
QRect sourceRect = m_pixmap.rect();
|
2011-11-14 14:36:36 +01:00
|
|
|
QRect destRect;
|
|
|
|
switch (m_fillMode) {
|
|
|
|
case PreserveAspectFit: {
|
2011-11-14 16:34:46 +01:00
|
|
|
QSize scaled = m_pixmap.size();
|
|
|
|
|
|
|
|
scaled.scale(boundingRect().size().toSize(), Qt::KeepAspectRatio);
|
|
|
|
destRect = QRect(QPoint(0, 0), scaled);
|
2012-08-17 18:19:14 +02:00
|
|
|
destRect.moveCenter(boundingRect().center().toPoint());
|
2011-11-14 14:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PreserveAspectCrop: {
|
2012-08-17 18:19:14 +02:00
|
|
|
destRect = boundingRect().toRect();
|
|
|
|
sourceRect = destRect;
|
|
|
|
sourceRect.moveCenter(m_pixmap.rect().center());
|
2011-11-14 14:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TileVertically: {
|
2011-11-14 16:34:46 +01:00
|
|
|
painter->scale(width()/(qreal)m_pixmap.width(), 1);
|
|
|
|
destRect = boundingRect().toRect();
|
|
|
|
destRect.setWidth(destRect.width() / (width()/(qreal)m_pixmap.width()));
|
2011-11-14 14:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TileHorizontally: {
|
2011-11-14 16:34:46 +01:00
|
|
|
painter->scale(1, height()/(qreal)m_pixmap.height());
|
|
|
|
destRect = boundingRect().toRect();
|
|
|
|
destRect.setHeight(destRect.height() / (height()/(qreal)m_pixmap.height()));
|
2011-11-14 14:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Stretch:
|
|
|
|
case Tile:
|
|
|
|
default:
|
|
|
|
destRect = boundingRect().toRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_fillMode >= Tile) {
|
|
|
|
painter->drawTiledPixmap(destRect, m_pixmap);
|
|
|
|
} else {
|
|
|
|
painter->drawPixmap(destRect, m_pixmap, m_pixmap.rect());
|
|
|
|
}
|
|
|
|
|
2011-11-14 16:34:46 +01:00
|
|
|
painter->restore();
|
2011-02-10 18:41:47 +01:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:58:21 +02:00
|
|
|
bool QPixmapItem::isNull() const
|
|
|
|
{
|
|
|
|
return m_pixmap.isNull();
|
|
|
|
}
|
2011-02-10 18:41:47 +01:00
|
|
|
|
|
|
|
#include "qpixmapitem.moc"
|