This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capturehelper.cpp
80 lines (60 loc) · 2.31 KB
/
capturehelper.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
#include <QImage>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QDateTime>
#include <QDir>
#include <QDesktopServices>
#include "capturehelper.h"
CaptureHelper::CaptureHelper(QObject *parent) : QObject(parent)
{
}
CaptureHelper::~CaptureHelper()
{
}
bool CaptureHelper::captureDeclarativeItem(QDeclarativeItem *item)
{
if (item != NULL) {
QImage image(item->boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
QStyleOptionGraphicsItem style_option;
style_option.exposedRect = item->boundingRect();
item->paint(&painter, &style_option, NULL);
QList<QDeclarativeItem*> sorted_children;
for (int i = 0; i < item->children().size(); i++) {
QDeclarativeItem *child = dynamic_cast<QDeclarativeItem*>(item->children().at(i));
if (child != NULL) {
if (sorted_children.size() == 0) {
sorted_children.append(child);
} else {
int j;
for (j = 0; j < sorted_children.size(); j++) {
if (sorted_children.at(j)->zValue() > child->zValue()) {
sorted_children.insert(j, child);
break;
}
}
if (j == sorted_children.size()) {
sorted_children.append(child);
}
}
}
}
for (int i = 0; i < sorted_children.size(); i++) {
QDeclarativeItem *child = sorted_children.at(i);
painter.save();
painter.translate(child->x(), child->y());
style_option.exposedRect = child->boundingRect();
child->paint(&painter, &style_option, NULL);
painter.restore();
}
QString file_path = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
QString file_name = "ChristmasTree-" + QDateTime::currentDateTime().toString("dd.MM.yyyy.hh.mm.ss") + ".jpg";
if (QDir::root().mkpath(file_path) && image.save(file_path + QDir::separator() + file_name)) {
return true;
} else {
return false;
}
} else {
return false;
}
}