-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtable.cpp
315 lines (256 loc) · 5.93 KB
/
table.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "table.h"
#include "text.h"
#include "./parts/documentpart.h"
#include <QVector>
#include <QDebug>
#include <QDebug>
namespace Docx {
const QString strtblRow = QStringLiteral("w:tr");
const QString strtblCell = QStringLiteral("w:tc");
Table::Table(DocumentPart *part, const QDomElement &element)
: m_part(part)
{
m_dom = part->m_dom;
m_ctTbl = new CT_Tbl(this, element);
loadExistRowElement();
}
/*!
* \brief 如果有行则初始化
*/
void Table::loadExistRowElement()
{
QDomNodeList reles = m_ctTbl->m_tblEle.childNodes();
if (!reles.isEmpty()) {
for (int i = 0; i < reles.count(); i++) {
QDomElement rowEle = reles.at(i).toElement();
if (rowEle.nodeName() != strtblRow)
continue;
Row *row = new Row(rowEle, this);
m_rows.append(row);
qDebug() << m_rows.count();
}
}
}
Cell *Table::cell(int rowIndex, int colIndex)
{
Row *row = m_rows.at(rowIndex);
return row->cells().at(colIndex);
}
Table::~Table()
{
delete m_ctTbl;
}
/*!
* \brief 添加列
* \return
*/
Column *Table::addColumn()
{
QDomElement gridCol = m_ctTbl->m_tblGrid->addGridCol();
for (Row *row : m_rows) {
row->addTc();
}
return new Column(gridCol, m_ctTbl->m_tblGrid->count(), this);
}
/*!
* \brief 添加行
* \return
*/
Row *Table::addRow()
{
QDomElement rowEle = m_dom->createElement(strtblRow);
Row *row = new Row(rowEle, this);
m_rows.append(row);
for (int i = 0; i< m_ctTbl->m_tblGrid->count(); i++) {
row->addTc();
}
m_ctTbl->m_tblEle.appendChild(rowEle);
return row;
}
/*!
* \brief 得到rowIndex的行内所有的cell
*
* index
* \param rowIdx
* \return
*/
QList<Cell *> Table::rowCells(int rowIndex)
{
Row *row = m_rows.at(rowIndex);
return row->cells();
}
QList<Row *> Table::rows()
{
return m_rows;
}
/*!
* \brief 设置样式
* \param style
*/
void Table::setStyle(const QString &style)
{
m_ctTbl->setStyle(style);
}
void Table::setAlignment(WD_TABLE_ALIGNMENT alignment)
{
m_ctTbl->setAlignment(alignment);
}
Columns::Columns()
{
}
Columns::~Columns()
{
}
Column::Column(const QDomElement &tlGrid, int gridIndex, Table *table)
: m_grid(tlGrid), m_index(gridIndex), m_table(table)
{
}
Length Column::width() const
{
return Length();
}
void Column::setWidth()
{
}
Column::~Column()
{
}
Row::Row(const QDomElement &element, Table *table)
: m_ele(element), m_table(table)
{
m_dom = m_table->m_dom;
m_part= m_table->m_part;
loadExistElement();
}
/*!
* \brief 初始化列
*/
void Row::loadExistElement()
{
QDomNodeList eleList = m_ele.childNodes();
if (eleList.isEmpty())
return;
for (int i = 0; i < eleList.count(); i++) {
QDomElement celEle = eleList.at(i).toElement();
if (celEle.nodeName() != strtblCell)
continue;
Cell *cell = new Cell(celEle, this);
m_cells.append(cell);
// 纵向
QString strMerge = cell->m_tc->vMerge();
if (!cell->m_tc->m_vMerge.isNull() && strMerge != QStringLiteral("restart")) {
Row *uprow = m_table->m_rows.last();
Cell *upcell = uprow->m_cells.at(m_cells.count() - 1);
celEle = QDomElement(upcell->m_tc->ele());
//
m_cells.removeAll(cell);
delete cell;
cell = new Cell(celEle, this);
m_cells.append(cell);
}
// 横向
int spanCount = cell->m_tc->gridSpan();
if (spanCount > 1)
for (int i = 1; i < spanCount; i++) {
Cell *hcell = new Cell(celEle, this);
m_cells.append(hcell);
}
}
//qDebug() << "Current row's cell count" << m_cells.count();
}
void Row::addTc()
{
QDomElement celEle = m_dom->createElement(strtblCell);
Cell *cell = new Cell(celEle, this);
m_cells.append(cell);
m_ele.appendChild(celEle);
}
Table *Row::table() const
{
return m_table;
}
QList<Cell *> Row::cells() const
{
return m_cells;
}
Table *Row::table()
{
return m_table;
}
int Row::rowIndex()
{
return m_table->m_rows.indexOf(this);
}
Row::~Row()
{
}
Cell::Cell(const QDomElement &element, Row *row)
: m_row(row)
{
m_dom = row->m_dom;
m_part = row->m_part;
m_tc = QSharedPointer<CT_Tc>(new CT_Tc(this, element));
if (!m_tc->m_isLoad)
addParagraph();
else {
QDomNode node = element.lastChild();
if (!node.isNull() && node.nodeName() == QStringLiteral("w:p")) {
QDomElement pEle = node.toElement();
m_currentpara = new Paragraph(m_part, pEle);
m_paras.append(m_currentpara);
}
}
}
Paragraph *Cell::addParagraph(const QString &text, const QString &style)
{
QDomElement pEle = m_dom->createElement(QStringLiteral("w:p"));
m_currentpara = new Paragraph(m_part, pEle);
if (!text.isEmpty())
m_currentpara->addRun(text, style);
m_tc->m_ele.appendChild(pEle);
m_paras.append(m_currentpara);
return m_currentpara;
}
void Cell::addText(const QString &text)
{
m_currentpara->addRun(text);
}
Table *Cell::addTable(int rows, int cols, const QString &style)
{
QDomElement pEle = m_dom->createElement(QStringLiteral("w:tbl"));
Table *table = new Table(m_part, pEle);
m_tc->m_ele.appendChild(pEle);
for (int i = 0; i < cols; i++) {
table->addColumn();
}
for (int i = 0; i < rows; i++) {
table->addRow();
}
table->setStyle(style);
addParagraph();
return table;
}
Cell *Cell::merge(Cell *other)
{
QSharedPointer<CT_Tc> tc = this->m_tc;
QSharedPointer<CT_Tc> tc2 = other->m_tc;
CT_Tc *tcReturn = tc->merge(tc2);
return tcReturn->m_cell;
}
int Cell::cellIndex()
{
return m_row->m_cells.indexOf(this);
}
int Cell::rowIndex()
{
return m_row->rowIndex();
}
Table *Cell::table()
{
return m_row->m_table;
}
Cell::~Cell()
{
qDeleteAll(m_paras);
}
}