KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.3KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2006 Tomas Mecir <mecirt@gmail.com>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include "manipulator_data.h"
  17. #include <tdelocale.h>
  18. #include "kspread_cell.h"
  19. #include "kspread_sheet.h"
  20. using namespace KSpread;
  21. AbstractDataManipulator::AbstractDataManipulator ()
  22. {
  23. m_format = false;
  24. }
  25. AbstractDataManipulator::~AbstractDataManipulator ()
  26. {
  27. oldData.clear ();
  28. }
  29. bool AbstractDataManipulator::process (Element* element)
  30. {
  31. TQRect range = element->rect().normalize();
  32. for (int col = range.left(); col <= range.right(); ++col)
  33. for (int row = range.top(); row <= range.bottom(); ++row) {
  34. Value val;
  35. TQString text;
  36. bool parse = false;
  37. FormatType fmtType = No_format;
  38. if (m_reverse) {
  39. // reverse - use the stored value
  40. if (oldData.contains (col) && oldData[col].contains (row)) {
  41. val = oldData[col][row].val;
  42. text = oldData[col][row].text;
  43. fmtType = oldData[col][row].format;
  44. parse = false;
  45. }
  46. } else {
  47. val = newValue (element, col, row, &parse, &fmtType);
  48. if (parse)
  49. text = val.asString();
  50. }
  51. // we have the data - set it !
  52. if (parse) {
  53. Cell *cell = m_sheet->nonDefaultCell (col, row);
  54. cell->setCellText (text);
  55. } else {
  56. Cell *cell = m_sheet->cellAt (col, row);
  57. if (!(val.isEmpty() && cell->isDefault()))
  58. // nothing if value and cell both empty
  59. {
  60. Cell *cell = m_sheet->nonDefaultCell (col, row);
  61. cell->setCellValue (val, fmtType, text);
  62. }
  63. }
  64. }
  65. return true;
  66. }
  67. bool AbstractDataManipulator::preProcessing ()
  68. {
  69. // not the first run - data already stored ...
  70. if (!m_firstrun) return true;
  71. Region::Iterator endOfList(cells().end());
  72. for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
  73. {
  74. TQRect range = (*it)->rect().normalize();
  75. for (int col = range.left(); col <= range.right(); ++col)
  76. for (int row = range.top(); row <= range.bottom(); ++row)
  77. {
  78. Cell* cell = m_sheet->cellAt(col, row);
  79. if (cell != m_sheet->defaultCell()) // non-default cell - remember it
  80. {
  81. ADMStorage st;
  82. if (cell->isFormula())
  83. st.text = cell->text();
  84. st.val = m_sheet->value (col, row);
  85. st.format = cell->formatType();
  86. oldData[col][row] = st;
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. DataManipulator::DataManipulator ()
  93. : m_format (No_format),
  94. m_parsing (false)
  95. {
  96. // default name for DataManipulator, can be changed using setName
  97. m_name = i18n ("Change Value");
  98. }
  99. DataManipulator::~DataManipulator ()
  100. {
  101. }
  102. Value DataManipulator::newValue (Element *element, int col, int row,
  103. bool *parsing, FormatType *formatType)
  104. {
  105. *parsing = m_parsing;
  106. if (m_format != No_format)
  107. *formatType = m_format;
  108. TQRect range = element->rect().normalize();
  109. int colidx = range.left() - col;
  110. int rowidx = range.top() - row;
  111. return data.element (colidx, rowidx);
  112. }
  113. ArrayFormulaManipulator::ArrayFormulaManipulator ()
  114. {
  115. m_name = i18n ("Set Array Formula");
  116. }
  117. ArrayFormulaManipulator::~ArrayFormulaManipulator ()
  118. {
  119. }
  120. Value ArrayFormulaManipulator::newValue (Element *element, int col, int row,
  121. bool *parsing, FormatType *)
  122. {
  123. *parsing = true;
  124. TQRect range = element->rect().normalize();
  125. int colidx = col - range.left();
  126. int rowidx = row - range.top();
  127. // fill in the cells ... top-left one gets the formula, the rest gets =INDEX
  128. // TODO: also fill in information about cells being a part of a range for GUI
  129. if (colidx || rowidx) {
  130. return (cellRef + TQString::number (rowidx+1) + ";" +
  131. TQString::number (colidx+1) + ")");
  132. } else {
  133. Cell *cell = m_sheet->nonDefaultCell (col, row);
  134. cellRef = "=INDEX(" + cell->name() + ";";
  135. return m_text;
  136. }
  137. }
  138. ProtectedCheck::ProtectedCheck ()
  139. {
  140. }
  141. ProtectedCheck::~ProtectedCheck ()
  142. {
  143. }
  144. bool ProtectedCheck::check ()
  145. {
  146. if (!m_sheet->isProtected())
  147. return false;
  148. bool prot = false;
  149. Region::Iterator endOfList(cells().end());
  150. for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
  151. {
  152. Region::Element *element = *it;
  153. TQRect range = element->rect().normalize();
  154. for (int col = range.left(); col <= range.right(); ++col)
  155. {
  156. for (int row = range.top(); row <= range.bottom(); ++row)
  157. {
  158. Cell *cell = m_sheet->cellAt (col, row);
  159. if (!cell->format()->notProtected (col, row))
  160. {
  161. prot = true;
  162. break;
  163. }
  164. }
  165. if (prot) break;
  166. }
  167. }
  168. return prot;
  169. }