pineapple-pictures/graphicsscene.cpp

85 lines
2.1 KiB
C++
Raw Permalink Normal View History

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>
2019-09-30 13:11:43 +08:00
#include <QMovie>
#include <QLabel>
#include <QPainter>
2019-09-29 15:52:35 +08:00
GraphicsScene::GraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
showText(tr("Drag image here"));
2019-09-29 15:52:35 +08:00
}
GraphicsScene::~GraphicsScene()
{
}
void GraphicsScene::showImage(const QPixmap &pixmap)
{
this->clear();
QGraphicsPixmapItem * pixmapItem = this->addPixmap(pixmap);
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();
QGraphicsSvgItem * svgItem = new QGraphicsSvgItem(filepath);
this->addItem(svgItem);
m_theThing = svgItem;
this->setSceneRect(m_theThing->boundingRect());
}
2019-09-30 13:11:43 +08:00
void GraphicsScene::showGif(const QString &filepath)
{
this->clear();
QMovie * movie = new QMovie(filepath);
QLabel * label = new QLabel;
label->setStyleSheet("background-color:rgba(225,255,255,0);");
2019-09-30 13:11:43 +08:00
label->setMovie(movie);
this->addWidget(label);
movie->start();
m_theThing = this->addRect(QRect(QPoint(0, 0), label->sizeHint()),
QPen(Qt::transparent));
2019-09-30 13:11:43 +08:00
this->setSceneRect(m_theThing->boundingRect());
}
bool GraphicsScene::trySetTransformationMode(Qt::TransformationMode mode)
{
QGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast<QGraphicsPixmapItem *>(m_theThing);
if (pixmapItem) {
pixmapItem->setTransformationMode(mode);
return true;
}
return false;
}
QPixmap GraphicsScene::renderToPixmap()
{
QPixmap pixmap(sceneRect().toRect().size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
render(&p, sceneRect());
return pixmap;
}