QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 {
37  return DEFAULT_SORT_KEY;
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 }
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsBearingNumericFormat::FormatDirectionOption
FormatDirectionOption
Directional formatting option, which controls how bearing direction is described in the returned stri...
Definition: qgsbearingnumericformat.h:49
QgsBearingNumericFormat::directionFormat
FormatDirectionOption directionFormat() const
Returns the directional formatting option, which controls how bearing direction is described in the r...
Definition: qgsbearingnumericformat.cpp:107
QgsBearingNumericFormat::visibleName
QString visibleName() const override
Returns the translated, user-visible name for this format.
Definition: qgsbearingnumericformat.cpp:30
qgis.h
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:259
QgsBearingNumericFormat::suggestSampleValue
double suggestSampleValue() const override
Returns a suggested sample value which nicely represents the current format configuration.
Definition: qgsbearingnumericformat.cpp:40
QgsBearingNumericFormat::id
QString id() const override
Returns a unique id for this numeric format.
Definition: qgsbearingnumericformat.cpp:25
qgsbearingnumericformat.h
QgsBearingNumericFormat::UseRangeNegative180ToPositive180
@ UseRangeNegative180ToPositive180
Return values between -180 and 180.
Definition: qgsbearingnumericformat.h:65
QgsBearingNumericFormat::clone
QgsBearingNumericFormat * clone() const override
Clones the format, returning a new object.
Definition: qgsbearingnumericformat.cpp:87
QgsBearingNumericFormat::sortKey
int sortKey() override
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
Definition: qgsbearingnumericformat.cpp:35
QgsBasicNumericFormat::setConfiguration
virtual void setConfiguration(const QVariantMap &configuration, const QgsReadWriteContext &context)
Sets the format's configuration.
Definition: qgsbasicnumericformat.cpp:163
QgsBearingNumericFormat::create
QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const override
Creates a new copy of the format, using the supplied configuration.
Definition: qgsbearingnumericformat.cpp:92
QgsBearingNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition: qgsbearingnumericformat.cpp:45
QgsBearingNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgsbearingnumericformat.cpp:100
QgsBearingNumericFormat::UseRange0To180WithEWDirectionalSuffix
@ UseRange0To180WithEWDirectionalSuffix
Return values between 0 and 180, with a E or W directional suffix.
Definition: qgsbearingnumericformat.h:64
QgsBasicNumericFormat::configuration
QVariantMap configuration(const QgsReadWriteContext &context) const override
Returns the current configuration of the formatter.
Definition: qgsbasicnumericformat.cpp:150
QgsBasicNumericFormat::formatDouble
QString formatDouble(double value, const QgsNumericFormatContext &context) const override
Returns a formatted string representation of a numeric double value.
Definition: qgsbasicnumericformat.cpp:59
QgsBearingNumericFormat::UseRange0To360
@ UseRange0To360
Return values between 0 to 360.
Definition: qgsbearingnumericformat.h:66
QgsBearingNumericFormat::setConfiguration
void setConfiguration(const QVariantMap &configuration, const QgsReadWriteContext &context) override
Sets the format's configuration.
Definition: qgsbearingnumericformat.cpp:117
QgsNumericFormat::DEFAULT_SORT_KEY
static constexpr int DEFAULT_SORT_KEY
Definition: qgsnumericformat.h:354
QgsBearingNumericFormat
A numeric formatter which returns a text representation of a direction/bearing.
Definition: qgsbearingnumericformat.h:28
QgsBearingNumericFormat::setDirectionFormat
void setDirectionFormat(FormatDirectionOption format)
Sets the directional formatting option, which controls how bearing direction is described in the retu...
Definition: qgsbearingnumericformat.cpp:112
QgsNumericFormatContext
A context for numeric formats.
Definition: qgsnumericformat.h:34
QgsBearingNumericFormat::QgsBearingNumericFormat
QgsBearingNumericFormat()
Default constructor.
Definition: qgsbearingnumericformat.cpp:21