File indexing completed on 2024-05-12 16:35:37

0001 /* This file is part of the KDE project
0002    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
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 #ifndef KSPREAD_ODF_SAVING_CONTEXT
0021 #define KSPREAD_ODF_SAVING_CONTEXT
0022 
0023 #include "Cell.h"
0024 #include "GenValidationStyle.h"
0025 #include "Sheet.h"
0026 #include "calligra_sheets_limits.h"
0027 
0028 #include <KoShapeSavingContext.h>
0029 
0030 #include <QMap>
0031 #include <QMultiHash>
0032 
0033 class KoShape;
0034 
0035 namespace Calligra
0036 {
0037 namespace Sheets
0038 {
0039 namespace Odf
0040 {
0041 
0042 /**
0043  * \ingroup OpenDocument
0044  * Data used while saving.
0045  */
0046 class OdfSavingContext
0047 {
0048 public:
0049     explicit OdfSavingContext(KoShapeSavingContext &shapeContext)
0050             : shapeContext(shapeContext) {}
0051 
0052     void insertCellAnchoredShape(const Sheet *sheet, int row, int column, KoShape* shape) {
0053         Q_ASSERT_X(1 <= column && column <= KS_colMax, __FUNCTION__, QString("%1 out of bounds").arg(column).toLocal8Bit());
0054         Q_ASSERT_X(1 <= row && row <= KS_rowMax, __FUNCTION__, QString("%1 out of bounds").arg(row).toLocal8Bit());
0055         m_cellAnchoredShapes[sheet][row].insert(column, shape);
0056     }
0057 
0058     bool rowHasCellAnchoredShapes(const Sheet* sheet, int row) const {
0059         AnchoredShapes::const_iterator it = m_cellAnchoredShapes.constFind(sheet);
0060         if (it == m_cellAnchoredShapes.constEnd())
0061             return false;
0062         return (*it).contains(row);
0063     }
0064 
0065     bool cellHasAnchoredShapes(const Sheet *sheet, int row, int column) const {
0066         AnchoredShapes::const_iterator it = m_cellAnchoredShapes.constFind(sheet);
0067         if (it == m_cellAnchoredShapes.constEnd())
0068             return false;
0069         AnchoredShape::const_iterator rit = (*it).constFind(row);
0070         if (rit == (*it).constEnd())
0071             return false;
0072         return (*rit).contains(column);
0073     }
0074 
0075     int nextAnchoredShape(const Sheet *sheet, int row, int column) const {
0076         AnchoredShapes::const_iterator it = m_cellAnchoredShapes.constFind(sheet);
0077         if (it != m_cellAnchoredShapes.constEnd()) {
0078             AnchoredShape::const_iterator rit = (*it).constFind(row);
0079             if (rit != (*it).constEnd()) {
0080                 QMultiHash<int, KoShape*>::const_iterator cit((*rit).constBegin()), cend((*rit).constEnd());
0081                 for (; cit != cend; ++cit)
0082                     if (cit.key() > column) return cit.key();  // found one
0083 
0084             }
0085         }
0086         return 0;
0087     }
0088 
0089     QList<KoShape*> cellAnchoredShapes(const Sheet *sheet, int row, int column) const {
0090         AnchoredShapes::const_iterator it = m_cellAnchoredShapes.constFind(sheet);
0091         if (it != m_cellAnchoredShapes.constEnd()) {
0092             AnchoredShape::const_iterator rit = (*it).constFind(row);
0093             if (rit != (*it).constEnd()) {
0094                 return (*rit).values(column);
0095             }
0096         }
0097         return QList<KoShape*>();
0098     }
0099 
0100 public:
0101     KoShapeSavingContext& shapeContext;
0102     GenValidationStyles valStyle;
0103     QMap<int, Style> columnDefaultStyles;
0104     QMap<int, Style> rowDefaultStyles;
0105 
0106 private:
0107     typedef QHash < int /*row*/, QMultiHash < int /*col*/, KoShape* > > AnchoredShape;
0108     typedef QHash < const Sheet*, AnchoredShape > AnchoredShapes;
0109     AnchoredShapes m_cellAnchoredShapes;
0110 };
0111 
0112 } // namespace Odf
0113 } // namespace Sheets
0114 } // namespace Calligra
0115 
0116 #endif // KSPREAD_ODF_SAVING_CONTEXT