File indexing completed on 2024-05-26 16:15:52

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2009 KO GmbH <cbo@kogmbh.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoTableColumnAndRowStyleManager.h"
0021 
0022 #include "styles/KoTableColumnStyle.h"
0023 #include "styles/KoTableRowStyle.h"
0024 #include "styles/KoTableCellStyle.h"
0025 #include "styles/KoTableStyle.h"
0026 
0027 #include <QVector>
0028 #include <QVariant>
0029 #include <QTextTable>
0030 
0031 #include "TextDebug.h"
0032 
0033 class Q_DECL_HIDDEN KoTableColumnAndRowStyleManager::Private : public QSharedData
0034 {
0035 public:
0036     Private()  { }
0037     ~Private() {
0038     }
0039     QVector<KoTableColumnStyle> tableColumnStyles;
0040     QVector<KoTableRowStyle> tableRowStyles;
0041 
0042     QVector<KoTableCellStyle*> defaultRowCellStyles;
0043     QVector<KoTableCellStyle*> defaultColumnCellStyles;
0044 };
0045 
0046 KoTableColumnAndRowStyleManager::KoTableColumnAndRowStyleManager()
0047     : d(new Private())
0048 {
0049 }
0050 
0051 KoTableColumnAndRowStyleManager::KoTableColumnAndRowStyleManager(const KoTableColumnAndRowStyleManager &rhs)
0052     : d(rhs.d)
0053 {
0054 }
0055 
0056 KoTableColumnAndRowStyleManager &KoTableColumnAndRowStyleManager::operator=(const KoTableColumnAndRowStyleManager &rhs)
0057 {
0058     d = rhs.d;
0059 
0060     return *this;
0061 }
0062 
0063 KoTableColumnAndRowStyleManager::~KoTableColumnAndRowStyleManager()
0064 {
0065 }
0066 
0067 KoTableColumnAndRowStyleManager KoTableColumnAndRowStyleManager::getManager(QTextTable *table)
0068 {
0069     QTextTableFormat tableFormat = table->format();
0070 
0071     if (tableFormat.hasProperty(KoTableStyle::ColumnAndRowStyleManager)) {
0072         return  tableFormat.property(KoTableStyle::ColumnAndRowStyleManager).value<KoTableColumnAndRowStyleManager>();
0073     } else {
0074         KoTableColumnAndRowStyleManager carsManager;
0075 
0076         QVariant var;
0077         var.setValue(carsManager);
0078         tableFormat.setProperty(KoTableStyle::ColumnAndRowStyleManager, var);
0079         table->setFormat(tableFormat);
0080         return carsManager;
0081     }
0082 }
0083 
0084 void KoTableColumnAndRowStyleManager::setColumnStyle(int column, const KoTableColumnStyle &columnStyle)
0085 {
0086     Q_ASSERT(column >= 0);
0087 
0088     if (column < 0) {
0089         return;
0090     }
0091 
0092     if (column < d->tableColumnStyles.size() && d->tableColumnStyles.value(column) == columnStyle) {
0093         return;
0094     }
0095 
0096     // TODO: just resize() if needed should work as well
0097     d->tableColumnStyles.reserve(column+1);
0098     while (column >= d->tableColumnStyles.size())
0099         d->tableColumnStyles.append(KoTableColumnStyle());
0100 
0101     d->tableColumnStyles.replace(column, columnStyle);
0102 }
0103 
0104 void KoTableColumnAndRowStyleManager::insertColumns(int column, int numberColumns, const KoTableColumnStyle &columnStyle)
0105 {
0106     Q_ASSERT(column >= 0);
0107     Q_ASSERT(numberColumns >= 0);
0108 
0109     if (column < 0 || numberColumns < 0) {
0110         return;
0111     }
0112 
0113     // TODO: just resize() if needed should work as well
0114     d->tableColumnStyles.reserve(column + numberColumns);
0115     while (column > d->tableColumnStyles.size())
0116         d->tableColumnStyles.append(KoTableColumnStyle());
0117 
0118     d->tableColumnStyles.insert(column, numberColumns, columnStyle);
0119 }
0120 
0121 void KoTableColumnAndRowStyleManager::removeColumns(int column, int numberColumns)
0122 {
0123     Q_ASSERT(column >= 0);
0124     Q_ASSERT(numberColumns >= 0);
0125 
0126     if (column >= d->tableColumnStyles.size() || column < 0 || numberColumns < 0) {
0127         return;
0128     }
0129 
0130     d->tableColumnStyles.remove(column, numberColumns);
0131 }
0132 
0133 KoTableColumnStyle KoTableColumnAndRowStyleManager::columnStyle(int column) const
0134 {
0135     Q_ASSERT(column >= 0);
0136 
0137     if (column < 0) {
0138         return KoTableColumnStyle();
0139     }
0140 
0141     return d->tableColumnStyles.value(column);
0142 }
0143 
0144 void KoTableColumnAndRowStyleManager::setRowStyle(int row, const KoTableRowStyle &rowStyle)
0145 {
0146     Q_ASSERT(row >= 0);
0147 
0148     if (row < 0) {
0149         return;
0150     }
0151 
0152     if (row < d->tableRowStyles.size() && d->tableRowStyles.value(row) == rowStyle) {
0153         return;
0154     }
0155 
0156     // TODO: just resize() if needed should work as well
0157     d->tableRowStyles.reserve(row+1);
0158     while (row >= d->tableRowStyles.size())
0159         d->tableRowStyles.append(KoTableRowStyle());
0160 
0161     d->tableRowStyles.replace(row, rowStyle);
0162 }
0163 
0164 void KoTableColumnAndRowStyleManager::insertRows(int row, int numberRows, const KoTableRowStyle &rowStyle)
0165 {
0166     Q_ASSERT(row >= 0);
0167     Q_ASSERT(numberRows >= 0);
0168 
0169     if (row < 0 || numberRows < 0) {
0170         return;
0171     }
0172 
0173     // TODO: just resize() if needed should work as well
0174     d->tableRowStyles.reserve(row + numberRows);
0175     while (row > d->tableRowStyles.size())
0176         d->tableRowStyles.append(KoTableRowStyle());
0177 
0178     d->tableRowStyles.insert(row, numberRows, rowStyle);
0179 }
0180 
0181 void KoTableColumnAndRowStyleManager::removeRows(int row, int numberRows)
0182 {
0183     Q_ASSERT(row >= 0);
0184     Q_ASSERT(numberRows >= 0);
0185 
0186     if (row >= d->tableRowStyles.size() || row < 0 || numberRows < 0) {
0187         return;
0188     }
0189 
0190     d->tableRowStyles.remove(row, numberRows);
0191 }
0192 
0193 KoTableRowStyle KoTableColumnAndRowStyleManager::rowStyle(int row) const
0194 {
0195     Q_ASSERT(row >= 0);
0196 
0197     if (row < 0) {
0198         return KoTableRowStyle();
0199     }
0200 
0201     return d->tableRowStyles.value(row);
0202 }
0203 
0204 KoTableCellStyle* KoTableColumnAndRowStyleManager::defaultColumnCellStyle(int column) const
0205 {
0206     Q_ASSERT(column >= 0);
0207 
0208     return d->defaultColumnCellStyles.value(column);
0209 }
0210 
0211 void KoTableColumnAndRowStyleManager::setDefaultColumnCellStyle(int column, KoTableCellStyle* cellStyle)
0212 {
0213     Q_ASSERT(column >= 0);
0214 
0215     if (column < d->defaultColumnCellStyles.size() && d->defaultColumnCellStyles.value(column) == cellStyle) {
0216         return;
0217     }
0218 
0219     while (column > d->defaultColumnCellStyles.size())
0220         d->defaultColumnCellStyles.append(0);
0221 
0222     d->defaultColumnCellStyles.append(cellStyle);
0223 }
0224 
0225 KoTableCellStyle* KoTableColumnAndRowStyleManager::defaultRowCellStyle(int row) const
0226 {
0227     Q_ASSERT(row >= 0);
0228 
0229     return d->defaultRowCellStyles.value(row);
0230 }
0231 
0232 void KoTableColumnAndRowStyleManager::setDefaultRowCellStyle(int row, KoTableCellStyle* cellStyle)
0233 {
0234     Q_ASSERT(row >= 0);
0235 
0236     if (row < d->defaultRowCellStyles.size() && d->defaultRowCellStyles.value(row) == cellStyle) {
0237         return;
0238     }
0239 
0240     while (row > d->defaultRowCellStyles.size())
0241         d->defaultRowCellStyles.append(0);
0242 
0243     d->defaultRowCellStyles.append(cellStyle);
0244 }