1 /* This file is part of the KDE project
2  * Copyright (C) 2009 KO GmbH <cbo@kogmbh.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "KoTableColumnAndRowStyleManager.h"
21 
22 #include "styles/KoTableColumnStyle.h"
23 #include "styles/KoTableRowStyle.h"
24 #include "styles/KoTableCellStyle.h"
25 #include "styles/KoTableStyle.h"
26 
27 #include <QVector>
28 #include <QVariant>
29 #include <QTextTable>
30 
31 #include "TextDebug.h"
32 
33 class Q_DECL_HIDDEN KoTableColumnAndRowStyleManager::Private : public QSharedData
34 {
35 public:
Private()36     Private()  { }
~Private()37     ~Private() {
38     }
39     QVector<KoTableColumnStyle> tableColumnStyles;
40     QVector<KoTableRowStyle> tableRowStyles;
41 
42     QVector<KoTableCellStyle*> defaultRowCellStyles;
43     QVector<KoTableCellStyle*> defaultColumnCellStyles;
44 };
45 
KoTableColumnAndRowStyleManager()46 KoTableColumnAndRowStyleManager::KoTableColumnAndRowStyleManager()
47     : d(new Private())
48 {
49 }
50 
KoTableColumnAndRowStyleManager(const KoTableColumnAndRowStyleManager & rhs)51 KoTableColumnAndRowStyleManager::KoTableColumnAndRowStyleManager(const KoTableColumnAndRowStyleManager &rhs)
52     : d(rhs.d)
53 {
54 }
55 
operator =(const KoTableColumnAndRowStyleManager & rhs)56 KoTableColumnAndRowStyleManager &KoTableColumnAndRowStyleManager::operator=(const KoTableColumnAndRowStyleManager &rhs)
57 {
58     d = rhs.d;
59 
60     return *this;
61 }
62 
~KoTableColumnAndRowStyleManager()63 KoTableColumnAndRowStyleManager::~KoTableColumnAndRowStyleManager()
64 {
65 }
66 
getManager(QTextTable * table)67 KoTableColumnAndRowStyleManager KoTableColumnAndRowStyleManager::getManager(QTextTable *table)
68 {
69     QTextTableFormat tableFormat = table->format();
70 
71     if (tableFormat.hasProperty(KoTableStyle::ColumnAndRowStyleManager)) {
72         return  tableFormat.property(KoTableStyle::ColumnAndRowStyleManager).value<KoTableColumnAndRowStyleManager>();
73     } else {
74         KoTableColumnAndRowStyleManager carsManager;
75 
76         QVariant var;
77         var.setValue(carsManager);
78         tableFormat.setProperty(KoTableStyle::ColumnAndRowStyleManager, var);
79         table->setFormat(tableFormat);
80         return carsManager;
81     }
82 }
83 
setColumnStyle(int column,const KoTableColumnStyle & columnStyle)84 void KoTableColumnAndRowStyleManager::setColumnStyle(int column, const KoTableColumnStyle &columnStyle)
85 {
86     Q_ASSERT(column >= 0);
87 
88     if (column < 0) {
89         return;
90     }
91 
92     if (column < d->tableColumnStyles.size() && d->tableColumnStyles.value(column) == columnStyle) {
93         return;
94     }
95 
96     // TODO: just resize() if needed should work as well
97     d->tableColumnStyles.reserve(column+1);
98     while (column >= d->tableColumnStyles.size())
99         d->tableColumnStyles.append(KoTableColumnStyle());
100 
101     d->tableColumnStyles.replace(column, columnStyle);
102 }
103 
insertColumns(int column,int numberColumns,const KoTableColumnStyle & columnStyle)104 void KoTableColumnAndRowStyleManager::insertColumns(int column, int numberColumns, const KoTableColumnStyle &columnStyle)
105 {
106     Q_ASSERT(column >= 0);
107     Q_ASSERT(numberColumns >= 0);
108 
109     if (column < 0 || numberColumns < 0) {
110         return;
111     }
112 
113     // TODO: just resize() if needed should work as well
114     d->tableColumnStyles.reserve(column + numberColumns);
115     while (column > d->tableColumnStyles.size())
116         d->tableColumnStyles.append(KoTableColumnStyle());
117 
118     d->tableColumnStyles.insert(column, numberColumns, columnStyle);
119 }
120 
removeColumns(int column,int numberColumns)121 void KoTableColumnAndRowStyleManager::removeColumns(int column, int numberColumns)
122 {
123     Q_ASSERT(column >= 0);
124     Q_ASSERT(numberColumns >= 0);
125 
126     if (column >= d->tableColumnStyles.size() || column < 0 || numberColumns < 0) {
127         return;
128     }
129 
130     d->tableColumnStyles.remove(column, numberColumns);
131 }
132 
columnStyle(int column) const133 KoTableColumnStyle KoTableColumnAndRowStyleManager::columnStyle(int column) const
134 {
135     Q_ASSERT(column >= 0);
136 
137     if (column < 0) {
138         return KoTableColumnStyle();
139     }
140 
141     return d->tableColumnStyles.value(column);
142 }
143 
setRowStyle(int row,const KoTableRowStyle & rowStyle)144 void KoTableColumnAndRowStyleManager::setRowStyle(int row, const KoTableRowStyle &rowStyle)
145 {
146     Q_ASSERT(row >= 0);
147 
148     if (row < 0) {
149         return;
150     }
151 
152     if (row < d->tableRowStyles.size() && d->tableRowStyles.value(row) == rowStyle) {
153         return;
154     }
155 
156     // TODO: just resize() if needed should work as well
157     d->tableRowStyles.reserve(row+1);
158     while (row >= d->tableRowStyles.size())
159         d->tableRowStyles.append(KoTableRowStyle());
160 
161     d->tableRowStyles.replace(row, rowStyle);
162 }
163 
insertRows(int row,int numberRows,const KoTableRowStyle & rowStyle)164 void KoTableColumnAndRowStyleManager::insertRows(int row, int numberRows, const KoTableRowStyle &rowStyle)
165 {
166     Q_ASSERT(row >= 0);
167     Q_ASSERT(numberRows >= 0);
168 
169     if (row < 0 || numberRows < 0) {
170         return;
171     }
172 
173     // TODO: just resize() if needed should work as well
174     d->tableRowStyles.reserve(row + numberRows);
175     while (row > d->tableRowStyles.size())
176         d->tableRowStyles.append(KoTableRowStyle());
177 
178     d->tableRowStyles.insert(row, numberRows, rowStyle);
179 }
180 
removeRows(int row,int numberRows)181 void KoTableColumnAndRowStyleManager::removeRows(int row, int numberRows)
182 {
183     Q_ASSERT(row >= 0);
184     Q_ASSERT(numberRows >= 0);
185 
186     if (row >= d->tableRowStyles.size() || row < 0 || numberRows < 0) {
187         return;
188     }
189 
190     d->tableRowStyles.remove(row, numberRows);
191 }
192 
rowStyle(int row) const193 KoTableRowStyle KoTableColumnAndRowStyleManager::rowStyle(int row) const
194 {
195     Q_ASSERT(row >= 0);
196 
197     if (row < 0) {
198         return KoTableRowStyle();
199     }
200 
201     return d->tableRowStyles.value(row);
202 }
203 
defaultColumnCellStyle(int column) const204 KoTableCellStyle* KoTableColumnAndRowStyleManager::defaultColumnCellStyle(int column) const
205 {
206     Q_ASSERT(column >= 0);
207 
208     return d->defaultColumnCellStyles.value(column);
209 }
210 
setDefaultColumnCellStyle(int column,KoTableCellStyle * cellStyle)211 void KoTableColumnAndRowStyleManager::setDefaultColumnCellStyle(int column, KoTableCellStyle* cellStyle)
212 {
213     Q_ASSERT(column >= 0);
214 
215     if (column < d->defaultColumnCellStyles.size() && d->defaultColumnCellStyles.value(column) == cellStyle) {
216         return;
217     }
218 
219     while (column > d->defaultColumnCellStyles.size())
220         d->defaultColumnCellStyles.append(0);
221 
222     d->defaultColumnCellStyles.append(cellStyle);
223 }
224 
defaultRowCellStyle(int row) const225 KoTableCellStyle* KoTableColumnAndRowStyleManager::defaultRowCellStyle(int row) const
226 {
227     Q_ASSERT(row >= 0);
228 
229     return d->defaultRowCellStyles.value(row);
230 }
231 
setDefaultRowCellStyle(int row,KoTableCellStyle * cellStyle)232 void KoTableColumnAndRowStyleManager::setDefaultRowCellStyle(int row, KoTableCellStyle* cellStyle)
233 {
234     Q_ASSERT(row >= 0);
235 
236     if (row < d->defaultRowCellStyles.size() && d->defaultRowCellStyles.value(row) == cellStyle) {
237         return;
238     }
239 
240     while (row > d->defaultRowCellStyles.size())
241         d->defaultRowCellStyles.append(0);
242 
243     d->defaultRowCellStyles.append(cellStyle);
244 }
245