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

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2004 Tomas Mecir <mecirt@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef CALLIGRA_SHEETS_VALUE_FORMATTER
0022 #define CALLIGRA_SHEETS_VALUE_FORMATTER
0023 
0024 #include <QDateTime>
0025 
0026 #include "Global.h"
0027 #include "Number.h"
0028 #include "Style.h"
0029 
0030 namespace Calligra
0031 {
0032 namespace Sheets
0033 {
0034 class CalculationSettings;
0035 class Value;
0036 class ValueConverter;
0037 
0038 /**
0039  * \ingroup Value
0040  * Generates a textual representation of a Value with a given formatting.
0041  */
0042 class CALLIGRA_SHEETS_ODF_EXPORT ValueFormatter
0043 {
0044 public:
0045     /**
0046      * Constructor.
0047      */
0048     explicit ValueFormatter(const ValueConverter* converter);
0049 
0050     /**
0051      * Returns the calculation settings this ValueFormatter uses.
0052      */
0053     const CalculationSettings* settings() const;
0054 
0055     /**
0056      * Creates a textual representation of \p value with the explicit given
0057      * formattings.
0058      * \param value the value
0059      * \param formatType the value format, e.g. number, date
0060      * \param precision the number of decimals
0061      * \param floatFormat the number format, i.e. signed/unsigned information
0062      * \param prefix the preceding text
0063      * \param postfix the subsequent text
0064      * \param currencySymbol the currency symbol
0065      * \param formatString the Qt format string
0066      * \param thousandsSep whether to use thousands separator
0067      */
0068     Value formatText(const Value& value,
0069                      Format::Type formatType, int precision = -1,
0070                      Style::FloatFormat floatFormat = Style::OnlyNegSigned,
0071                      const QString& prefix = QString(),
0072                      const QString& postfix = QString(),
0073                      const QString& currencySymbol = QString(),
0074                      const QString& formatString = QString(),
0075                      bool thousandsSep = true);
0076 
0077     /**
0078      * Creates a date format.
0079      * \param date the date
0080      * \param formatType the value format, e.g. number, date
0081      * \param formatString the Qt format string
0082      */
0083     QString dateFormat(const QDate& date, Format::Type formatType, const QString& formatString = QString() );
0084 
0085     /**
0086      * Creates a time format.
0087      * \param time the time
0088      * \param formatType the value format, e.g. number, date
0089      * \param formatString the Qt format string
0090      */
0091     QString timeFormat(const QDateTime& time, Format::Type formatType, const QString& formatString = QString() );
0092 
0093     /**
0094      * Creates a date and time format.
0095      * \param time the time
0096      * \param formatType the value format, e.g. number, date
0097      * \param formatString the Qt format string
0098      */
0099     QString dateTimeFormat(const QDateTime& time, Format::Type formatType, const QString& formatString = QString() );
0100 
0101     /**
0102      * Determines the formatting type that should be used to format this value
0103      * in a cell with a given format type
0104      * \param value the value
0105      * \param formatType the value format, e.g. number, date
0106      */
0107     Format::Type determineFormatting(const Value& value, Format::Type formatType);
0108 
0109 protected:
0110 
0111     /**
0112      * Creates a number format.
0113      * \param value the value
0114      * \param precision the number of decimals
0115      * \param formatType the value format, e.g. number, date
0116      * \param floatFormat the number format, i.e. signed/unsigned information
0117      * \param currencySymbol the currency symbol
0118      * \param formatString the Qt format string
0119      * \param thousandsSep whether to use thousands separator
0120      */
0121     QString createNumberFormat(Number value, int precision,
0122                                Format::Type formatType,
0123                                Style::FloatFormat floatFormat,
0124                                const QString& currencySymbol,
0125                                const QString& formatString,
0126                                bool thousandsSep);
0127 
0128     /**
0129      * Creates a fraction format.
0130      * \param value the value
0131      * \param formatType the value format, e.g. number, date
0132      */
0133     QString fractionFormat(Number value, Format::Type formatType);
0134 
0135     /**
0136      * Creates a complex number format.
0137      * \param value the initial value
0138      * \param precision the number of decimals
0139      * \param formatType the value format, e.g. number, date
0140      * \param floatFormat the number format, i.e. signed/unsigned information
0141      * \param currencySymbol the currency symbol
0142      * \param thousandsSep whether to use thousands separator
0143      */
0144     QString complexFormat(const Value& value, int precision,
0145                           Format::Type formatType,
0146                           Style::FloatFormat floatFormat,
0147                           const QString& currencySymbol,
0148                           bool thousandsSep);
0149 
0150     /**
0151      * Removes the trailing zeros and the decimal symbol \p decimalSymbol in
0152      * \p string , if necessary.
0153      * \return the truncated string
0154      */
0155     QString removeTrailingZeros(const QString& string, const QString& decimalSymbol);
0156 
0157 private:
0158     const ValueConverter* m_converter;
0159 };
0160 
0161 } // namespace Sheets
0162 } // namespace Calligra
0163 
0164 #endif  //CALLIGRA_SHEETS_VALUE_FORMATTER