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.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
2020-09-21 11:19:15 +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.
|
|
|
|
|
**
|
2020-09-21 11:19:15 +02:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-06-18 11:02:48 +02:00
|
|
|
**
|
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"
|
|
|
|
|
|
2016-03-17 16:02:20 +01:00
|
|
|
#include "exportdialog.h"
|
2018-01-12 15:32:10 +01:00
|
|
|
#include "multiexportdialog.h"
|
2015-05-29 13:23:52 +02:00
|
|
|
#include "imageviewerfile.h"
|
|
|
|
|
|
2016-03-17 16:02:20 +01:00
|
|
|
#include <coreplugin/messagemanager.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QGraphicsRectItem>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QWheelEvent>
|
|
|
|
|
#include <QMouseEvent>
|
2016-03-17 16:02:20 +01:00
|
|
|
#include <QImage>
|
|
|
|
|
#include <QPainter>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QPixmap>
|
2016-03-17 16:02:20 +01:00
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
#include <qmath.h>
|
|
|
|
|
|
2016-03-21 12:19:49 +01:00
|
|
|
#ifndef QT_NO_SVG
|
|
|
|
|
#include <QGraphicsSvgItem>
|
|
|
|
|
#include <QSvgRenderer>
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-06-18 11:02:48 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 12:24:06 +01:00
|
|
|
ImageView::~ImageView() = default;
|
2010-06-18 11:02:48 +02:00
|
|
|
|
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();
|
2019-04-10 17:40:38 +02:00
|
|
|
p->setRenderHint(QPainter::SmoothPixmapTransform, false);
|
2015-05-29 13:23:52 +02:00
|
|
|
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
|
|
|
|
|
p->restore();
|
2012-04-27 15:00:02 +04:00
|
|
|
}
|
|
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
QImage ImageView::renderSvg(const QSize &imageSize) const
|
|
|
|
|
{
|
|
|
|
|
QImage image(imageSize, QImage::Format_ARGB32);
|
|
|
|
|
image.fill(Qt::transparent);
|
|
|
|
|
QPainter painter;
|
|
|
|
|
painter.begin(&image);
|
|
|
|
|
#ifndef QT_NO_SVG
|
2018-11-11 12:24:06 +01:00
|
|
|
auto svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem);
|
2018-01-12 15:32:10 +01:00
|
|
|
QTC_ASSERT(svgItem, return image);
|
|
|
|
|
svgItem->renderer()->render(&painter, QRectF(QPointF(), QSizeF(imageSize)));
|
|
|
|
|
#endif
|
|
|
|
|
painter.end();
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageView::exportSvg(const ExportData &ed)
|
|
|
|
|
{
|
|
|
|
|
const bool result = renderSvg(ed.size).save(ed.fileName);
|
|
|
|
|
if (result) {
|
|
|
|
|
const QString message = tr("Exported \"%1\", %2x%3, %4 bytes")
|
|
|
|
|
.arg(QDir::toNativeSeparators(ed.fileName))
|
|
|
|
|
.arg(ed.size.width()).arg(ed.size.height())
|
|
|
|
|
.arg(QFileInfo(ed.fileName).size());
|
|
|
|
|
Core::MessageManager::write(message);
|
|
|
|
|
} else {
|
|
|
|
|
const QString message = tr("Could not write file \"%1\".").arg(QDir::toNativeSeparators(ed.fileName));
|
|
|
|
|
QMessageBox::critical(this, tr("Export Image"), message);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 13:36:38 +02:00
|
|
|
#ifndef QT_NO_SVG
|
2018-01-12 15:32:10 +01:00
|
|
|
static QString suggestedExportFileName(const QFileInfo &fi)
|
|
|
|
|
{
|
|
|
|
|
return fi.absolutePath() + QLatin1Char('/') + fi.baseName()
|
|
|
|
|
+ QStringLiteral(".png");
|
|
|
|
|
}
|
2018-04-23 13:36:38 +02:00
|
|
|
#endif
|
2018-01-12 15:32:10 +01:00
|
|
|
|
|
|
|
|
QSize ImageView::svgSize() const
|
|
|
|
|
{
|
|
|
|
|
QSize result;
|
|
|
|
|
#ifndef QT_NO_SVG
|
|
|
|
|
if (const QGraphicsSvgItem *svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem))
|
|
|
|
|
result = svgItem->boundingRect().size().toSize();
|
|
|
|
|
#endif // !QT_NO_SVG
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 16:02:20 +01:00
|
|
|
void ImageView::exportImage()
|
|
|
|
|
{
|
|
|
|
|
#ifndef QT_NO_SVG
|
2018-11-11 12:24:06 +01:00
|
|
|
auto svgItem = qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem);
|
2016-03-17 16:02:20 +01:00
|
|
|
QTC_ASSERT(svgItem, return);
|
|
|
|
|
|
|
|
|
|
const QFileInfo origFi = m_file->filePath().toFileInfo();
|
|
|
|
|
ExportDialog exportDialog(this);
|
|
|
|
|
exportDialog.setWindowTitle(tr("Export %1").arg(origFi.fileName()));
|
2018-01-12 15:32:10 +01:00
|
|
|
exportDialog.setExportSize(svgSize());
|
|
|
|
|
exportDialog.setExportFileName(suggestedExportFileName(origFi));
|
2016-03-17 16:02:20 +01:00
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
while (exportDialog.exec() == QDialog::Accepted && !exportSvg(exportDialog.exportData())) {}
|
|
|
|
|
#endif // !QT_NO_SVG
|
|
|
|
|
}
|
2016-03-17 16:02:20 +01:00
|
|
|
|
2018-01-12 15:32:10 +01:00
|
|
|
void ImageView::exportMultiImages()
|
|
|
|
|
{
|
|
|
|
|
#ifndef QT_NO_SVG
|
|
|
|
|
QTC_ASSERT(qgraphicsitem_cast<QGraphicsSvgItem *>(m_imageItem), return);
|
|
|
|
|
|
|
|
|
|
const QFileInfo origFi = m_file->filePath().toFileInfo();
|
|
|
|
|
const QSize size = svgSize();
|
|
|
|
|
const QString title =
|
2018-07-13 13:43:45 +02:00
|
|
|
tr("Export a Series of Images from %1 (%2x%3)")
|
2018-01-12 15:32:10 +01:00
|
|
|
.arg(origFi.fileName()).arg(size.width()).arg(size.height());
|
|
|
|
|
MultiExportDialog multiExportDialog;
|
|
|
|
|
multiExportDialog.setWindowTitle(title);
|
|
|
|
|
multiExportDialog.setExportFileName(suggestedExportFileName(origFi));
|
|
|
|
|
multiExportDialog.setSvgSize(size);
|
|
|
|
|
multiExportDialog.suggestSizes();
|
|
|
|
|
|
|
|
|
|
while (multiExportDialog.exec() == QDialog::Accepted) {
|
|
|
|
|
const auto exportData = multiExportDialog.exportData();
|
|
|
|
|
bool ok = true;
|
|
|
|
|
for (const auto &data : exportData) {
|
|
|
|
|
if (!exportSvg(data)) {
|
|
|
|
|
ok = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-03-17 16:02:20 +01:00
|
|
|
}
|
2018-01-12 15:32:10 +01:00
|
|
|
if (ok)
|
|
|
|
|
break;
|
2016-03-17 16:02:20 +01:00
|
|
|
}
|
|
|
|
|
#endif // !QT_NO_SVG
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2018-11-11 12:24:06 +01:00
|
|
|
if (auto 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)
|
|
|
|
|
{
|
2019-08-30 10:12:38 +02:00
|
|
|
qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->angleDelta().y() / 240.0);
|
2010-06-18 11:02:48 +02:00
|
|
|
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
|