forked from MaurycyLiebner/enve
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathimagebox.cpp
More file actions
202 lines (173 loc) · 6.54 KB
/
Copy pathimagebox.cpp
File metadata and controls
202 lines (173 loc) · 6.54 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
#
# Friction - https://friction.graphics
#
# Copyright (c) Ole-André Rodlie and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See 'README.md' for more information.
#
*/
// Fork of enve - Copyright (C) 2016-2020 Maurycy Liebner
#include "Boxes/imagebox.h"
#include <QMenu>
#include "FileCacheHandlers/imagecachehandler.h"
#include "fileshandler.h"
#include "filesourcescache.h"
#include "GUI/edialogs.h"
#include "typemenu.h"
//#include "paintbox.h"
#include "svgexporter.h"
#include "svgexporthelpers.h"
ImageFileHandler* imageFileHandlerGetter(const QString& path) {
return FilesHandler::sInstance->getFileHandler<ImageFileHandler>(path);
}
ImageBox::ImageBox() : BoundingBox("Image", eBoxType::image),
mFileHandler(this,
[](const QString& path) {
return imageFileHandlerGetter(path);
},
[this](ImageFileHandler* obj) {
return fileHandlerAfterAssigned(obj);
},
[this](ConnContext& conn, ImageFileHandler* obj) {
fileHandlerConnector(conn, obj);
}) {
}
ImageBox::ImageBox(const QString &filePath) : ImageBox() {
setFilePath(filePath);
}
void ImageBox::fileHandlerConnector(ConnContext &conn, ImageFileHandler *obj) {
conn << connect(obj, &ImageFileHandler::pathChanged,
this, &ImageBox::prp_afterWholeInfluenceRangeChanged);
conn << connect(obj, &ImageFileHandler::reloaded,
this, &ImageBox::prp_afterWholeInfluenceRangeChanged);
}
void ImageBox::fileHandlerAfterAssigned(ImageFileHandler *obj) {
Q_UNUSED(obj);
}
void ImageBox::writeBoundingBox(eWriteStream& dst) const {
BoundingBox::writeBoundingBox(dst);
dst.writeFilePath(mFileHandler->path());
}
void ImageBox::readBoundingBox(eReadStream& src) {
BoundingBox::readBoundingBox(src);
const QString path = src.readFilePath();
setFilePathNoRename(path);
}
QDomElement ImageBox::prp_writePropertyXEV_impl(const XevExporter& exp) const {
auto result = BoundingBox::prp_writePropertyXEV_impl(exp);
const QString& absSrc = mFileHandler.path();
XevExportHelpers::setAbsAndRelFileSrc(absSrc, result, exp);
return result;
}
void ImageBox::prp_readPropertyXEV_impl(const QDomElement& ele, const XevImporter& imp) {
BoundingBox::prp_readPropertyXEV_impl(ele, imp);
const QString absSrc = XevExportHelpers::getAbsAndRelFileSrc(ele, imp);
setFilePathNoRename(absSrc);
}
void ImageBox::setFilePathNoRename(const QString &path) {
mPath = path;
mFileHandler.assign(path);
prp_afterWholeInfluenceRangeChanged();
}
void ImageBox::setFilePath(const QString &path) {
setFilePathNoRename(path);
rename(QFileInfo(path).completeBaseName());
}
QString ImageBox::filePath() const
{
return mFileHandler.path();
}
bool ImageBox::hasImage() const
{
return mFileHandler && mFileHandler->hasImage();
}
sk_sp<SkImage> ImageBox::image() const
{
return mFileHandler ? mFileHandler->getImage() : nullptr;
}
void ImageBox::reload() {
if(mFileHandler) mFileHandler->reloadAction();
}
void ImageBox::setupCanvasMenu(PropertyMenu * const menu)
{
if (menu->hasActionsForType<ImageBox>()) { return; }
menu->addedActionsForType<ImageBox>();
const PropertyMenu::PlainSelectedOp<ImageBox> reloadOp =
[](ImageBox * box) { box->reload(); };
menu->addPlainAction(QIcon::fromTheme("loop"), tr("Reload"), reloadOp);
const PropertyMenu::PlainSelectedOp<ImageBox> setSrcOp =
[](ImageBox * box) { box->changeSourceFile(); };
menu->addPlainAction(QIcon::fromTheme("document-new"), tr("Set Source File"), setSrcOp);
menu->addSeparator();
BoundingBox::setupCanvasMenu(menu);
}
void ImageBox::changeSourceFile() {
const QString filters = FileExtensions::imageFilters();
QString importPath = eDialogs::openFile("Change Source", mFileHandler.path(),
"Image Files (" + filters + " *.ora)");
if(!importPath.isEmpty()) setFilePath(importPath);
}
void ImageBox::setupRenderData(const qreal relFrame, const QMatrix& parentM,
BoxRenderData * const data,
Canvas* const scene)
{
if (!mFileHandler) { mFileHandler.assign(mPath); }
BoundingBox::setupRenderData(relFrame, parentM, data, scene);
const auto imgData = static_cast<ImageBoxRenderData*>(data);
if (mFileHandler->hasImage()) {
imgData->setContainer(mFileHandler->getImageContainer());
} else {
const auto loader = mFileHandler->scheduleLoad();
if (loader) { loader->addDependent(imgData); }
}
}
stdsptr<BoxRenderData> ImageBox::createRenderData()
{
if (!mFileHandler) { mFileHandler.assign(mPath); }
return enve::make_shared<ImageBoxRenderData>(mFileHandler, this);
}
void ImageBox::saveSVG(SvgExporter& exp, DomEleTask* const eleTask) const {
const QString imageId = SvgExportHelpers::ptrToStr(mFileHandler.data());
const auto expPtr = &exp;
const auto generate = [expPtr, eleTask, imageId](const sk_sp<SkImage>& image) {
if(!image) return;
SvgExportHelpers::defImage(*expPtr, image, imageId);
auto& use = eleTask->initialize("use");
use.setAttribute("href", "#" + imageId);
};
if(mFileHandler->hasImage()) {
const auto image = mFileHandler->getImage();
generate(image);
} else {
const auto task = mFileHandler->scheduleLoad();
if(!task) return;
const qptr<const ImageBox> thisPtr = this;
const stdptr<DomEleTask> eleTaskPtr = eleTask;
task->addDependent(
{[thisPtr, eleTaskPtr, imageId, generate]() {
if(!eleTaskPtr || !thisPtr) return;
const auto image = thisPtr->mFileHandler->getImage();
generate(image);
}, nullptr});
task->addDependent(eleTask);
}
}
void ImageBoxRenderData::loadImageFromHandler() {
if(fSrcCacheHandler) {
setContainer(fSrcCacheHandler->getImageContainer());
}
}