Commit b67fbd6b authored by Pierre Ducroquet's avatar Pierre Ducroquet

Rewrite the way date/time fields are handled. Now, most of the work is done by DateVariable...

I'd like comments about the changes in DateVariable, I'm pretty sure things can be improved there...


svn path=/trunk/koffice/; revision=689625
parent de2491b1
......@@ -829,70 +829,17 @@ void KoTextLoader::loadSpan(KoTextLoadingContext& context, const KoXmlElement& p
dateFormat = dataFormat.prefix + dataFormat.formatStr + dataFormat.suffix;
}
}
kDebug() << "Final date format :" << dateFormat << endl;
KoInlineObject *dateObject = dateFactory->createInlineObject(new KoProperties());
QString result;
KoProperties *dateProperties = new KoProperties();
dateProperties->setProperty("fixed", QVariant(ts.attributeNS(KoXmlNS::text, "fixed") == "true"));
dateProperties->setProperty("time", ts.attributeNS(KoXmlNS::text, localName + "-value"));
dateProperties->setProperty("definition", dateFormat);
dateProperties->setProperty("adjust", ts.attributeNS(KoXmlNS::text, localName + "-adjust"));
if (dateFormat.isEmpty())
dateProperties->setProperty("displayType", localName);
else
dateProperties->setProperty("displayType", "custom");
QDateTime dateTime = QDateTime::fromString(ts.attributeNS(KoXmlNS::text, localName + "-value"), Qt::ISODate);
if (ts.attributeNS(KoXmlNS::text, "fixed") != "true")
dateTime = QDateTime::currentDateTime();
QString adjustTime = ts.attributeNS(KoXmlNS::text, localName + "-adjust");
if (!adjustTime.isEmpty()) {
int multiplier = 1;
if (adjustTime.contains("-"))
multiplier = -1;
QString timePart, datePart;
QStringList parts = adjustTime.mid(adjustTime.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")
dateTime = dateTime.addSecs(multiplier * 3600 * value);
else if (rx.cap(2) == "M")
dateTime = dateTime.addSecs(multiplier * 60 * value);
else if (rx.cap(2) == "S")
dateTime = dateTime.addSecs(multiplier * value);
}
pos += rx.matchedLength();
}
}
if (!datePart.isEmpty()) {
int pos = 0;
while ((pos = rx.indexIn(timePart, pos)) != -1) {
value = rx.cap(1).toInt(&valueOk);
if (valueOk) {
if (rx.cap(2) == "Y")
dateTime = dateTime.addYears(multiplier * value);
else if (rx.cap(2) == "M")
dateTime = dateTime.addMonths(multiplier * value);
else if (rx.cap(2) == "D")
dateTime = dateTime.addDays(multiplier * value);
}
pos += rx.matchedLength();
}
}
}
if (localName == "date") {
if (dateFormat.isEmpty())
result = dateTime.date().toString(Qt::LocalDate);
else
result = dateTime.date().toString(dateFormat);
} else {
if (dateFormat.isEmpty())
result = dateTime.time().toString(Qt::LocalDate);
else
result = dateTime.time().toString(dateFormat);
}
((KoVariable *)dateObject)->setValue(result);
KoInlineObject *dateObject = dateFactory->createInlineObject(dateProperties);
textObjectManager->insertInlineObject(cursor, dateObject);
}
}
......
......@@ -25,14 +25,78 @@
DateVariable::DateVariable(DateType type)
: KoVariable(),
m_type(type),
m_offset(0)
m_displayType(Custom),
m_daysOffset(0),
m_monthsOffset(0),
m_yearsOffset(0),
m_secsOffset(0)
{
m_time = QDateTime::currentDateTime();
}
void DateVariable::setProperties(const KoProperties *props) {
m_definition = props->stringProperty("definition");
m_offset = props->intProperty("offset", 0);
if (!props->stringProperty("time").isEmpty())
m_time = QDateTime::fromString(props->stringProperty("time"), Qt::ISODate);
if (props->boolProperty("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;
QString adjustTime = props->stringProperty("adjust");
if (!adjustTime.isEmpty()) {
m_daysOffset = 0;
m_monthsOffset = 0;
m_yearsOffset = 0;
m_secsOffset = 0;
int multiplier = 1;
if (adjustTime.contains("-"))
multiplier = -1;
QString timePart, datePart;
QStringList parts = adjustTime.mid(adjustTime.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();
}
}
}
update();
}
......@@ -49,15 +113,49 @@ void DateVariable::setDefinition(const QString &definition) {
update();
}
void DateVariable::setOffset(int offset) {
m_offset = offset;
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:
setValue(m_time.addDays(m_offset).toString(m_definition));
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;
}
}
......@@ -34,7 +34,14 @@
class DateVariable : public KoVariable {
public:
enum DateType {
Fixed
Fixed,
AutoUpdate
};
enum DisplayType {
Date,
Time,
Custom
};
/**
......@@ -49,16 +56,26 @@ public:
QString definition() const { return m_definition; }
void setDefinition(const QString &definition);
int offset() const { return m_offset; }
void setOffset(int offset);
int daysOffset() const { return m_daysOffset; }
void setDaysOffset(int daysOffset);
int monthsOffset() const { return m_monthsOffset; }
void setMonthsOffset(int monthsOffset);
int yearsOffset() const { return m_yearsOffset; }
void setYearsOffset(int yearsOffset);
int secsOffset() const { return m_secsOffset; }
void setSecsOffset(int secsOffset);
private:
void update();
DateType m_type;
DisplayType m_displayType;
QString m_definition;
QDateTime m_time;
int m_offset;
int m_daysOffset, m_monthsOffset, m_yearsOffset, m_secsOffset;
};
#endif
......@@ -110,7 +110,7 @@ void FixedDateFormat::listClicked(QListWidgetItem *item) {
}
void FixedDateFormat::offsetChanged(int offset) {
m_variable->setOffset(offset);
m_variable->setDaysOffset(offset);
}
void FixedDateFormat::insertCustomButtonPressed() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment