1 /* This file is part of the KDE project
2  * Copyright (C) 2006-2009 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
4  * Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
5  * Copyright (C) 2009 KO GmbH <cbo@kogmbh.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef KOSECTIONSTYLE_H
23 #define KOSECTIONSTYLE_H
24 
25 #include "KoColumns.h"
26 #include "KoText.h"
27 #include "kotext_export.h"
28 
29 #include <QObject>
30 #include <QTextFormat>
31 
32 class QTextFrame;
33 class QTextFrameFormat;
34 class KoGenStyle;
35 class KoOdfLoadingContext;
36 
37 class QString;
38 class QVariant;
39 
40 /**
41  * A container for all properties for the section wide style.
42  * Each section in the main text either is based on a section style, or its not. Where
43  * it is based on a section style this is indicated that it has a property 'StyleId'
44  * with an integer as value.  The integer value corresponds to the styleId() output of
45  * a specific KoSectionStyle.
46  * @see KoStyleManager
47  */
48 class KOTEXT_EXPORT KoSectionStyle : public QObject
49 {
50     Q_OBJECT
51 public:
52     enum Property {
53         StyleId = QTextFormat::UserProperty + 1,
54         TextProgressionDirection,
55         ColumnCount,
56         ColumnData,
57         ColumnGapWidth,
58         SeparatorStyle,
59         SeparatorColor,
60         SeparatorVerticalAlignment,
61         SeparatorWidth,
62         SeparatorHeight
63     };
64 
65     /// Constructor
66     explicit KoSectionStyle(QObject *parent = 0);
67     /// Creates a KoSectionStyle with the given frame format and \a parent
68     explicit KoSectionStyle(const QTextFrameFormat &frameFormat, QObject *parent = 0);
69     /// Destructor
70     ~KoSectionStyle() override;
71 
72     /// creates a clone of this style with the specified parent
73     KoSectionStyle *clone(QObject *parent = 0) const;
74 
75 
76     /// duplicated property from QTextBlockFormat
77     void setLeftMargin(qreal margin);
78     /// duplicated property from QTextBlockFormat
79     qreal leftMargin() const;
80     /// duplicated property from QTextBlockFormat
81     void setRightMargin(qreal margin);
82     /// duplicated property from QTextBlockFormat
83     qreal rightMargin() const;
84 
85     KoText::Direction textProgressionDirection() const;
86 
87     void setTextProgressionDirection(KoText::Direction dir);
88 
89     /// See similar named method on QTextBlockFormat
90     void setBackground(const QBrush &brush);
91     /// See similar named method on QTextBlockFormat
92     QBrush background() const;
93     /// See similar named method on QTextBlockFormat
94     void clearBackground();
95 
96 #if 0
97     as this is a duplicate of leftMargin, lets make it very clear we are using that one.
98     /// duplicated property from QTextBlockFormat
99     void setIndent(int indent);
100     /// duplicated property from QTextBlockFormat
101     int indent() const;
102 #endif
103 
104     void setColumnCount(int columnCount);
105     int columnCount() const;
106 
107     void setColumnGapWidth(qreal columnGapWidth);
108     qreal columnGapWidth() const;
109 
110     void setColumnData(const QVector<KoColumns::ColumnDatum> &columnData);
111     QVector<KoColumns::ColumnDatum> columnData() const;
112 
113     void setSeparatorStyle(KoColumns::SeparatorStyle separatorStyle);
114     KoColumns::SeparatorStyle separatorStyle() const;
115 
116     void setSeparatorColor(const QColor &separatorColor);
117     QColor separatorColor() const;
118 
119     void setSeparatorVerticalAlignment(KoColumns::SeparatorVerticalAlignment separatorVerticalAlignment);
120     KoColumns::SeparatorVerticalAlignment separatorVerticalAlignment() const;
121 
122     void setSeparatorWidth(qreal separatorWidth);
123     qreal separatorWidth() const;
124 
125     void setSeparatorHeight(int separatorHeight);
126     int separatorHeight() const;
127 
128     /// set the parent style this one inherits its unset properties from.
129     void setParentStyle(KoSectionStyle *parent);
130 
131     /// return the parent style
132     KoSectionStyle *parentStyle() const;
133 
134     /// return the name of the style.
135     QString name() const;
136 
137     /// set a user-visible name on the style.
138     void setName(const QString &name);
139 
140     /// each style has a unique ID (non persistent) given out by the styleManager
141     int styleId() const;
142 
143     /// each style has a unique ID (non persistent) given out by the styleManager
144     void setStyleId(int id);
145 
146     /// copy all the properties from the other style to this style, effectively duplicating it.
147     void copyProperties(const KoSectionStyle *style);
148 
149     void unapplyStyle(QTextFrame &section) const;
150 
151     /**
152      * Apply this style to a frameFormat by copying all properties from this, and parent
153      * styles to the target frame format.  Note that the character format will not be applied
154      * using this method, use the other applyStyle() method for that.
155      */
156     void applyStyle(QTextFrameFormat &format) const;
157 
158     /**
159      * Apply this style to the section (QTextFrame) by copying all properties from this and parent
160      * to the target frame formats.
161      */
162     void applyStyle(QTextFrame &section) const;
163 
164     void remove(int key);
165 
166     /// Compare the section of this style with the other
167     bool operator==(const KoSectionStyle &other) const;
168     /// Compare the section properties of this style with other
169     bool compareSectionProperties(const KoSectionStyle &other) const;
170 
171     void removeDuplicates(const KoSectionStyle &other);
172 
173     /**
174      * Load the style form the element
175      *
176      * @param context the odf loading context
177      * @param element the element containing the
178      */
179     void loadOdf(const KoXmlElement *element, KoOdfLoadingContext &context);
180 
181     void saveOdf(KoGenStyle &style);
182 
183     /**
184      * Returns true if this section style has the property set.
185      * Note that this method does not delegate to the parent style.
186      * @param key the key as found in the Property enum
187      */
188     bool hasProperty(int key) const;
189 
190     /**
191      * Set a property with key to a certain value, overriding the value from the parent style.
192      * If the value set is equal to the value of the parent style, the key will be removed instead.
193      * @param key the Property to set.
194      * @param value the new value to set on this style.
195      * @see hasProperty(), value()
196      */
197     void setProperty(int key, const QVariant &value);
198     /**
199      * Return the value of key as represented on this style, taking into account parent styles.
200      * You should consider using the direct accessors for individual properties instead.
201      * @param key the Property to request.
202      * @returns a QVariant which holds the property value.
203      */
204     QVariant value(int key) const;
205 
206 Q_SIGNALS:
207     void nameChanged(const QString &newName);
208 
209 private:
210     class Private;
211     Private * const d;
212 };
213 
214 Q_DECLARE_METATYPE(KoSectionStyle *)
215 
216 #endif
217