2022-06-19 16:17:38 +08:00
|
|
|
// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2019-09-29 15:52:35 +08:00
|
|
|
#include "graphicsscene.h"
|
|
|
|
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
#include <QUrl>
|
2019-09-29 23:53:29 +08:00
|
|
|
#include <QGraphicsSvgItem>
|
2024-06-28 18:40:50 +08:00
|
|
|
#include <QSvgRenderer>
|
2019-09-30 13:11:43 +08:00
|
|
|
#include <QMovie>
|
2020-01-01 14:51:46 +08:00
|
|
|
#include <QPainter>
|
2019-09-29 15:52:35 +08:00
|
|
|
|
2021-06-20 14:59:44 +08:00
|
|
|
class PGraphicsPixmapItem : public QGraphicsPixmapItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr)
|
|
|
|
: QGraphicsPixmapItem(pixmap, parent)
|
|
|
|
{}
|
|
|
|
|
2024-11-06 20:28:39 +08:00
|
|
|
enum { Type = UserType + 1 };
|
|
|
|
int type() const override { return Type; }
|
|
|
|
|
2021-06-20 14:59:44 +08:00
|
|
|
void setScaleHint(float scaleHint) {
|
|
|
|
m_scaleHint = scaleHint;
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:08:14 +08:00
|
|
|
const QPixmap & scaledPixmap(float scaleHint) {
|
|
|
|
if (qFuzzyCompare(scaleHint, m_cachedScaleHint)) return m_cachedPixmap;
|
|
|
|
QSizeF resizedScale(boundingRect().size());
|
|
|
|
resizedScale *= scaleHint;
|
2022-10-06 14:24:11 +08:00
|
|
|
QPixmap && sourcePixmap = pixmap();
|
|
|
|
m_cachedPixmap = sourcePixmap.scaled(
|
|
|
|
resizedScale.toSize() * sourcePixmap.devicePixelRatioF(),
|
2022-04-03 19:08:14 +08:00
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation);
|
|
|
|
m_cachedScaleHint = scaleHint;
|
|
|
|
return m_cachedPixmap;
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:59:44 +08:00
|
|
|
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget) override
|
|
|
|
{
|
|
|
|
if (transformationMode() == Qt::FastTransformation) {
|
|
|
|
return QGraphicsPixmapItem::paint(painter, option, widget);
|
|
|
|
} else {
|
|
|
|
// painter->setRenderHints(QPainter::Antialiasing);
|
|
|
|
painter->drawPixmap(QRectF(offset(), boundingRect().size()).toRect(),
|
2022-04-03 19:08:14 +08:00
|
|
|
scaledPixmap(m_scaleHint));
|
2021-06-20 14:59:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_scaleHint = 1;
|
2022-04-03 19:08:14 +08:00
|
|
|
float m_cachedScaleHint = -1;
|
|
|
|
QPixmap m_cachedPixmap;
|
2021-06-20 14:59:44 +08:00
|
|
|
};
|
|
|
|
|
2023-05-01 19:10:39 +08:00
|
|
|
class PGraphicsMovieItem : public QGraphicsItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PGraphicsMovieItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) {}
|
|
|
|
|
2024-11-06 20:28:39 +08:00
|
|
|
enum { Type = UserType + 2 };
|
|
|
|
int type() const override { return Type; }
|
|
|
|
|
2023-05-01 19:10:39 +08:00
|
|
|
void setMovie(QMovie* movie) {
|
|
|
|
if (m_movie) m_movie->disconnect();
|
|
|
|
m_movie.reset(movie);
|
|
|
|
m_movie->connect(m_movie.data(), &QMovie::updated, [this](){
|
|
|
|
this->update();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF boundingRect() const override {
|
|
|
|
if (m_movie) { return m_movie->frameRect(); }
|
|
|
|
else { return QRectF(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override {
|
|
|
|
if (m_movie) {
|
|
|
|
painter->drawPixmap(m_movie->frameRect(), m_movie->currentPixmap(), m_movie->frameRect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:28:39 +08:00
|
|
|
inline QMovie * movie() const {
|
|
|
|
return m_movie.data();
|
|
|
|
}
|
|
|
|
|
2023-05-01 19:10:39 +08:00
|
|
|
private:
|
|
|
|
QScopedPointer<QMovie> m_movie;
|
|
|
|
};
|
|
|
|
|
2019-09-29 15:52:35 +08:00
|
|
|
GraphicsScene::GraphicsScene(QObject *parent)
|
|
|
|
: QGraphicsScene(parent)
|
|
|
|
{
|
2019-10-06 17:31:27 +08:00
|
|
|
showText(tr("Drag image here"));
|
2019-09-29 15:52:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsScene::~GraphicsScene()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsScene::showImage(const QPixmap &pixmap)
|
|
|
|
{
|
|
|
|
this->clear();
|
2021-06-20 14:59:44 +08:00
|
|
|
PGraphicsPixmapItem * pixmapItem = new PGraphicsPixmapItem(pixmap);
|
|
|
|
this->addItem(pixmapItem);
|
2020-09-26 10:47:28 +08:00
|
|
|
pixmapItem->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
|
|
|
|
m_theThing = pixmapItem;
|
2019-09-29 15:52:35 +08:00
|
|
|
this->setSceneRect(m_theThing->boundingRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsScene::showText(const QString &text)
|
|
|
|
{
|
|
|
|
this->clear();
|
|
|
|
QGraphicsTextItem * textItem = this->addText(text);
|
|
|
|
textItem->setDefaultTextColor(QColor("White"));
|
|
|
|
m_theThing = textItem;
|
|
|
|
this->setSceneRect(m_theThing->boundingRect());
|
|
|
|
}
|
2019-09-29 23:53:29 +08:00
|
|
|
|
|
|
|
void GraphicsScene::showSvg(const QString &filepath)
|
|
|
|
{
|
|
|
|
this->clear();
|
2024-06-28 18:40:50 +08:00
|
|
|
QGraphicsSvgItem * svgItem = new QGraphicsSvgItem();
|
|
|
|
QSvgRenderer * render = new QSvgRenderer(svgItem);
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
|
|
|
|
// Qt 6.7.0's SVG support is terrible caused by huge memory usage, see QTBUG-124287
|
|
|
|
// Qt 6.7.1's is somewhat better, memory issue seems fixed, but still laggy when zoom in
|
|
|
|
// Anyway let's disable it for now.
|
|
|
|
render->setOptions(QtSvg::Tiny12FeaturesOnly);
|
|
|
|
#endif // QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
|
|
|
|
render->load(filepath);
|
|
|
|
svgItem->setSharedRenderer(render);
|
2019-09-29 23:53:29 +08:00
|
|
|
this->addItem(svgItem);
|
|
|
|
m_theThing = svgItem;
|
|
|
|
this->setSceneRect(m_theThing->boundingRect());
|
|
|
|
}
|
2019-09-30 13:11:43 +08:00
|
|
|
|
2021-04-15 00:00:18 +08:00
|
|
|
void GraphicsScene::showAnimated(const QString &filepath)
|
2019-09-30 13:11:43 +08:00
|
|
|
{
|
|
|
|
this->clear();
|
2023-05-01 19:10:39 +08:00
|
|
|
|
|
|
|
PGraphicsMovieItem * animatedItem = new PGraphicsMovieItem();
|
|
|
|
QMovie * movie = new QMovie(filepath);
|
2019-09-30 13:11:43 +08:00
|
|
|
movie->start();
|
2023-05-01 19:10:39 +08:00
|
|
|
animatedItem->setMovie(movie);
|
|
|
|
this->addItem(animatedItem);
|
|
|
|
m_theThing = animatedItem;
|
|
|
|
|
2019-09-30 13:11:43 +08:00
|
|
|
this->setSceneRect(m_theThing->boundingRect());
|
|
|
|
}
|
2020-01-01 14:51:46 +08:00
|
|
|
|
2021-06-20 14:59:44 +08:00
|
|
|
bool GraphicsScene::trySetTransformationMode(Qt::TransformationMode mode, float scaleHint)
|
2020-02-13 19:45:14 +08:00
|
|
|
{
|
2021-06-20 14:59:44 +08:00
|
|
|
PGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast<PGraphicsPixmapItem *>(m_theThing);
|
2020-02-13 19:45:14 +08:00
|
|
|
if (pixmapItem) {
|
|
|
|
pixmapItem->setTransformationMode(mode);
|
2021-06-20 14:59:44 +08:00
|
|
|
pixmapItem->setScaleHint(scaleHint);
|
2020-02-13 19:45:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:28:39 +08:00
|
|
|
bool GraphicsScene::togglePauseAnimation()
|
|
|
|
{
|
|
|
|
PGraphicsMovieItem * animatedItem = qgraphicsitem_cast<PGraphicsMovieItem *>(m_theThing);
|
|
|
|
if (animatedItem) {
|
|
|
|
animatedItem->movie()->setPaused(animatedItem->movie()->state() != QMovie::Paused);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphicsScene::skipAnimationFrame(int delta)
|
|
|
|
{
|
|
|
|
PGraphicsMovieItem * animatedItem = qgraphicsitem_cast<PGraphicsMovieItem *>(m_theThing);
|
|
|
|
if (animatedItem) {
|
|
|
|
const int frameCount = animatedItem->movie()->frameCount();
|
|
|
|
const int currentFrame = animatedItem->movie()->currentFrameNumber();
|
|
|
|
const int targetFrame = (currentFrame + delta) % frameCount;
|
|
|
|
animatedItem->movie()->setPaused(true);
|
|
|
|
return animatedItem->movie()->jumpToFrame(targetFrame);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:51:46 +08:00
|
|
|
QPixmap GraphicsScene::renderToPixmap()
|
|
|
|
{
|
2021-07-06 13:25:53 +08:00
|
|
|
PGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast<PGraphicsPixmapItem *>(m_theThing);
|
|
|
|
if (pixmapItem) {
|
|
|
|
return pixmapItem->pixmap();
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:51:46 +08:00
|
|
|
QPixmap pixmap(sceneRect().toRect().size());
|
2020-01-05 14:38:39 +08:00
|
|
|
pixmap.fill(Qt::transparent);
|
2020-01-01 14:51:46 +08:00
|
|
|
QPainter p(&pixmap);
|
|
|
|
render(&p, sceneRect());
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|