KoShapeSavingContext.h 8.83 KB
Newer Older
1 2
/* This file is part of the KDE project
   Copyright (C) 2004-2006 David Faure <faure@kde.org>
3
   Copyright (C) 2007-2008 Thorsten Zachmann <zachmann@kde.org>
4
   Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
Benjamin Port's avatar
Benjamin Port committed
5
   Copyright (C) 2010 Benjamin Port <port.benjamin@gmail.com>
6

7 8 9
   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
10
   version 2 of the License, or (at your option) any later version.
11

12 13 14 15
   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.
16

17 18 19 20 21 22
   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.
*/

23 24
#ifndef KOSHAPESAVINGCONTEXT_H
#define KOSHAPESAVINGCONTEXT_H
25

26
#include "flake_export.h"
27

28
#include <QImage>
29
#include <QTransform>
Benjamin Port's avatar
Benjamin Port committed
30
#include <QTextBlockUserData>
31
#include <KoElementReference.h>
32 33 34 35

class KoShape;
class KoXmlWriter;
class KoGenStyles;
36
class KoDataCenterBase;
37
class KoEmbeddedDocumentSaver;
38
class KoImageData;
Thorsten Zachmann's avatar
Thorsten Zachmann committed
39
class KoMarker;
40
class KoShapeLayer;
41
class KoStore;
42
class KoSharedSavingData;
43
class KoShapeSavingContextPrivate;
44

Thomas Zander's avatar
Thomas Zander committed
45 46 47
/**
 * The set of data for the ODF file format used during saving of a shape.
 */
48 49 50 51
class FLAKE_EXPORT KoShapeSavingContext
{
public:
    /// The Style used for saving the shape
52
    enum ShapeSavingOption {
53 54
        /**
         * If set the style of family presentation is used, when not set the
55 56 57
         * family graphic is used.
         * See OpenDocument 9.2.15 Common Drawing Shape Attributes / Style
         */
58 59
        PresentationShape = 1,
        /**
60 61
         * Save the draw:id used for referencing the shape. If draw:id is saved, xml:id is also
         * saved.
62 63 64 65 66 67
         * See OpenDocument 9.2.15 Common Drawing Shape Attributes / ID
         */
        DrawId = 2,
        /**
         * If set the automatic style will be marked as being needed in styles.xml
         */
68
        AutoStyleInStyleXml = 4,
69 70 71
        /**
         * If set duplicate master pages will be merged to one
         */
72
        UniqueMasterPages = 8
73
    };
74
    Q_DECLARE_FLAGS(ShapeSavingOptions, ShapeSavingOption)
75 76 77 78

    /**
     * @brief Constructor
     * @param xmlWriter used for writing the xml
79
     * @param mainStyles for saving the styles
80
     * @param embeddedSaver for saving embedded documents
81
     */
Thomas Zander's avatar
Thomas Zander committed
82
    KoShapeSavingContext(KoXmlWriter &xmlWriter, KoGenStyles &mainStyles,
Thomas Zander's avatar
Thomas Zander committed
83
                         KoEmbeddedDocumentSaver &embeddedSaver);
84
    virtual ~KoShapeSavingContext();
85 86 87 88 89 90

    /**
     * @brief Get the xml writer
     *
     * @return xmlWriter
     */
Thomas Zander's avatar
Thomas Zander committed
91
    KoXmlWriter &xmlWriter();
92

93 94 95
    /**
     * @brief Set the xml writer
     *
96
     * Change the xmlWriter that is used in the Context e.g. for saving to styles.xml
97 98 99 100
     * instead of content.xml
     *
     * @param xmlWriter to use
     */
Thomas Zander's avatar
Thomas Zander committed
101
    void setXmlWriter(KoXmlWriter &xmlWriter);
102

103 104 105
    /**
     * @brief Get the main styles
     *
106
     * @return main styles
107
     */
Thomas Zander's avatar
Thomas Zander committed
108
    KoGenStyles &mainStyles();
109

110 111 112 113 114
    /**
     * @brief Get the embedded document saver
     *
     * @return embedded document saver
     */
Thomas Zander's avatar
Thomas Zander committed
115
    KoEmbeddedDocumentSaver &embeddedSaver();
116

117 118 119 120 121
    /**
     * @brief Check if an option is set
     *
     * @return ture if the option is set, false otherwise
     */
122
    bool isSet(ShapeSavingOption option) const;
123 124 125 126 127 128

    /**
     * @brief Set the options to use
     *
     * @param options to use
     */
129
    void setOptions(ShapeSavingOptions options);
130

Thomas Zander's avatar
Thomas Zander committed
131
    /// add an option to the set of options stored on this context, will leave the other options intact.
132
    void addOption(ShapeSavingOption option);
133

Thomas Zander's avatar
Thomas Zander committed
134
    /// remove an option, will leave the other options intact.
135
    void removeOption(ShapeSavingOption option);
136

137 138 139 140 141
    /**
     * @brief Get the options used
     *
     * @return options used
     */
142
    ShapeSavingOptions options() const;
143

144

145
    /**
146 147 148 149 150 151 152 153 154 155 156
     * @brief xmlid returns an element reference that can be related to the given referent. If there is a
     *   prefix given, this prefix will be used in addition to either the counter or the uuid.
     * @param referent the object we are referring to
     * @param prefix a prefix for the xml:id string
     * @param counter if counter is true, shapesavingcontext will use a counter to create the xml:id
     * @return a KoElementReference; if insert is false and referent doesn't exist yet in the list, the elementrefence will be invalid.
     */
    KoElementReference xmlid(const void *referent, const QString& prefix = QString::null, KoElementReference::GenerationOption counter = KoElementReference::UUID);

    /**
     * @brief existingXmlid retrieve an existing xml id or invalid xml id if the referent object doesn't exist
157
     */
158
    KoElementReference existingXmlid(const void *referent);
159

160 161
    /**
     * @brief Clear out all given draw ids
162
     * @param prefix: removes all xml:id's that have the given prefix.
163 164 165 166 167 168
     *
     * This is needed for checking if master pages are the same. In normal saving
     * this should not be called.
     *
     * @see KoPAPastePage::process
     */
169
    void clearXmlIds(const QString &prefix);
Benjamin Port's avatar
Benjamin Port committed
170

171 172 173 174
    /**
     * Adds a layer to save into a layer-set in styles.xml according to 9.1.2/9.1.3 odf spec
     * @param layer the layer to save
     */
Thomas Zander's avatar
Thomas Zander committed
175
    void addLayerForSaving(const KoShapeLayer *layer);
176 177 178 179

    /**
     * Saves the layers added with addLayerForSaving to the xml writer
     */
Thomas Zander's avatar
Thomas Zander committed
180
    void saveLayerSet(KoXmlWriter &xmlWriter) const;
181 182 183 184 185 186 187

    /**
     * remove all layers
     *
     * This can be used for saving different layer sets per page.
     */
    void clearLayers();
188

189 190 191
    /**
     * Get the image href under which the image will be saved in the store
     */
Thomas Zander's avatar
Thomas Zander committed
192
    QString imageHref(KoImageData *image);
193

194 195 196 197 198 199
    /**
     * Get the image href under which the image will be save in the store
     *
     * This should only be used for temporary images that are onle there during
     * saving, e.g. a pixmap representation of a draw:frame
     */
Thomas Zander's avatar
Thomas Zander committed
200
    QString imageHref(QImage &image);
201

202 203 204
    /**
     * Get the images that needs to be saved to the store
     */
205
    QMap<qint64, QString> imagesToSave();
206

Thorsten Zachmann's avatar
Thorsten Zachmann committed
207 208 209 210 211
    /**
     * Get the reference to use for the marker lookup
     */
    QString markerRef(const KoMarker *marker);

212 213 214
    /**
     * Add data center
     */
215
    void addDataCenter(KoDataCenterBase *dataCenter);
216 217 218 219

    /**
     * Save the data centers
     *
220
     * This calls KoDataCenterBase::completeSaving()
221
     * @returns false if an error occurred, which typically cancels the save.
222
     */
Thomas Zander's avatar
Thomas Zander committed
223
    bool saveDataCenter(KoStore *store, KoXmlWriter *manifestWriter);
224

225 226 227 228 229 230 231 232 233 234
    /**
     * Add shared data
     *
     * This can be use to pass data between shapes on saving. E.g. The presentation page layout
     * styles. With that e.g. the styles only need to be saved once and can be used everywhere
     * without creating them again.
     *
     * The ownership of the added data is passed to the context. The KoShapeSavingContext will
     * delete the added data when it is destroyed.
     *
235
     * Data inserted for a specific id will not be overwritten by calling addSharedData with
236 237 238 239 240 241
     * the same id again.
     *
     * You get an assertion when the id is already existing.
     *
     * @see KoSharedSavingData
     */
Thomas Zander's avatar
Thomas Zander committed
242
    void addSharedData(const QString &id, KoSharedSavingData *data);
243 244 245 246 247 248 249 250 251

    /**
     * Get the shared data.
     *
     * @see KoSharedLoadingData
     *
     * @param id The id used to identify the shared data.
     * @return The shared data for the id or 0 if there is no shared data for the id.
     */
Thomas Zander's avatar
Thomas Zander committed
252
    KoSharedSavingData *sharedData(const QString &id) const;
253

254
    /**
255 256 257 258 259 260
     * Add an offset that will be applied to the shape position when saved
     *
     * This is needed e.g. for shapes anchored to a text shape as the position is
     * saved as offset to the anchor.
     *
     * @param shape The shape for which the offset should be added.
Thomas Zander's avatar
Thomas Zander committed
261
     * @param matrix The offset which should be applied on saving the position.
262
     */
263
    void addShapeOffset(const KoShape *shape, const QTransform &matrix);
264 265 266 267 268 269

    /**
     * Remove an offset from the saved offset list
     *
     * @param shape The shape for which the offset should be removed.
     */
Thomas Zander's avatar
Thomas Zander committed
270
    void removeShapeOffset(const KoShape *shape);
271 272 273 274 275

    /**
     * Get the offest that will be applied to the shape position when saved.
     *
     * @param shape The shape for which the offset should be get.
276
     * @return the saved offset or QTransform() when offset is not set.
277
     */
278
    QTransform shapeOffset(const KoShape *shape) const;
279

Thomas Zander's avatar
Thomas Zander committed
280
private:
281
    KoShapeSavingContextPrivate *d;
282 283
};

284
Q_DECLARE_OPERATORS_FOR_FLAGS(KoShapeSavingContext::ShapeSavingOptions)
285

286
#endif // KOSHAPESAVINGCONTEXT_H