-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoccontroller.cpp
More file actions
140 lines (114 loc) · 5.63 KB
/
doccontroller.cpp
File metadata and controls
140 lines (114 loc) · 5.63 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
#include "doccontroller.h"
#include "mainwindow.h"
#include "characterdata.h"
#include <QDebug>
#include <QMessageBox>
#include "pugixml/pugixml.hpp"
void CDocController::SetMainWindow(MainWindow* MainWindow)
{
_mainWindow = MainWindow;
}
// Example of doc file
// https://sebsauvage.net/wiki/doku.php?id=word_document_generation
void CDocController::ConvertSubDataToDoc(const QString& fileName, const QString& strTitle, const std::vector<CSubData>& subData, FemaleLinesStyle style, bool useUnderline)
{
pugi::xml_document doc;
pugi::xml_node htmlNode = doc.append_child("html");
htmlNode.append_attribute("xmlns:o") = "urn:schemas-microsoft-com:office:office";
htmlNode.append_attribute("xmlns:w") = "urn:schemas-microsoft-com:office:word";
htmlNode.append_attribute("xmlns") = "http://www.w3.org/TR/REC-html40";
pugi::xml_node headNode = htmlNode.append_child("head");
pugi::xml_node styleNode = headNode.append_child("style");
styleNode.append_child(pugi::node_pcdata).set_value("body { font-family: Times New Roman; font-size: 14pt; line-height: 150%; } \
table, th, td { border: 1px solid black; border-collapse: collapse; } \
th, td { border: 1px solid black; } ");
pugi::xml_node bodyNode = htmlNode.append_child("body");
pugi::xml_node titleNode = bodyNode.append_child("p");
titleNode.append_attribute("align") = "right";
pugi::xml_node boldTitleNode = titleNode.append_child("b");
boldTitleNode.append_child(pugi::node_pcdata).set_value(strTitle.toStdString().c_str());
std::set<QString> femaleNames, maleNames;
for (const CSubData& data : subData) {
if (data._character->_gender == Gender::female) {
femaleNames.insert(data._character->_name);
}
else {
maleNames.insert(data._character->_name);
}
}
for (const QString& name : femaleNames) {
pugi::xml_node characterNameNode = bodyNode.append_child("p");
characterNameNode.append_child(pugi::node_pcdata).set_value(name.toStdString().c_str());
}
for (const QString& name : maleNames) {
pugi::xml_node characterNameNode = bodyNode.append_child("p");
characterNameNode.append_child(pugi::node_pcdata).set_value(name.toStdString().c_str());
}
pugi::xml_node tableNode = bodyNode.append_child("table");
tableNode.append_attribute("width") = "100%";
pugi::xml_node trTitleNode = tableNode.append_child("tr");
std::vector<QString> titles {"Timecode", "Characters", "Replicas"};
for (QString& tdTitle : titles) {
pugi::xml_node tdTitleNode = trTitleNode.append_child("th");
tdTitleNode.append_child(pugi::node_pcdata).set_value(tdTitle.toStdString().c_str());
}
for (const CSubData& data : subData) {
pugi::xml_node trFillingNode = tableNode.append_child("tr");
pugi::xml_node tdTimestampNode = trFillingNode.append_child("td");
tdTimestampNode.append_attribute("valign") = "top";
tdTimestampNode.append_child(pugi::node_pcdata).set_value(data._startTime.toString("hh:mm:ss").toStdString().c_str());
pugi::xml_node tdNameNode = trFillingNode.append_child("td");
tdNameNode.append_attribute("valign") = "top";
pugi::xml_node parentNode = tdNameNode;
if (data._character->_gender == Gender::female) {
if (style.isBold) {
parentNode = parentNode.append_child("b");
}
if (style.isUnderline) {
parentNode = parentNode.append_child("u");
}
if (style.isItalic) {
parentNode = parentNode.append_child("i");
}
}
parentNode.append_child(pugi::node_pcdata).set_value(data._character->_name.toStdString().c_str());
pugi::xml_node tdLineNode = trFillingNode.append_child("td");
if (!useUnderline) {
tdLineNode.append_child(pugi::node_pcdata).set_value(data._line.toStdString().c_str());
continue;
}
if (data._line.size() == 0) {
continue;
}
int firstUnderline = data._line[0] == '_' ? 0 : -1;
int firstRegular = data._line[0] == '_' ? -1 : 0;
for (int iUnderline = 1; iUnderline < data._line.size(); ++iUnderline) {
if (data._line[iUnderline] != '_') {
continue;
}
if (firstUnderline == -1) {
firstUnderline = iUnderline + 1;
QString temp = data._line.sliced(firstRegular, iUnderline - firstRegular);
tdLineNode.append_child(pugi::node_pcdata).set_value(temp.toStdString().c_str());
firstRegular = -1;
}
else {
firstRegular = iUnderline + 1;
QString temp = data._line.sliced(firstUnderline, iUnderline - firstUnderline);
pugi::xml_node bLineNode = tdLineNode.append_child("b");
pugi::xml_node uLineNode = bLineNode.append_child("u");
uLineNode.append_child(pugi::node_pcdata).set_value(temp.toStdString().c_str());
firstUnderline = -1;
}
}
if (firstRegular != -1) {
QString temp = data._line.sliced(firstRegular, data._line.size() - firstRegular);
tdLineNode.append_child(pugi::node_pcdata).set_value(temp.toStdString().c_str());
}
}
doc.save_file(fileName.toLocal8Bit().data(), "\t", pugi::format_raw | pugi::format_no_declaration);
QMessageBox msgBox;
msgBox.setWindowTitle("Export to doc");
msgBox.setText("Export is completed");
msgBox.exec();
}