kspread

manipulator_data.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Tomas Mecir <mecirt@gmail.com>
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.
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
21#include "manipulator_data.h"
22
23#include <tdelocale.h>
24
25#include "kspread_cell.h"
26#include "kspread_sheet.h"
27
28using namespace KSpread;
29
30AbstractDataManipulator::AbstractDataManipulator ()
31{
32 m_format = false;
33}
34
35AbstractDataManipulator::~AbstractDataManipulator ()
36{
37 oldData.clear ();
38}
39
40bool AbstractDataManipulator::process (Element* element)
41{
42 TQRect range = element->rect().normalize();
43 for (int col = range.left(); col <= range.right(); ++col)
44 for (int row = range.top(); row <= range.bottom(); ++row) {
45 Value val;
46 TQString text;
47 bool parse = false;
48 FormatType fmtType = No_format;
49 if (m_reverse) {
50 // reverse - use the stored value
51 if (oldData.contains (col) && oldData[col].contains (row)) {
52 val = oldData[col][row].val;
53 text = oldData[col][row].text;
54 fmtType = oldData[col][row].format;
55 parse = false;
56 }
57 } else {
58 val = newValue (element, col, row, &parse, &fmtType);
59 if (parse)
60 text = val.asString();
61 }
62
63 // we have the data - set it !
64 if (parse) {
65 Cell *cell = m_sheet->nonDefaultCell (col, row);
66 cell->setCellText (text);
67 } else {
68 Cell *cell = m_sheet->cellAt (col, row);
69 if (!(val.isEmpty() && cell->isDefault()))
70 // nothing if value and cell both empty
71 {
72 Cell *cell = m_sheet->nonDefaultCell (col, row);
73 cell->setCellValue (val, fmtType, text);
74 }
75 }
76 }
77 return true;
78}
79
80bool AbstractDataManipulator::preProcessing ()
81{
82 // not the first run - data already stored ...
83 if (!m_firstrun) return true;
84
85 Region::Iterator endOfList(cells().end());
86 for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
87 {
88 TQRect range = (*it)->rect().normalize();
89 for (int col = range.left(); col <= range.right(); ++col)
90 for (int row = range.top(); row <= range.bottom(); ++row)
91 {
92 Cell* cell = m_sheet->cellAt(col, row);
93 if (cell != m_sheet->defaultCell()) // non-default cell - remember it
94 {
95 ADMStorage st;
96
97 if (cell->isFormula())
98 st.text = cell->text();
99 st.val = m_sheet->value (col, row);
100 st.format = cell->formatType();
101 oldData[col][row] = st;
102 }
103 }
104 }
105 return true;
106}
107
108DataManipulator::DataManipulator ()
109 : m_format (No_format),
110 m_parsing (false)
111{
112 // default name for DataManipulator, can be changed using setName
113 m_name = i18n ("Change Value");
114}
115
116DataManipulator::~DataManipulator ()
117{
118}
119
120Value DataManipulator::newValue (Element *element, int col, int row,
121 bool *parsing, FormatType *formatType)
122{
123 *parsing = m_parsing;
124 if (m_format != No_format)
125 *formatType = m_format;
126 TQRect range = element->rect().normalize();
127 int colidx = range.left() - col;
128 int rowidx = range.top() - row;
129 return data.element (colidx, rowidx);
130}
131
132ArrayFormulaManipulator::ArrayFormulaManipulator ()
133{
134 m_name = i18n ("Set Array Formula");
135}
136
137ArrayFormulaManipulator::~ArrayFormulaManipulator ()
138{
139}
140
141Value ArrayFormulaManipulator::newValue (Element *element, int col, int row,
142 bool *parsing, FormatType *)
143{
144 *parsing = true;
145 TQRect range = element->rect().normalize();
146 int colidx = col - range.left();
147 int rowidx = row - range.top();
148
149 // fill in the cells ... top-left one gets the formula, the rest gets =INDEX
150 // TODO: also fill in information about cells being a part of a range for GUI
151 if (colidx || rowidx) {
152 return (cellRef + TQString::number (rowidx+1) + ";" +
153 TQString::number (colidx+1) + ")");
154 } else {
155 Cell *cell = m_sheet->nonDefaultCell (col, row);
156 cellRef = "=INDEX(" + cell->name() + ";";
157 return m_text;
158 }
159}
160
161ProtectedCheck::ProtectedCheck ()
162{
163}
164
165ProtectedCheck::~ProtectedCheck ()
166{
167}
168
169bool ProtectedCheck::check ()
170{
171 if (!m_sheet->isProtected())
172 return false;
173
174 bool prot = false;
175 Region::Iterator endOfList(cells().end());
176 for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
177 {
178 Region::Element *element = *it;
179 TQRect range = element->rect().normalize();
180
181 for (int col = range.left(); col <= range.right(); ++col)
182 {
183 for (int row = range.top(); row <= range.bottom(); ++row)
184 {
185 Cell *cell = m_sheet->cellAt (col, row);
186 if (!cell->format()->notProtected (col, row))
187 {
188 prot = true;
189 break;
190 }
191 }
192 if (prot) break;
193 }
194 }
195 return prot;
196}
197
For every cell in the spread sheet there is a Cell object.
FormatType formatType() const
Return the format of this cell.
TQString text() const
Return the text the user entered.
TQString name() const
Returns the name of the cell.
void setCellValue(const Value &value, FormatType fmtType=No_format, const TQString &inputText=TQString())
Sets the value for this cell and its formatting and input text, if appropriate.
bool isFormula() const
Returns true if this cell holds a formula.
void setCellText(const TQString &_text, bool asString=false)
The high-level method for setting text, when the user inputs it.
Format * format() const
Returns the format object of this cell.
bool isDefault() const
Returns true if this is a default cell (with row and column equal to zero).
Base class for region elements, which can be points or ranges.
Definition region.h:325
TQValueList< Element * > & cells() const
Definition region.cpp:649
Provides a wrapper for cell value.
Value element(unsigned column, unsigned row) const
Return an element in the array value.
Format format() const
Returns the format of the value (i.e.
bool isEmpty() const
Returns true if empty.
TQString asString() const
Returns the string value of this value.
The KSpread namespace.
Definition commands.h:39
AbstractDataManipulator - provides storage of old cell data (for undo) and has an abstract method for...