1/* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 Copyright 2004 Tomas Mecir <mecirt@gmail.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef CALLIGRA_SHEETS_VALUE_FORMATTER
22#define CALLIGRA_SHEETS_VALUE_FORMATTER
23
24#include <QDateTime>
25
26#include "Global.h"
27#include "Number.h"
28#include "Style.h"
29
30namespace Calligra
31{
32namespace Sheets
33{
34class CalculationSettings;
35class Value;
36class ValueConverter;
37
38/**
39 * \ingroup Value
40 * Generates a textual representation of a Value with a given formatting.
41 */
42class CALLIGRA_SHEETS_ODF_EXPORT ValueFormatter
43{
44public:
45 /**
46 * Constructor.
47 */
48 explicit ValueFormatter(const ValueConverter* converter);
49
50 /**
51 * Returns the calculation settings this ValueFormatter uses.
52 */
53 const CalculationSettings* settings() const;
54
55 /**
56 * Creates a textual representation of \p value with the explicit given
57 * formattings.
58 * \param formatType the value format, e.g. number, date
59 * \param precision the number of decimals
60 * \param floatFormat the number format, i.e. signed/unsigned information
61 * \param prefix the preceding text
62 * \param postfix the subsequent text
63 * \param currencySymbol the currency symbol
64 */
65 Value formatText(const Value& value,
66 Format::Type formatType, int precision = -1,
67 Style::FloatFormat floatFormat = Style::OnlyNegSigned,
68 const QString& prefix = QString(),
69 const QString& postfix = QString(),
70 const QString& currencySymbol = QString(),
71 const QString& formatString = QString(),
72 bool thousandsSep = true);
73
74 /**
75 * Creates a date format.
76 * \param formatType the value format, e.g. number, date
77 */
78 QString dateFormat(const QDate& date, Format::Type formatType, const QString& formatString = QString() );
79
80 /**
81 * Creates a time format.
82 * \param formatType the value format, e.g. number, date
83 */
84 QString timeFormat(const QDateTime& time, Format::Type formatType, const QString& formatString = QString() );
85
86 /**
87 * Creates a date and time format.
88 * \param formatType the value format, e.g. number, date
89 */
90 QString dateTimeFormat(const QDateTime& time, Format::Type formatType, const QString& formatString = QString() );
91
92protected:
93 /**
94 * Determines the formatting type that should be used to format this value
95 * in a cell with a given format type
96 * \param formatType the value format, e.g. number, date
97 */
98 Format::Type determineFormatting(const Value& value, Format::Type formatType);
99
100 /**
101 * Creates a number format.
102 * \param precision the number of decimals
103 * \param formatType the value format, e.g. number, date
104 * \param floatFormat the number format, i.e. signed/unsigned information
105 * \param currencySymbol the currency symbol
106 */
107 QString createNumberFormat(Number value, int precision,
108 Format::Type formatType,
109 Style::FloatFormat floatFormat,
110 const QString& currencySymbol,
111 const QString& formatString,
112 bool thousandsSep);
113
114 /**
115 * Creates a fraction format.
116 * \param formatType the value format, e.g. number, date
117 */
118 QString fractionFormat(Number value, Format::Type formatType);
119
120 /**
121 * Creates a complex number format.
122 * \param precision the number of decimals
123 * \param formatType the value format, e.g. number, date
124 * \param floatFormat the number format, i.e. signed/unsigned information
125 * \param currencySymbol the currency symbol
126 */
127 QString complexFormat(const Value& value, int precision,
128 Format::Type formatType,
129 Style::FloatFormat floatFormat,
130 const QString& currencySymbol,
131 bool thousandsSep);
132
133 /**
134 * Removes the trailing zeros and the decimal symbol \p decimalSymbol in
135 * \p string , if necessary.
136 * \return the truncated string
137 */
138 QString removeTrailingZeros(const QString& string, const QString& decimalSymbol);
139
140private:
141 const ValueConverter* m_converter;
142};
143
144} // namespace Sheets
145} // namespace Calligra
146
147#endif //CALLIGRA_SHEETS_VALUE_FORMATTER
148