File: KoColumns.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (280 lines) | stat: -rw-r--r-- 9,900 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
/* This file is part of the KDE project
   Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "KoColumns.h"

#include "KoXmlWriter.h"
#include "KoGenStyle.h"
#include "KoXmlReader.h"
#include "KoXmlNS.h"
#include "KoUnit.h"
#include "OdfDebug.h"

static const int defaultColumnCount = 1;
static const KoColumns::SeparatorStyle defaultSeparatorStyle = KoColumns::None;
static const int defaultSeparatorHeight = 100;
static const  Qt::GlobalColor defaultSeparatorColor = Qt::black;
static const KoColumns::SeparatorVerticalAlignment defaultSeparatorVerticalAlignment = KoColumns::AlignTop;
static const int defaultColumnGapWidth = 17; // in pt, ~ 6mm



const char * KoColumns::separatorStyleString(KoColumns::SeparatorStyle separatorStyle)
{
    const char * result;

    //  skip KoColumns::None, is default
    if (separatorStyle == Solid) {
        result = "solid";
    } else if (separatorStyle == Dotted) {
        result = "dotted";
    } else if (separatorStyle == Dashed) {
        result = "dashed";
    } else if (separatorStyle == DotDashed) {
        result = "dot-dashed";
    } else {
        result = "none";
    }

    return result;
}

const char * KoColumns::separatorVerticalAlignmentString(KoColumns::SeparatorVerticalAlignment separatorVerticalAlignment)
{
    const char * result;

    //  skip KoColumns::AlignTop, is default
    if (separatorVerticalAlignment == AlignVCenter) {
        result = "middle";
    } else if (separatorVerticalAlignment == AlignBottom) {
        result = "bottom";
    } else {
        result = "top";
    }

    return result;
}

KoColumns::SeparatorVerticalAlignment KoColumns::parseSeparatorVerticalAlignment(const QString &value)
{
    // default to AlignTop
    SeparatorVerticalAlignment result = defaultSeparatorVerticalAlignment;

    if (! value.isEmpty()) {
        // skip "top", is default
        if (value == QLatin1String("middle")) {
            result = AlignVCenter;
        } else if (value == QLatin1String("bottom")) {
            result = AlignBottom;
        }
    }

    return result;
}

QColor KoColumns::parseSeparatorColor(const QString &value)
{
    QColor result(value);

    if (! result.isValid())
        // default is black, cmp. ODF 1.2 ยง19.467
        result = QColor(defaultSeparatorColor);

    return result;
}

// TODO: see if there is another parse method somewhere which can be used here
int KoColumns::parseSeparatorHeight(const QString &value)
{
    int result = defaultSeparatorHeight;

    // only try to convert if it ends with a %, so is also not empty
    if (value.endsWith(QLatin1Char('%'))) {
        bool ok = false;
        // try to convert
        result = value.leftRef(value.length()-1).toInt(&ok);
        // reset to 100% if conversion failed (which sets result to 0)
        if (! ok) {
            result = defaultSeparatorHeight;
        }
    }

    return result;
}

KoColumns::SeparatorStyle KoColumns::parseSeparatorStyle(const QString &value)
{
    SeparatorStyle result = None;
    if (! value.isEmpty()) {
        //  skip "none", is default
        if (value == QLatin1String("solid")) {
            result = Solid;
        } else if (value == QLatin1String("dotted")) {
            result = Dotted;
        } else if (value == QLatin1String("dashed")) {
            result = Dashed;
        } else if (value == QLatin1String("dot-dashed")) {
            result = DotDashed;
        }
    }

    return result;
}

int KoColumns::parseRelativeWidth(const QString &value)
{
    int result = 0;

    // only try to convert if it ends with a *, so is also not empty
    if (value.endsWith(QLatin1Char('*'))) {
        bool ok = false;
        // try to convert
        result = value.leftRef(value.length()-1).toInt(&ok);
        if (! ok) {
            result = 0;
        }
    }

    return result;
}

KoColumns::KoColumns()
  : count(defaultColumnCount)
  , gapWidth(defaultColumnGapWidth)
  , separatorStyle(defaultSeparatorStyle)
  , separatorColor(defaultSeparatorColor)
  , separatorVerticalAlignment(defaultSeparatorVerticalAlignment)
  , separatorHeight(defaultSeparatorHeight)
{
}

void KoColumns::reset()
{
    count = defaultColumnCount;
    gapWidth = defaultColumnGapWidth;
    separatorStyle = defaultSeparatorStyle;
    separatorColor = QColor(defaultSeparatorColor);
    separatorVerticalAlignment = defaultSeparatorVerticalAlignment;
    separatorHeight = defaultSeparatorHeight;
}

bool KoColumns::operator==(const KoColumns& rhs) const {
    return
        count == rhs.count &&
        (columnData.isEmpty() && rhs.columnData.isEmpty() ?
             (qAbs(gapWidth - rhs.gapWidth) <= 1E-10) :
             (columnData == rhs.columnData));
}

bool KoColumns::operator!=(const KoColumns& rhs) const {
    return
        count != rhs.count ||
        (columnData.isEmpty() && rhs.columnData.isEmpty() ?
             qAbs(gapWidth - rhs.gapWidth) > 1E-10 :
             ! (columnData == rhs.columnData));
}

void KoColumns::loadOdf(const KoXmlElement &style)
{
    KoXmlElement columnsElement = KoXml::namedItemNS(style, KoXmlNS::style, "columns");
    if (!columnsElement.isNull()) {
        count = columnsElement.attributeNS(KoXmlNS::fo, "column-count").toInt();
        if (count < 1)
            count = 1;
        gapWidth = KoUnit::parseValue(columnsElement.attributeNS(KoXmlNS::fo, "column-gap"));

        KoXmlElement columnSep = KoXml::namedItemNS(columnsElement, KoXmlNS::style, "column-sep");
        if (! columnSep.isNull()) {
            separatorStyle = parseSeparatorStyle(columnSep.attributeNS(KoXmlNS::style, "style"));
            separatorWidth = KoUnit::parseValue(columnSep.attributeNS(KoXmlNS::style, "width"));
            separatorHeight = parseSeparatorHeight(columnSep.attributeNS(KoXmlNS::style, "height"));
            separatorColor = parseSeparatorColor(columnSep.attributeNS(KoXmlNS::style, "color"));
            separatorVerticalAlignment =
                parseSeparatorVerticalAlignment(columnSep.attributeNS(KoXmlNS::style, "vertical-align"));
        }

        KoXmlElement columnElement;
        forEachElement(columnElement, columnsElement) {
            if(columnElement.localName() != QLatin1String("column") ||
               columnElement.namespaceURI() != KoXmlNS::style)
                continue;

            ColumnDatum datum;
            datum.leftMargin = KoUnit::parseValue(columnElement.attributeNS(KoXmlNS::fo, "start-indent"), 0.0);
            datum.rightMargin = KoUnit::parseValue(columnElement.attributeNS(KoXmlNS::fo, "end-indent"), 0.0);
            datum.topMargin = KoUnit::parseValue(columnElement.attributeNS(KoXmlNS::fo, "space-before"), 0.0);
            datum.bottomMargin = KoUnit::parseValue(columnElement.attributeNS(KoXmlNS::fo, "space-after"), 0.0);
            datum.relativeWidth = parseRelativeWidth(columnElement.attributeNS(KoXmlNS::style, "rel-width"));
            // on a bad relativeWidth just drop all data
            if (datum.relativeWidth <= 0) {
                columnData.clear();
                break;
            }

            columnData.append(datum);
        }

        if (! columnData.isEmpty() && count != columnData.count()) {
            warnOdf << "Found not as many <style:column> elements as attribute fo:column-count has set:"<< count;
            columnData.clear();
        }
    } else {
        reset();
    }
}

void KoColumns::saveOdf(KoGenStyle &style) const
{
    if (count > 1) {
        QBuffer buffer;
        buffer.open(QIODevice::WriteOnly);
        KoXmlWriter writer(&buffer);

        writer.startElement("style:columns");
        writer.addAttribute("fo:column-count", count);
        if (columnData.isEmpty()) {
            writer.addAttributePt("fo:column-gap", gapWidth);
        }

        if (separatorStyle != KoColumns::None) {
            writer.startElement("style:column-sep");
            writer.addAttribute("style:style", separatorStyleString(separatorStyle));
            writer.addAttributePt("style:width", separatorWidth);
            writer.addAttribute("style:height", QString::number(separatorHeight)+QLatin1Char('%'));
            writer.addAttribute("style:color", separatorColor.name());
            writer.addAttribute("style:vertical-align", separatorVerticalAlignmentString(separatorVerticalAlignment));
            writer.endElement(); // style:column-sep
        }

        foreach(const ColumnDatum &cd, columnData) {
            writer.startElement("style:column");
            writer.addAttributePt("fo:start-indent", cd.leftMargin);
            writer.addAttributePt("fo:end-indent", cd.rightMargin);
            writer.addAttributePt("fo:space-before", cd.topMargin);
            writer.addAttributePt("fo:space-after", cd.bottomMargin);
            writer.addAttribute("style:rel-width", QString::number(cd.relativeWidth)+QLatin1Char('*'));
            writer.endElement(); // style:column
        }

        writer.endElement(); // style:columns

        QString contentElement = QString::fromUtf8(buffer.buffer(), buffer.buffer().size());
        style.addChildElement("style:columns", contentElement);
    }
}