1 /* This file is part of the KDE project
2  * Copyright (C) 2008 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2011 C. Boemann <cbo@boemann.dk>
4  * Copyright (C) 2011-2012 Pierre Stirnweiss <pstirnweiss@googlemail.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifndef MODEL_H
22 #define MODEL_H
23 
24 #include "AbstractStylesModel.h"
25 
26 #include <QAbstractListModel>
27 
28 #include <QSize>
29 
30 class KoStyleThumbnailer;
31 
32 class KoStyleManager;
33 class KoParagraphStyle;
34 class KoCharacterStyle;
35 
36 class QImage;
37 
38 /** This class is used to provide widgets (like the @class StylesCombo) the styles available to the document being worked on. The @class StylesModel can be of two types: character styles or paragraph styles type. This allows the widget to ignore the type of style it is handling.
39   * Character styles in ODF can be specified in two ways. First, a named character style, specifying character formatting properties. It is meant to be used on a couple of individual characters. Secondly, a paragraph style also specifies character formatting properties, which are to be considered the default for that particular paragraph.
40   * For this reason, the @class Stylesmodel, when of the type @value characterStyle, do not list the paragraph style names. Only the specific named character styles are listed. Additionally, as the first item, a virtual style "As paragraph" is provided. Selecting this "style" will set the character properties as specified by the paragraph style currently applied to the selection.
41   * This class requires that a @class KoStyleManager and a @class KoStyleThumbnailer be set. See below methods.
42   *
43   * The StylesModel re-implement the AbstractStylesModel interface. Several components assume the following properties:
44   * - the StylesModel is a flat list of items (this also means that "parent" QModelIndexes are always invalid)
45   * - the StylesModel has only one column
46   * - there is no header in the model
47   * - only the following methods are used when updating the underlying model's data: resetModel, insertRows, moveRows, removeRows
48 */
49 
50 class StylesModel : public AbstractStylesModel
51 {
52     Q_OBJECT
53 
54 public:
55     enum CategoriesInternalIds {
56         NoneStyleId = -1
57     };
58 
59     explicit StylesModel(KoStyleManager *styleManager, AbstractStylesModel::Type modelType, QObject *parent = 0);
60     ~StylesModel() override;
61 
62     /** Re-implemented from QAbstractItemModel. */
63 
64     QModelIndex index(int row, int column=0, const QModelIndex &parent = QModelIndex()) const override;
65 
66     int rowCount(const QModelIndex &parent) const override;
67 
68     QVariant data(const QModelIndex &index, int role) const override;
69 
70     Qt::ItemFlags flags(const QModelIndex &index) const override;
71 
72     QModelIndex parent(const QModelIndex &child) const override;
73 
74     int columnCount(const QModelIndex &parent) const override;
75 
76     /** *********************************** */
77     /** Specific methods of the StylesModel */
78 
79     /** ************************* */
80     /** Initialising of the model */
81 
82     /** Specify if the combo should provide the virtual style None. This style is a virtual style which equates to no style. It is only relevant for character styles.
83         In case the "None" character style is selected, the character formatting properties of the paragraph style are used.
84         A @class StylesModel of the @enum Type ParagraphStyle always has this property set to false.
85         On the other hand, the default for a @class StylesModel of the @enum Type CharacterStyle is true.
86 
87         It is important to set this before setting the stylemanager on the model. The flag is used when populating the styles from the KoStyleManager.
88     */
89     void setProvideStyleNone(bool provide);
90 
91     /** Sets the @class KoStyleManager of the model. Setting this will populate the styles. It is required that a @param manager is set before using the model.
92       * CAUTION: Populating the style will select the first inserted item. If this model is already set on a view, this might cause the view to emit an item selection changed signal.
93     */
94     void setStyleManager(KoStyleManager *manager);
95 
96     /** Sets the @class KoStyleThumbnailer of the model. It is required that a @param thumbnailer is set before using the model. */
97     void setStyleThumbnailer(KoStyleThumbnailer *thumbnailer) override;
98 
99 
100     /** *************** */
101     /** Using the model */
102 
103     /** Return a @class QModelIndex for the specified @param style.
104       * @param style may be either a character or paragraph style.
105     */
106     QModelIndex indexOf(const KoCharacterStyle *style) const override;
107 
108     /** Returns a QImage which is a preview of the style specified by @param row of the given @param size.
109       * If size isn't specified, the default size of the given @class KoStyleThumbnailer is used.
110     */
111     QImage stylePreview(int row, const QSize &size = QSize()) override;
112 //    QImage stylePreview(QModelIndex &index, const QSize &size = QSize());
113 
114     /** Specifies which paragraph style is currently the active one (on the current paragraph). This is used in order to properly preview the "As paragraph" virtual character style. */
115     void setCurrentParagraphStyle(int styleId);
116 
117     /** Return the first index at list. */
118     QModelIndex firstStyleIndex();
119 
120     /** Return style id list. */
121     QList<int> StyleList();
122 
123     /** Return new styles and their ids. */
124     QHash<int, KoParagraphStyle *> draftParStyleList();
125     QHash<int, KoCharacterStyle *> draftCharStyleList();
126 
127     /** Add a paragraph style to paragraph style list but this style is not applied. */
128     void addDraftParagraphStyle(KoParagraphStyle *style);
129 
130     /** Add a character style to character style list but this style is not applied. */
131     void addDraftCharacterStyle(KoCharacterStyle *style);
132 
133     /** we call this when we apply our unapplied styles and we clear our list. */
134     void clearDraftStyles();
135 
136     /** We call this when we want a clear style model. */
137     void clearStyleModel();
138 
139     /** Returns the type of styles in the model */
140     AbstractStylesModel::Type stylesType() const override;
141 
142 private Q_SLOTS:
143     void removeParagraphStyle(KoParagraphStyle*);
144     void removeCharacterStyle(KoCharacterStyle*);
145     void updateName(int styleId);
146 
147 public Q_SLOTS:
148     void addParagraphStyle(KoParagraphStyle*);
149     void addCharacterStyle(KoCharacterStyle*);
150 
151 
152 private:
153     void updateParagraphStyles();
154     void updateCharacterStyles();
155 
156 protected:
157     QList<int> m_styleList; // list of style IDs
158     QHash<int, KoParagraphStyle *> m_draftParStyleList; // list of new styles that are not applied
159     QHash<int, KoCharacterStyle *> m_draftCharStyleList;
160 
161 private:
162     KoStyleManager *m_styleManager;
163 
164     KoParagraphStyle *m_currentParagraphStyle;
165     KoCharacterStyle *m_defaultCharacterStyle;
166 
167     bool m_provideStyleNone;
168 };
169 
170 #endif
171