File: DateVariable.cpp

package info (click to toggle)
koffice 1%3A2.2.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 157,656 kB
  • ctags: 110,762
  • sloc: cpp: 889,358; xml: 22,758; ansic: 6,533; python: 5,224; perl: 2,771; sh: 1,805; yacc: 1,304; ruby: 1,219; sql: 720; lex: 455; makefile: 76
file content (261 lines) | stat: -rw-r--r-- 7,961 bytes parent folder | download
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
/* This file is part of the KDE project
 * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
 * Copyright (C) 2008,2010 Thorsten Zachmann <zachmann@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 "DateVariable.h"
#include "FixedDateFormat.h"

#include <KoProperties.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <KoShapeLoadingContext.h>
#include <KoShapeSavingContext.h>
#include <KoOdfLoadingContext.h>
#include <KoOdfStylesReader.h>

DateVariable::DateVariable(DateType type)
        : KoVariable()
        , m_type(type)
        , m_displayType(Custom)
        , m_daysOffset(0)
        , m_monthsOffset(0)
        , m_yearsOffset(0)
        , m_secsOffset(0)
{
    m_time = QDateTime::currentDateTime();
}

DateVariable::~DateVariable()
{
}

void DateVariable::saveOdf(KoShapeSavingContext & context)
{
    // TODO support data-style-name
    KoXmlWriter *writer = &context.xmlWriter();
    if (m_displayType == Time) {
        writer->startElement("text:time", false);
    } else {
        writer->startElement("text:date", false);
    }
    if (m_type == Fixed) {
        writer->addAttribute("text:fixed", "true");
    } else {
        writer->addAttribute("text:fixed", "false");
    }
    if (m_displayType == Time) {
        writer->addAttribute("text:time-value", m_time.toString(Qt::ISODate));
    } else {
        writer->addAttribute("text:date-value", m_time.toString(Qt::ISODate));
    }
    writer->addTextNode(value());
    writer->endElement();
}

bool DateVariable::loadOdf(const KoXmlElement & element, KoShapeLoadingContext & context)
{
    const QString localName(element.localName());
    QString dateFormat = "";
    QString dataStyle = element.attributeNS(KoXmlNS::style, "data-style-name");
    if (!dataStyle.isEmpty()) {
        if (context.odfLoadingContext().stylesReader().dataFormats().contains(dataStyle)) {
            KoOdfNumberStyles::NumericStyleFormat dataFormat = context.odfLoadingContext().stylesReader().dataFormats().value(dataStyle);
            dateFormat = dataFormat.prefix + dataFormat.formatStr + dataFormat.suffix;
        }
    }

    //dateProperties.setProperty("fixed", QVariant(element.attributeNS(KoXmlNS::text, "fixed") == "true"));
    if (element.attributeNS(KoXmlNS::text, "fixed", "false") == "true") {
        m_type = Fixed;
    } else {
        m_type = AutoUpdate;
    }

    //dateProperties.setProperty("time", element.attributeNS(KoXmlNS::text, localName + "-value"));
    const QString value(element.attributeNS(KoXmlNS::text, localName + "-value", ""));
    if (!value.isEmpty()) {
        m_time = QDateTime::fromString(value, Qt::ISODate);
    } else {
        //TODO see 6.2.1 Date Fields
    }

    //dateProperties.setProperty("definition", dateFormat);
    m_definition = dateFormat;

    if (dateFormat.isEmpty())
        if (localName == "time") {
            m_displayType = Time;
        } else {
            m_displayType = Date;
        }
    else {
        m_displayType = Custom;
    }

    //dateProperties.setProperty("adjust", element.attributeNS(KoXmlNS::text, localName + "-adjust"));
    const QString adjust(element.attributeNS(KoXmlNS::text, localName + "-adjust", ""));
    adjustTime(adjust);
    update();
    return true;
}

void DateVariable::setProperties(const KoProperties *props)
{
    m_definition = props->stringProperty("definition");
    if (!props->stringProperty("time").isEmpty())
        m_time = QDateTime::fromString(props->stringProperty("time"), Qt::ISODate);
    if (props->intProperty("id") == Fixed)
        m_type = Fixed;
    else
        m_type = AutoUpdate;
    QString displayTypeProp = props->stringProperty("displayType", "custom");
    if (displayTypeProp == "custom")
        m_displayType = Custom;
    else if (displayTypeProp == "date")
        m_displayType = Date;
    else if (displayTypeProp == "time")
        m_displayType = Time;
    adjustTime(props->stringProperty("adjust"));
    update();
}

QWidget *DateVariable::createOptionsWidget()
{
    switch (m_type) {
    case Fixed:
        return new FixedDateFormat(this);
    default:;
    }
    return 0;
}

void DateVariable::setDefinition(const QString &definition)
{
    m_definition = definition;
    update();
}

void DateVariable::setSecsOffset(int offset)
{
    m_secsOffset = offset;
    update();
}

void DateVariable::setDaysOffset(int offset)
{
    m_daysOffset = offset;
    update();
}

void DateVariable::setMonthsOffset(int offset)
{
    m_monthsOffset = offset;
    update();
}

void DateVariable::setYearsOffset(int offset)
{
    m_yearsOffset = offset;
    update();
}

void DateVariable::update()
{
    QDateTime target;
    switch (m_type) {
    case Fixed:
        target = m_time;
        break;
    case AutoUpdate:
        target = QDateTime::currentDateTime();
        break;
    }
    target = target.addSecs(m_secsOffset);
    target = target.addDays(m_daysOffset);
    target = target.addMonths(m_monthsOffset);
    target = target.addYears(m_yearsOffset);
    switch (m_displayType) {
    case Custom:
        setValue(target.toString(m_definition));
        break;
    case Time:
        setValue(target.time().toString(Qt::LocalDate));
        break;
    case Date:
        setValue(target.date().toString(Qt::LocalDate));
        break;
    }
}

void DateVariable::adjustTime(const QString & value)
{
    if (!value.isEmpty()) {
        m_daysOffset = 0;
        m_monthsOffset = 0;
        m_yearsOffset = 0;
        m_secsOffset = 0;
        int multiplier = 1;
        if (value.contains("-")) {
            multiplier = -1;
        }
        QString timePart;
        QString datePart;
        QStringList parts = value.mid(value.indexOf('P') + 1).split('T');
        datePart = parts[0];
        if (parts.size() > 1) {
            timePart = parts[1];
        }
        QRegExp rx("([0-9]+)([DHMSY])");
        int value;
        bool valueOk;
        if (!timePart.isEmpty()) {
            int pos = 0;
            while ((pos = rx.indexIn(timePart, pos)) != -1) {
                value = rx.cap(1).toInt(&valueOk);
                if (valueOk) {
                    if (rx.cap(2) == "H") {
                        m_secsOffset += multiplier * 3600 * value;
                    } else if (rx.cap(2) == "M") {
                        m_secsOffset += multiplier * 60 * value;
                    } else if (rx.cap(2) == "S") {
                        m_secsOffset += multiplier * value;
                    }
                }
                pos += rx.matchedLength();
            }
        }
        if (!datePart.isEmpty()) {
            int pos = 0;
            while ((pos = rx.indexIn(datePart, pos)) != -1) {
                value = rx.cap(1).toInt(&valueOk);
                if (valueOk) {
                    if (rx.cap(2) == "Y") {
                        m_yearsOffset += multiplier * value;
                    } else if (rx.cap(2) == "M") {
                        m_monthsOffset += multiplier * value;
                    } else if (rx.cap(2) == "D") {
                        m_daysOffset += multiplier * value;
                    }
                }
                pos += rx.matchedLength();
            }
        }
    }
}