Home · All Classes · Main Classes · Deprecated

mimagewidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (directui@nokia.com)
00006 **
00007 ** This file is part of libmeegotouch.
00008 **
00009 ** If you have questions regarding the use of this file, please contact
00010 ** Nokia at directui@nokia.com.
00011 **
00012 ** This library is free software; you can redistribute it and/or
00013 ** modify it under the terms of the GNU Lesser General Public
00014 ** License version 2.1 as published by the Free Software Foundation
00015 ** and appearing in the file LICENSE.LGPL included in the packaging
00016 ** of this file.
00017 **
00018 ****************************************************************************/
00019 
00020 #include <QPainter>
00021 #include <QGraphicsSceneEvent>
00022 #include <QFileInfo>
00023 #include <QPixmap>
00024 
00025 #include "mimagewidget.h"
00026 #include "mimagewidget_p.h"
00027 #include "mtheme.h"
00028 #include "mwidgetstyle.h"
00029 
00030 #include "mwidgetcreator.h"
00031 M_REGISTER_WIDGET(MImageWidget)
00032 
00033 MImageWidgetPrivate::MImageWidgetPrivate()
00034     : MWidgetControllerPrivate(),
00035       pixmap(0),
00036       ownPixmap(true)
00037 {
00038 }
00039 
00040 MImageWidgetPrivate::~MImageWidgetPrivate()
00041 {
00042     if (ownPixmap)
00043         delete pixmap;
00044     else
00045         MTheme::releasePixmap(pixmap);
00046 }
00047 
00048 void MImageWidgetPrivate::cleanUp()
00049 {
00050     if (pixmap != 0) {
00051         if (ownPixmap)
00052             delete pixmap;
00053         else
00054             MTheme::releasePixmap(pixmap);
00055         pixmap = 0;
00056     }
00057     image = QImage();
00058 }
00059 
00060 QSizeF MImageWidgetPrivate::imageDataSize(const QRectF& cropRect) const
00061 {
00062     QSizeF imageSize = QSizeF(0, 0);
00063 
00064     if ((pixmap != 0 && !pixmap->isNull()) || (!image.isNull())) {
00065         if (cropRect.isEmpty()) {
00066             if (image.isNull())
00067                 imageSize = pixmap->size();
00068             else
00069                 imageSize = image.size();
00070         } else {
00071             imageSize = cropRect.size();
00072         }
00073     }
00074 
00075     return imageSize;
00076 }
00077 
00078 void MImageWidgetPrivate::setPixmap(const QPixmap* pixmap, bool takeOwnership)
00079 {
00080     cleanUp();
00081 
00082     this->pixmap = pixmap;
00083     ownPixmap = takeOwnership;
00084 }
00085 
00086 void MImageWidgetPrivate::setImage(const QImage& image)
00087 {
00088     cleanUp();
00089 
00090     this->image = image;
00091 }
00092 
00093 const QPixmap* MImageWidgetPrivate::getPixmap()
00094 {
00095     return this->pixmap;
00096 }
00097 
00098 const QImage& MImageWidgetPrivate::getImage()
00099 {
00100     return this->image;
00101 }
00102 
00103 MImageWidget::MImageWidget(QGraphicsItem *parent) :
00104     MWidgetController(new MImageWidgetPrivate(), new MImageWidgetModel(), parent)
00105 {
00106 }
00107 
00108 MImageWidget::MImageWidget(const QString &id, QGraphicsItem *parent) :
00109     MWidgetController(new MImageWidgetPrivate(), new MImageWidgetModel(), parent)
00110 {
00111     setImage(id);
00112 }
00113 
00114 MImageWidget::MImageWidget(const QImage *image, QGraphicsItem *parent) :
00115     MWidgetController(new MImageWidgetPrivate(), new MImageWidgetModel(), parent)
00116 {
00117     Q_D(MImageWidget);
00118     d->image = *image;
00119 }
00120 
00121 MImageWidget::MImageWidget(const QPixmap *pixmap, QGraphicsItem *parent) :
00122     MWidgetController(new MImageWidgetPrivate(), new MImageWidgetModel(), parent)
00123 {
00124     Q_D(MImageWidget);
00125     d->pixmap = new QPixmap(*pixmap);
00126 }
00127 
00128 MImageWidget::MImageWidget(const MImageWidget &other) :
00129     MWidgetController(new MImageWidgetPrivate(), new MImageWidgetModel(), 0)
00130 {
00131     *this = other;
00132 }
00133 
00134 MImageWidget &MImageWidget::operator=(const MImageWidget &other)
00135 {
00136     Q_D(MImageWidget);
00137 
00138     d->cleanUp();
00139 
00140     if (other.d_func()->ownPixmap) {
00141         if (other.d_func()->pixmap)
00142             d->pixmap = new QPixmap(*(other.d_func()->pixmap));
00143     } else
00144         setImage(other.model()->imageId(), other.model()->imageSize());
00145 
00146     d->ownPixmap = other.d_func()->ownPixmap;
00147 
00148     d->image = other.d_func()->image;
00149 
00150     qreal fx(0), fy(0);
00151 
00152     other.zoomFactor(&fx, &fy);
00153     setZoomFactor(fx, fy);
00154 
00155     model()->setCrop(other.crop());
00156     model()->setAspectRatioMode(other.aspectRatioMode());
00157 
00158     return *this;
00159 }
00160 
00161 MImageWidget::~MImageWidget()
00162 {
00163 }
00164 
00165 void MImageWidget::setImage(const QString &id, const QSize &s)
00166 {
00167     model()->beginTransaction();
00168     model()->setImageId(id);
00169     model()->setImageSize(s);
00170     model()->commitTransaction();
00171 
00172     model()->setCrop(QRect());
00173 
00174     updateGeometry();
00175     update();
00176 }
00177 
00178 QString MImageWidget::image() const
00179 {
00180     return model()->imageId();
00181 }
00182 
00183 QString MImageWidget::imageId() const
00184 {
00185     return model()->imageId();
00186 }
00187 
00188 QSize MImageWidget::imageSize() const
00189 {
00190     Q_D(const MImageWidget);
00191     if (d->pixmap)
00192         return d->pixmap->size();
00193     else
00194         return d->image.size();
00195 }
00196 
00197 const QPixmap *MImageWidget::pixmap() const
00198 {
00199     Q_D(const MImageWidget);
00200     if (d->pixmap)
00201         return d->pixmap;
00202     else {
00203         d->imagePlaceHolder = QPixmap::fromImage(d->image);
00204         return &d->imagePlaceHolder;
00205     }
00206 }
00207 
00208 void MImageWidget::setZoomFactor(qreal factor)
00209 {
00210     if (factor < 0.0) return;
00211 
00212     model()->setZoomFactorX(factor);
00213     model()->setZoomFactorY(factor);
00214 }
00215 
00216 void MImageWidget::setZoomFactor(qreal fx, qreal fy)
00217 {
00218     if (fx >= 0.0)
00219         model()->setZoomFactorX(fx);
00220 
00221     if (fy >= 0.0)
00222         model()->setZoomFactorY(fy);
00223 
00224     // If zoomFactorX not equals zoomFactorY, set mode to Qt::IgnoreAspectRatio automatic
00225     if (fx != fy)
00226         model()->setAspectRatioMode(Qt::IgnoreAspectRatio);
00227 }
00228 
00229 void MImageWidget::zoomFactor(qreal *fx, qreal *fy) const
00230 {
00231     Q_D(const MImageWidget);
00232 
00233     if(fx)
00234         *fx = model()->zoomFactorX();
00235     if(fy)
00236         *fy = model()->zoomFactorY();
00237 
00238     if ((fx && *fx == 0) || (fy && *fy == 0)) {
00239         // If the zoom factor is 0, calculate it with imageSize, targetSize and widgetSize
00240         QSizeF buffer;
00241         QSizeF imageSize = d->imageDataSize(model()->crop());
00242 
00243         if (imageSize.isEmpty())
00244             return;
00245 
00246         buffer = imageSize;
00247 
00248         QSizeF marginSize = QSizeF(style()->marginLeft() + style()->marginRight(), style()->marginTop() + style()->marginBottom());
00249         QSizeF widgetSize = size() - marginSize;
00250 
00251         buffer.scale(widgetSize, model()->aspectRatioMode());
00252 
00253         if (fx && *fx == 0)
00254             *fx = buffer.width() / imageSize.width();
00255 
00256         if (fy && *fy == 0)
00257             *fy = buffer.height() / imageSize.height();
00258     }
00259 }
00260 
00261 void MImageWidget::zoomIn()
00262 {
00263     qreal fx(0), fy(0);
00264 
00265     zoomFactor(&fx, &fy);
00266     setZoomFactor(fx * 2.0, fy * 2.0);
00267 }
00268 
00269 void MImageWidget::zoomOut()
00270 {
00271     qreal fx(0), fy(0);
00272 
00273     zoomFactor(&fx, &fy);
00274     setZoomFactor(fx / 2.0, fy / 2.0);
00275 }
00276 
00277 void MImageWidget::setAspectRatioMode(Qt::AspectRatioMode mode)
00278 {
00279     if (model()->aspectRatioMode() == mode)
00280         return;
00281     model()->setAspectRatioMode(mode);
00282 }
00283 
00284 Qt::AspectRatioMode MImageWidget::aspectRatioMode() const
00285 {
00286     return model()->aspectRatioMode();
00287 }
00288 
00289 void MImageWidget::setCrop(const QRectF &rect)
00290 {
00291     Q_D(MImageWidget);
00292     if (d->pixmap == 0 && d->image.isNull())
00293         return;
00294 
00295     if (!rect.isValid())
00296         return;
00297 
00298     model()->setCrop(rect);
00299 }
00300 
00301 QRectF MImageWidget::crop() const
00302 {
00303     return model()->crop();
00304 }
00305 
00306 void MImageWidget::setImage(const QString &id)
00307 {
00308     setImage(id, QSize());
00309 }
00310 
00311 void MImageWidget::setImage(const QImage &image)
00312 {
00313     Q_D(MImageWidget);
00314     setImage(QString(), QSize());
00315 
00316     d->setImage(image);
00317 
00318     model()->setCrop(QRect());
00319 
00320     updateGeometry();
00321     update();
00322 }
00323 
00324 void MImageWidget::setPixmap(const QPixmap &pixmap)
00325 {
00326     Q_D(MImageWidget);
00327     setImage(QString(), QSize());
00328 
00329     QPixmap* newPixmap = new QPixmap(pixmap);
00330     d->setPixmap(newPixmap, true);
00331 
00332     model()->setCrop(QRect());
00333 
00334     updateGeometry();
00335     update();
00336 }

Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:20 (PDT)
Doxygen 1.7.1
MeeGo Touch