2010-06-18 11:02:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-06-18 11:02:48 +02:00
|
|
|
** All rights reserved.
|
|
|
|
|
** Copyright (c) 2010 Denis Mingulov.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
|
**
|
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
|
** met:
|
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
|
** distribution.
|
|
|
|
|
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|
|
|
|
** the names of its contributors may be used to endorse or promote
|
|
|
|
|
** products derived from this software without specific prior written
|
|
|
|
|
** permission.
|
|
|
|
|
**
|
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
2011-05-06 15:05:37 +02:00
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
#include "imageview.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtGui/QWheelEvent>
|
|
|
|
|
#include <QtGui/QMouseEvent>
|
|
|
|
|
#include <QtGui/QGraphicsRectItem>
|
|
|
|
|
#include <QtGui/QPixmap>
|
2010-09-13 12:01:12 +02:00
|
|
|
#ifndef QT_NO_SVG
|
2010-06-18 11:02:48 +02:00
|
|
|
#include <QtSvg/QGraphicsSvgItem>
|
2010-09-13 12:01:12 +02:00
|
|
|
#endif
|
2010-06-18 11:02:48 +02:00
|
|
|
#include <QtGui/QImageReader>
|
|
|
|
|
#include <qmath.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ImageViewer {
|
|
|
|
|
namespace Constants {
|
|
|
|
|
const qreal DEFAULT_SCALE_FACTOR = 1.2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
struct ImageViewPrivate
|
|
|
|
|
{
|
|
|
|
|
ImageViewPrivate() : imageItem(0), backgroundItem(0), outlineItem(0) {}
|
|
|
|
|
QGraphicsItem *imageItem;
|
|
|
|
|
QGraphicsRectItem *backgroundItem;
|
|
|
|
|
QGraphicsRectItem *outlineItem;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ImageView::ImageView(QWidget *parent)
|
|
|
|
|
: QGraphicsView(parent),
|
2011-09-02 12:16:56 +02:00
|
|
|
d(new ImageViewPrivate())
|
2010-06-18 11:02:48 +02:00
|
|
|
{
|
|
|
|
|
setScene(new QGraphicsScene(this));
|
|
|
|
|
setTransformationAnchor(AnchorUnderMouse);
|
|
|
|
|
setDragMode(ScrollHandDrag);
|
|
|
|
|
setViewportUpdateMode(FullViewportUpdate);
|
2010-06-18 11:02:49 +02:00
|
|
|
setFrameShape(QFrame::NoFrame);
|
2010-06-18 11:02:49 +02:00
|
|
|
setRenderHint(QPainter::SmoothPixmapTransform);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// Prepare background check-board pattern
|
|
|
|
|
QPixmap tilePixmap(64, 64);
|
|
|
|
|
tilePixmap.fill(Qt::white);
|
|
|
|
|
QPainter tilePainter(&tilePixmap);
|
|
|
|
|
QColor color(220, 220, 220);
|
|
|
|
|
tilePainter.fillRect(0, 0, 0x20, 0x20, color);
|
|
|
|
|
tilePainter.fillRect(0x20, 0x20, 0x20, 0x20, color);
|
|
|
|
|
tilePainter.end();
|
|
|
|
|
|
|
|
|
|
setBackgroundBrush(tilePixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageView::~ImageView()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::drawBackground(QPainter *p, const QRectF &)
|
|
|
|
|
{
|
|
|
|
|
p->save();
|
|
|
|
|
p->resetTransform();
|
|
|
|
|
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
|
|
|
|
|
p->restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageView::openFile(QString fileName)
|
|
|
|
|
{
|
2010-09-13 12:01:12 +02:00
|
|
|
#ifndef QT_NO_SVG
|
2010-06-18 11:02:48 +02:00
|
|
|
bool isSvg = false;
|
2010-09-13 12:01:12 +02:00
|
|
|
#endif
|
2010-06-18 11:02:48 +02:00
|
|
|
QByteArray format = QImageReader::imageFormat(fileName);
|
|
|
|
|
|
|
|
|
|
// if it is impossible to recognize a file format - file will not be open correctly
|
|
|
|
|
if (format.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-09-13 12:01:12 +02:00
|
|
|
#ifndef QT_NO_SVG
|
2010-06-18 11:02:48 +02:00
|
|
|
if (format.startsWith("svg"))
|
|
|
|
|
isSvg = true;
|
2010-09-13 12:01:12 +02:00
|
|
|
#endif
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
QGraphicsScene *s = scene();
|
|
|
|
|
|
2011-09-02 12:16:56 +02:00
|
|
|
bool drawBackground = (d->backgroundItem ? d->backgroundItem->isVisible() : false);
|
|
|
|
|
bool drawOutline = (d->outlineItem ? d->outlineItem->isVisible() : true);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
s->clear();
|
|
|
|
|
resetTransform();
|
|
|
|
|
|
|
|
|
|
// image
|
2010-09-13 12:01:12 +02:00
|
|
|
#ifndef QT_NO_SVG
|
2010-06-18 11:02:48 +02:00
|
|
|
if (isSvg) {
|
2011-09-02 12:16:56 +02:00
|
|
|
d->imageItem = new QGraphicsSvgItem(fileName);
|
2010-09-13 12:01:12 +02:00
|
|
|
} else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2010-06-18 11:02:48 +02:00
|
|
|
QPixmap pixmap(fileName);
|
2010-06-18 11:02:49 +02:00
|
|
|
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
|
|
|
|
|
pixmapItem->setTransformationMode(Qt::SmoothTransformation);
|
2011-09-02 12:16:56 +02:00
|
|
|
d->imageItem = pixmapItem;
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
2011-09-02 12:16:56 +02:00
|
|
|
d->imageItem->setCacheMode(QGraphicsItem::NoCache);
|
|
|
|
|
d->imageItem->setZValue(0);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// background item
|
2011-09-02 12:16:56 +02:00
|
|
|
d->backgroundItem = new QGraphicsRectItem(d->imageItem->boundingRect());
|
|
|
|
|
d->backgroundItem->setBrush(Qt::white);
|
|
|
|
|
d->backgroundItem->setPen(Qt::NoPen);
|
|
|
|
|
d->backgroundItem->setVisible(drawBackground);
|
|
|
|
|
d->backgroundItem->setZValue(-1);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// outline
|
2011-09-02 12:16:56 +02:00
|
|
|
d->outlineItem = new QGraphicsRectItem(d->imageItem->boundingRect());
|
2010-06-18 11:02:49 +02:00
|
|
|
QPen outline(Qt::black, 1, Qt::DashLine);
|
2010-06-18 11:02:48 +02:00
|
|
|
outline.setCosmetic(true);
|
2011-09-02 12:16:56 +02:00
|
|
|
d->outlineItem->setPen(outline);
|
|
|
|
|
d->outlineItem->setBrush(Qt::NoBrush);
|
|
|
|
|
d->outlineItem->setVisible(drawOutline);
|
|
|
|
|
d->outlineItem->setZValue(1);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
2011-09-02 12:16:56 +02:00
|
|
|
s->addItem(d->backgroundItem);
|
|
|
|
|
s->addItem(d->imageItem);
|
|
|
|
|
s->addItem(d->outlineItem);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// if image size is 0x0, then it is not loaded
|
2011-09-02 12:16:56 +02:00
|
|
|
if (d->imageItem->boundingRect().height() == 0 && d->imageItem->boundingRect().width() == 0)
|
2010-06-18 11:02:48 +02:00
|
|
|
return false;
|
|
|
|
|
emitScaleFactor();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::setViewBackground(bool enable)
|
|
|
|
|
{
|
2011-09-02 12:16:56 +02:00
|
|
|
if (!d->backgroundItem)
|
2010-06-18 11:02:48 +02:00
|
|
|
return;
|
|
|
|
|
|
2011-09-02 12:16:56 +02:00
|
|
|
d->backgroundItem->setVisible(enable);
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::setViewOutline(bool enable)
|
|
|
|
|
{
|
2011-09-02 12:16:56 +02:00
|
|
|
if (!d->outlineItem)
|
2010-06-18 11:02:48 +02:00
|
|
|
return;
|
|
|
|
|
|
2011-09-02 12:16:56 +02:00
|
|
|
d->outlineItem->setVisible(enable);
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::doScale(qreal factor)
|
|
|
|
|
{
|
|
|
|
|
scale(factor, factor);
|
|
|
|
|
emitScaleFactor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::wheelEvent(QWheelEvent *event)
|
|
|
|
|
{
|
|
|
|
|
qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->delta() / 240.0);
|
|
|
|
|
doScale(factor);
|
|
|
|
|
event->accept();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::zoomIn()
|
|
|
|
|
{
|
|
|
|
|
doScale(Constants::DEFAULT_SCALE_FACTOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::zoomOut()
|
|
|
|
|
{
|
|
|
|
|
doScale(1. / Constants::DEFAULT_SCALE_FACTOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::resetToOriginalSize()
|
|
|
|
|
{
|
|
|
|
|
resetTransform();
|
|
|
|
|
emitScaleFactor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::fitToScreen()
|
|
|
|
|
{
|
2011-09-02 12:16:56 +02:00
|
|
|
fitInView(d->imageItem, Qt::KeepAspectRatio);
|
2010-06-18 11:02:48 +02:00
|
|
|
emitScaleFactor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::emitScaleFactor()
|
|
|
|
|
{
|
|
|
|
|
// get scale factor directly from the transform matrix
|
|
|
|
|
qreal factor = transform().m11();
|
|
|
|
|
emit scaleFactorChanged(factor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ImageView
|