forked from qt-creator/qt-creator
ImageViewer: Support splitting
Change-Id: Id789bd4a77d14ff990f3c9092a9ba533ac4fab7a Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
@@ -39,16 +39,12 @@
|
||||
|
||||
#include "imageview.h"
|
||||
|
||||
#include <QFile>
|
||||
#include "imageviewerfile.h"
|
||||
|
||||
#include <QWheelEvent>
|
||||
#include <QMouseEvent>
|
||||
#include <QMovie>
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QPixmap>
|
||||
#ifndef QT_NO_SVG
|
||||
#include <QGraphicsSvgItem>
|
||||
#endif
|
||||
#include <QImageReader>
|
||||
#include <qmath.h>
|
||||
|
||||
namespace ImageViewer {
|
||||
@@ -58,46 +54,8 @@ namespace Constants {
|
||||
|
||||
namespace Internal {
|
||||
|
||||
class MovieItem : public QGraphicsPixmapItem
|
||||
{
|
||||
public:
|
||||
MovieItem(QMovie *movie)
|
||||
: m_movie(movie)
|
||||
{
|
||||
setPixmap(m_movie->currentPixmap());
|
||||
}
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
|
||||
{
|
||||
const bool smoothTransform = painter->worldTransform().m11() < 1;
|
||||
painter->setRenderHint(QPainter::SmoothPixmapTransform, smoothTransform);
|
||||
painter->drawPixmap(offset(), m_movie->currentPixmap());
|
||||
}
|
||||
|
||||
private:
|
||||
QMovie *m_movie;
|
||||
};
|
||||
|
||||
struct ImageViewPrivate
|
||||
{
|
||||
ImageViewPrivate()
|
||||
: imageItem(0)
|
||||
, backgroundItem(0)
|
||||
, outlineItem(0)
|
||||
, movie(0)
|
||||
, moviePaused(true)
|
||||
{}
|
||||
|
||||
QGraphicsItem *imageItem;
|
||||
QGraphicsRectItem *backgroundItem;
|
||||
QGraphicsRectItem *outlineItem;
|
||||
QMovie *movie;
|
||||
bool moviePaused;
|
||||
};
|
||||
|
||||
ImageView::ImageView(QWidget *parent)
|
||||
: QGraphicsView(parent),
|
||||
d(new ImageViewPrivate())
|
||||
ImageView::ImageView(ImageViewerFile *file)
|
||||
: m_file(file)
|
||||
{
|
||||
setScene(new QGraphicsScene(this));
|
||||
setTransformationAnchor(AnchorUnderMouse);
|
||||
@@ -120,7 +78,44 @@ ImageView::ImageView(QWidget *parent)
|
||||
|
||||
ImageView::~ImageView()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void ImageView::reset()
|
||||
{
|
||||
scene()->clear();
|
||||
resetTransform();
|
||||
}
|
||||
|
||||
void ImageView::createScene()
|
||||
{
|
||||
m_imageItem = m_file->createGraphicsItem();
|
||||
if (!m_imageItem) // failed to load
|
||||
return;
|
||||
m_imageItem->setCacheMode(QGraphicsItem::NoCache);
|
||||
m_imageItem->setZValue(0);
|
||||
|
||||
// background item
|
||||
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);
|
||||
|
||||
// outline
|
||||
m_outlineItem = new QGraphicsRectItem(m_imageItem->boundingRect());
|
||||
QPen outline(Qt::black, 1, Qt::DashLine);
|
||||
outline.setCosmetic(true);
|
||||
m_outlineItem->setPen(outline);
|
||||
m_outlineItem->setBrush(Qt::NoBrush);
|
||||
m_outlineItem->setVisible(m_showOutline);
|
||||
m_outlineItem->setZValue(1);
|
||||
|
||||
QGraphicsScene *s = scene();
|
||||
s->addItem(m_backgroundItem);
|
||||
s->addItem(m_imageItem);
|
||||
s->addItem(m_outlineItem);
|
||||
|
||||
emitScaleFactor();
|
||||
}
|
||||
|
||||
void ImageView::drawBackground(QPainter *p, const QRectF &)
|
||||
@@ -131,118 +126,18 @@ void ImageView::drawBackground(QPainter *p, const QRectF &)
|
||||
p->restore();
|
||||
}
|
||||
|
||||
bool ImageView::openFile(QString fileName)
|
||||
{
|
||||
#ifndef QT_NO_SVG
|
||||
bool isSvg = false;
|
||||
#endif
|
||||
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;
|
||||
|
||||
#ifndef QT_NO_SVG
|
||||
if (format.startsWith("svg"))
|
||||
isSvg = true;
|
||||
#endif
|
||||
QGraphicsScene *s = scene();
|
||||
|
||||
bool drawBackground = (d->backgroundItem ? d->backgroundItem->isVisible() : false);
|
||||
bool drawOutline = (d->outlineItem ? d->outlineItem->isVisible() : true);
|
||||
|
||||
s->clear();
|
||||
resetTransform();
|
||||
delete d->movie;
|
||||
d->movie = 0;
|
||||
|
||||
// image
|
||||
#ifndef QT_NO_SVG
|
||||
if (isSvg) {
|
||||
d->imageItem = new QGraphicsSvgItem(fileName);
|
||||
emit imageSizeChanged(QSize());
|
||||
} else
|
||||
#endif
|
||||
if (QMovie::supportedFormats().contains(format)) {
|
||||
d->movie = new QMovie(fileName, QByteArray(), this);
|
||||
d->movie->setCacheMode(QMovie::CacheAll);
|
||||
connect(d->movie, SIGNAL(finished()), d->movie, SLOT(start()));
|
||||
connect(d->movie, SIGNAL(updated(QRect)), this, SLOT(updatePixmap(QRect)));
|
||||
connect(d->movie, SIGNAL(resized(QSize)), this, SLOT(pixmapResized(QSize)));
|
||||
d->movie->start();
|
||||
d->moviePaused = false;
|
||||
d->imageItem = new MovieItem(d->movie);
|
||||
} else {
|
||||
QPixmap pixmap(fileName);
|
||||
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
|
||||
pixmapItem->setTransformationMode(Qt::SmoothTransformation);
|
||||
d->imageItem = pixmapItem;
|
||||
emit imageSizeChanged(pixmap.size());
|
||||
}
|
||||
d->imageItem->setCacheMode(QGraphicsItem::NoCache);
|
||||
d->imageItem->setZValue(0);
|
||||
|
||||
// background item
|
||||
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);
|
||||
|
||||
// outline
|
||||
d->outlineItem = new QGraphicsRectItem(d->imageItem->boundingRect());
|
||||
QPen outline(Qt::black, 1, Qt::DashLine);
|
||||
outline.setCosmetic(true);
|
||||
d->outlineItem->setPen(outline);
|
||||
d->outlineItem->setBrush(Qt::NoBrush);
|
||||
d->outlineItem->setVisible(drawOutline);
|
||||
d->outlineItem->setZValue(1);
|
||||
|
||||
s->addItem(d->backgroundItem);
|
||||
s->addItem(d->imageItem);
|
||||
s->addItem(d->outlineItem);
|
||||
|
||||
// if image size is 0x0, then it is not loaded
|
||||
if (d->imageItem->boundingRect().height() == 0 && d->imageItem->boundingRect().width() == 0)
|
||||
return false;
|
||||
emitScaleFactor();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ImageView::isAnimated() const
|
||||
{
|
||||
return d->movie;
|
||||
}
|
||||
|
||||
bool ImageView::isPaused() const
|
||||
{
|
||||
return d->moviePaused;
|
||||
}
|
||||
|
||||
void ImageView::setPaused(bool paused)
|
||||
{
|
||||
if (!d->movie)
|
||||
return;
|
||||
|
||||
d->movie->setPaused(paused);
|
||||
d->moviePaused = paused;
|
||||
}
|
||||
|
||||
void ImageView::setViewBackground(bool enable)
|
||||
{
|
||||
if (!d->backgroundItem)
|
||||
return;
|
||||
|
||||
d->backgroundItem->setVisible(enable);
|
||||
m_showBackground = enable;
|
||||
if (m_backgroundItem)
|
||||
m_backgroundItem->setVisible(enable);
|
||||
}
|
||||
|
||||
void ImageView::setViewOutline(bool enable)
|
||||
{
|
||||
if (!d->outlineItem)
|
||||
return;
|
||||
|
||||
d->outlineItem->setVisible(enable);
|
||||
m_showOutline = enable;
|
||||
if (m_outlineItem)
|
||||
m_outlineItem->setVisible(enable);
|
||||
}
|
||||
|
||||
void ImageView::doScale(qreal factor)
|
||||
@@ -258,22 +153,11 @@ void ImageView::doScale(qreal factor)
|
||||
|
||||
scale(actualFactor, actualFactor);
|
||||
emitScaleFactor();
|
||||
if (QGraphicsPixmapItem *pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(d->imageItem))
|
||||
if (QGraphicsPixmapItem *pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(m_imageItem))
|
||||
pixmapItem->setTransformationMode(
|
||||
transform().m11() < 1 ? Qt::SmoothTransformation : Qt::FastTransformation);
|
||||
}
|
||||
|
||||
void ImageView::updatePixmap(const QRect &rect)
|
||||
{
|
||||
if (d->imageItem)
|
||||
d->imageItem->update(rect);
|
||||
}
|
||||
|
||||
void ImageView::pixmapResized(const QSize &size)
|
||||
{
|
||||
emit imageSizeChanged(size);
|
||||
}
|
||||
|
||||
void ImageView::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->delta() / 240.0);
|
||||
@@ -299,7 +183,7 @@ void ImageView::resetToOriginalSize()
|
||||
|
||||
void ImageView::fitToScreen()
|
||||
{
|
||||
fitInView(d->imageItem, Qt::KeepAspectRatio);
|
||||
fitInView(m_imageItem, Qt::KeepAspectRatio);
|
||||
emitScaleFactor();
|
||||
}
|
||||
|
||||
@@ -312,18 +196,12 @@ void ImageView::emitScaleFactor()
|
||||
|
||||
void ImageView::showEvent(QShowEvent *)
|
||||
{
|
||||
if (!d->movie)
|
||||
return;
|
||||
|
||||
d->movie->setPaused(d->moviePaused);
|
||||
m_file->updateVisibility();
|
||||
}
|
||||
|
||||
void ImageView::hideEvent(QHideEvent *)
|
||||
{
|
||||
if (!d->movie)
|
||||
return;
|
||||
|
||||
d->movie->setPaused(true);
|
||||
m_file->updateVisibility();
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
@@ -45,18 +45,18 @@
|
||||
namespace ImageViewer {
|
||||
namespace Internal {
|
||||
|
||||
class ImageViewerFile;
|
||||
|
||||
class ImageView : public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ImageView(QWidget *parent = 0);
|
||||
ImageView(ImageViewerFile *file);
|
||||
~ImageView();
|
||||
|
||||
bool openFile(QString fileName);
|
||||
bool isAnimated() const;
|
||||
bool isPaused() const;
|
||||
void setPaused(bool paused);
|
||||
void reset();
|
||||
void createScene();
|
||||
|
||||
signals:
|
||||
void scaleFactorChanged(qreal factor);
|
||||
@@ -72,9 +72,6 @@ public slots:
|
||||
|
||||
private slots:
|
||||
void emitScaleFactor();
|
||||
void doScale(qreal factor);
|
||||
void updatePixmap(const QRect &rect);
|
||||
void pixmapResized(const QSize &size);
|
||||
|
||||
protected:
|
||||
void drawBackground(QPainter *p, const QRectF &rect);
|
||||
@@ -83,7 +80,14 @@ protected:
|
||||
void wheelEvent(QWheelEvent *event);
|
||||
|
||||
private:
|
||||
struct ImageViewPrivate *d;
|
||||
void doScale(qreal factor);
|
||||
|
||||
ImageViewerFile *m_file;
|
||||
QGraphicsItem *m_imageItem = 0;
|
||||
QGraphicsRectItem *m_backgroundItem = 0;
|
||||
QGraphicsRectItem *m_outlineItem = 0;
|
||||
bool m_showBackground = false;
|
||||
bool m_showOutline = true;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
@@ -54,21 +54,53 @@ namespace Internal {
|
||||
struct ImageViewerPrivate
|
||||
{
|
||||
QString displayName;
|
||||
ImageViewerFile *file;
|
||||
QSharedPointer<ImageViewerFile> file;
|
||||
ImageView *imageView;
|
||||
QWidget *toolbar;
|
||||
Ui::ImageViewerToolbar ui_toolbar;
|
||||
};
|
||||
|
||||
/*!
|
||||
Tries to change the \a button icon to the icon specified by \a name
|
||||
from the current theme. Returns \c true if icon is updated, \c false
|
||||
otherwise.
|
||||
*/
|
||||
static bool updateButtonIconByTheme(QAbstractButton *button, const QString &name)
|
||||
{
|
||||
QTC_ASSERT(button, return false);
|
||||
QTC_ASSERT(!name.isEmpty(), return false);
|
||||
|
||||
if (QIcon::hasThemeIcon(name)) {
|
||||
button->setIcon(QIcon::fromTheme(name));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
ImageViewer::ImageViewer(QWidget *parent)
|
||||
: IEditor(parent),
|
||||
d(new ImageViewerPrivate)
|
||||
{
|
||||
d->file = new ImageViewerFile(this);
|
||||
d->imageView = new ImageView();
|
||||
d->file.reset(new ImageViewerFile);
|
||||
ctor();
|
||||
}
|
||||
|
||||
ImageViewer::ImageViewer(const QSharedPointer<ImageViewerFile> &document, QWidget *parent)
|
||||
: IEditor(parent),
|
||||
d(new ImageViewerPrivate)
|
||||
{
|
||||
d->file = document;
|
||||
ctor();
|
||||
}
|
||||
|
||||
void ImageViewer::ctor()
|
||||
{
|
||||
d->imageView = new ImageView(d->file.data());
|
||||
|
||||
setContext(Core::Context(Constants::IMAGEVIEWER_ID));
|
||||
setWidget(d->imageView);
|
||||
setDuplicateSupported(true);
|
||||
|
||||
// toolbar
|
||||
d->toolbar = new QWidget();
|
||||
@@ -108,8 +140,14 @@ ImageViewer::ImageViewer(QWidget *parent)
|
||||
d->imageView, SLOT(setViewOutline(bool)));
|
||||
connect(d->ui_toolbar.toolButtonPlayPause, &Core::CommandButton::clicked,
|
||||
this, &ImageViewer::playToggled);
|
||||
connect(d->imageView, SIGNAL(imageSizeChanged(QSize)),
|
||||
this, SLOT(imageSizeUpdated(QSize)));
|
||||
connect(d->file.data(), &ImageViewerFile::imageSizeChanged,
|
||||
this, &ImageViewer::imageSizeUpdated);
|
||||
connect(d->file.data(), &ImageViewerFile::aboutToReload,
|
||||
d->imageView, &ImageView::reset);
|
||||
connect(d->file.data(), &ImageViewerFile::reloadFinished,
|
||||
d->imageView, &ImageView::createScene);
|
||||
connect(d->file.data(), &ImageViewerFile::isPausedChanged,
|
||||
this, &ImageViewer::updatePauseAction);
|
||||
connect(d->imageView, SIGNAL(scaleFactorChanged(qreal)),
|
||||
this, SLOT(scaleFactorUpdate(qreal)));
|
||||
}
|
||||
@@ -123,20 +161,15 @@ ImageViewer::~ImageViewer()
|
||||
|
||||
bool ImageViewer::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
||||
{
|
||||
if (!d->imageView->openFile(realFileName)) {
|
||||
*errorString = tr("Cannot open image file %1.").arg(QDir::toNativeSeparators(realFileName));
|
||||
if (!d->file->open(errorString, fileName, realFileName))
|
||||
return false;
|
||||
}
|
||||
d->file->setFilePath(Utils::FileName::fromString(fileName));
|
||||
d->ui_toolbar.toolButtonPlayPause->setVisible(d->imageView->isAnimated());
|
||||
setPaused(!d->imageView->isAnimated());
|
||||
// d_ptr->file->setMimeType
|
||||
d->imageView->createScene();
|
||||
return true;
|
||||
}
|
||||
|
||||
Core::IDocument *ImageViewer::document()
|
||||
{
|
||||
return d->file;
|
||||
return d->file.data();
|
||||
}
|
||||
|
||||
QWidget *ImageViewer::toolBar()
|
||||
@@ -144,6 +177,13 @@ QWidget *ImageViewer::toolBar()
|
||||
return d->toolbar;
|
||||
}
|
||||
|
||||
Core::IEditor *ImageViewer::duplicate()
|
||||
{
|
||||
auto other = new ImageViewer(d->file);
|
||||
other->d->imageView->createScene();
|
||||
return other;
|
||||
}
|
||||
|
||||
void ImageViewer::imageSizeUpdated(const QSize &size)
|
||||
{
|
||||
QString imageSizeText;
|
||||
@@ -158,25 +198,6 @@ void ImageViewer::scaleFactorUpdate(qreal factor)
|
||||
d->ui_toolbar.labelInfo->setText(info);
|
||||
}
|
||||
|
||||
/*!
|
||||
Tries to change the \a button icon to the icon specified by \a name
|
||||
from the current theme. Returns \c true if icon is updated, \c false
|
||||
otherwise.
|
||||
*/
|
||||
|
||||
bool ImageViewer::updateButtonIconByTheme(QAbstractButton *button, const QString &name)
|
||||
{
|
||||
QTC_ASSERT(button, return false);
|
||||
QTC_ASSERT(!name.isEmpty(), return false);
|
||||
|
||||
if (QIcon::hasThemeIcon(name)) {
|
||||
button->setIcon(QIcon::fromTheme(name));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImageViewer::switchViewBackground()
|
||||
{
|
||||
d->ui_toolbar.toolButtonBackground->click();
|
||||
@@ -214,19 +235,21 @@ void ImageViewer::togglePlay()
|
||||
|
||||
void ImageViewer::playToggled()
|
||||
{
|
||||
bool paused = d->imageView->isPaused();
|
||||
setPaused(!paused);
|
||||
d->file->setPaused(!d->file->isPaused());
|
||||
}
|
||||
|
||||
void ImageViewer::setPaused(bool paused)
|
||||
void ImageViewer::updatePauseAction()
|
||||
{
|
||||
d->imageView->setPaused(paused);
|
||||
if (paused) {
|
||||
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Play Animation"));
|
||||
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/play-small.png")));
|
||||
} else {
|
||||
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Pause Animation"));
|
||||
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/pause-small.png")));
|
||||
bool isMovie = d->file->type() == ImageViewerFile::TypeMovie;
|
||||
d->ui_toolbar.toolButtonPlayPause->setVisible(isMovie);
|
||||
if (isMovie) {
|
||||
if (d->file->isPaused()) {
|
||||
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Play Animation"));
|
||||
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/play-small.png")));
|
||||
} else {
|
||||
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Pause Animation"));
|
||||
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/pause-small.png")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -59,6 +59,8 @@ public:
|
||||
Core::IDocument *document();
|
||||
QWidget *toolBar();
|
||||
|
||||
IEditor *duplicate();
|
||||
|
||||
public slots:
|
||||
void imageSizeUpdated(const QSize &size);
|
||||
void scaleFactorUpdate(qreal factor);
|
||||
@@ -75,11 +77,10 @@ private slots:
|
||||
void playToggled();
|
||||
|
||||
private:
|
||||
ImageViewer(const QSharedPointer<ImageViewerFile> &document, QWidget *parent = 0);
|
||||
void ctor();
|
||||
void updatePauseAction();
|
||||
|
||||
bool updateButtonIconByTheme(QAbstractButton *button, const QString &name);
|
||||
void setPaused(bool paused);
|
||||
|
||||
private:
|
||||
struct ImageViewerPrivate *d;
|
||||
};
|
||||
|
||||
|
@@ -33,21 +33,113 @@
|
||||
#include "imageviewer.h"
|
||||
#include "imageviewerconstants.h"
|
||||
|
||||
#include <coreplugin/editormanager/documentmodel.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#ifndef QT_NO_SVG
|
||||
#include <QGraphicsSvgItem>
|
||||
#endif
|
||||
#include <QImageReader>
|
||||
#include <QMovie>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
|
||||
namespace ImageViewer {
|
||||
namespace Internal {
|
||||
|
||||
ImageViewerFile::ImageViewerFile(ImageViewer *parent)
|
||||
: Core::IDocument(parent)
|
||||
class MovieItem : public QObject, public QGraphicsPixmapItem
|
||||
{
|
||||
public:
|
||||
MovieItem(QMovie *movie)
|
||||
: m_movie(movie)
|
||||
{
|
||||
setPixmap(m_movie->currentPixmap());
|
||||
connect(m_movie, &QMovie::updated, this, [this](const QRectF &rect) {
|
||||
update(rect);
|
||||
});
|
||||
}
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
|
||||
{
|
||||
const bool smoothTransform = painter->worldTransform().m11() < 1;
|
||||
painter->setRenderHint(QPainter::SmoothPixmapTransform, smoothTransform);
|
||||
painter->drawPixmap(offset(), m_movie->currentPixmap());
|
||||
}
|
||||
|
||||
private:
|
||||
QMovie *m_movie;
|
||||
};
|
||||
|
||||
ImageViewerFile::ImageViewerFile()
|
||||
{
|
||||
setId(Constants::IMAGEVIEWER_ID);
|
||||
m_editor = parent;
|
||||
connect(this, &ImageViewerFile::mimeTypeChanged, this, &ImageViewerFile::changed);
|
||||
}
|
||||
|
||||
ImageViewerFile::~ImageViewerFile()
|
||||
{
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
bool ImageViewerFile::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
||||
{
|
||||
QTC_CHECK(fileName == realFileName); // does not support auto save
|
||||
|
||||
cleanUp();
|
||||
m_type = TypeInvalid;
|
||||
|
||||
QByteArray format = QImageReader::imageFormat(fileName);
|
||||
// if it is impossible to recognize a file format - file will not be open correctly
|
||||
if (format.isEmpty()) {
|
||||
if (errorString)
|
||||
*errorString = tr("Image format not supported.");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifndef QT_NO_SVG
|
||||
if (format.startsWith("svg")) {
|
||||
m_type = TypeSvg;
|
||||
m_tempSvgItem = new QGraphicsSvgItem(fileName);
|
||||
QRectF bound = m_tempSvgItem->boundingRect();
|
||||
if (bound.width() == 0 && bound.height() == 0) {
|
||||
if (errorString)
|
||||
*errorString = tr("Failed to read SVG image.");
|
||||
return false;
|
||||
}
|
||||
emit imageSizeChanged(QSize());
|
||||
} else
|
||||
#endif
|
||||
if (QMovie::supportedFormats().contains(format)) {
|
||||
m_type = TypeMovie;
|
||||
m_movie = new QMovie(fileName, QByteArray(), this);
|
||||
m_movie->setCacheMode(QMovie::CacheAll);
|
||||
connect(m_movie, &QMovie::finished, m_movie, &QMovie::start);
|
||||
connect(m_movie, &QMovie::resized, this, &ImageViewerFile::imageSizeChanged);
|
||||
m_movie->start();
|
||||
m_isPaused = false; // force update
|
||||
setPaused(true);
|
||||
} else {
|
||||
m_type = TypePixmap;
|
||||
m_pixmap = new QPixmap(fileName);
|
||||
if (m_pixmap->isNull()) {
|
||||
if (errorString)
|
||||
*errorString = tr("Failed to read image.");
|
||||
delete m_pixmap;
|
||||
return false;
|
||||
}
|
||||
emit imageSizeChanged(m_pixmap->size());
|
||||
}
|
||||
|
||||
setFilePath(Utils::FileName::fromString(fileName));
|
||||
Utils::MimeDatabase mdb;
|
||||
setMimeType(mdb.mimeTypeForFile(fileName).name());
|
||||
return true;
|
||||
}
|
||||
|
||||
Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
|
||||
{
|
||||
if (type == TypeRemoved || type == TypePermissions)
|
||||
@@ -67,7 +159,83 @@ bool ImageViewerFile::reload(QString *errorString,
|
||||
emit changed();
|
||||
return true;
|
||||
}
|
||||
return m_editor->open(errorString, filePath().toString(), filePath().toString());
|
||||
emit aboutToReload();
|
||||
bool success = open(errorString, filePath().toString(), filePath().toString());
|
||||
emit reloadFinished(success);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ImageViewerFile::isPaused() const
|
||||
{
|
||||
return m_isPaused;
|
||||
}
|
||||
|
||||
void ImageViewerFile::setPaused(bool paused)
|
||||
{
|
||||
if (!m_movie || m_isPaused == paused)
|
||||
return;
|
||||
m_isPaused = paused;
|
||||
m_movie->setPaused(paused);
|
||||
emit isPausedChanged(m_isPaused);
|
||||
}
|
||||
|
||||
QGraphicsItem *ImageViewerFile::createGraphicsItem() const
|
||||
{
|
||||
QGraphicsItem *val = 0;
|
||||
switch (m_type) {
|
||||
case TypeInvalid:
|
||||
break;
|
||||
case TypeSvg:
|
||||
#ifndef QT_NO_SVG
|
||||
if (m_tempSvgItem) {
|
||||
val = m_tempSvgItem;
|
||||
m_tempSvgItem = 0;
|
||||
} else {
|
||||
val = new QGraphicsSvgItem(filePath().toString());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case TypeMovie:
|
||||
val = new MovieItem(m_movie);
|
||||
break;
|
||||
case TypePixmap: {
|
||||
auto pixmapItem = new QGraphicsPixmapItem(*m_pixmap);
|
||||
pixmapItem->setTransformationMode(Qt::SmoothTransformation);
|
||||
val = pixmapItem;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
ImageViewerFile::ImageType ImageViewerFile::type() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
void ImageViewerFile::updateVisibility()
|
||||
{
|
||||
if (!m_movie || m_isPaused)
|
||||
return;
|
||||
bool visible = false;
|
||||
foreach (Core::IEditor *editor, Core::DocumentModel::editorsForDocument(this)) {
|
||||
if (editor->widget()->isVisible()) {
|
||||
visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_movie->setPaused(!visible);
|
||||
}
|
||||
|
||||
void ImageViewerFile::cleanUp()
|
||||
{
|
||||
delete m_pixmap;
|
||||
delete m_movie;
|
||||
#ifndef QT_NO_SVG
|
||||
delete m_tempSvgItem;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ImageViewerFile::save(QString *errorString, const QString &fileName, bool autoSave)
|
||||
|
@@ -34,6 +34,17 @@
|
||||
|
||||
#include <coreplugin/idocument.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QGraphicsItem;
|
||||
class QMovie;
|
||||
class QPixmap;
|
||||
|
||||
#ifndef QT_NO_SVG
|
||||
class QGraphicsSvgItem;
|
||||
#endif
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace ImageViewer {
|
||||
namespace Internal {
|
||||
|
||||
@@ -44,8 +55,17 @@ class ImageViewerFile : public Core::IDocument
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ImageViewerFile(ImageViewer *parent = 0);
|
||||
enum ImageType {
|
||||
TypeInvalid,
|
||||
TypeSvg,
|
||||
TypeMovie,
|
||||
TypePixmap
|
||||
};
|
||||
|
||||
ImageViewerFile();
|
||||
~ImageViewerFile();
|
||||
|
||||
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
|
||||
bool save(QString *errorString, const QString &fileName, bool autoSave);
|
||||
bool setContents(const QByteArray &contents);
|
||||
|
||||
@@ -58,8 +78,28 @@ public:
|
||||
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
|
||||
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
|
||||
|
||||
bool isPaused() const;
|
||||
void setPaused(bool paused);
|
||||
|
||||
QGraphicsItem *createGraphicsItem() const;
|
||||
ImageType type() const;
|
||||
|
||||
void updateVisibility();
|
||||
|
||||
signals:
|
||||
void imageSizeChanged(const QSize &size);
|
||||
void isPausedChanged(bool paused);
|
||||
|
||||
private:
|
||||
ImageViewer *m_editor;
|
||||
void cleanUp();
|
||||
|
||||
ImageType m_type = TypeInvalid;
|
||||
#ifndef QT_NO_SVG
|
||||
mutable QGraphicsSvgItem *m_tempSvgItem = 0;
|
||||
#endif
|
||||
QMovie *m_movie = 0;
|
||||
QPixmap *m_pixmap = 0;
|
||||
bool m_isPaused = false;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
Reference in New Issue
Block a user