-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicsscene.cpp
81 lines (71 loc) · 2.06 KB
/
graphicsscene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "graphicsscene.h"
#include <QGraphicsSceneMouseEvent>
#include <QMimeData>
#include <QDebug>
#include <QGraphicsItem>
#include <QUrl>
#include <QGraphicsSvgItem>
#include <QMovie>
#include <QLabel>
#include <QPainter>
GraphicsScene::GraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
showText(tr("Drag image here"));
}
GraphicsScene::~GraphicsScene()
{
}
void GraphicsScene::showImage(const QPixmap &pixmap)
{
this->clear();
QGraphicsPixmapItem * pixmapItem = this->addPixmap(pixmap);
pixmapItem->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
m_theThing = pixmapItem;
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());
}
void GraphicsScene::showSvg(const QString &filepath)
{
this->clear();
QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(filepath);
this->addItem(svgItem);
m_theThing = svgItem;
this->setSceneRect(m_theThing->boundingRect());
}
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);");
label->setMovie(movie);
this->addWidget(label);
movie->start();
m_theThing = this->addRect(QRect(QPoint(0, 0), label->sizeHint()),
QPen(Qt::transparent));
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());
QPainter p(&pixmap);
render(&p, sceneRect());
return pixmap;
}