PeriDyno 1.2.1
Loading...
Searching...
No Matches
WSaveWidget.cpp
Go to the documentation of this file.
1#include "WSaveWidget.h"
2#include "WMainWindow.h"
3
4#include <filesystem>
6#include <variant>
7#include <Wt/WAnchor.h>
8#include <Wt/WLineEdit.h>
9#include <Wt/WMessageBox.h>
10#include <Wt/WPanel.h>
11#include <Wt/WProgressBar.h>
12#include <Wt/WPushButton.h>
13
15 : mParent(parent)
16{
17 this->setLayoutSizeAware(true);
18 //this->setOverflow(Wt::Overflow::Auto);
19 //this->setHeight(Wt::WLength("100%"));
20 this->setMargin(10);
21
22 //auto layout = this->setLayout(std::make_unique<Wt::WBorderLayout>());
23
24 mWidth = width;
25
27
29
30}
31
33
34
36{
37 auto panel = this->addNew<Wt::WPanel>();
38 panel->setTitle("Save File");
39 panel->setCollapsible(false);
40 panel->setWidth(mWidth);
41
42 auto container = panel->setCentralWidget(std::make_unique<Wt::WContainerWidget>());
43 mSaveLayout = container->setLayout(std::make_unique<Wt::WVBoxLayout>());
44
45 auto saveFileText = mSaveLayout->addWidget(std::make_unique<Wt::WText>("Please Input File Name:"));
46
47 auto saveFileNameEdit = mSaveLayout->addWidget(std::make_unique<Wt::WLineEdit>());
48 saveFileNameEdit->setMaxLength(256);
49 saveFileNameEdit->setMargin(10, Wt::Side::Right);
50
51
52 auto downloadButton = mSaveLayout->addWidget(std::make_unique< Wt::WPushButton>("Generated"));
53 downloadButton->setMargin(10, Wt::Side::Top);
54 downloadButton->setStyleClass("btn-primary");
55
56 mSaveOut = mSaveLayout->addWidget(std::make_unique<Wt::WText>());
57
58 downloadButton->clicked().connect([=]
59 {
60
61 std::string fileName = saveFileNameEdit->text().toUTF8();
62 std::cout << fileName << std::endl;
63 if (!fileName.empty())
64 {
65 if (isValidFileName(fileName))
66 {
67 save(fileName);
68 mSaveOut->setText("Generating Successfully, Click Download!");
69 saveFileNameEdit->setText("");
70 }
71 else
72 {
73 mSaveOut->setText("File Name is InValid!");
74 saveFileNameEdit->setText("");
75 }
76
77 }
78 else
79 {
80
81 std::cout << fileName << std::endl;
82 mSaveOut->setText("File Name Cannot Be Empty!");
83 saveFileNameEdit->setText("");
84 }
85 });
86}
87
89{
90 auto panel = this->addNew<Wt::WPanel>();
91 panel->setTitle("Upload File");
92 panel->setCollapsible(false);
93 panel->setWidth(mWidth);
94
95 auto container = panel->setCentralWidget(std::make_unique<Wt::WContainerWidget>());
96 auto layout = container->setLayout(std::make_unique<Wt::WVBoxLayout>());
97
98 Wt::WFileUpload* fu = layout->addWidget(std::make_unique<Wt::WFileUpload>());
99 fu->setProgressBar(std::make_unique<Wt::WProgressBar>());
100 fu->setMargin(10, Wt::Side::Right);
101 fu->setMultiple(false);
102
103 // Provide a button to start uploading.
104 Wt::WPushButton* uploadButton = layout->addWidget(std::make_unique<Wt::WPushButton>("Send"));
105 uploadButton->setMargin(10, Wt::Side::Top);
106 uploadButton->setStyleClass("btn-primary");
107
108 mUploadOut = layout->addWidget(std::make_unique<Wt::WText>());
109
110 // Upload when the button is clicked.
111 uploadButton->clicked().connect([=] {
112 if (uploadButton->text() == "Send")
113 {
114 if (fu->canUpload())
115 {
116 fu->upload();
117 uploadButton->setText("ReUpload");
118 }
119 else
120 {
121 mUploadOut->setText("Cannot Upload File");
122 }
123
124 }
125 else if (uploadButton->text() == "ReUpload")
126 {
127 recreate();
128 }
129 });
130
131 // React to a succesfull upload.
132 fu->uploaded().connect([=] {
133
134 std::string filePath = uploadFile(fu);
135 if (!filePath.empty())
136 {
138
139 auto result = scnLoader->load(filePath);
140
141 if (std::holds_alternative<std::string>(result))
142 {
143 std::string error = std::get<std::string>(result);
144 if (error.compare("Error Load") == 0)
145 {
146 Wt::WMessageBox::show("Error", "Error Load!", Wt::StandardButton::Ok);
147 }
148 else if (error.compare("Error Version") == 0)
149 {
150 Wt::WMessageBox::show("Error", "Version Error detected!", Wt::StandardButton::Ok);
151 }
152 else
153 {
154 Wt::WMessageBox::show("Error", "Unknown Error!", Wt::StandardButton::Ok);
155 }
156 }
157 else if (std::holds_alternative<std::shared_ptr<dyno::SceneGraph>>(result))
158 {
159 mParent->setScene(std::get<std::shared_ptr<dyno::SceneGraph>>(result));
160 mParent->createRightPanel();
161 mUploadOut->setText("File upload is finished.");
162 }
163 }
164 else
165 {
166 mUploadOut->setText("Upload File IS Empty");
167 }
168 });
169
170 // React to a file upload problem.
171 fu->fileTooLarge().connect([=] {
172 mUploadOut->setText("File is too large.");
173 });
174}
175
176void WSaveWidget::save(std::string fileName)
177{
178 std::string filePath = removeXmlExtension(fileName) + ".xml";
179
181 //scnLoader->save(dyno::SceneGraphFactory::instance()->active(), filePath);
182 scnLoader->save(mParent->getScene(), filePath);
183
184 if (std::filesystem::exists(filePath))
185 {
186 auto xmlResource = std::make_shared<downloadResource>(filePath);
187
188 Wt::WLink link = Wt::WLink(xmlResource);
189 link.setTarget(Wt::LinkTarget::NewWindow);
190 auto anchor = mSaveLayout->addWidget(std::make_unique<Wt::WAnchor>(link, "Download File"));
191
192 anchor->clicked().connect([=] {
193 mSaveLayout->removeWidget(anchor);
194 std::filesystem::remove(filePath);
195 mSaveOut->setText("");
196 });
197 }
198 else
199 {
200 mSaveOut->setText("Failed To Generate File!");
201 }
202}
203
204std::string WSaveWidget::removeXmlExtension(const std::string& filename) {
205 // ²éÕÒ ".xml" µÄλÖÃ
206 size_t pos = filename.rfind(".xml");
207 if (pos != std::string::npos) {
208 return filename.substr(0, pos);
209 }
210 return filename;
211}
212
213bool WSaveWidget::isValidFileName(const std::string& filename) {
214 // Windows
215 const std::string invalidChars = "<>:\"/\\|?*";
216
217 return std::none_of(invalidChars.begin(), invalidChars.end(),
218 [&filename](char c) { return filename.find(c) != std::string::npos; });
219}
220
222{
223 this->clear();
224 this->setLayoutSizeAware(true);
225 this->setOverflow(Wt::Overflow::Auto);
226 this->setHeight(Wt::WLength("100%"));
227 this->setMargin(10);
228
229 this->createSavePanel();
230 this->createUploadPanel();
231}
232
233std::string WSaveWidget::uploadFile(Wt::WFileUpload* upload)
234{
235 if (upload->uploadedFiles().size() > 0)
236 {
237 for (const auto& file : upload->uploadedFiles())
238 {
239 std::string savePath = file.clientFileName();
240 std::string tempFilePath = file.spoolFileName();
241 std::ifstream src(tempFilePath, std::ios::binary);
242 std::ofstream dst(savePath, std::ios::binary);
243
244 dst << src.rdbuf();
245
246 if (dst)
247 {
248 //Wt::WMessageBox::show("Success", "File save path." + savePath, Wt::StandardButton::Ok);
249 src.close();
250 dst.close();
251 return savePath;
252 }
253 else
254 {
255 Wt::WMessageBox::show("Error", "File save failure.", Wt::StandardButton::Ok);
256 }
257 src.close();
258 dst.close();
259 }
260 }
261}
Wt::WText * mSaveOut
Definition WSaveWidget.h:45
bool isValidFileName(const std::string &filename)
void createSavePanel()
WSaveWidget(WMainWindow *parent, int width)
std::string removeXmlExtension(const std::string &filename)
void createUploadPanel()
std::string uploadFile(Wt::WFileUpload *upload)
Wt::WText * mUploadOut
Definition WSaveWidget.h:46
WMainWindow * mParent
Definition WSaveWidget.h:49
Wt::WVBoxLayout * mSaveLayout
Definition WSaveWidget.h:48
void save(std::string fileName)
static SceneLoaderFactory & getInstance()
Get the ObjectFactory singleton instance.
SceneLoader * getEntryByFileExtension(std::string extension)
Get an entry given a file extension.
virtual LoadResult load(const std::string filename)
virtual bool save(std::shared_ptr< SceneGraph > scn, const std::string filename)