1/* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "BindingManager.h"
21
22#include "BindingStorage.h"
23#include "CellStorage.h"
24#include "Map.h"
25#include "Region.h"
26#include "Sheet.h"
27
28#include <QAbstractItemModel>
29
30using namespace Calligra::Sheets;
31
32class BindingManager::Private
33{
34public:
35 const Map* map;
36};
37
38BindingManager::BindingManager(const Map* map)
39 : d(new Private)
40{
41 d->map = map;
42}
43
44BindingManager::~BindingManager()
45{
46 delete d;
47}
48
49const QAbstractItemModel* BindingManager::createModel(const QString& regionName)
50{
51 const Region region(regionName, d->map);
52 if (!region.isValid() || !region.isContiguous() || !region.firstSheet()) {
53 return 0;
54 }
55 Binding binding(region);
56 region.firstSheet()->cellStorage()->setBinding(region, binding);
57 return binding.model();
58}
59
60bool BindingManager::removeModel(const QAbstractItemModel* model)
61{
62 QList< QPair<QRectF, Binding> > bindings;
63 const QRect rect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax));
64 const QList<Sheet*> sheets = d->map->sheetList();
65 for (int i = 0; i < sheets.count(); ++i) {
66 Sheet* const sheet = sheets[i];
67 bindings = sheet->cellStorage()->bindingStorage()->intersectingPairs(Region(rect, sheet));
68 for (int j = 0; j < bindings.count(); ++j) {
69 if (bindings[j].second.model() == model) {
70 const Region region(bindings[j].first.toRect(), sheet);
71 sheet->cellStorage()->removeBinding(region, bindings[j].second);
72 return true;
73 }
74 }
75 }
76 return false;
77}
78
79bool BindingManager::isCellRegionValid(const QString& regionName) const
80{
81 const Region region(regionName, d->map);
82 return (region.isValid() && region.isContiguous() && region.firstSheet());
83}
84
85void BindingManager::regionChanged(const Region& region)
86{
87 Sheet* sheet;
88 QList< QPair<QRectF, Binding> > bindings;
89 Region::ConstIterator end(region.constEnd());
90 for (Region::ConstIterator it = region.constBegin(); it != end; ++it) {
91 sheet = (*it)->sheet();
92 const Region changedRegion((*it)->rect(), sheet);
93 bindings = sheet->cellStorage()->bindingStorage()->intersectingPairs(changedRegion);
94 for (int j = 0; j < bindings.count(); ++j)
95 bindings[j].second.update(changedRegion);
96 }
97}
98
99void BindingManager::updateAllBindings()
100{
101 QList< QPair<QRectF, Binding> > bindings;
102 const QRect rect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax));
103 const QList<Sheet*> sheets = d->map->sheetList();
104 for (int i = 0; i < sheets.count(); ++i) {
105 bindings = sheets[i]->cellStorage()->bindingStorage()->intersectingPairs(Region(rect, sheets[i]));
106 for (int j = 0; j < bindings.count(); ++j)
107 bindings[j].second.update(Region(bindings[j].first.toRect(), sheets[i]));
108 }
109}
110
111#include "BindingManager.moc"
112