• 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
qpixmapitem.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 "qpixmapitem.h"
21 
22 #include <QPainter>
23 
24 
25 QPixmapItem::QPixmapItem(QDeclarativeItem *parent)
26  : QDeclarativeItem(parent),
27  m_smooth(false),
28  m_fillMode(QPixmapItem::Stretch)
29 {
30  setFlag(QGraphicsItem::ItemHasNoContents, false);
31 }
32 
33 
34 QPixmapItem::~QPixmapItem()
35 {
36 }
37 
38 void QPixmapItem::setPixmap(const QPixmap &pixmap)
39 {
40  bool oldPixmapNull = m_pixmap.isNull();
41  m_pixmap = pixmap;
42  update();
43  emit nativeWidthChanged();
44  emit nativeHeightChanged();
45  emit pixmapChanged();
46  if (oldPixmapNull != m_pixmap.isNull()) {
47  emit nullChanged();
48  }
49 }
50 
51 QPixmap QPixmapItem::pixmap() const
52 {
53  return m_pixmap;
54 }
55 
56 void QPixmapItem::setSmooth(const bool smooth)
57 {
58  if (smooth == m_smooth) {
59  return;
60  }
61  m_smooth = smooth;
62  update();
63 }
64 
65 bool QPixmapItem::smooth() const
66 {
67  return m_smooth;
68 }
69 
70 int QPixmapItem::nativeWidth() const
71 {
72  return m_pixmap.size().width();
73 }
74 
75 int QPixmapItem::nativeHeight() const
76 {
77  return m_pixmap.size().height();
78 }
79 
80 QPixmapItem::FillMode QPixmapItem::fillMode() const
81 {
82  return m_fillMode;
83 }
84 
85 void QPixmapItem::setFillMode(QPixmapItem::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 QPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
97 {
98  Q_UNUSED(option);
99  Q_UNUSED(widget);
100 
101  if (m_pixmap.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_pixmap.rect();
109  QRect destRect;
110  switch (m_fillMode) {
111  case PreserveAspectFit: {
112  QSize scaled = m_pixmap.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_pixmap.rect().center());
123  break;
124  }
125  case TileVertically: {
126  painter->scale(width()/(qreal)m_pixmap.width(), 1);
127  destRect = boundingRect().toRect();
128  destRect.setWidth(destRect.width() / (width()/(qreal)m_pixmap.width()));
129  break;
130  }
131  case TileHorizontally: {
132  painter->scale(1, height()/(qreal)m_pixmap.height());
133  destRect = boundingRect().toRect();
134  destRect.setHeight(destRect.height() / (height()/(qreal)m_pixmap.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, m_pixmap);
145  } else {
146  painter->drawPixmap(destRect, m_pixmap, m_pixmap.rect());
147  }
148 
149  painter->restore();
150 }
151 
152 bool QPixmapItem::isNull() const
153 {
154  return m_pixmap.isNull();
155 }
156 
157 #include "qpixmapitem.moc"
QPixmapItem::Stretch
Definition: qpixmapitem.h:40
QPixmap::size
QSize size() const
QWidget
QPixmapItem::~QPixmapItem
~QPixmapItem()
Definition: qpixmapitem.cpp:34
QPixmapItem::setFillMode
void setFillMode(FillMode mode)
Definition: qpixmapitem.cpp:85
QSize::width
int width() const
QRectF::toRect
QRect toRect() const
QPixmap::width
int width() const
QPixmapItem::nativeHeightChanged
void nativeHeightChanged()
QPixmapItem::fillMode
FillMode fillMode() const
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
QPainter::scale
void scale(qreal sx, qreal sy)
QPixmapItem::setPixmap
void setPixmap(const QPixmap &pixmap)
Definition: qpixmapitem.cpp:38
QPixmapItem::PreserveAspectCrop
Definition: qpixmapitem.h:42
QPainter::save
void save()
QPixmapItem::nativeWidth
int nativeWidth() const
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
QPixmapItem::smooth
bool smooth() const
QPixmapItem::FillMode
FillMode
Definition: qpixmapitem.h:39
QGraphicsItem::update
void update(const QRectF &rect)
QPixmapItem::Tile
Definition: qpixmapitem.h:43
QPixmapItem::setSmooth
void setSmooth(const bool smooth)
Definition: qpixmapitem.cpp:56
QPixmapItem::isNull
bool isNull() const
Definition: qpixmapitem.cpp:152
QPixmapItem::TileVertically
Definition: qpixmapitem.h:44
QPixmapItem::pixmap
QPixmap pixmap() const
QRect
QPixmapItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: qpixmapitem.cpp:96
QPixmapItem::fillModeChanged
void fillModeChanged()
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QRect::setWidth
void setWidth(int width)
QRect::center
QPoint center() const
QPixmap
QPixmapItem
Definition: qpixmapitem.h:26
QPixmap::isNull
bool isNull() const
QSize
QPixmap::height
int height() const
QPixmapItem::nullChanged
void nullChanged()
QPainter::restore
void restore()
QPixmapItem::nativeWidthChanged
void nativeWidthChanged()
QRect::width
int width() const
QPixmapItem::pixmapChanged
void pixmapChanged()
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
QPixmapItem::QPixmapItem
QPixmapItem(QDeclarativeItem *parent=0)
Definition: qpixmapitem.cpp:25
QRect::setHeight
void setHeight(int height)
qpixmapitem.h
QSize::height
int height() const
QStyleOptionGraphicsItem
QPixmapItem::TileHorizontally
Definition: qpixmapitem.h:45
QDeclarativeItem
QRect::moveCenter
void moveCenter(const QPoint &position)
QPixmapItem::PreserveAspectFit
Definition: qpixmapitem.h:41
QPixmap::rect
QRect rect() const
QPixmapItem::nativeHeight
int nativeHeight() const
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