File indexing completed on 2024-04-28 16:21:17

0001 /* This file is part of the KDE project
0002    Copyright 2007 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 #include "BindingManager.h"
0021 
0022 #include "BindingStorage.h"
0023 #include "CellStorage.h"
0024 #include "Map.h"
0025 #include "Region.h"
0026 #include "Sheet.h"
0027 
0028 #include <QAbstractItemModel>
0029 
0030 using namespace Calligra::Sheets;
0031 
0032 class BindingManager::Private
0033 {
0034 public:
0035     const Map* map;
0036 };
0037 
0038 BindingManager::BindingManager(const Map* map)
0039         : d(new Private)
0040 {
0041     d->map = map;
0042 }
0043 
0044 BindingManager::~BindingManager()
0045 {
0046     delete d;
0047 }
0048 
0049 const QAbstractItemModel* BindingManager::createModel(const QString& regionName)
0050 {
0051     const Region region(regionName, d->map);
0052     if (!region.isValid() || !region.isContiguous() || !region.firstSheet()) {
0053         return 0;
0054     }
0055     Binding binding(region);
0056     region.firstSheet()->cellStorage()->setBinding(region, binding);
0057     return binding.model();
0058 }
0059 
0060 bool BindingManager::removeModel(const QAbstractItemModel* model)
0061 {
0062     QList< QPair<QRectF, Binding> > bindings;
0063     const QRect rect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax));
0064     const QList<Sheet*> sheets = d->map->sheetList();
0065     for (int i = 0; i < sheets.count(); ++i) {
0066         Sheet* const sheet = sheets[i];
0067         bindings = sheet->cellStorage()->bindingStorage()->intersectingPairs(Region(rect, sheet));
0068         for (int j = 0; j < bindings.count(); ++j) {
0069             if (bindings[j].second.model() == model) {
0070                 const Region region(bindings[j].first.toRect(), sheet);
0071                 sheet->cellStorage()->removeBinding(region, bindings[j].second);
0072                 return true;
0073             }
0074         }
0075     }
0076     return false;
0077 }
0078 
0079 bool BindingManager::isCellRegionValid(const QString& regionName) const
0080 {
0081     const Region region(regionName, d->map);
0082     return (region.isValid() && region.isContiguous() && region.firstSheet());
0083 }
0084 
0085 void BindingManager::regionChanged(const Region& region)
0086 {
0087     Sheet* sheet;
0088     QList< QPair<QRectF, Binding> > bindings;
0089     Region::ConstIterator end(region.constEnd());
0090     for (Region::ConstIterator it = region.constBegin(); it != end; ++it) {
0091         sheet = (*it)->sheet();
0092         const Region changedRegion((*it)->rect(), sheet);
0093         bindings = sheet->cellStorage()->bindingStorage()->intersectingPairs(changedRegion);
0094         for (int j = 0; j < bindings.count(); ++j)
0095             bindings[j].second.update(changedRegion);
0096     }
0097 }
0098 
0099 void BindingManager::updateAllBindings()
0100 {
0101     QList< QPair<QRectF, Binding> > bindings;
0102     const QRect rect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax));
0103     const QList<Sheet*> sheets = d->map->sheetList();
0104     for (int i = 0; i < sheets.count(); ++i) {
0105         bindings = sheets[i]->cellStorage()->bindingStorage()->intersectingPairs(Region(rect, sheets[i]));
0106         for (int j = 0; j < bindings.count(); ++j)
0107             bindings[j].second.update(Region(bindings[j].first.toRect(), sheets[i]));
0108     }
0109 }