Qt 4.8
qdeclarativeimage.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativeimage_p.h"
43 #include "private/qdeclarativeimage_p_p.h"
44 
45 #include <QKeyEvent>
46 #include <QPainter>
47 
49 
50 
111 {
112 }
113 
115  : QDeclarativeImageBase(dd, parent)
116 {
117 }
118 
120 {
121 }
122 
124 {
125  Q_D(const QDeclarativeImage);
126  return d->pix.pixmap();
127 }
128 
130 {
132  if (!d->url.isEmpty())
133  return;
134  d->setPixmap(pix);
135 }
136 
138 {
140  pix.setPixmap(pixmap);
141 
142  q->pixmapChange();
144 
145  q->update();
146 }
147 
247 {
248  Q_D(const QDeclarativeImage);
249  return d->fillMode;
250 }
251 
253 {
255  if (d->fillMode == mode)
256  return;
257  d->fillMode = mode;
258  update();
261 }
262 
278 {
279  Q_D(const QDeclarativeImage);
280  return d->paintedWidth;
281 }
282 
284 {
285  Q_D(const QDeclarativeImage);
286  return d->paintedHeight;
287 }
288 
408 {
410 
411  if (d->fillMode == PreserveAspectFit) {
412  if (!d->pix.width() || !d->pix.height()) {
413  setImplicitWidth(0);
415  return;
416  }
417  qreal w = widthValid() ? width() : d->pix.width();
418  qreal widthScale = w / qreal(d->pix.width());
419  qreal h = heightValid() ? height() : d->pix.height();
420  qreal heightScale = h / qreal(d->pix.height());
421  if (widthScale <= heightScale) {
422  d->paintedWidth = w;
423  d->paintedHeight = widthScale * qreal(d->pix.height());
424  } else if(heightScale < widthScale) {
425  d->paintedWidth = heightScale * qreal(d->pix.width());
426  d->paintedHeight = h;
427  }
428  if (widthValid() && !heightValid()) {
429  setImplicitHeight(d->paintedHeight);
430  } else {
431  setImplicitHeight(d->pix.height());
432  }
433  if (heightValid() && !widthValid()) {
434  setImplicitWidth(d->paintedWidth);
435  } else {
436  setImplicitWidth(d->pix.width());
437  }
438  } else if (d->fillMode == PreserveAspectCrop) {
439  if (!d->pix.width() || !d->pix.height())
440  return;
441  qreal widthScale = width() / qreal(d->pix.width());
442  qreal heightScale = height() / qreal(d->pix.height());
443  if (widthScale < heightScale) {
444  widthScale = heightScale;
445  } else if(heightScale < widthScale) {
446  heightScale = widthScale;
447  }
448 
449  d->paintedHeight = heightScale * qreal(d->pix.height());
450  d->paintedWidth = widthScale * qreal(d->pix.width());
451  } else {
452  d->paintedWidth = width();
453  d->paintedHeight = height();
454  }
456 }
457 
458 void QDeclarativeImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
459 {
460  QDeclarativeImageBase::geometryChanged(newGeometry, oldGeometry);
462 }
463 
465 {
466  Q_D(const QDeclarativeImage);
467  return QRectF(0, 0, qMax(d->mWidth, d->paintedWidth), qMax(d->mHeight, d->paintedHeight));
468 }
469 
528 {
530  if (d->pix.pixmap().isNull() )
531  return;
532 
533  int drawWidth = width();
534  int drawHeight = height();
535  bool doClip = false;
537  qreal widthScale = width() / qreal(d->pix.width());
538  qreal heightScale = height() / qreal(d->pix.height());
539 
540  if (width() != d->pix.width() || height() != d->pix.height()) {
541  if (d->fillMode >= Tile) {
542  if (d->fillMode == TileVertically) {
543  transform.scale(widthScale, 1.0);
544  drawWidth = d->pix.width();
545  } else if (d->fillMode == TileHorizontally) {
546  transform.scale(1.0, heightScale);
547  drawHeight = d->pix.height();
548  }
549  } else {
550  if (d->fillMode == PreserveAspectFit) {
551  if (widthScale <= heightScale) {
552  heightScale = widthScale;
553  transform.translate(0, (height() - heightScale * d->pix.height()) / 2);
554  } else if(heightScale < widthScale) {
555  widthScale = heightScale;
556  transform.translate((width() - widthScale * d->pix.width()) / 2, 0);
557  }
558  } else if (d->fillMode == PreserveAspectCrop) {
559  if (widthScale < heightScale) {
560  widthScale = heightScale;
561  transform.translate((width() - widthScale * d->pix.width()) / 2, 0);
562  } else if(heightScale < widthScale) {
563  heightScale = widthScale;
564  transform.translate(0, (height() - heightScale * d->pix.height()) / 2);
565  }
566  }
567  transform.scale(widthScale, heightScale);
568  drawWidth = d->pix.width();
569  drawHeight = d->pix.height();
570  doClip = clip();
571  }
572  }
573 
574  QTransform oldTransform;
575  bool oldAA = p->testRenderHint(QPainter::Antialiasing);
576  bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform);
577  if (d->smooth)
579  if (doClip) {
580  p->save();
581  p->setClipRect(QRectF(0, 0, d->mWidth, d->mHeight), Qt::IntersectClip);
582  }
583  if (d->mirror)
584  transform.translate(drawWidth, 0).scale(-1.0, 1.0);
585  if (!transform.isIdentity()) {
586  oldTransform = p->transform();
587  p->setWorldTransform(transform * oldTransform);
588  }
589 
590  if (d->fillMode >= Tile)
591  p->drawTiledPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix);
592  else
593  p->drawPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix, QRectF(0, 0, drawWidth, drawHeight));
594 
595  if (d->smooth) {
598  }
599  if (doClip)
600  p->restore();
601  if (!transform.isIdentity())
602  p->setWorldTransform(oldTransform);
603 }
604 
606 {
608  // PreserveAspectFit calculates the implicit size differently so we
609  // don't call our superclass pixmapChange(), since that would
610  // result in the implicit size being set incorrectly, then updated
611  // in updatePaintedGeometry()
612  if (d->fillMode != PreserveAspectFit)
615 }
616 
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double qreal
Definition: qglobal.h:1193
const QTransform & transform() const
Returns the world transformation matrix.
Definition: qpainter.cpp:9558
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeParserStatus ** d
bool widthValid() const
Returns whether the width property has been set explicitly.
QRectF boundingRect() const
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
bool heightValid() const
Returns whether the height property has been set explicitly.
QTransform & translate(qreal dx, qreal dy)
Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to t...
Definition: qtransform.cpp:417
bool testRenderHint(RenderHint hint) const
Returns true if hint is set; otherwise returns false.
Definition: qpainter.h:457
void update(const QRectF &rect=QRectF())
Schedules a redraw of the area covered by rect in this item.
#define Q_D(Class)
Definition: qglobal.h:2482
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define Q_Q(Class)
Definition: qglobal.h:2483
QDeclarativeImage(QDeclarativeItem *parent=0)
bool isIdentity() const
Returns true if the matrix is the identity matrix, otherwise returns false.
Definition: qtransform.h:204
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint...
Definition: qpainter.cpp:7620
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QDeclarativeItem class provides the most basic of all visual items in QML.
void paintedGeometryChanged()
#define emit
Definition: qobjectdefs.h:76
bool clip() const
void setImplicitWidth(qreal)
Sets the implied width of the item to w.
void setPixmap(const QPixmap &pix)
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
This function is called to handle this item&#39;s changes in geometry from oldGeometry to newGeometry...
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
This function is called to handle this item&#39;s changes in geometry from oldGeometry to newGeometry...
void setPixmap(const QPixmap &)
void drawTiledPixmap(const QRectF &rect, const QPixmap &pm, const QPointF &offset=QPointF())
Draws a tiled pixmap, inside the given rectangle with its origin at the given position.
Definition: qpainter.cpp:7146
Status status() const
void setRenderHints(RenderHints hints, bool on=true)
Sets the given render hints on the painter if on is true; otherwise clears the render hints...
Definition: qpainter.cpp:7649
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
qreal paintedHeight() const
qreal paintedWidth() const
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630
QPixmap pixmap() const
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QDeclarativeListProperty< QGraphicsTransform > transform()
FillMode fillMode() const
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
QTransform & scale(qreal sx, qreal sy)
Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the mat...
Definition: qtransform.cpp:485
void setImplicitHeight(qreal)
Sets the implied height of the item to h.
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
Definition: qstyleoption.h:867
void setFillMode(FillMode)
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65