source: s10k/Vago/windowMessagesWizard/wmfinalpage.cpp@ 1140

Last change on this file since 1140 was 1093, checked in by s10k, 7 years ago

Vago 1.4

File size: 13.8 KB
Line 
1#include "wmfinalpage.h"
2#include "ui_wmfinalpage.h"
3
4WmFinalPage::WmFinalPage(QString AppDir, QString wmLocation, QList<std::shared_ptr<WmPage> > &pages, QWidget *parent) :
5 QWizardPage(parent), ui(new Ui::wmfinalpage), pages(pages)
6{
7 ui->setupUi(this);
8 this->wmLocation=wmLocation;
9
10 this->oniSplitCommands = new QStringList();
11 this->myConverter = new Converter(AppDir, this->oniSplitCommands);
12
13 ui->lbComplete->setText("<html>The wizard is now complete. The window messages have been converted. "
14 "You can view all converted files clicking <a href=' '>here.</a><br />"
15 "<br />Click restart to create more window messages from the wizard beggining, "
16 "otherwise click finish.</html>"); // Don't use rich text in qtdesigner because it generates platform dependent code
17
18 connectSlots();
19}
20
21void WmFinalPage::openSoundsFolder(){
22 QDesktopServices::openUrl(QUrl("file:///"+this->wmLocation));
23}
24
25void WmFinalPage::initializePage(){
26 startProcessing();
27}
28
29void WmFinalPage::startProcessing(){
30 // Check if images folder exists and create it if necessary
31 QDir saveDir(this->wmLocation);
32
33 if(!saveDir.exists())
34 {
35 saveDir.mkpath("."); // http://stackoverflow.com/questions/2241808/checking-if-a-folder-exists-and-creating-folders-in-qt-c thanks Petrucio
36 }
37
38 QString filePath = this->wmLocation + "/" + field("leFileName").toString() + ".xml";
39
40 int globalSectionCounter = -1;
41 int currentIGPGId = globalSectionCounter;
42 int currentIGSAId = globalSectionCounter;
43 int currentIGSTId = globalSectionCounter;
44
45 pugi::xml_document doc;
46
47 pugi::xml_node rootNode = doc.append_child("Oni");
48 pugi::xml_node typeNode;
49
50 // Define type node
51 switch(static_cast<WINDOW_TYPE>(field("cbWindowType").toInt())){
52 case WINDOW_TYPE::OBJECTIVE:
53 {
54 typeNode = rootNode.append_child("OPge");
55 typeNode.append_attribute("id").set_value(++globalSectionCounter);
56 typeNode.append_child("LevelNumber").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(field("leLevelId").toString()));
57 typeNode.append_child("Pages").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(QString("#") + QString::number(++globalSectionCounter)));
58 pugi::xml_node igpaNode = rootNode.append_child("IGPA");
59 igpaNode.append_attribute("id").set_value(globalSectionCounter);
60 pugi::xml_node pagesNode = igpaNode.append_child("Pages");
61 currentIGPGId = globalSectionCounter;
62 for(int i=0; i<this->pages.size(); i++){
63 pagesNode.append_child("Link").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
64 }
65 break;
66 }
67 case WINDOW_TYPE::DIARY:
68 typeNode = rootNode.append_child("DPge");
69 typeNode.append_attribute("id").set_value(++globalSectionCounter);
70 typeNode.append_child("LevelNumber").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(field("leLevelId").toString()));
71 typeNode.append_child("PageNumber").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(field("lePageNumber").toString()));
72 typeNode.append_child("IsLearnedMove").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(QString::number(static_cast<int>(field("cbIsLearnedMove").toBool()))));
73 currentIGPGId = globalSectionCounter;
74 typeNode.append_child("Page").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
75 break;
76 case WINDOW_TYPE::HELP:
77 typeNode = rootNode.append_child("HPge");
78 typeNode.append_attribute("id").set_value(++globalSectionCounter);
79 currentIGPGId = globalSectionCounter;
80 typeNode.append_child("Page").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
81 break;
82 case WINDOW_TYPE::TEXT_CONSOLE:
83 {
84 typeNode = rootNode.append_child("TxtC");
85 typeNode.append_attribute("id").set_value(++globalSectionCounter);
86 typeNode.append_child("Pages").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
87 pugi::xml_node igpaNode = rootNode.append_child("IGPA");
88 igpaNode.append_attribute("id").set_value(globalSectionCounter);
89 currentIGPGId = globalSectionCounter;
90 pugi::xml_node pagesNode = igpaNode.append_child("Pages");
91 for(int i=0; i<this->pages.size(); i++){
92 pagesNode.append_child("Link").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
93 }
94 break;
95 }
96 case WINDOW_TYPE::WEAPON:
97 typeNode = rootNode.append_child("WPge");
98 typeNode.append_attribute("id").set_value(++globalSectionCounter);
99 typeNode.append_child("WeaponClass").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(field("leWeaponClassName").toString()));
100 currentIGPGId = globalSectionCounter;
101 typeNode.append_child("Page").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
102 break;
103 case WINDOW_TYPE::ITEM:
104 typeNode = rootNode.append_child("IPge");
105 typeNode.append_attribute("id").set_value(++globalSectionCounter);
106 typeNode.append_child("PageNumber").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(field("lePageNumber").toString()));
107 currentIGPGId = globalSectionCounter;
108 typeNode.append_child("Page").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
109 break;
110 case WINDOW_TYPE::ENUM_END:
111 UtilVago::showAndLogErrorPopUp("An error ocurred: WmFinalPage::startProcessing invalid WINDOW_TYPE");
112 break;
113 }
114
115 // Write pages text
116 for(int i=0; i<this->pages.size(); i++){
117
118 QTextEdit &mainText = this->pages[i]->getMainText();
119 int mainTextNumberOfRows = mainText.toPlainText().split("\n").size();
120
121 QTextEdit &footerText = this->pages[i]->getFooterText();
122 int footerTextNumberOfRows = footerText.toPlainText().split("\n").size();
123
124 // Create TXMP image if exists
125 QString imageLocation = this->pages[i]->getMiddleImage().toolTip();
126 QString imageTXMPName;
127
128 if(!imageLocation.isEmpty()){
129
130 imageTXMPName = "TXMP" + QFileInfo(imageLocation).baseName();
131
132 this->oniSplitCommands->clear();
133 this->oniSplitCommands->append("-create:txmp " + Util::String::insertQuotes(this->wmLocation) + " -format:bgr32 " + Util::String::insertQuotes(imageLocation));
134
135 this->myConverter->start(); // finally process the onisplit commands
136 this->myConverter->wait(); // wait for it to complete
137 }
138
139 // IGPG
140 pugi::xml_node igpgNode = rootNode.append_child("IGPG");
141
142 igpgNode.append_attribute("id").set_value(++currentIGPGId);
143
144 pugi::xml_node igpgFontNode = igpgNode.append_child("Font");
145 igpgFontNode.append_child("Family").append_child(pugi::xml_node_type::node_pcdata).set_value("TSFFTahoma");
146 igpgFontNode.append_child("Style").append_child(pugi::xml_node_type::node_pcdata).set_value("Bold");
147 igpgFontNode.append_child("Color").append_child(pugi::xml_node_type::node_pcdata).set_value("255 127 0");
148 igpgFontNode.append_child("Size").append_child(pugi::xml_node_type::node_pcdata).set_value("12");
149 igpgFontNode.append_child("Flags").append_child(pugi::xml_node_type::node_pcdata).set_value("Family Style Color Size");
150
151 currentIGSAId = globalSectionCounter;
152
153 igpgNode.append_child("Image").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(imageTXMPName));
154 igpgNode.append_child("Text1").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
155 igpgNode.append_child("Text2").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
156
157 // IGSA
158
159 ////// Main Text
160 pugi::xml_node igsaMainTextNode = rootNode.append_child("IGSA");
161 igsaMainTextNode.append_attribute("id").set_value(++currentIGSAId);
162 pugi::xml_node igsaMainTextStringsNode = igsaMainTextNode.append_child("Strings");
163
164 currentIGSTId = globalSectionCounter;
165
166 for(int i=0; i<mainTextNumberOfRows; i++){
167 igsaMainTextStringsNode.append_child("Link").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
168 }
169
170 ////// Footer
171 pugi::xml_node igsaFooterTextNode = rootNode.append_child("IGSA");
172 igsaFooterTextNode.append_attribute("id").set_value(++currentIGSAId);
173 pugi::xml_node igsaFooterTextStringsNode = igsaFooterTextNode.append_child("Strings");
174 for(int i=0; i<footerTextNumberOfRows; i++){
175 igsaFooterTextStringsNode.append_child("Link").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("#" + QString::number(++globalSectionCounter)));
176 }
177
178 // IGSt
179
180 auto setIGStXmlSection = [&rootNode, &currentIGSTId](QTextEdit &currentTextEdit, int numberOfRows){
181
182 for(int i=0; i<numberOfRows; i++){
183
184 QTextCursor currentCursor = currentTextEdit.textCursor();
185 currentCursor.setPosition(currentTextEdit.document()->findBlockByLineNumber(i).position());
186 currentCursor.movePosition(QTextCursor::StartOfLine);
187 currentCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
188 QTextCharFormat format = currentCursor.charFormat();
189 QString colorRGB = QString::number(format.foreground().color().red()) + " " +
190 QString::number(format.foreground().color().green()) + " " +
191 QString::number(format.foreground().color().blue());
192 QString style = (QString(format.fontItalic() ? "Italic " : "") + (format.fontWeight() == QFont::Bold ? "Bold " : "")).trimmed();
193
194 if(style.isEmpty()){
195 style = "Normal";
196 }
197
198
199 pugi::xml_node igstMainTextNode = rootNode.append_child("IGSt");
200 igstMainTextNode.append_attribute("id").set_value(++currentIGSTId);
201
202 pugi::xml_node igstMainTextFontNode = igstMainTextNode.append_child("Font");
203 igstMainTextFontNode.append_child("Family").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR("TSFF" + format.font().family()));
204 igstMainTextFontNode.append_child("Style").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(style));
205 igstMainTextFontNode.append_child("Color").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(colorRGB));
206 igstMainTextFontNode.append_child("Size").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(QString::number(format.font().pointSize())));
207 igstMainTextFontNode.append_child("Flags").append_child(pugi::xml_node_type::node_pcdata).set_value("Family Style Color Size");
208
209 QString currentRowText = currentCursor.selection().toPlainText();
210
211 pugi::xml_node currentIGStNode = igstMainTextNode.append_child("Text");
212
213 if(!currentRowText.trimmed().isEmpty()){
214 currentIGStNode.append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(currentRowText));
215 }
216 else{
217 // Empty paragraph use OniSplit correct representation (<Text xml:space="preserve"> </Text>)
218 currentIGStNode.append_child(pugi::xml_node_type::node_pcdata).set_value(" ");
219 currentIGStNode.append_attribute("xml:space").set_value("preserve");
220 }
221 }
222
223 };
224
225 ////// Main Text
226 setIGStXmlSection(mainText, mainTextNumberOfRows);
227
228 ////// Footer
229 setIGStXmlSection(footerText, footerTextNumberOfRows);
230 }
231
232 if(!doc.save_file(QSTR_TO_CSTR(filePath))){
233 UtilVago::showAndLogErrorPopUpLogButton("Couldn't create " + filePath + " file!");
234 return;
235 }
236
237 // Convert XML file to Oni
238 this->oniSplitCommands->clear();
239 this->oniSplitCommands->append("-create " + Util::String::insertQuotes(this->wmLocation) + " " + Util::String::insertQuotes(filePath));
240
241 this->myConverter->start(); // finally process the onisplit commands
242 this->myConverter->wait(); // wait for it to complete
243}
244
245void WmFinalPage::catchOSplitProcessingErrors(QString result, int numErrors){
246
247 if(numErrors!=0){
248 QString sNumErrors=QString::number(numErrors);
249 if(numErrors>1){
250 UtilVago::showErrorPopUpLogButton(result+"\n This is the last of " + sNumErrors + " errors.");
251 }
252 else{
253 UtilVago::showErrorPopUpLogButton(result);
254 }
255 }
256}
257
258void WmFinalPage::connectSlots(){
259 //This signal is for thread that is working setup the progress bar (make it visible and set it's min-max)
260 connect(this->myConverter, SIGNAL(resultConversion(QString, int)), this, SLOT(catchOSplitProcessingErrors(QString, int)));
261 connect(ui->lbComplete, SIGNAL(linkActivated(const QString & )), this, SLOT(openSoundsFolder()));
262}
263
264WmFinalPage::~WmFinalPage()
265{
266 delete this->oniSplitCommands;
267 delete this->myConverter;
268 delete ui;
269}
Note: See TracBrowser for help on using the repository browser.