1/* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 Copyright 2004 Tomas Mecir <mecirt@gmail.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef CALLIGRA_SHEETS_DEPENDENCY_MANAGER
22#define CALLIGRA_SHEETS_DEPENDENCY_MANAGER
23
24#include <QObject>
25
26#include "Region.h"
27
28class KoUpdater;
29
30namespace Calligra
31{
32namespace Sheets
33{
34class Region;
35
36/**
37 * \ingroup Value
38 * Manages the dependencies between cells caused by references in formulas.
39 * This dependency information is used for the recalculation of the cells.
40 */
41class CALLIGRA_SHEETS_ODF_EXPORT DependencyManager : public QObject
42{
43 Q_OBJECT
44 friend class TestDependencies;
45 friend class RecalcManager;
46
47public:
48 /** constructor */
49 explicit DependencyManager(const Map *map);
50 /** destructor */
51 ~DependencyManager();
52
53 /** clear all data */
54 void reset();
55
56 /**
57 * Handles the fact, that formulas have changed in \p region.
58 * The \p region needs to contain only those areas, in which
59 * each cell has a changed formula. That can also be a removed
60 * formula. This class has no chance to know the old formula
61 * locations, but the caller of this method has. So, usually the
62 * \p region consists of several cell locations, not cell ranges.
63 * The caller has to take care of that, because each and every
64 * cell in \p region is traversed.
65 */
66 void regionChanged(const Region& region);
67
68 /**
69 * Updates the whole \p map.
70 */
71 void updateAllDependencies(const Map* map, KoUpdater *updater = 0);
72
73 /**
74 * Returns the cell depths.
75 * \return the cell depths
76 */
77 QMap<Cell, int> depths() const;
78
79 /**
80 * Returns the region, that consumes the value of \p cell.
81 *
82 * I.e. the returned region contains all cells, that have
83 * got a formula referencing \p cell. Even if the formula
84 * references a complete cell range or a named area, that
85 * contains \p cell.
86 *
87 * \return region consuming \p cell 's value
88 */
89 Region consumingRegion(const Cell& cell) const;
90
91 /**
92 * Returns the region, that is reduced to those parts of \p region, that provide values.
93 * \return region providing values for others
94 */
95 Region reduceToProvidingRegion(const Region& region) const;
96
97 /**
98 * Adjusts formulas after cut & paste operations or column/row insertions/deletions.
99 *
100 * \param movedRegion the region, that was moved
101 * \param destination the new upper left corner of the region
102 */
103 void regionMoved(const Region& movedRegion, const Cell& destination);
104
105public Q_SLOTS:
106 void namedAreaModified(const QString&);
107
108 /**
109 * Called after a sheet was added.
110 */
111 void addSheet(Sheet *sheet);
112
113 /**
114 * Called after a sheet was removed.
115 */
116 void removeSheet(Sheet *sheet);
117
118protected:
119 /**
120 * \param cell the cell which formula should be altered
121 * \param oldLocation the location/range, that was cut
122 * \param offset the relative movement and new sheet, if applicable
123 *
124 * \see regionMoved()
125 */
126 void updateFormula(const Cell& cell, const Region::Element* oldLocation, const Region::Point& offset);
127
128private:
129 Q_DISABLE_COPY(DependencyManager)
130
131 class Private;
132 Private * const d;
133};
134
135} // namespace Sheets
136} // namespace Calligra
137
138#endif // CALLIGRA_SHEETS_DEPENDENCY_MANAGER
139