File indexing completed on 2024-05-12 16:29:17

0001 /*
0002  *  Copyright (c) 2010 Carlos Licea <carlos@kdab.com>
0003  *
0004  *  This library is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU Lesser General Public License as published
0006  *  by the Free Software Foundation; either version 2.1 of the License, or
0007  *  (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
0012  *  GNU Lesser General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU Lesser General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include "KoTable.h"
0020 #include "KoColumn.h"
0021 #include "KoRow.h"
0022 #include "KoCell.h"
0023 
0024 #include <KoXmlWriter.h>
0025 
0026 KOSTYLE_DECLARE_SHARED_POINTER_IMPL(KoTable)
0027 
0028 KoTable::KoTable()
0029 : m_columns()
0030 , m_rows()
0031 , m_cells()
0032 , m_rowCount()
0033 , m_columnCount()
0034 , m_style(0)
0035 , m_printable(true)
0036 , m_name()
0037 , m_protected(false)
0038 , m_protectionKey()
0039 {
0040 }
0041 
0042 KoTable::~KoTable()
0043 {
0044 }
0045 
0046 KoCell* KoTable::cellAt(int row, int column)
0047 {
0048     const QPair<int,int> key(row,column);
0049     KoCell* cell = m_cells.value(key);
0050 
0051     if(!cell) {
0052         cell = new KoCell();
0053         m_cells.insert(key, cell);
0054         m_columnCount = qMax(column+1, m_columnCount);
0055         m_rowCount = qMax(row+1, m_rowCount);
0056     }
0057 
0058     return cell;
0059 }
0060 
0061 KoColumn* KoTable::columnAt(int column)
0062 {
0063     KoColumn* columnAt = m_columns.value(column);
0064 
0065     if(!columnAt) {
0066         columnAt = new KoColumn();
0067         if(column >= m_columns.size()) {
0068             m_columns.resize(column+1);
0069         }
0070         m_columns.insert(column, columnAt);
0071         m_columnCount = qMax(column+1, m_columnCount);
0072     }
0073 
0074     return columnAt;
0075 }
0076 
0077 KoRow* KoTable::rowAt(int row)
0078 {
0079     KoRow* rowAt = m_rows.value(row);
0080 
0081     if(!rowAt) {
0082         rowAt = new KoRow();
0083         if(row >= m_rows.size()) {
0084             m_rows.resize(row+1);
0085         }
0086         m_rows.replace(row, rowAt);
0087         m_rowCount = qMax(row+1, m_rowCount);
0088     }
0089 
0090     return rowAt;
0091 }
0092 
0093 int KoTable::columnCount() const
0094 {
0095     return m_columnCount;
0096 }
0097 
0098 int KoTable::rowCount() const
0099 {
0100     return m_rowCount;
0101 }
0102 
0103 void KoTable::saveOdf(KoXmlWriter& writer, KoGenStyles& styles)
0104 {
0105     writer.startElement("table:table");
0106 
0107     writer.addAttribute("table:name", m_name);
0108     writer.addAttribute("table:protected", m_protected ? "true" : "false" );
0109     if(!m_protectionKey.isEmpty()) {
0110         writer.addAttribute("table:protection-key", m_protectionKey);
0111     }
0112     if(!m_protectionAlgorithm.isEmpty()) {
0113         writer.addAttribute("table:protection-key-digest-algorithm", m_protectionAlgorithm);
0114     }
0115 
0116     if(m_style) {
0117         m_style->setName(m_style->saveOdf(styles));
0118         writer.addAttribute("table:style-name", m_style->name());
0119     }
0120 
0121     {
0122         KoColumn defaultColumn;
0123         for(int c = 0; c < columnCount(); ++c) {
0124             KoColumn* column = m_columns.value(c);
0125             if(column) {
0126                 column->saveOdf(writer, styles);
0127             }
0128             else {
0129                 defaultColumn.saveOdf(writer, styles);
0130             }
0131         }
0132     }
0133 
0134     {
0135         KoRow defaultRow;
0136         for(int r = 0; r < rowCount(); ++r) {
0137             KoRow* row = m_rows.value(r);
0138             if(row) {
0139                 row->saveOdf(writer, styles);
0140 
0141                 KoCell defaultCell;
0142                 for(int c = 0; c < columnCount(); ++c) {
0143                     KoCell* cell = m_cells.value(QPair<int,int>(r,c));
0144                     if(cell) {
0145                         cell->saveOdf(writer, styles);
0146                     }
0147                     else {
0148                         defaultCell.saveOdf(writer, styles);
0149                     }
0150                 }
0151 
0152                 row->finishSaveOdf(writer, styles);
0153             }
0154             else {
0155                 defaultRow.saveOdf(writer, styles);
0156 
0157                 KoCell defaultCell;
0158                 for(int c = 0; c < columnCount(); ++c) {
0159                     KoCell* cell = m_cells.value(QPair<int,int>(r,c));
0160                     if(cell) {
0161                         cell->saveOdf(writer, styles);
0162                     }
0163                     else {
0164                         defaultCell.saveOdf(writer, styles);
0165                     }
0166                 }
0167 
0168                 defaultRow.finishSaveOdf(writer, styles);
0169             }
0170         }
0171     }
0172 
0173     writer.endElement();// table:table
0174 }
0175 
0176 void KoTable::setName(const QString& name)
0177 {
0178     m_name = name;
0179 }
0180 
0181 QString KoTable::name() const
0182 {
0183     return m_name;
0184 }
0185 
0186 void KoTable::setPrintable(bool printable)
0187 {
0188     m_printable = printable;
0189 }
0190 
0191 bool KoTable::printable() const
0192 {
0193     return m_printable;
0194 }
0195 
0196 void KoTable::setTableStyle(KoTblStyle::Ptr style)
0197 {
0198     m_style = style;
0199 }
0200 
0201 KoTblStyle::Ptr KoTable::tableStyle()
0202 {
0203     return m_style;
0204 }
0205 
0206 void KoTable::setProtected(bool isProtected)
0207 {
0208     m_protected = isProtected;
0209 }
0210 
0211 void KoTable::setProtectionKey(const QString &password, const QString &protectionAlgorithmUri)
0212 {
0213     m_protectionKey = password;
0214     m_protectionAlgorithm = protectionAlgorithmUri;
0215 }
0216 
0217 QString KoTable::protectionKey() const
0218 {
0219     return m_protectionKey;
0220 }
0221 
0222 QString KoTable::protectionalgorithm() const
0223 {
0224     return m_protectionAlgorithm;
0225 }
0226 
0227 bool KoTable::isPprotected() const
0228 {
0229     return m_protected;
0230 }
0231