summaryrefslogtreecommitdiffstats
path: root/src/declarative/extra/qnumberformat.h
blob: 6ee333c54142b9e50873b92990821a2352bf87ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef NUMBERFORMAT_H
#define NUMBERFORMAT_H 

#include "qml.h"
#include <QtGui>


QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Declarative)

// TODO 
// be able to set Locale, instead of default system for dynamic formatting
// add currency support
// add additional syntax, extend to format scientific, percentiles, significant digits etc


class QNumberFormat : public QObject
{
        Q_OBJECT
        Q_ENUMS(NumberType)
public:
        QNumberFormat(QObject *parent=0);
        ~QNumberFormat();

        enum NumberType {
            Percent,
            Scientific,
            Currency,
            Decimal
        };

        //external property, only visible
        Q_PROPERTY(QString text READ text NOTIFY textChanged);

        //mutatable properties to modify the output (text)
        Q_PROPERTY(qreal number READ number WRITE setNumber); 
        Q_PROPERTY(QString format READ format WRITE setFormat);
        Q_PROPERTY(QLocale locale READ locale WRITE setLocale);

        //Format specific settings
        Q_PROPERTY(unsigned short groupingSeparator READ groupingSeparator WRITE setGroupingSeparator);
        Q_PROPERTY(unsigned short decimalSeperator READ decimalSeparator WRITE setDecimalSeparator);
        Q_PROPERTY(unsigned int groupingSize READ groupingSize WRITE setGroupingSize);
        Q_PROPERTY(unsigned short currencySymbol READ currencySymbol WRITE setCurrencySymbol);


        QString text() const { return _text; }

        qreal number() const { return _number; }
        void setNumber(qreal n) {
            if (_number == n)
                return;
            _number = n;
            updateText(); 
        }

        QString format() const { return _format; }
        void setFormat(const QString &format) {
            if (format.isEmpty())
                _format = QString::null;
            else if (_format == format)
                return;

            _format = format;
            updateText(); 
        }

        QLocale locale() const { return _locale; }
        void setLocale(const QLocale &locale) { _locale = locale; updateText(); }

        //Do we deal with unicode standard? or create our own
        // ### since this is the backend for the number conversions, we will use the unicode
        // the front-end will handle the QChar/QString -> short int 

        unsigned short groupingSeparator() { return _groupingSeparator.unicode(); }
        void setGroupingSeparator(unsigned short unicodeSymbol) 
        {
            _groupingSeparator = QChar(unicodeSymbol);
        }

        unsigned short decimalSeparator() { return _decimalSeparator.unicode(); }
        void setDecimalSeparator(unsigned short unicodeSymbol)
        {
            _decimalSeparator = QChar(unicodeSymbol);
        }

        unsigned short currencySymbol() { return _currencySymbol.unicode(); }
        void setCurrencySymbol(unsigned short unicodeSymbol)
        {
            _currencySymbol = QChar(unicodeSymbol);
        }

        unsigned int groupingSize() { return _groupingSize; }
        void setGroupingSize(unsigned int size)
        {
            _groupingSize = size;
        }

Q_SIGNALS:
        void textChanged();

private:
        void updateText();
        void handleFormat();
        QString formatInteger(const QString &formatInt, const QString &integer);
        QString formatDecimal(const QString &formatDec, const QString &decimal);

        qreal _number;
        NumberType _type;
        QChar _groupingSeparator;
        QChar _decimalSeparator;
        QChar _currencySymbol;
        unsigned int _groupingSize;

        QLocale _locale;
        QString _format;

        // only hooked member at the moment
        QString _text;

};
QML_DECLARE_TYPE(QNumberFormat);


QT_END_NAMESPACE

QT_END_HEADER
#endif