File indexing completed on 2024-04-28 16:21:20

0001 /* This file is part of the KDE project
0002    Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>
0003    Copyright 1998, 1999 Torben Weis <weis@kde.org>
0004    Copyright 1999- 2006 The KSpread Team <calligra-devel@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef CALLIGRA_SHEETS_CONDITION_H
0023 #define CALLIGRA_SHEETS_CONDITION_H
0024 
0025 #include "Style.h"
0026 #include "Value.h"
0027 
0028 #include <QDomElement>
0029 #include <QLinkedList>
0030 #include <QSharedData>
0031 #include <QVariant>
0032 
0033 #include <KoXmlReader.h>
0034 
0035 class QDomDocument;
0036 class QString;
0037 class KoGenStyle;
0038 
0039 namespace Calligra
0040 {
0041 namespace Sheets
0042 {
0043 class Cell;
0044 class ValueConverter;
0045 class ValueParser;
0046 
0047 /**
0048  * \class Conditional
0049  * \ingroup Style
0050  * Conditional formatting.
0051  * Holds the actual condition and the applicable style for conditional
0052  * Cell formattings.
0053  */
0054 class CALLIGRA_SHEETS_ODF_EXPORT Conditional
0055 {
0056 public:
0057     enum Type { None, Equal, Superior, Inferior, SuperiorEqual,
0058                 InferiorEqual, Between, Different, DifferentTo,
0059                 IsTrueFormula
0060               };
0061 
0062     Value          value1;
0063     Value          value2;
0064     QString        styleName;
0065     Type           cond;
0066     QString        baseCellAddress;
0067 
0068     Conditional();
0069 
0070     bool operator==(const Conditional &other) const;
0071 };
0072 
0073 
0074 class Conditions;
0075 uint qHash(const Conditions& conditions);
0076 uint qHash(const Conditional& condition);
0077 
0078 /**
0079  * \class Conditions
0080  * \ingroup Style
0081  * Manages a set of conditions for a cell.
0082  */
0083 class CALLIGRA_SHEETS_ODF_EXPORT Conditions
0084 {
0085 public:
0086     /**
0087      * Constructor.
0088      */
0089     Conditions();
0090 
0091     /**
0092      * Copy Constructor.
0093      */
0094     Conditions(const Conditions& other);
0095 
0096     /**
0097      * Destructor.
0098      */
0099     ~Conditions();
0100 
0101     /**
0102      * \return \c true if there are no conditions defined
0103      */
0104     bool isEmpty() const;
0105 
0106     /**
0107      * \return the style that matches first (or 0 if no condition matches)
0108      */
0109     Style testConditions(const Cell &cell) const;
0110 
0111     /**
0112      * Retrieve the current list of conditions we're checking
0113      */
0114     QLinkedList<Conditional> conditionList() const;
0115 
0116     /**
0117      * Replace the current list of conditions with this new one
0118      */
0119     void setConditionList(const QLinkedList<Conditional> & list);
0120 
0121     /**
0122       * Add a new condition.
0123       */
0124     void addCondition(Conditional cond);
0125 
0126     /**
0127      * Returns an optional default style, which is returned by testConditons if none of
0128      * the conditions matches.
0129      */
0130     Style defaultStyle() const;
0131 
0132     /**
0133      * Set an optional default style. This style is returned by testConditions if none of
0134      * the conditions matches.
0135      */
0136     void setDefaultStyle(const Style& style);
0137 
0138     /**
0139      * \ingroup NativeFormat
0140      * Takes a parsed DOM element and recreates the conditions structure out of
0141      * it
0142      */
0143     void loadConditions(const KoXmlElement &element, const ValueParser *parser);
0144 
0145     /**
0146      * \ingroup NativeFormat
0147      * Saves the conditions to a DOM tree structure.
0148      * \return the DOM element for the conditions.
0149      */
0150     QDomElement saveConditions(QDomDocument &doc, ValueConverter *converter) const;
0151 
0152     /// \note implementation to make QMap happy (which is needed by RectStorage)
0153     bool operator<(const Conditions& conditions) const {
0154         return qHash(*this) < qHash(conditions);
0155     }
0156     void operator=(const Conditions& other);
0157     bool operator==(const Conditions& other) const;
0158     inline bool operator!=(const Conditions& other) const {
0159         return !operator==(other);
0160     }
0161 
0162 private:
0163     /**
0164      * Use this function to see what conditions actually apply currently
0165      *
0166      * \param condition a reference to a condition that will be set to the
0167      *                  matching condition.  If none of the conditions are true
0168      *                  then this parameter is undefined on exit (check the
0169      *                  return value).
0170      *
0171      * \return true if one of the conditions is true, false if not.
0172      */
0173     bool currentCondition(const Cell& cell, Conditional & condition) const;
0174 
0175     bool isTrueFormula(const Cell& cell, const QString& formula, const QString& baseCellAddress) const;
0176 
0177     class Private;
0178     QSharedDataPointer<Private> d;
0179 };
0180 
0181 } // namespace Sheets
0182 } // namespace Calligra
0183 
0184 Q_DECLARE_METATYPE(Calligra::Sheets::Conditions)
0185 Q_DECLARE_TYPEINFO(Calligra::Sheets::Conditions, Q_MOVABLE_TYPE);
0186 
0187 #endif // CALLIGRA_SHEETS_CONDITION_H