Warning, file /office/calligra/libs/odf/KoGenStyles.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004-2006 David Faure <faure@kde.org> 0003 Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org> 0004 Copyright (C) 2009 Thomas Zander <zander@kde.org> 0005 Copyright (C) 2010 Jarosław Staniek <staniek@kde.org> 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Library General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Library General Public License for more details. 0016 0017 You should have received a copy of the GNU Library General Public License 0018 along with this library; see the file COPYING.LIB. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 * Boston, MA 02110-1301, USA. 0021 */ 0022 0023 #ifndef KOGENSTYLES_H 0024 #define KOGENSTYLES_H 0025 0026 #include <QVector> 0027 #include <QMultiMap> 0028 #include <QString> 0029 0030 #include "KoGenStyle.h" 0031 0032 class KoStore; 0033 class KoFontFace; 0034 0035 /** 0036 * @brief Repository of styles used during saving ODF documents. 0037 * 0038 * Each instance of KoGenStyles is a collection of styles whose names 0039 * are in the same "namespace". 0040 * This means there should be one instance for all styles in <office:styles>, 0041 * and <office:automatic-styles>, another instance for number formats, another 0042 * one for draw styles, and another one for list styles. 0043 * 0044 * "Style" in this context only means "a collection of properties". 0045 * The "Gen" means both "Generic" and "Generated" :) 0046 * 0047 * The basic design principle is the flyweight pattern: if you need 0048 * a style with the same properties from two different places, you 0049 * get the same object. Here it means rather you get the same name for the style, 0050 * and it will get saved only once to the file. 0051 * 0052 * KoGenStyles features sharing, creation on demand, and name generation. 0053 * Since this is used for saving only, it doesn't feature refcounting, nor 0054 * removal of individual styles. 0055 * 0056 * @note The use of KoGenStyles isn't mandatory, of course. If the application 0057 * is already designed with user and automatic styles in mind for a given 0058 * set of properties, it can go ahead and save all styles directly (after 0059 * ensuring they have unique names). 0060 * 0061 * @author David Faure <faure@kde.org> 0062 */ 0063 class KOODF_EXPORT KoGenStyles 0064 { 0065 public: 0066 /// Single style with assigned name 0067 struct NamedStyle { 0068 const KoGenStyle* style; ///< @note owned by the collection 0069 QString name; 0070 }; 0071 0072 typedef QMultiMap<KoGenStyle, QString> StyleMap; 0073 0074 KoGenStyles(); 0075 ~KoGenStyles(); 0076 0077 /** 0078 * Those are flags for the insert() call. 0079 * 0080 * By default (NoFlag), the generated style names will look like "name1", "name2". 0081 * If DontAddNumberToName is set, the first name that will be tried is "name", and only if 0082 * that one exists, then "name1" is tried. Set DontAddNumberToName if the name given as 0083 * argument is supposed to be the full style name. 0084 * If AllowDuplicates is set, a unique style name is generated even if a similar KoGenStyle 0085 * already exists. In other words, the collection will now contain two equal KoGenStyle 0086 * and generate them with different style names. 0087 */ 0088 enum InsertionFlag { 0089 NoFlag = 0, 0090 DontAddNumberToName = 1, 0091 AllowDuplicates = 2 0092 }; 0093 Q_DECLARE_FLAGS(InsertionFlags, InsertionFlag) 0094 0095 /** 0096 * Look up a style in the collection, inserting it if necessary. 0097 * This assigns a name to the style and returns it. 0098 * 0099 * @param style the style to look up. 0100 * @param baseName proposed (base) name for the style. Note that with the ODF, 0101 * the style name is never shown to the user (there's a separate display-name 0102 * attribute for that). So there are little reasons to use named styles anyway. 0103 * But this attribute can be used for clarity of the files. 0104 * If this name is already in use (for another style), then a number is appended 0105 * to it until unused name is found. 0106 * @param flags see Flags 0107 * 0108 * @return the name that has been assigned for the inserted style 0109 */ 0110 QString insert(const KoGenStyle &style, const QString &baseName = QString(), InsertionFlags flags = NoFlag); 0111 0112 /** 0113 * Return the entire collection of styles 0114 * Use this for saving the styles 0115 */ 0116 StyleMap styles() const; 0117 0118 /** 0119 * Return all styles of a given type (NOT marked for styles.xml). 0120 * 0121 * Use this only if you have special needs. 0122 * 0123 * @param type the style type, see the KoGenStyle constructor 0124 * @see insert() 0125 */ 0126 QVector<KoGenStyles::NamedStyle> styles(KoGenStyle::Type type) const; 0127 0128 /** 0129 * @return an existing style by name. If no such style exists, 0 is returned. 0130 */ 0131 const KoGenStyle *style(const QString &name, const QByteArray &family) const; 0132 0133 /** 0134 * @return an existing style by name, which can be modified. 0135 * If no such style exists, 0 is returned. 0136 * @warning This is DANGEROUS. 0137 * It basically defeats the purpose of insert()! 0138 * Only do this if you know for sure no other 'user' of that style will 0139 * be affected. 0140 */ 0141 KoGenStyle* styleForModification(const QString &name, const QByteArray &family); 0142 0143 /** 0144 * Mark a given automatic style as being needed in styles.xml. 0145 * For instance styles used by headers and footers need to go there, since 0146 * they are saved in styles.xml, and styles.xml must be independent from content.xml. 0147 * 0148 * Equivalent to using KoGenStyle::setAutoStyleInStylesDotXml() but this can be done after insert(). 0149 * 0150 * This operation can't be undone; once styles are promoted they can't go back 0151 * to being content.xml-only. 0152 * 0153 * @see styles, KoGenStyle::setAutoStyleInStylesDotXml 0154 */ 0155 void markStyleForStylesXml(const QString &name, const QByteArray &family); 0156 0157 /** 0158 * Insert a font face declaration. 0159 * @a face should have non-empty "name" parameter, i.e. should not be null. 0160 * 0161 * Declaration with given name replaces previously inserted declaration with the same name. 0162 * 0163 * See odf 2.6 Font Face Declarations 0164 * and odf 14.6 Font Face Declaration. 0165 */ 0166 void insertFontFace(const KoFontFace &face); 0167 0168 /** 0169 * @return font face declaration for name @a name 0170 * or null font face (see KoFontFace::isNull()) if there is no such font face. 0171 * 0172 * See odf 2.6 Font Face Declarations 0173 * and odf 14.6 Font Face Declaration. 0174 */ 0175 KoFontFace fontFace(const QString &name) const; 0176 0177 /** 0178 * Save the styles into the styles.xml file 0179 * 0180 * This saves all styles and font face declarations to the styles.xml file which 0181 * belong there. This creates the file and creates an entry in the manifest. 0182 * 0183 * @param store 0184 * @param mainfestwriter 0185 * @return true on success 0186 */ 0187 bool saveOdfStylesDotXml(KoStore *store, KoXmlWriter *manifestWriter) const; 0188 0189 /** 0190 * Placement of styles saved in saveOdfStyles() or inserted in insertRawOdfStyles(). 0191 */ 0192 enum StylesPlacement { 0193 /** 0194 * Creates document's office:styles tag and saves all document styles there 0195 * or inserts raw styles into document's office:styles. 0196 */ 0197 DocumentStyles, 0198 /** 0199 * Creates styles.xml's office:master-styles tag and saves all master styles there 0200 * or inserts raw styles into styles.xml's office:automatic-styles. 0201 */ 0202 MasterStyles, 0203 /** 0204 * Creates document's office:automatic-styles tag and saves all automatic styles there 0205 * or inserts raw styles into document's office:automatic-styles. 0206 */ 0207 DocumentAutomaticStyles, 0208 /** 0209 * Creates styles.xml's office:automatic-styles tag and saves all automatic styles there 0210 * or inserts raw styles into style.xml's office:automatic-styles. 0211 */ 0212 StylesXmlAutomaticStyles, 0213 /** 0214 * Creates document's office:font-face-decls tag and saves all document styles there 0215 * or inserts raw styles into document's office:font-face-decls. 0216 */ 0217 FontFaceDecls 0218 }; 0219 0220 /** 0221 * Save styles of given type. 0222 * 0223 * @param placement see StylesPlacement 0224 * @param xmlWriter target writer 0225 */ 0226 void saveOdfStyles(StylesPlacement placement, KoXmlWriter *xmlWriter) const; 0227 0228 /** 0229 * Insert extra styles of given type. 0230 * 0231 * This inserts extra styles as raw xml into a given placement. 0232 * The information is collected and written back when saveOdfStyles() is called. 0233 * This method is useful for testing purposes. 0234 * 0235 * @param placement see StylesPlacement 0236 * @param xml the raw xml string 0237 */ 0238 void insertRawOdfStyles(StylesPlacement placement, const QByteArray &xml); 0239 0240 /** 0241 * register a relation for a previously inserted style to a previously inserted target style. 0242 * This allows you to insert a style relation based on generated names. 0243 */ 0244 void insertStyleRelation(const QString &source, const QString &target, const char *tagName); 0245 0246 private: 0247 friend KOODF_EXPORT QDebug operator<<(QDebug dbg, const KoGenStyles& styles); 0248 0249 class Private; 0250 Private * const d; 0251 }; 0252 0253 Q_DECLARE_OPERATORS_FOR_FLAGS(KoGenStyles::InsertionFlags) 0254 Q_DECLARE_TYPEINFO(KoGenStyles::NamedStyle, Q_MOVABLE_TYPE); 0255 0256 //! Debug stream operator. 0257 KOODF_EXPORT QDebug operator<<(QDebug dbg, const KoGenStyles& styles); 0258 0259 #endif /* KOGENSTYLES_H */