• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

QtExtraComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • qtextracomponents
qimageitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Marco Martin <mart@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "qimageitem.h"
21 
22 #include <QPainter>
23 
24 
25 QImageItem::QImageItem(QDeclarativeItem *parent)
26  : QDeclarativeItem(parent),
27  m_smooth(false),
28  m_fillMode(QImageItem::Stretch)
29 {
30  setFlag(QGraphicsItem::ItemHasNoContents, false);
31 }
32 
33 
34 QImageItem::~QImageItem()
35 {
36 }
37 
38 void QImageItem::setImage(const QImage &image)
39 {
40  bool oldImageNull = m_image.isNull();
41  m_image = image;
42  update();
43  emit nativeWidthChanged();
44  emit nativeHeightChanged();
45  emit imageChanged();
46  if (oldImageNull != m_image.isNull()) {
47  emit nullChanged();
48  }
49 }
50 
51 QImage QImageItem::image() const
52 {
53  return m_image;
54 }
55 
56 void QImageItem::setSmooth(const bool smooth)
57 {
58  if (smooth == m_smooth) {
59  return;
60  }
61  m_smooth = smooth;
62  update();
63 }
64 
65 bool QImageItem::smooth() const
66 {
67  return m_smooth;
68 }
69 
70 int QImageItem::nativeWidth() const
71 {
72  return m_image.size().width();
73 }
74 
75 int QImageItem::nativeHeight() const
76 {
77  return m_image.size().height();
78 }
79 
80 QImageItem::FillMode QImageItem::fillMode() const
81 {
82  return m_fillMode;
83 }
84 
85 void QImageItem::setFillMode(QImageItem::FillMode mode)
86 {
87  if (mode == m_fillMode) {
88  return;
89  }
90 
91  m_fillMode = mode;
92  update();
93  emit fillModeChanged();
94 }
95 
96 void QImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
97 {
98  Q_UNUSED(option);
99  Q_UNUSED(widget);
100 
101  if (m_image.isNull()) {
102  return;
103  }
104  painter->save();
105  painter->setRenderHint(QPainter::Antialiasing, m_smooth);
106  painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
107 
108  QRect sourceRect = m_image.rect();
109  QRect destRect;
110  switch (m_fillMode) {
111  case PreserveAspectFit: {
112  QSize scaled = m_image.size();
113 
114  scaled.scale(boundingRect().size().toSize(), Qt::KeepAspectRatio);
115  destRect = QRect(QPoint(0, 0), scaled);
116  destRect.moveCenter(boundingRect().center().toPoint());
117  break;
118  }
119  case PreserveAspectCrop: {
120  destRect = boundingRect().toRect();
121  sourceRect = destRect;
122  sourceRect.moveCenter(m_image.rect().center());
123  break;
124  }
125  case TileVertically: {
126  painter->scale(width()/(qreal)m_image.width(), 1);
127  destRect = boundingRect().toRect();
128  destRect.setWidth(destRect.width() / (width()/(qreal)m_image.width()));
129  break;
130  }
131  case TileHorizontally: {
132  painter->scale(1, height()/(qreal)m_image.height());
133  destRect = boundingRect().toRect();
134  destRect.setHeight(destRect.height() / (height()/(qreal)m_image.height()));
135  break;
136  }
137  case Stretch:
138  case Tile:
139  default:
140  destRect = boundingRect().toRect();
141  }
142 
143  if (m_fillMode >= Tile) {
144  painter->drawTiledPixmap(destRect, QPixmap::fromImage(m_image));
145  } else {
146  painter->drawImage(destRect, m_image, sourceRect);
147  }
148 
149  painter->restore();
150 }
151 
152 bool QImageItem::isNull() const
153 {
154  return m_image.isNull();
155 }
156 
157 #include "qimageitem.moc"
QImageItem::nullChanged
void nullChanged()
QImageItem::Tile
Definition: qimageitem.h:43
QWidget
QSize::width
int width() const
QRectF::toRect
QRect toRect() const
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
QImageItem::TileVertically
Definition: qimageitem.h:44
QImageItem::setFillMode
void setFillMode(FillMode mode)
Definition: qimageitem.cpp:85
QPainter::scale
void scale(qreal sx, qreal sy)
QImageItem::nativeWidth
int nativeWidth() const
QPainter::save
void save()
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QPainter::drawTiledPixmap
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
QRect::height
int height() const
QSize::scale
void scale(int width, int height, Qt::AspectRatioMode mode)
QPoint
QImage::isNull
bool isNull() const
QImageItem::PreserveAspectCrop
Definition: qimageitem.h:42
QImageItem::~QImageItem
~QImageItem()
Definition: qimageitem.cpp:34
QGraphicsItem::update
void update(const QRectF &rect)
QImageItem::FillMode
FillMode
Definition: qimageitem.h:39
QImageItem::isNull
bool isNull() const
Definition: qimageitem.cpp:152
QRect
qimageitem.h
QImage::width
int width() const
QPainter
QRect::setWidth
void setWidth(int width)
QImage::rect
QRect rect() const
QImageItem::setSmooth
void setSmooth(const bool smooth)
Definition: qimageitem.cpp:56
QRect::center
QPoint center() const
QImageItem::image
QImage image() const
QImageItem::nativeHeight
int nativeHeight() const
QImageItem::nativeWidthChanged
void nativeWidthChanged()
QImageItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: qimageitem.cpp:96
QSize
QImage
QPainter::restore
void restore()
QImageItem::Stretch
Definition: qimageitem.h:40
QRect::width
int width() const
QImageItem
Definition: qimageitem.h:26
QPainter::drawImage
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
QRect::setHeight
void setHeight(int height)
QImageItem::fillMode
FillMode fillMode() const
QSize::height
int height() const
QImageItem::imageChanged
void imageChanged()
QImage::size
QSize size() const
QImageItem::QImageItem
QImageItem(QDeclarativeItem *parent=0)
Definition: qimageitem.cpp:25
QImageItem::TileHorizontally
Definition: qimageitem.h:45
QStyleOptionGraphicsItem
QImage::height
int height() const
QDeclarativeItem
QImageItem::smooth
bool smooth() const
QImageItem::fillModeChanged
void fillModeChanged()
QRect::moveCenter
void moveCenter(const QPoint &position)
QImageItem::setImage
void setImage(const QImage &image)
Definition: qimageitem.cpp:38
QImageItem::PreserveAspectFit
Definition: qimageitem.h:41
QImageItem::nativeHeightChanged
void nativeHeightChanged()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

QtExtraComponents

Skip menu "QtExtraComponents"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal