File: MsooXmlDrawingTableStyle.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (316 lines) | stat: -rw-r--r-- 11,241 bytes parent folder | download | duplicates (2)
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
316
#include "MsooXmlDrawingTableStyle.h"
#include <QDebug>

//#define MSOOXMLDRAWING_DEBUG_TABLES
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
#include <kdebug.h>
#endif

using namespace MSOOXML;

DrawingTableStyleConverterProperties::DrawingTableStyleConverterProperties()
: TableStyleConverterProperties()
, m_role(DrawingTableStyle::WholeTbl)
{
}

DrawingTableStyleConverterProperties::~DrawingTableStyleConverterProperties()
{
}

DrawingTableStyleConverterProperties::Roles DrawingTableStyleConverterProperties::roles() const
{
    return m_role;
}

void DrawingTableStyleConverterProperties::setRoles(DrawingTableStyleConverterProperties::Roles roles)
{
    m_role = roles;
}

DrawingTableStyleConverter::DrawingTableStyleConverter(MSOOXML::DrawingTableStyleConverterProperties const& properties, DrawingTableStyle* style)
: TableStyleConverter(properties.rowCount(), properties.columnCount())
, m_style(style)
, m_properties(properties)
{
}

DrawingTableStyleConverter::~DrawingTableStyleConverter()
{
}

void DrawingTableStyleConverter::applyStyle(MSOOXML::DrawingTableStyle::Type type, KoCellStyle::Ptr& style,
                                            int row, int column, const QPair<int, int> &spans)
{
    if (!m_style) {
        return;
    }

    TableStyleProperties* styleProperties = m_style->properties(type);

//     if (type == DrawingTableStyle::WholeTbl) {
//         styleProperties->bordersToEdgesOnly = true;
//     }

    TableStyleConverter::applyStyle(styleProperties, style, row, column, spans);
}

KoCellStyle::Ptr DrawingTableStyleConverter::style(int row, int column, const QPair<int, int> &spans)
{
    Q_ASSERT(row >= 0);
    Q_ASSERT(row < m_properties.rowCount());
    Q_ASSERT(column >= 0);
    Q_ASSERT(column < m_properties.columnCount());

    //TODO: can we magically improve the creation of the styles?  For now I'll
    //take the naive approach and say no.  There are way, way too many things
    //to take into account so, reusing the styles doesn't seem feasible.

    KoCellStyle::Ptr cellStyle = KoCellStyle::create();

    // When specified, the conditional formats are applied in the following
    // order (therefore subsequent formats override properties on previous
    // formats):
    //
    // * Whole table
    // * Banded columns, even column banding
    // * Banded rows, even row banding
    // * First row, last row
    // * First column, last column
    // * Top left, top right, bottom left, bottom right
    //
    // NOTE: The standard doesn't say what happens to colliding properties in
    // the same hierarchy level (say, it's the first column and last column, or
    // it's first row and last row at once, I assume left to right priority on
    // the elements in the previous list.)
    //
    // See MSOOXML Table Styles ยง17.7.6 for details.

    const DrawingTableStyleConverterProperties::Roles& role = m_properties.roles();
    const int lastRow = m_properties.rowCount() - 1;
    const int lastColumn = m_properties.columnCount() - 1;

#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "==> [styles.xml] TABLE-level:";
#endif
    applyStyle(DrawingTableStyle::WholeTbl, cellStyle, row, column, spans);
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "<== [END] [styles.xml] TABLE-level:";
#endif
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "==> [local] TABLE-level:";
#endif
    TableStyleConverter::applyStyle(m_properties.localDefaultCellStyle(), cellStyle, row, column, spans);
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "<== [END] [local] TABLE-level:";
#endif

    if (role & DrawingTableStyleConverterProperties::ColumnBanded) {
        // If last column is activated and it's last column, banding is not applied
        if (!(role & DrawingTableStyleConverterProperties::LastCol && column == lastColumn)) {
            // First column active means that the banding is shifted by one
            if (role & DrawingTableStyleConverterProperties::FirstCol) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
                kDebug() << "====> [ColumnBanded] FirstCol:";
#endif
                // Banding not applied if it's the first column
                if (column != 0) {
                    // Banding reversed
                    if ((column % (m_properties.columnBandSize() * 2)) < m_properties.columnBandSize()) {
                        applyStyle(DrawingTableStyle::Band2Vertical, cellStyle, row, column, spans);
                    }
                    else {
                        applyStyle(DrawingTableStyle::Band1Vertical, cellStyle, row, column, spans);
                    }
                }
            }
            else {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
                kDebug() << "====> [ColumnBanded] Other:";
#endif
                //Is the column in the even band?
                if ((column % (m_properties.columnBandSize() * 2)) < m_properties.columnBandSize()) {
                    applyStyle(DrawingTableStyle::Band1Vertical, cellStyle, row, column, spans);
                }
                else {
                    applyStyle(DrawingTableStyle::Band2Vertical, cellStyle, row, column, spans);
                }
            }
        }
    }

    if (role & DrawingTableStyleConverterProperties::RowBanded) {
        // If last row is activated and it's last row, banding is not applied
        if (!(role & DrawingTableStyleConverterProperties::LastRow && row == lastRow)) {
            // First row active means that the banding is shifted by one
            if (role & DrawingTableStyleConverterProperties::FirstRow) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
                kDebug() << "====> [RowBanded] FirstRow:";
#endif
                // Banding not applied if it's the first row
                if (row != 0) {
                    // In case the first row is activated, the banding applying is reversed
                    if( (row % (m_properties.rowBandSize() * 2)) < m_properties.rowBandSize()) {
                        applyStyle(DrawingTableStyle::Band2Horizontal, cellStyle, row, column, spans);
                    }
                    else {
                        applyStyle(DrawingTableStyle::Band1Horizontal, cellStyle, row, column, spans);
                    }
                }
            }
            else {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
                kDebug() << "====> [RowBanded] Other:";
#endif
                //Is the row in the even band?
                if( (row % (m_properties.rowBandSize() * 2)) < m_properties.rowBandSize()) {
                    applyStyle(DrawingTableStyle::Band1Horizontal, cellStyle, row, column, spans);
                }
                else {
                    applyStyle(DrawingTableStyle::Band2Horizontal, cellStyle, row, column, spans);
                }
            }
        }
    }

    //NOTE: According to test data, at least for the first table cell the
    //FirstCol format applies before the FirstRow format.  Also the previous
    //approach with RowBanded cells might be an option.

    if(role & DrawingTableStyleConverterProperties::FirstCol) {
        if(column == 0) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> FirstCol:";
#endif
            applyStyle(DrawingTableStyle::FirstCol, cellStyle, row, column, spans);
        }
    }

    if (role & DrawingTableStyleConverterProperties::FirstRow) {
        if(row == 0) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> FirstRow:";
#endif
            applyStyle(DrawingTableStyle::FirstRow, cellStyle, row, column, spans);
        }
    }

    if (role & DrawingTableStyleConverterProperties::LastRow) {
        if(row == lastRow) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> LastRow:";
#endif
            applyStyle(DrawingTableStyle::LastRow, cellStyle, row, column, spans);
        }
    }



    if(role & DrawingTableStyleConverterProperties::LastCol) {
        if(column == lastColumn) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> LastCol:";
#endif
            applyStyle(DrawingTableStyle::LastCol, cellStyle, row, column, spans);
        }
    }

    if(role & DrawingTableStyleConverterProperties::NeCell) {
        if(row == 0 && column == 0) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> NeCell:";
#endif
            applyStyle(DrawingTableStyle::NeCell, cellStyle, row, column, spans);
        }
    }

    if(role & DrawingTableStyleConverterProperties::NwCell) {
        if(row == 0 && column == lastColumn) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> NwCell:";
#endif
            applyStyle(DrawingTableStyle::NwCell, cellStyle, row, column, spans);
        }
    }

    if(role & DrawingTableStyleConverterProperties::SeCell) {
        if(row == lastRow && column == 0) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> SeCell:";
#endif
            applyStyle(DrawingTableStyle::SeCell, cellStyle, row, column, spans);
        }
    }

    if(role & DrawingTableStyleConverterProperties::SwCell) {
        if(row == lastRow && column == lastColumn) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
            kDebug() << "====> SwCell:";
#endif
            applyStyle(DrawingTableStyle::SwCell, cellStyle, row, column, spans);
        }
    }

    TableStyleProperties* localProperties = m_properties.localStyles().localStyle(row, -1);
    if (localProperties) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
        kDebug() << "==> ROW-level:";
#endif
        TableStyleConverter::applyStyle(localProperties, cellStyle, row, column, spans);
        localProperties = 0;
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
        kDebug() << "<== [END] ROW-level:";
#endif
    }

    localProperties = m_properties.localStyles().localStyle(row, column);
    if (localProperties) {
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
        kDebug() << "==> CELL-level:";
#endif
        TableStyleConverter::applyStyle(localProperties, cellStyle, row, column, spans);
        localProperties = 0;
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
        kDebug() << "<== [END] CELL-level:";
#endif
    }

    //RE-APPLY table-level border properties from tblBorders
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "==> [REAPPLY]: TABLE/ROW-level properties";
#endif
    TableStyleProperties* tableProperties = 0;
    if (m_style) {
        tableProperties = m_style->properties(DrawingTableStyle::WholeTbl);
    }
    reapplyTableLevelBordersStyle(tableProperties,
                                  m_properties.localDefaultCellStyle(),
                                  m_properties.localStyles().localStyle(row, -1),
                                  cellStyle, row, column, spans);
#ifdef MSOOXMLDRAWING_DEBUG_TABLES
    kDebug() << "<== [END] [REAPPLY]: TABLE/ROW-level properties";
#endif

    return cellStyle;
}

DrawingTableStyle::DrawingTableStyle()
: m_properties()
{
}

DrawingTableStyle::~DrawingTableStyle()
{
//     FIXME: we crash because of this.
//     qDeleteAll(m_properties.values());
}

void DrawingTableStyle::addProperties(DrawingTableStyle::Type type, TableStyleProperties* properties)
{
    m_properties.insert(type, properties);
}

TableStyleProperties* DrawingTableStyle::properties(DrawingTableStyle::Type type) const
{
    return m_properties.value(type);
}