QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgis.h"
21 #include "qgsnumericformat.h"
22 #include <cmath>
23 
30 class CORE_EXPORT QgsFractionNumericFormat : public QgsNumericFormat
31 {
32  public:
33 
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 
54  bool useDedicatedUnicodeCharacters() const;
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  }
156  while ( iteration++ < 100 ); // limit to 100 iterations, should be sufficient for realistic purposes
157  return false;
158  }
159 
164  static QString toUnicodeSuperscript( const QString &input );
165 
170  static QString toUnicodeSubscript( const QString &input );
171 
172  protected:
173 
177  virtual void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
178 
179  private:
180 
181  bool mUseDedicatedUnicode = false;
182  bool mUseUnicodeSuperSubscript = true;
183  bool mShowThousandsSeparator = true;
184  bool mShowPlusSign = false;
185  QChar mThousandsSeparator;
186 };
187 
188 #endif // QGSFRACTIONNUMERICFORMAT_H
A numeric formatter which returns a vulgar fractional representation of a decimal value (e....
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.
A context for numeric formats.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual QString formatDouble(double value, const QgsNumericFormatContext &context) const =0
Returns a formatted string representation of a numeric double value.
virtual int sortKey()
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
virtual QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const =0
Creates a new copy of the format, using the supplied configuration.
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.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
The class is used as a container of context for various read/write operations on other 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:598
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76