File indexing completed on 2024-05-12 16:35:11

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Tomas Mecir <mecirt@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; only
0007    version 2 of the License.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 
0021 #ifndef CALLIGRA_SHEETS_DATA_MANIPULATORS
0022 #define CALLIGRA_SHEETS_DATA_MANIPULATORS
0023 
0024 #include "AbstractRegionCommand.h"
0025 #include "Global.h"
0026 #include "Style.h"
0027 #include "Value.h"
0028 
0029 #include "sheets_common_export.h"
0030 
0031 
0032 namespace Calligra
0033 {
0034 namespace Sheets
0035 {
0036 
0037 /**
0038  * \ingroup Commands
0039  * \brief Abstract command for setting values.
0040  */
0041 class CALLIGRA_SHEETS_COMMON_EXPORT AbstractDataManipulator : public AbstractRegionCommand
0042 {
0043 public:
0044     explicit AbstractDataManipulator(KUndo2Command *parent = 0);
0045     ~AbstractDataManipulator() override;
0046 
0047     bool process(Element* element) override;
0048 
0049 protected:
0050     /** Return new value. row/col are relative to sheet, not element.
0051     If the function sets *parse to true, the value will be treated as an
0052     user-entered string and parsed by Cell. */
0053     virtual Value newValue(Element *element, int col, int row,
0054                            bool *parse, Format::Type *fmtType) = 0;
0055 
0056     /** do we want to change this cell ? */
0057     virtual bool wantChange(Element *element, int col, int row) {
0058         Q_UNUSED(element)
0059         Q_UNUSED(col)
0060         Q_UNUSED(row)
0061         return true;
0062     }
0063 
0064     /**
0065      * Starts the undo recording.
0066      */
0067     bool preProcessing() override;
0068 
0069     /**
0070      * Processes the region. Calls process(Element*).
0071      */
0072     bool mainProcessing() override;
0073 
0074     /**
0075      * Stops the undo recording and stores the old data.
0076      */
0077     bool postProcessing() override;
0078 };
0079 
0080 /**
0081  * \ingroup Commands
0082  * \brief Abstract command for setting values/styles.
0083  */
0084 class AbstractDFManipulator : public AbstractDataManipulator
0085 {
0086 public:
0087     explicit AbstractDFManipulator(KUndo2Command *parent = 0);
0088     ~AbstractDFManipulator() override;
0089     bool process(Element* element) override;
0090 
0091     /** returns whether this manipulator changes formats */
0092     bool changeFormat() {
0093         return m_changeformat;
0094     }
0095     /** set whether this manipulator changes formats */
0096     void setChangeFormat(bool chf) {
0097         m_changeformat = chf;
0098     }
0099 protected:
0100     /** this method should return new format for a given cell */
0101     virtual Style newFormat(Element *element, int col, int row) = 0;
0102 
0103     bool m_changeformat : 1;
0104 };
0105 
0106 
0107 /**
0108  * \ingroup Commands
0109  * \brief Sets values of a cell range.
0110  */
0111 class DataManipulator : public AbstractDataManipulator
0112 {
0113 public:
0114     explicit DataManipulator(KUndo2Command *parent = 0);
0115     ~DataManipulator() override;
0116     void setParsing(bool val) {
0117         m_parsing = val;
0118     }
0119     void setExpandMatrix(bool expand) {
0120         m_expandMatrix = expand;
0121     }
0122     /** set the values for the range. Can be either a single value, or
0123     a value array */
0124     void setValue(Value val) {
0125         m_data = val;
0126     }
0127     /** If set, all cells shall be switched to this format. If parsing is
0128     true, the resulting value may end up being different. */
0129     void setFormat(Format::Type fmtType) {
0130         m_format = fmtType;
0131     }
0132 protected:
0133     bool preProcessing() override;
0134     bool process(Element* element) override;
0135     Value newValue(Element *element, int col, int row, bool *, Format::Type *) override;
0136     bool wantChange(Element *element, int col, int row) override;
0137 
0138     Value m_data;
0139     Format::Type m_format;
0140     bool m_parsing : 1;
0141     bool m_expandMatrix : 1;
0142 };
0143 
0144 
0145 /**
0146  * \ingroup Commands
0147  * \brief Fills a value series into a cell range.
0148  */
0149 class SeriesManipulator : public AbstractDataManipulator
0150 {
0151 public:
0152     enum Series { Column, Row, Linear, Geometric };
0153 
0154     SeriesManipulator();
0155     ~SeriesManipulator() override;
0156 
0157     /** Setup the series. This sets the necessary parameters, and also the
0158     correct range. */
0159     void setupSeries(const QPoint &_marker, double start, double end,
0160                      double step, Series mode, Series type);
0161 protected:
0162     Value newValue(Element *element, int col, int row, bool *,
0163                            Format::Type *) override;
0164 
0165     Series m_type;
0166     Value m_start, m_step, m_prev;
0167     int m_last;
0168 };
0169 
0170 
0171 /**
0172  * \ingroup Commands
0173  * \brief Fills values into a cell range.
0174  */
0175 class FillManipulator : public AbstractDFManipulator
0176 {
0177 public:
0178     FillManipulator();
0179     ~FillManipulator() override;
0180 
0181     enum Direction { Up = 0, Down, Left, Right };
0182 
0183     void setDirection(Direction d) {
0184         m_dir = d;
0185     }
0186 protected:
0187     Value newValue(Element *element, int col, int row,
0188                            bool *parse, Format::Type *fmtType) override;
0189     Style newFormat(Element *element, int col, int row) override;
0190     Direction m_dir;
0191 };
0192 
0193 
0194 /**
0195  * \ingroup Commands
0196  * \brief Converts string values to upper-/lowercase.
0197  */
0198 class CaseManipulator: public AbstractDataManipulator
0199 {
0200 public:
0201     CaseManipulator();
0202     ~CaseManipulator() override;
0203 
0204     enum CaseMode {
0205         Upper = 0,
0206         Lower,
0207         FirstUpper
0208     };
0209     void changeMode(CaseMode mode) {
0210         m_mode = mode;
0211     }
0212     void changeLowerCase();
0213     void changeFirstUpper();
0214 protected:
0215     Value newValue(Element *element, int col, int row,
0216                            bool *parse, Format::Type *fmtType) override;
0217 
0218     /** do we want to change this cell ? */
0219     bool wantChange(Element *element, int col, int row) override;
0220 
0221     CaseMode m_mode;
0222 };
0223 
0224 
0225 /**
0226  * \ingroup Commands
0227  * \brief Inserts/Removes cells by shifting other cells.
0228  */
0229 class ShiftManipulator : public AbstractRegionCommand
0230 {
0231 public:
0232     enum Direction { ShiftRight, ShiftBottom };
0233     explicit ShiftManipulator(KUndo2Command *parent = 0);
0234     ~ShiftManipulator() override;
0235     void setDirection(Direction direction) {
0236         m_direction = direction;
0237     }
0238     void setReverse(bool reverse) override;
0239 
0240 protected:
0241     bool process(Element*) override;
0242     bool preProcessing() override;
0243     bool mainProcessing() override;
0244     bool postProcessing() override;
0245 
0246 private:
0247     Direction m_direction;
0248 
0249     enum Mode { Insert, Delete };
0250     Mode m_mode;
0251 };
0252 
0253 } // namespace Sheets
0254 } // namespace Calligra
0255 
0256 #endif  // CALLIGRA_SHEETS_DATA_MANIPULATORS