QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsscalemethodwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsscalemethodwidget.cpp
3 ------------------------
4 begin : March 2025
5 copyright : (C) 2025 by Nyall Dawson
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsapplication.h"
21
22#include <QComboBox>
23#include <QHBoxLayout>
24#include <QLabel>
25#include <QString>
26
27#include "moc_qgsscalemethodwidget.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QWidget( parent )
33{
34 mCombo = new QComboBox();
35 mCombo->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
36
37 mCombo->addItem( tr( "Average Top, Middle and Bottom Scales" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalAverage ) );
38 mCombo->addItem( tr( "Calculate along Top of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalTop ) );
39 mCombo->addItem( tr( "Calculate along Middle of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalMiddle ) );
40 mCombo->addItem( tr( "Calculate along Bottom of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalBottom ) );
41 mCombo->addItem( tr( "Always Calculate at Equator" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::AtEquator ) );
42
43 QHBoxLayout *hLayout = new QHBoxLayout();
44 hLayout->setContentsMargins( 0, 0, 0, 0 );
45 hLayout->addWidget( mCombo, 1 );
46
47 // bit of fiddlyness here -- we want the initial spacing to only be visible
48 // when the warning label is shown, so it's embedded inside mWarningLabel
49 // instead of outside it
50 mWarningLabelContainer = new QWidget();
51 QHBoxLayout *warningLayout = new QHBoxLayout();
52 warningLayout->setContentsMargins( 0, 0, 0, 0 );
53 mWarningLabel = new QLabel();
54 const QIcon icon = QgsApplication::getThemeIcon( u"mIconWarning.svg"_s );
55 const int size = static_cast<int>( std::max( 24.0, mCombo->minimumSize().height() * 0.5 ) );
56 mWarningLabel->setPixmap( icon.pixmap( icon.actualSize( QSize( size, size ) ) ) );
57 const int labelMargin = static_cast<int>( std::round( mCombo->fontMetrics().horizontalAdvance( 'X' ) ) );
58 warningLayout->insertSpacing( 0, labelMargin / 2 );
59 warningLayout->insertWidget( 1, mWarningLabel );
60 mWarningLabelContainer->setLayout( warningLayout );
61 hLayout->addWidget( mWarningLabelContainer );
62 mWarningLabelContainer->hide();
63
64 setLayout( hLayout );
65
66 setFocusPolicy( Qt::FocusPolicy::StrongFocus );
67 setFocusProxy( mCombo );
68
69 connect( mCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsScaleMethodWidget::methodChanged );
70 connect( mCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsScaleMethodWidget::updateWarning );
71}
72
74{
75 return mCombo->currentData().value< Qgis::ScaleCalculationMethod >();
76}
77
79{
80 mCombo->setCurrentIndex( mCombo->findData( QVariant::fromValue( method ) ) );
81 updateWarning();
82}
83
84void QgsScaleMethodWidget::updateWarning()
85{
86 switch ( scaleMethod() )
87 {
92 mWarningLabelContainer->hide();
93 break;
94
96 {
97 mWarningLabelContainer->show();
98 const QString warning = u"<p>%1</p><p>%2</p>"_s.arg( tr( "This method will calculate misleading scales when the map extent is not close to the "
99 "equator, however it ensures that the scale remains constant and does not "
100 "change as the map is panned." ),
101 tr( "This setting is valid for maps in a geographic (latitude/longitude) CRS only." ) );
102 mWarningLabel->setToolTip( warning );
103
104 break;
105 }
106 }
107}
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5392
@ HorizontalTop
Calculate horizontally, across top of map.
Definition qgis.h:5393
@ HorizontalMiddle
Calculate horizontally, across midle of map.
Definition qgis.h:5394
@ AtEquator
Always calculate the scale at the equator, regardless of the actual visible map extent....
Definition qgis.h:5397
@ HorizontalAverage
Calculate horizontally, using the average of the top, middle and bottom scales.
Definition qgis.h:5396
@ HorizontalBottom
Calculate horizontally, across bottom of map.
Definition qgis.h:5395
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsScaleMethodWidget(QWidget *parent=nullptr)
Constructor for QgsScaleMethodWidget, with the specified parent widget.
void setScaleMethod(Qgis::ScaleCalculationMethod method)
Sets the selected blend mode.
void methodChanged()
Emitted when the selected method is changed.
Qgis::ScaleCalculationMethod scaleMethod