Commit 7c9e8f87 authored by Eike Ziller's avatar Eike Ziller

ImageViewer: Support splitting

Change-Id: Id789bd4a77d14ff990f3c9092a9ba533ac4fab7a
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent 45194515
...@@ -39,16 +39,12 @@ ...@@ -39,16 +39,12 @@
#include "imageview.h" #include "imageview.h"
#include <QFile> #include "imageviewerfile.h"
#include <QWheelEvent> #include <QWheelEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QMovie>
#include <QGraphicsRectItem> #include <QGraphicsRectItem>
#include <QPixmap> #include <QPixmap>
#ifndef QT_NO_SVG
#include <QGraphicsSvgItem>
#endif
#include <QImageReader>
#include <qmath.h> #include <qmath.h>
namespace ImageViewer { namespace ImageViewer {
...@@ -58,46 +54,8 @@ namespace Constants { ...@@ -58,46 +54,8 @@ namespace Constants {
namespace Internal { namespace Internal {
class MovieItem : public QGraphicsPixmapItem ImageView::ImageView(ImageViewerFile *file)
{ : m_file(file)
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())
{ {
setScene(new QGraphicsScene(this)); setScene(new QGraphicsScene(this));
setTransformationAnchor(AnchorUnderMouse); setTransformationAnchor(AnchorUnderMouse);
...@@ -120,129 +78,66 @@ ImageView::ImageView(QWidget *parent) ...@@ -120,129 +78,66 @@ ImageView::ImageView(QWidget *parent)
ImageView::~ImageView() ImageView::~ImageView()
{ {
delete d;
} }
void ImageView::drawBackground(QPainter *p, const QRectF &) void ImageView::reset()
{ {
p->save(); scene()->clear();
p->resetTransform(); resetTransform();
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
p->restore();
} }
bool ImageView::openFile(QString fileName) void ImageView::createScene()
{ {
#ifndef QT_NO_SVG m_imageItem = m_file->createGraphicsItem();
bool isSvg = false; if (!m_imageItem) // failed to load
#endif return;
QByteArray format = QImageReader::imageFormat(fileName); m_imageItem->setCacheMode(QGraphicsItem::NoCache);
m_imageItem->setZValue(0);
// 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 // background item
d->backgroundItem = new QGraphicsRectItem(d->imageItem->boundingRect()); m_backgroundItem = new QGraphicsRectItem(m_imageItem->boundingRect());
d->backgroundItem->setBrush(Qt::white); m_backgroundItem->setBrush(Qt::white);
d->backgroundItem->setPen(Qt::NoPen); m_backgroundItem->setPen(Qt::NoPen);
d->backgroundItem->setVisible(drawBackground); m_backgroundItem->setVisible(m_showBackground);
d->backgroundItem->setZValue(-1); m_backgroundItem->setZValue(-1);
// outline // outline
d->outlineItem = new QGraphicsRectItem(d->imageItem->boundingRect()); m_outlineItem = new QGraphicsRectItem(m_imageItem->boundingRect());
QPen outline(Qt::black, 1, Qt::DashLine); QPen outline(Qt::black, 1, Qt::DashLine);
outline.setCosmetic(true); outline.setCosmetic(true);
d->outlineItem->setPen(outline); m_outlineItem->setPen(outline);
d->outlineItem->setBrush(Qt::NoBrush); m_outlineItem->setBrush(Qt::NoBrush);
d->outlineItem->setVisible(drawOutline); m_outlineItem->setVisible(m_showOutline);
d->outlineItem->setZValue(1); m_outlineItem->setZValue(1);
s->addItem(d->backgroundItem); QGraphicsScene *s = scene();
s->addItem(d->imageItem); s->addItem(m_backgroundItem);
s->addItem(d->outlineItem); s->addItem(m_imageItem);
s->addItem(m_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(); emitScaleFactor();
return true;
}
bool ImageView::isAnimated() const
{
return d->movie;
} }
bool ImageView::isPaused() const void ImageView::drawBackground(QPainter *p, const QRectF &)
{
return d->moviePaused;
}
void ImageView::setPaused(bool paused)
{ {
if (!d->movie) p->save();
return; p->resetTransform();
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
d->movie->setPaused(paused); p->restore();
d->moviePaused = paused;
} }
void ImageView::setViewBackground(bool enable) void ImageView::setViewBackground(bool enable)
{ {
if (!d->backgroundItem) m_showBackground = enable;
return; if (m_backgroundItem)
m_backgroundItem->setVisible(enable);
d->backgroundItem->setVisible(enable);
} }
void ImageView::setViewOutline(bool enable) void ImageView::setViewOutline(bool enable)
{ {
if (!d->outlineItem) m_showOutline = enable;
return; if (m_outlineItem)
m_outlineItem->setVisible(enable);
d->outlineItem->setVisible(enable);
} }
void ImageView::doScale(qreal factor) void ImageView::doScale(qreal factor)
...@@ -258,22 +153,11 @@ void ImageView::doScale(qreal factor) ...@@ -258,22 +153,11 @@ void ImageView::doScale(qreal factor)
scale(actualFactor, actualFactor); scale(actualFactor, actualFactor);
emitScaleFactor(); emitScaleFactor();
if (QGraphicsPixmapItem *pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(d->imageItem)) if (QGraphicsPixmapItem *pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(m_imageItem))
pixmapItem->setTransformationMode( pixmapItem->setTransformationMode(
transform().m11() < 1 ? Qt::SmoothTransformation : Qt::FastTransformation); 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) void ImageView::wheelEvent(QWheelEvent *event)
{ {
qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->delta() / 240.0); qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->delta() / 240.0);
...@@ -299,7 +183,7 @@ void ImageView::resetToOriginalSize() ...@@ -299,7 +183,7 @@ void ImageView::resetToOriginalSize()
void ImageView::fitToScreen() void ImageView::fitToScreen()
{ {
fitInView(d->imageItem, Qt::KeepAspectRatio); fitInView(m_imageItem, Qt::KeepAspectRatio);
emitScaleFactor(); emitScaleFactor();
} }
...@@ -312,18 +196,12 @@ void ImageView::emitScaleFactor() ...@@ -312,18 +196,12 @@ void ImageView::emitScaleFactor()
void ImageView::showEvent(QShowEvent *) void ImageView::showEvent(QShowEvent *)
{ {
if (!d->movie) m_file->updateVisibility();
return;
d->movie->setPaused(d->moviePaused);
} }
void ImageView::hideEvent(QHideEvent *) void ImageView::hideEvent(QHideEvent *)
{ {
if (!d->movie) m_file->updateVisibility();
return;
d->movie->setPaused(true);
} }
} // namespace Internal } // namespace Internal
......
...@@ -45,18 +45,18 @@ ...@@ -45,18 +45,18 @@
namespace ImageViewer { namespace ImageViewer {
namespace Internal { namespace Internal {
class ImageViewerFile;
class ImageView : public QGraphicsView class ImageView : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
public: public:
ImageView(QWidget *parent = 0); ImageView(ImageViewerFile *file);
~ImageView(); ~ImageView();
bool openFile(QString fileName); void reset();
bool isAnimated() const; void createScene();
bool isPaused() const;
void setPaused(bool paused);
signals: signals:
void scaleFactorChanged(qreal factor); void scaleFactorChanged(qreal factor);
...@@ -72,9 +72,6 @@ public slots: ...@@ -72,9 +72,6 @@ public slots:
private slots: private slots:
void emitScaleFactor(); void emitScaleFactor();
void doScale(qreal factor);
void updatePixmap(const QRect &rect);
void pixmapResized(const QSize &size);
protected: protected:
void drawBackground(QPainter *p, const QRectF &rect); void drawBackground(QPainter *p, const QRectF &rect);
...@@ -83,7 +80,14 @@ protected: ...@@ -83,7 +80,14 @@ protected:
void wheelEvent(QWheelEvent *event); void wheelEvent(QWheelEvent *event);
private: 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 } // namespace Internal
......
...@@ -54,21 +54,53 @@ namespace Internal { ...@@ -54,21 +54,53 @@ namespace Internal {
struct ImageViewerPrivate struct ImageViewerPrivate
{ {
QString displayName; QString displayName;
ImageViewerFile *file; QSharedPointer<ImageViewerFile> file;
ImageView *imageView; ImageView *imageView;
QWidget *toolbar; QWidget *toolbar;
Ui::ImageViewerToolbar ui_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) ImageViewer::ImageViewer(QWidget *parent)
: IEditor(parent), : IEditor(parent),
d(new ImageViewerPrivate) d(new ImageViewerPrivate)
{ {
d->file = new ImageViewerFile(this); d->file.reset(new ImageViewerFile);
d->imageView = new ImageView(); 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)); setContext(Core::Context(Constants::IMAGEVIEWER_ID));
setWidget(d->imageView); setWidget(d->imageView);
setDuplicateSupported(true);
// toolbar // toolbar
d->toolbar = new QWidget(); d->toolbar = new QWidget();
...@@ -108,8 +140,14 @@ ImageViewer::ImageViewer(QWidget *parent) ...@@ -108,8 +140,14 @@ ImageViewer::ImageViewer(QWidget *parent)
d->imageView, SLOT(setViewOutline(bool))); d->imageView, SLOT(setViewOutline(bool)));
connect(d->ui_toolbar.toolButtonPlayPause, &Core::CommandButton::clicked, connect(d->ui_toolbar.toolButtonPlayPause, &Core::CommandButton::clicked,
this, &ImageViewer::playToggled); this, &ImageViewer::playToggled);
connect(d->imageView, SIGNAL(imageSizeChanged(QSize)), connect(d->file.data(), &ImageViewerFile::imageSizeChanged,
this, SLOT(imageSizeUpdated(QSize))); 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)), connect(d->imageView, SIGNAL(scaleFactorChanged(qreal)),
this, SLOT(scaleFactorUpdate(qreal))); this, SLOT(scaleFactorUpdate(qreal)));
} }
...@@ -123,20 +161,15 @@ ImageViewer::~ImageViewer() ...@@ -123,20 +161,15 @@ ImageViewer::~ImageViewer()
bool ImageViewer::open(QString *errorString, const QString &fileName, const QString &realFileName) bool ImageViewer::open(QString *errorString, const QString &fileName, const QString &realFileName)
{ {
if (!d->imageView->openFile(realFileName)) { if (!d->file->open(errorString, fileName, realFileName))
*errorString = tr("Cannot open image file %1.").arg(QDir::toNativeSeparators(realFileName));
return false; return false;
} d->imageView->createScene();
d->file->setFilePath(Utils::FileName::fromString(fileName));
d->ui_toolbar.toolButtonPlayPause->setVisible(d->imageView->isAnimated());
setPaused(!d->imageView->isAnimated());
// d_ptr->file->setMimeType
return true; return true;
} }
Core::IDocument *ImageViewer::document() Core::IDocument *ImageViewer::document()
{ {
return d->file; return d->file.data();
} }
QWidget *ImageViewer::toolBar() QWidget *ImageViewer::toolBar()
...@@ -144,6 +177,13 @@ QWidget *ImageViewer::toolBar() ...@@ -144,6 +177,13 @@ QWidget *ImageViewer::toolBar()
return d->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) void ImageViewer::imageSizeUpdated(const QSize &size)
{ {
QString imageSizeText; QString imageSizeText;
...@@ -158,25 +198,6 @@ void ImageViewer::scaleFactorUpdate(qreal factor) ...@@ -158,25 +198,6 @@ void ImageViewer::scaleFactorUpdate(qreal factor)
d->ui_toolbar.labelInfo->setText(info); 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() void ImageViewer::switchViewBackground()
{ {
d->ui_toolbar.toolButtonBackground->click(); d->ui_toolbar.toolButtonBackground->click();
...@@ -214,19 +235,21 @@ void ImageViewer::togglePlay() ...@@ -214,19 +235,21 @@ void ImageViewer::togglePlay()
void ImageViewer::playToggled() void ImageViewer::playToggled()
{ {
bool paused = d->imageView->isPaused(); d->file->setPaused(!d->file->isPaused());
setPaused(!paused);
} }
void ImageViewer::setPaused(bool paused) void ImageViewer::updatePauseAction()
{ {
d->imageView->setPaused(paused); bool isMovie = d->file->type() == ImageViewerFile::TypeMovie;
if (paused) { d->ui_toolbar.toolButtonPlayPause->setVisible(isMovie);
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Play Animation")); if (isMovie) {
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/play-small.png"))); if (d->file->isPaused()) {
} else { d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Play Animation"));
d->ui_toolbar.toolButtonPlayPause->setToolTipBase(tr("Pause Animation")); d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/play-small.png")));
d->ui_toolbar.toolButtonPlayPause->setIcon(QPixmap(QLatin1String(":/imageviewer/images/pause-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: ...@@ -59,6 +59,8 @@ public:
Core::IDocument *document(); Core::IDocument *document();
QWidget *toolBar(); QWidget *toolBar();
IEditor *duplicate();
public slots: public slots:
void imageSizeUpdated(const QSize &size); void imageSizeUpdated(const QSize &size);
void scaleFactorUpdate(qreal factor); void scaleFactorUpdate(qreal factor);
...@@ -75,11 +77,10 @@ private slots: ...@@ -75,11 +77,10 @@ private slots:
void playToggled(); void playToggled();