kspread

manipulator_data.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Tomas Mecir <mecirt@gmail.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 
00021 #include "manipulator_data.h"
00022 
00023 #include <klocale.h>
00024 
00025 #include "kspread_cell.h"
00026 #include "kspread_sheet.h"
00027 
00028 using namespace KSpread;
00029 
00030 AbstractDataManipulator::AbstractDataManipulator ()
00031 {
00032   m_format = false;
00033 }
00034 
00035 AbstractDataManipulator::~AbstractDataManipulator ()
00036 {
00037   oldData.clear ();
00038 }
00039 
00040 bool AbstractDataManipulator::process (Element* element)
00041 {
00042   QRect range = element->rect().normalize();
00043   for (int col = range.left(); col <= range.right(); ++col)
00044     for (int row = range.top(); row <= range.bottom(); ++row) {
00045       Value val;
00046       QString text;
00047       bool parse = false;
00048       FormatType fmtType = No_format;
00049       if (m_reverse) {
00050         // reverse - use the stored value
00051         if (oldData.contains (col) && oldData[col].contains (row)) {
00052           val = oldData[col][row].val;
00053           text = oldData[col][row].text;
00054           fmtType = oldData[col][row].format;
00055           parse = false;
00056         }
00057       } else {
00058         val = newValue (element, col, row, &parse, &fmtType);
00059         if (parse)
00060           text = val.asString();
00061       }
00062       
00063       // we have the data - set it !
00064       if (parse) {
00065         Cell *cell = m_sheet->nonDefaultCell (col, row);
00066         cell->setCellText (text);
00067       } else {
00068         Cell *cell = m_sheet->cellAt (col, row);
00069         if (!(val.isEmpty() && cell->isDefault()))
00070         // nothing if value and cell both empty
00071         {
00072           Cell *cell = m_sheet->nonDefaultCell (col, row);
00073           cell->setCellValue (val, fmtType, text);
00074         }
00075       }
00076     }
00077   return true;
00078 }
00079 
00080 bool AbstractDataManipulator::preProcessing ()
00081 {
00082   // not the first run - data already stored ...
00083   if (!m_firstrun) return true;
00084   
00085   Region::Iterator endOfList(cells().end());
00086   for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
00087   {
00088     QRect range = (*it)->rect().normalize();
00089     for (int col = range.left(); col <= range.right(); ++col)
00090       for (int row = range.top(); row <= range.bottom(); ++row)
00091       {
00092         Cell* cell = m_sheet->cellAt(col, row);
00093         if (cell != m_sheet->defaultCell())  // non-default cell - remember it
00094         {
00095           ADMStorage st;
00096           
00097           if (cell->isFormula())
00098             st.text = cell->text();
00099           st.val = m_sheet->value (col, row);
00100           st.format = cell->formatType();
00101           oldData[col][row] = st;
00102         }
00103       }
00104   }
00105   return true;
00106 }
00107 
00108 DataManipulator::DataManipulator ()
00109   : m_format (No_format),
00110   m_parsing (false)
00111 {
00112   // default name for DataManipulator, can be changed using setName
00113   m_name = i18n ("Change Value");
00114 }
00115 
00116 DataManipulator::~DataManipulator ()
00117 {
00118 }
00119 
00120 Value DataManipulator::newValue (Element *element, int col, int row,
00121     bool *parsing, FormatType *formatType)
00122 {
00123   *parsing = m_parsing;
00124   if (m_format != No_format)
00125     *formatType = m_format;
00126   QRect range = element->rect().normalize();
00127   int colidx = range.left() - col;
00128   int rowidx = range.top() - row;
00129   return data.element (colidx, rowidx);
00130 }
00131 
00132 ArrayFormulaManipulator::ArrayFormulaManipulator ()
00133 {
00134   m_name = i18n ("Set Array Formula");
00135 }
00136 
00137 ArrayFormulaManipulator::~ArrayFormulaManipulator ()
00138 {
00139 }
00140 
00141 Value ArrayFormulaManipulator::newValue (Element *element, int col, int row,
00142                                  bool *parsing, FormatType *)
00143 {
00144   *parsing = true;
00145   QRect range = element->rect().normalize();
00146   int colidx = col - range.left();
00147   int rowidx = row - range.top();
00148   
00149   // fill in the cells ... top-left one gets the formula, the rest gets =INDEX
00150   // TODO: also fill in information about cells being a part of a range for GUI
00151   if (colidx || rowidx) {
00152     return (cellRef + QString::number (rowidx+1) + ";" +
00153         QString::number (colidx+1) + ")");
00154   } else {
00155     Cell *cell = m_sheet->nonDefaultCell (col, row);
00156     cellRef = "=INDEX(" + cell->name() + ";";
00157     return m_text;
00158   }
00159 }
00160 
00161 ProtectedCheck::ProtectedCheck ()
00162 {
00163 }
00164 
00165 ProtectedCheck::~ProtectedCheck ()
00166 {
00167 }
00168 
00169 bool ProtectedCheck::check ()
00170 {
00171   if (!m_sheet->isProtected())
00172     return false;
00173   
00174   bool prot = false;
00175   Region::Iterator endOfList(cells().end());
00176   for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
00177   {
00178     Region::Element *element = *it;
00179     QRect range = element->rect().normalize();
00180 
00181     for (int col = range.left(); col <= range.right(); ++col)
00182     {
00183       for (int row = range.top(); row <= range.bottom(); ++row)
00184       {
00185         Cell *cell = m_sheet->cellAt (col, row);
00186         if (!cell->format()->notProtected (col, row))
00187         {
00188           prot = true;
00189           break;
00190         }
00191       }
00192       if (prot) break;
00193     }
00194   }
00195   return prot;
00196 }
00197 
KDE Home | KDE Accessibility Home | Description of Access Keys