2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Denis Mingulov.
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Copyright (C) 2016 Denis Mingulov.
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** BSD License Usage
|
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
|
** as follows:
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
|
|
|
|
** "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.
|
2016-01-15 14:57:40 +01:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
|
**
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
|
|
|
|
** 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."
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
****************************************************************************/
|
2011-05-06 15:05:37 +02:00
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
#include "imageview.h"
|
|
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
#include "imageviewerfile.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QWheelEvent>
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QGraphicsRectItem>
|
|
|
|
|
#include <QPixmap>
|
2010-06-18 11:02:48 +02:00
|
|
|
#include <qmath.h>
|
|
|
|
|
|
|
|
|
|
namespace ImageViewer {
|
|
|
|
|
namespace Constants {
|
|
|
|
|
const qreal DEFAULT_SCALE_FACTOR = 1.2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
ImageView::ImageView(ImageViewerFile *file)
|
|
|
|
|
: m_file(file)
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
void ImageView::reset()
|
2010-06-18 11:02:48 +02:00
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
scene()->clear();
|
|
|
|
|
resetTransform();
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
void ImageView::createScene()
|
2010-06-18 11:02:48 +02:00
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
m_imageItem = m_file->createGraphicsItem();
|
|
|
|
|
if (!m_imageItem) // failed to load
|
|
|
|
|
return;
|
|
|
|
|
m_imageItem->setCacheMode(QGraphicsItem::NoCache);
|
|
|
|
|
m_imageItem->setZValue(0);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// background item
|
2015-05-29 13:23:52 +02:00
|
|
|
m_backgroundItem = new QGraphicsRectItem(m_imageItem->boundingRect());
|
|
|
|
|
m_backgroundItem->setBrush(Qt::white);
|
|
|
|
|
m_backgroundItem->setPen(Qt::NoPen);
|
|
|
|
|
m_backgroundItem->setVisible(m_showBackground);
|
|
|
|
|
m_backgroundItem->setZValue(-1);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
// outline
|
2015-05-29 13:23:52 +02:00
|
|
|
m_outlineItem = new QGraphicsRectItem(m_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);
|
2015-05-29 13:23:52 +02:00
|
|
|
m_outlineItem->setPen(outline);
|
|
|
|
|
m_outlineItem->setBrush(Qt::NoBrush);
|
|
|
|
|
m_outlineItem->setVisible(m_showOutline);
|
|
|
|
|
m_outlineItem->setZValue(1);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
QGraphicsScene *s = scene();
|
|
|
|
|
s->addItem(m_backgroundItem);
|
|
|
|
|
s->addItem(m_imageItem);
|
|
|
|
|
s->addItem(m_outlineItem);
|
2010-06-18 11:02:48 +02:00
|
|
|
|
|
|
|
|
emitScaleFactor();
|
2012-05-10 15:14:21 +04:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 13:23:52 +02:00
|
|
|
void ImageView::drawBackground(QPainter *p, const QRectF &)
|
2012-05-10 15:14:21 +04:00
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
p->save();
|
|
|
|
|
p->resetTransform();
|
|
|
|
|
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
|
|
|
|
|
p->restore();
|
2012-04-27 15:00:02 +04:00
|
|
|
}
|
|
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
void ImageView::setViewBackground(bool enable)
|
|
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
m_showBackground = enable;
|
|
|
|
|
if (m_backgroundItem)
|
|
|
|
|
m_backgroundItem->setVisible(enable);
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::setViewOutline(bool enable)
|
|
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
m_showOutline = enable;
|
|
|
|
|
if (m_outlineItem)
|
|
|
|
|
m_outlineItem->setVisible(enable);
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::doScale(qreal factor)
|
|
|
|
|
{
|
2011-12-09 07:39:20 +01:00
|
|
|
qreal currentScale = transform().m11();
|
|
|
|
|
qreal newScale = currentScale * factor;
|
|
|
|
|
qreal actualFactor = factor;
|
|
|
|
|
// cap to 0.001 - 1000
|
|
|
|
|
if (newScale > 1000)
|
|
|
|
|
actualFactor = 1000./currentScale;
|
|
|
|
|
else if (newScale < 0.001)
|
|
|
|
|
actualFactor = 0.001/currentScale;
|
|
|
|
|
|
|
|
|
|
scale(actualFactor, actualFactor);
|
2010-06-18 11:02:48 +02:00
|
|
|
emitScaleFactor();
|
2015-05-29 13:23:52 +02:00
|
|
|
if (QGraphicsPixmapItem *pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(m_imageItem))
|
2014-07-24 16:29:40 +02:00
|
|
|
pixmapItem->setTransformationMode(
|
|
|
|
|
transform().m11() < 1 ? Qt::SmoothTransformation : Qt::FastTransformation);
|
2010-06-18 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
fitInView(m_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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-10 15:14:21 +04:00
|
|
|
void ImageView::showEvent(QShowEvent *)
|
|
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
m_file->updateVisibility();
|
2012-05-10 15:14:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::hideEvent(QHideEvent *)
|
|
|
|
|
{
|
2015-05-29 13:23:52 +02:00
|
|
|
m_file->updateVisibility();
|
2012-05-10 15:14:21 +04:00
|
|
|
}
|
|
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ImageView
|