QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsbearingnumericformat.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbearingnumericformat.cpp
3  ----------------------------
4  begin : January 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgis.h"
19 
20 
22 {
23 }
24 
26 {
27  return QStringLiteral( "bearing" );
28 }
29 
31 {
32  return QObject::tr( "Bearing" );
33 }
34 
36 {
38 }
39 
41 {
42  return 270.123;
43 }
44 
45 QString QgsBearingNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
46 {
47  switch ( mDirectionFormat )
48  {
50  {
51  value = fmod( value, 360.0 );
52  if ( value > 180 )
53  value -= 360;
54 
55  QString res = QgsBasicNumericFormat::formatDouble( std::fabs( value ), context );
56 
57  if ( res != QLatin1String( "0" ) && res != QLatin1String( "180" ) )
58  // TODO also test for 0.000, 180.000, etc
59  res += QChar( 176 ) + ( value < 0 ? QObject::tr( "W" ) : QObject::tr( "E" ) );
60  else
61  res += QChar( 176 );
62 
63  return res;
64  }
65 
67  {
68  value = fmod( value, 360.0 );
69  if ( value > 180 )
70  value -= 360;
71  if ( value < -180 )
72  value += 360;
73 
74  return QgsBasicNumericFormat::formatDouble( value, context ) + QChar( 176 );
75  }
76 
77  case UseRange0To360:
78  value = fmod( value, 360.0 );
79  if ( value < 0 )
80  value += 360;
81  return QgsBasicNumericFormat::formatDouble( value, context ) + QChar( 176 );
82  }
83 
84  return QgsBasicNumericFormat::formatDouble( value, context );
85 }
86 
88 {
89  return new QgsBearingNumericFormat( *this );
90 }
91 
92 QgsNumericFormat *QgsBearingNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const
93 {
94  std::unique_ptr< QgsBearingNumericFormat > res = std::make_unique< QgsBearingNumericFormat >();
95  res->setConfiguration( configuration, context );
96  res->mDirectionFormat = static_cast< FormatDirectionOption >( configuration.value( QStringLiteral( "direction_format" ), 0 ).toInt() );
97  return res.release();
98 }
99 
101 {
102  QVariantMap res = QgsBasicNumericFormat::configuration( context );
103  res.insert( QStringLiteral( "direction_format" ), static_cast< int >( mDirectionFormat ) );
104  return res;
105 }
106 
108 {
109  return mDirectionFormat;
110 }
111 
113 {
114  mDirectionFormat = directionFormat;
115 }
116 
117 void QgsBearingNumericFormat::setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context )
118 {
120  mDirectionFormat = static_cast< FormatDirectionOption >( configuration.value( QStringLiteral( "direction_format" ), 0 ).toInt() );
121 }
virtual void setConfiguration(const QVariantMap &configuration, const QgsReadWriteContext &context)
Sets the format's configuration.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
A numeric formatter which returns a text representation of a direction/bearing.
QgsBearingNumericFormat()
Default constructor.
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
FormatDirectionOption
Directional formatting option, which controls how bearing direction is described in the returned stri...
@ UseRange0To180WithEWDirectionalSuffix
Return values between 0 and 180, with a E or W directional suffix.
@ UseRange0To360
Return values between 0 to 360.
@ UseRangeNegative180ToPositive180
Return values between -180 and 180.
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
FormatDirectionOption directionFormat() const
Returns the directional formatting option, which controls how bearing direction is described in the r...
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
void setDirectionFormat(FormatDirectionOption format)
Sets the directional formatting option, which controls how bearing direction is described in the retu...
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
QString id() const override
Returns a unique id for this numeric format.
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
void setConfiguration(const QVariantMap &configuration, const QgsReadWriteContext &context) override
Sets the format's configuration.
QgsBearingNumericFormat * clone() const override
Clones the format, returning a new object.
QString visibleName() const override
Returns the translated, user-visible name for this format.
A context for numeric formats.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual int sortKey()
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
The class is used as a container of context for various read/write operations on other objects.