File indexing completed on 2024-04-28 16:21:30
0001 /* This file is part of the KDE project 0002 Copyright 2009 Johannes Simon <johannes.simon@gmail.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 // Ours 0021 #include "SheetAccessModel.h" 0022 #include "calligra_sheets_limits.h" 0023 #include "Map.h" 0024 #include "Binding.h" 0025 #include "BindingManager.h" 0026 #include "Damages.h" 0027 #include "Region.h" 0028 0029 // Qt 0030 #include <QList> 0031 #include <QMap> 0032 #include <QStandardItem> 0033 #include <QAbstractItemModel> 0034 #include <QVariant> 0035 0036 // Calligra 0037 //#include <KoStore.h> 0038 //#include <KoXmlWriter.h> 0039 //#include <KoShapeSavingContext.h> 0040 0041 Q_DECLARE_METATYPE(QPointer<QAbstractItemModel>) 0042 0043 namespace Calligra 0044 { 0045 namespace Sheets 0046 { 0047 0048 class SheetAccessModel::Private 0049 { 0050 public: 0051 Map *map; 0052 /// Stores in what column each Sheet is. We need this because 0053 /// a Sheet is removed from its Map before the sheetRemoved() signal 0054 /// is emitted, thus we can't ask the Map what index it had. 0055 QMap<Sheet*, int> cols; 0056 }; 0057 0058 SheetAccessModel::SheetAccessModel(Map *map) 0059 : d(new Private) 0060 { 0061 d->map = map; 0062 0063 connect(map, SIGNAL(sheetAdded(Sheet*)), 0064 this, SLOT(slotSheetAdded(Sheet*))); 0065 // FIXME: Check if we can simply connect sheetRevived() to slotSheetAdded() 0066 connect(map, SIGNAL(sheetRevived(Sheet*)), 0067 this, SLOT(slotSheetAdded(Sheet*))); 0068 connect(map, SIGNAL(sheetRemoved(Sheet*)), 0069 this, SLOT(slotSheetRemoved(Sheet*))); 0070 connect(map, SIGNAL(damagesFlushed(QList<Damage*>)), 0071 this, SLOT(handleDamages(QList<Damage*>))); 0072 0073 setRowCount(1); 0074 setColumnCount(0); 0075 } 0076 0077 SheetAccessModel::~SheetAccessModel() 0078 { 0079 delete d; 0080 } 0081 0082 void SheetAccessModel::slotSheetAdded(Sheet *sheet) 0083 { 0084 Q_ASSERT(!d->cols.contains(sheet)); 0085 0086 QStandardItem *item = new QStandardItem; 0087 QList<QStandardItem*> col; 0088 col.append(item); 0089 0090 // This region contains the entire sheet 0091 const Region region(1, 1, KS_colMax, KS_rowMax, sheet); 0092 const QPointer<QAbstractItemModel> model = const_cast<QAbstractItemModel*>( d->map->bindingManager()->createModel( region.name() ) ); 0093 0094 item->setData( QVariant::fromValue( model ), Qt::DisplayRole ); 0095 0096 const int sheetIndex = d->map->indexOf( sheet ); 0097 d->cols.insert(sheet, sheetIndex); 0098 0099 insertColumn( sheetIndex, col ); 0100 setHeaderData( sheetIndex, Qt::Horizontal, sheet->sheetName() ); 0101 } 0102 0103 void SheetAccessModel::slotSheetRemoved(Sheet *sheet) 0104 { 0105 Q_ASSERT(d->cols.contains(sheet)); 0106 removeColumn(d->cols[sheet]); 0107 d->cols.remove(sheet); 0108 } 0109 0110 void SheetAccessModel::handleDamages(const QList<Damage*>& damages) 0111 { 0112 QList<Damage*>::ConstIterator end(damages.end()); 0113 for (QList<Damage*>::ConstIterator it = damages.begin(); it != end; ++it) { 0114 Damage* damage = *it; 0115 if (!damage) { 0116 continue; 0117 } 0118 0119 if (damage->type() == Damage::Sheet) { 0120 SheetDamage* sheetDamage = static_cast<SheetDamage*>(damage); 0121 debugSheetsDamage << "Processing\t" << *sheetDamage; 0122 0123 if (sheetDamage->changes() & SheetDamage::Name) { 0124 Sheet *sheet = sheetDamage->sheet(); 0125 // We should never receive signals from sheets that are not in our model 0126 Q_ASSERT(d->cols.contains(sheet)); 0127 const int sheetIndex = d->cols[sheet]; 0128 setHeaderData(sheetIndex, Qt::Horizontal, sheet->sheetName()); 0129 } 0130 continue; 0131 } 0132 } 0133 } 0134 0135 } // namespace Sheets 0136 } // namespace Calligra