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

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 #ifndef CALLIGRA_SHEETS_SORT_MANIPULATOR
0021 #define CALLIGRA_SHEETS_SORT_MANIPULATOR
0022 
0023 #include "CellStorage.h"
0024 #include "DataManipulators.h"
0025 
0026 namespace Calligra
0027 {
0028 namespace Sheets
0029 {
0030 class CellStorage;
0031 
0032 /**
0033  * \ingroup Commands
0034  * \brief Sorts the values in a cell range.
0035  */
0036 class CALLIGRA_SHEETS_COMMON_EXPORT SortManipulator : public AbstractDFManipulator
0037 {
0038 public:
0039     SortManipulator();
0040     ~SortManipulator() override;
0041 
0042     bool process(Element* element) override;
0043 
0044     /** true if rows are to be sorted, false if columns are */
0045     void setSortRows(bool v) {
0046         m_rows = v;
0047     }
0048     /** skip first row/column, as it contains headers */
0049     void setSkipFirst(bool v) {
0050         m_skipfirst = v;
0051     }
0052     /** set whether cell formats should be moved with data */
0053     void setCopyFormat(bool v) {
0054         setChangeFormat(v);
0055     }
0056     /** set whether we will use a custom list */
0057     void setUseCustomList(bool v) {
0058         m_usecustomlist = v;
0059     }
0060     /** set a custom list that will be used */
0061     void setCustomList(const QStringList &l) {
0062         m_customlist = l;
0063     }
0064 
0065     /** 
0066      * Adds a sort criterion.
0067      * Sort criteria are used in order in which they're added.
0068      * \param index the column/row index. Indexed from 0.
0069      * \param order sort order (ascending/descending)
0070      * \param caseSensitivity case sensitivity
0071      */
0072     void addCriterion(int index, Qt::SortOrder order, Qt::CaseSensitivity caseSensitivity);
0073     void clearCriteria();
0074 
0075 protected:
0076     bool preProcessing() override;
0077     bool postProcessing() override;
0078     Value newValue(Element *element, int col, int row,
0079                            bool *parse, Format::Type *fmtType) override;
0080     Style newFormat(Element *element, int col, int row) override;
0081 
0082     /** sort the data, filling the "sorted" structure */
0083     void sort(Element *element);
0084     bool shouldReorder(Element *element, int first, int second);
0085 
0086     bool m_rows, m_skipfirst, m_usecustomlist;
0087     QStringList m_customlist;
0088 
0089     struct Criterion {
0090         int index;
0091         Qt::SortOrder order;
0092         Qt::CaseSensitivity caseSensitivity;
0093     };
0094     QList<Criterion> m_criteria;
0095 
0096     /** sorted order - which row/column will move to where */
0097     QMap<int, int> sorted;
0098 
0099     CellStorage* m_cellStorage; // temporary
0100     QHash<Cell, Style> m_styles; // temporary
0101     QHash<Cell, QString> m_formulas; // temporary; encoded formulas
0102 };
0103 
0104 } // namespace Sheets
0105 } // namespace Calligra
0106 
0107 #endif  // CALLIGRA_SHEETS_SORT_MANIPULATOR