File indexing completed on 2024-05-12 16:35:03

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Sebastian Sauer <mail@dipe.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "UserVariable.h"
0021 
0022 #include "UserVariableOptionsWidget.h"
0023 #include "VariablesDebug.h"
0024 
0025 #include <KoXmlReader.h>
0026 #include <KoXmlWriter.h>
0027 #include <KoProperties.h>
0028 #include <KoOdfLoadingContext.h>
0029 #include <KoShapeSavingContext.h>
0030 #include <KoShapeLoadingContext.h>
0031 #include <KoXmlNS.h>
0032 #include <KoInlineTextObjectManager.h>
0033 #include <KoVariableManager.h>
0034 #include <KoTextDocument.h>
0035 
0036 #include <klocalizedstring.h>
0037 
0038 #include <QTextInlineObject>
0039 
0040 UserVariable::UserVariable()
0041     : KoVariable(true)
0042     , m_variableManager(0)
0043     , m_property(0)
0044 {
0045 }
0046 
0047 KoVariableManager *UserVariable::variableManager()
0048 {
0049     if (m_variableManager) {
0050         return m_variableManager;
0051     }
0052 
0053     KoInlineTextObjectManager *textObjectManager = manager();
0054     Q_ASSERT(textObjectManager);
0055     m_variableManager = textObjectManager->variableManager();
0056     Q_ASSERT(m_variableManager);
0057     connect(m_variableManager, SIGNAL(valueChanged()), this, SLOT(valueChanged()));
0058 
0059     valueChanged(); // initial update
0060 
0061     return m_variableManager;
0062 }
0063 
0064 int UserVariable::property() const
0065 {
0066     return m_property;
0067 }
0068 
0069 const QString& UserVariable::name() const
0070 {
0071     return m_name;
0072 }
0073 
0074 void UserVariable::setName(const QString &name)
0075 {
0076     m_name = name;
0077     valueChanged();
0078 }
0079 
0080 KoOdfNumberStyles::NumericStyleFormat UserVariable::numberstyle() const
0081 {
0082     return m_numberstyle;
0083 }
0084 
0085 void UserVariable::setNumberStyle(KoOdfNumberStyles::NumericStyleFormat numberstyle)
0086 {
0087     m_numberstyle = numberstyle;
0088     valueChanged();
0089 }
0090 
0091 QWidget* UserVariable::createOptionsWidget()
0092 {
0093     UserVariableOptionsWidget *configWidget = new UserVariableOptionsWidget(this);
0094     return configWidget;
0095 }
0096 
0097 void UserVariable::valueChanged()
0098 {
0099     //TODO apply following also to plugins/variables/DateVariable.cpp:96
0100     //TODO handle formula
0101     QString value = variableManager()->value(m_name);
0102     value = KoOdfNumberStyles::format(value, m_numberstyle);
0103     setValue(value);
0104 }
0105 
0106 void UserVariable::readProperties(const KoProperties *props)
0107 {
0108     m_property = props->intProperty("varproperty");
0109     //m_name = props->stringProperty("varname");
0110     //debugVariables << m_property << m_name;
0111     //valueChanged();
0112 }
0113 
0114 void UserVariable::propertyChanged(Property property, const QVariant &value)
0115 {
0116     Q_UNUSED(property);
0117     Q_UNUSED(value);
0118     //setValue(value.toString());
0119 }
0120 
0121 void UserVariable::saveOdf(KoShapeSavingContext &context)
0122 {
0123     if (m_property == 0 && !variableManager()->userVariables().contains(m_name))
0124         return;
0125 
0126     KoXmlWriter *writer = &context.xmlWriter();
0127 
0128     if (m_property == KoInlineObject::UserGet)
0129         writer->startElement("text:user-field-get", false);
0130     else
0131         writer->startElement("text:user-field-input", false);
0132 
0133     if (!m_name.isEmpty())
0134         writer->addAttribute("text:name", m_name);
0135 
0136     QString styleName = KoOdfNumberStyles::saveOdfNumberStyle(context.mainStyles(), m_numberstyle);
0137     if (!styleName.isEmpty())
0138         writer->addAttribute("style:data-style-name", styleName);
0139 
0140     writer->addTextNode(value());
0141     writer->endElement();
0142 }
0143 
0144 bool UserVariable::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0145 {
0146     if (element.localName() == "user-field-get") {
0147         m_property = KoInlineObject::UserGet;
0148     } else if (element.localName() == "user-field-input") {
0149         m_property = KoInlineObject::UserInput;
0150     } else {
0151         m_property = 0;
0152     }
0153 
0154     m_name = element.attributeNS(KoXmlNS::text, "name");
0155 
0156     QString dataStyle = element.attributeNS(KoXmlNS::style, "data-style-name");
0157     if (!dataStyle.isEmpty() && context.odfLoadingContext().stylesReader().dataFormats().contains(dataStyle)) {
0158         m_numberstyle = context.odfLoadingContext().stylesReader().dataFormats().value(dataStyle).first;
0159     } else {
0160         m_numberstyle = KoOdfNumberStyles::NumericStyleFormat();
0161     }
0162 
0163     return true;
0164 }
0165 
0166 void UserVariable::resize(const QTextDocument *document, QTextInlineObject &object, int posInDocument, const QTextCharFormat &format, QPaintDevice *pd)
0167 {
0168     KoVariable::resize(document, object, posInDocument, format, pd);
0169 
0170     if (!m_variableManager) {
0171         variableManager();
0172     }
0173 }