Warning, file /office/calligra/libs/odf/KoElementReference.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2011-2012 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 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  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser 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 #ifndef KOELEMENTREFERENCE_H
0021 #define KOELEMENTREFERENCE_H
0022 
0023 #include <QSharedDataPointer>
0024 #include <QSharedData>
0025 #include <QUuid>
0026 
0027 #include "KoXmlReaderForward.h"
0028 
0029 #include "koodf_export.h"
0030 
0031 class KoXmlWriter;
0032 
0033 class KoElementReferenceData : public QSharedData
0034 {
0035 public:
0036 
0037     KoElementReferenceData()
0038     {
0039         xmlid = QUuid::createUuid().toString();
0040         xmlid.remove('{');
0041         xmlid.remove('}');
0042     }
0043 
0044     KoElementReferenceData(const KoElementReferenceData &other)
0045         : QSharedData(other)
0046         , xmlid(other.xmlid)
0047     {
0048     }
0049 
0050     ~KoElementReferenceData() {}
0051 
0052     QString xmlid;
0053 };
0054 
0055 /**
0056  * KoElementReference is used to store unique identifiers for elements in an odf document.
0057  * Element references are saved as xml:id and optionally for compatibility also as draw:id
0058  * and text:id.
0059  *
0060  * You can use element references wherever you would have used a QString to refer to the id
0061  * of an object.
0062  *
0063  * Element references are implicitly shared, so you can and should pass them along by value.
0064  */
0065 class KOODF_EXPORT KoElementReference
0066 {
0067 public:
0068 
0069     enum GenerationOption {
0070         UUID = 0,
0071         Counter = 1
0072     };
0073 
0074     enum SaveOption {
0075         XmlId = 0x0,
0076         DrawId = 0x1,
0077         TextId = 0x2
0078     };
0079     Q_DECLARE_FLAGS(SaveOptions, SaveOption)
0080 
0081     KoElementReference();
0082     explicit KoElementReference(const QString &prefix);
0083     KoElementReference(const QString &prefix, int counter);
0084     KoElementReference(const KoElementReference &other);
0085     KoElementReference &operator=(const KoElementReference &rhs);
0086     bool operator==(const KoElementReference &other) const;
0087     bool operator!=(const KoElementReference &other) const;
0088 
0089     /**
0090      * @return true if the xmlid is valid, i.e., not null
0091      */
0092     bool isValid() const;
0093 
0094     /**
0095      * @brief loadOdf creates a new KoElementReference from the given element. If the element
0096      *   does not have an xml:id, draw:id or text:id attribute, and invalid element reference
0097      *   is returned.
0098      * @param element the element that may contain xml:id, text:id or draw:id. xml:id has
0099      *    priority.
0100      * @return a new element reference
0101      */
0102     KoElementReference loadOdf(const KoXmlElement &element);
0103 
0104     /**
0105      * @brief saveOdf saves this element reference into the currently open element in the xml writer.
0106      * @param writer the writer we save to
0107      * @param saveOption determines which attributes we save. We always save the xml:id.
0108      */
0109     void saveOdf(KoXmlWriter *writer, SaveOption saveOption = XmlId) const;
0110 
0111     /**
0112      * @brief toString creates a QString from the element reference
0113      * @return a string that represents the element. Can be used in maps etc.
0114      */
0115     QString toString() const;
0116 
0117     /**
0118      * Invalidate the reference
0119      */
0120     void invalidate();
0121 
0122 
0123 private:
0124 
0125     QSharedDataPointer<KoElementReferenceData> d;
0126 };
0127 
0128 
0129 Q_DECLARE_OPERATORS_FOR_FLAGS(KoElementReference::SaveOptions)
0130 
0131 #endif // KOELEMENTREFERENCE_H