QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsfractionnumericformat.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfractionnumericformat.h
3 --------------------------
4 begin : March 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSFRACTIONNUMERICFORMAT_H
16#define QGSFRACTIONNUMERICFORMAT_H
17
18#include <cmath>
19
20#include "qgis.h"
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsnumericformat.h"
24
32{
33 public:
38
39 QString id() const override;
40 QString visibleName() const override;
41 int sortKey() override;
42 QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
43 QgsNumericFormat *clone() const override SIP_FACTORY;
44 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
45 QVariantMap configuration( const QgsReadWriteContext &context ) const override;
46 double suggestSampleValue() const override;
47
55
62 void setUseDedicatedUnicodeCharacters( bool enabled );
63
69 bool useUnicodeSuperSubscript() const;
70
76 void setUseUnicodeSuperSubscript( bool enabled );
77
82 bool showThousandsSeparator() const;
83
88 void setShowThousandsSeparator( bool show );
89
94 bool showPlusSign() const;
95
100 void setShowPlusSign( bool show );
101
108 QChar thousandsSeparator() const;
109
116 void setThousandsSeparator( QChar character );
117
131 static bool doubleToVulgarFraction( const double value, unsigned long long &numerator SIP_OUT, unsigned long long &denominator SIP_OUT, int &sign SIP_OUT, const double tolerance = 1e-10 )
132 {
133 sign = value < 0 ? -1 : 1;
134 double g = std::fabs( value );
135 unsigned long long a = 0;
136 unsigned long long b = 1;
137 unsigned long long c = 1;
138 unsigned long long d = 0;
139 unsigned long long s;
140 unsigned int iteration = 0;
141 do
142 {
143 s = std::floor( g );
144 numerator = a + s * c;
145 denominator = b + s * d;
146 a = c;
147 b = d;
148 c = numerator;
149 d = denominator;
150 g = 1.0 / ( g - s );
151 if ( qgsDoubleNear( static_cast< double >( sign ) * static_cast< double >( numerator ) / denominator, value, tolerance ) )
152 {
153 return true;
154 }
155 } while ( iteration++ < 100 ); // limit to 100 iterations, should be sufficient for realistic purposes
156 return false;
157 }
158
163 static QString toUnicodeSuperscript( const QString &input );
164
169 static QString toUnicodeSubscript( const QString &input );
170
171 protected:
175 virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
176
177 private:
178 bool mUseDedicatedUnicode = false;
179 bool mUseUnicodeSuperSubscript = true;
180 bool mShowThousandsSeparator = true;
181 bool mShowPlusSign = false;
182 QChar mThousandsSeparator;
183};
184
185#endif // QGSFRACTIONNUMERICFORMAT_H
void setUseUnicodeSuperSubscript(bool enabled)
Sets whether unicode superscript and subscript characters should be used, (e.g.
bool useDedicatedUnicodeCharacters() const
Returns true if dedicated unicode characters should be used, when the are available for the particula...
QChar thousandsSeparator() const
Returns any override for the thousands separator character.
void setUseDedicatedUnicodeCharacters(bool enabled)
Sets whether dedicated unicode characters should be used, when the are available for the particular f...
bool showThousandsSeparator() const
Returns true if the thousands grouping separator will be shown.
bool useUnicodeSuperSubscript() const
Returns true if unicode superscript and subscript characters should be used, (e.g.
bool showPlusSign() const
Returns true if a leading plus sign will be shown for positive values.
void setShowPlusSign(bool show)
Sets whether a leading plus sign will be shown for positive values.
void setShowThousandsSeparator(bool show)
Sets whether the thousands grouping separator will be shown.
QgsFractionNumericFormat()
Default constructor.
static bool doubleToVulgarFraction(const double value, unsigned long long &numerator, unsigned long long &denominator, int &sign, const double tolerance=1e-10)
Converts a double value to a vulgar fraction (e.g.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
void setThousandsSeparator(QChar character)
Sets an override character for the thousands separator character.
A context for numeric formats.
virtual QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const =0
Creates a new copy of the format, using the supplied configuration.
virtual QString formatDouble(double value, const QgsNumericFormatContext &context) const =0
Returns a formatted string representation of a numeric double value.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
virtual int sortKey()
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
QgsNumericFormat()=default
virtual QString visibleName() const =0
Returns the translated, user-visible name for this format.
virtual QString id() const =0
Returns a unique id for this numeric format.
virtual double suggestSampleValue() const
Returns a suggested sample value which nicely represents the current format configuration.
virtual QVariantMap configuration(const QgsReadWriteContext &context) const =0
Returns the current configuration of the formatter.
A container for the context for various read/write operations on objects.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83