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
|
/* This file is part of the KDE project
* Copyright (C) 2011 Sebastian Sauer <mail@dipe.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 "UserVariable.h"
#include "UserVariableOptionsWidget.h"
#include "VariablesDebug.h"
#include <KoXmlReader.h>
#include <KoXmlWriter.h>
#include <KoProperties.h>
#include <KoOdfLoadingContext.h>
#include <KoShapeSavingContext.h>
#include <KoShapeLoadingContext.h>
#include <KoXmlNS.h>
#include <KoInlineTextObjectManager.h>
#include <KoVariableManager.h>
#include <KoTextDocument.h>
#include <klocalizedstring.h>
#include <QTextInlineObject>
UserVariable::UserVariable()
: KoVariable(true)
, m_variableManager(0)
, m_property(0)
{
}
KoVariableManager *UserVariable::variableManager()
{
if (m_variableManager) {
return m_variableManager;
}
KoInlineTextObjectManager *textObjectManager = manager();
Q_ASSERT(textObjectManager);
m_variableManager = textObjectManager->variableManager();
Q_ASSERT(m_variableManager);
connect(m_variableManager, SIGNAL(valueChanged()), this, SLOT(valueChanged()));
valueChanged(); // initial update
return m_variableManager;
}
int UserVariable::property() const
{
return m_property;
}
const QString& UserVariable::name() const
{
return m_name;
}
void UserVariable::setName(const QString &name)
{
m_name = name;
valueChanged();
}
KoOdfNumberStyles::NumericStyleFormat UserVariable::numberstyle() const
{
return m_numberstyle;
}
void UserVariable::setNumberStyle(KoOdfNumberStyles::NumericStyleFormat numberstyle)
{
m_numberstyle = numberstyle;
valueChanged();
}
QWidget* UserVariable::createOptionsWidget()
{
UserVariableOptionsWidget *configWidget = new UserVariableOptionsWidget(this);
return configWidget;
}
void UserVariable::valueChanged()
{
//TODO apply following also to plugins/variables/DateVariable.cpp:96
//TODO handle formula
QString value = variableManager()->value(m_name);
value = KoOdfNumberStyles::format(value, m_numberstyle);
setValue(value);
}
void UserVariable::readProperties(const KoProperties *props)
{
m_property = props->intProperty("varproperty");
//m_name = props->stringProperty("varname");
//debugVariables << m_property << m_name;
//valueChanged();
}
void UserVariable::propertyChanged(Property property, const QVariant &value)
{
Q_UNUSED(property);
Q_UNUSED(value);
//setValue(value.toString());
}
void UserVariable::saveOdf(KoShapeSavingContext &context)
{
if (m_property == 0 && !variableManager()->userVariables().contains(m_name))
return;
KoXmlWriter *writer = &context.xmlWriter();
if (m_property == KoInlineObject::UserGet)
writer->startElement("text:user-field-get", false);
else
writer->startElement("text:user-field-input", false);
if (!m_name.isEmpty())
writer->addAttribute("text:name", m_name);
QString styleName = KoOdfNumberStyles::saveOdfNumberStyle(context.mainStyles(), m_numberstyle);
if (!styleName.isEmpty())
writer->addAttribute("style:data-style-name", styleName);
writer->addTextNode(value());
writer->endElement();
}
bool UserVariable::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
{
if (element.localName() == "user-field-get") {
m_property = KoInlineObject::UserGet;
} else if (element.localName() == "user-field-input") {
m_property = KoInlineObject::UserInput;
} else {
m_property = 0;
}
m_name = element.attributeNS(KoXmlNS::text, "name");
QString dataStyle = element.attributeNS(KoXmlNS::style, "data-style-name");
if (!dataStyle.isEmpty() && context.odfLoadingContext().stylesReader().dataFormats().contains(dataStyle)) {
m_numberstyle = context.odfLoadingContext().stylesReader().dataFormats().value(dataStyle).first;
} else {
m_numberstyle = KoOdfNumberStyles::NumericStyleFormat();
}
return true;
}
void UserVariable::resize(const QTextDocument *document, QTextInlineObject &object, int posInDocument, const QTextCharFormat &format, QPaintDevice *pd)
{
KoVariable::resize(document, object, posInDocument, format, pd);
if (!m_variableManager) {
variableManager();
}
}
|