QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
34
39
40 QString id() const override;
41 QString visibleName() const override;
42 int sortKey() override;
43 QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
44 QgsNumericFormat *clone() const override SIP_FACTORY;
45 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
46 QVariantMap configuration( const QgsReadWriteContext &context ) const override;
47 double suggestSampleValue() const override;
48
56
63 void setUseDedicatedUnicodeCharacters( bool enabled );
64
70 bool useUnicodeSuperSubscript() const;
71
77 void setUseUnicodeSuperSubscript( bool enabled );
78
83 bool showThousandsSeparator() const;
84
89 void setShowThousandsSeparator( bool show );
90
95 bool showPlusSign() const;
96
101 void setShowPlusSign( bool show );
102
109 QChar thousandsSeparator() const;
110
117 void setThousandsSeparator( QChar character );
118
132 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 )
133 {
134 sign = value < 0 ? -1 : 1;
135 double g = std::fabs( value );
136 unsigned long long a = 0;
137 unsigned long long b = 1;
138 unsigned long long c = 1;
139 unsigned long long d = 0;
140 unsigned long long s;
141 unsigned int iteration = 0;
142 do
143 {
144 s = std::floor( g );
145 numerator = a + s * c;
146 denominator = b + s * d;
147 a = c;
148 b = d;
149 c = numerator;
150 d = denominator;
151 g = 1.0 / ( g - s );
152 if ( qgsDoubleNear( static_cast< double >( sign )*static_cast< double >( numerator ) / denominator, value, tolerance ) )
153 {
154 return true;
155 }
156 }
157 while ( iteration++ < 100 ); // limit to 100 iterations, should be sufficient for realistic purposes
158 return false;
159 }
160
165 static QString toUnicodeSuperscript( const QString &input );
166
171 static QString toUnicodeSubscript( const QString &input );
172
173 protected:
174
178 virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
179
180 private:
181
182 bool mUseDedicatedUnicode = false;
183 bool mUseUnicodeSuperSubscript = true;
184 bool mShowThousandsSeparator = true;
185 bool mShowPlusSign = false;
186 QChar mThousandsSeparator;
187};
188
189#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:6607
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:84