QGIS API Documentation 3.43.0-Master (b60ef06885e)
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
6 email : nyall.dawson@gmail.com
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#include "qgsapplication.h"
20#include "moc_qgsscalemethodwidget.cpp"
21#include <QHBoxLayout>
22#include <QComboBox>
23#include <QLabel>
24
26 : QWidget( parent )
27{
28 mCombo = new QComboBox();
29 mCombo->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
30
31 mCombo->addItem( tr( "Average Top, Middle and Bottom Scales" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalAverage ) );
32 mCombo->addItem( tr( "Calculate along Top of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalTop ) );
33 mCombo->addItem( tr( "Calculate along Middle of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalMiddle ) );
34 mCombo->addItem( tr( "Calculate along Bottom of Map" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::HorizontalBottom ) );
35 mCombo->addItem( tr( "Always Calculate at Equator" ), QVariant::fromValue( Qgis::ScaleCalculationMethod::AtEquator ) );
36
37 QHBoxLayout *hLayout = new QHBoxLayout();
38 hLayout->setContentsMargins( 0, 0, 0, 0 );
39 hLayout->addWidget( mCombo, 1 );
40
41 // bit of fiddlyness here -- we want the initial spacing to only be visible
42 // when the warning label is shown, so it's embedded inside mWarningLabel
43 // instead of outside it
44 mWarningLabelContainer = new QWidget();
45 QHBoxLayout *warningLayout = new QHBoxLayout();
46 warningLayout->setContentsMargins( 0, 0, 0, 0 );
47 mWarningLabel = new QLabel();
48 const QIcon icon = QgsApplication::getThemeIcon( QStringLiteral( "mIconWarning.svg" ) );
49 const int size = static_cast<int>( std::max( 24.0, mCombo->minimumSize().height() * 0.5 ) );
50 mWarningLabel->setPixmap( icon.pixmap( icon.actualSize( QSize( size, size ) ) ) );
51 const int labelMargin = static_cast<int>( std::round( mCombo->fontMetrics().horizontalAdvance( 'X' ) ) );
52 warningLayout->insertSpacing( 0, labelMargin / 2 );
53 warningLayout->insertWidget( 1, mWarningLabel );
54 mWarningLabelContainer->setLayout( warningLayout );
55 hLayout->addWidget( mWarningLabelContainer );
56 mWarningLabelContainer->hide();
57
58 setLayout( hLayout );
59
60 setFocusPolicy( Qt::FocusPolicy::StrongFocus );
61 setFocusProxy( mCombo );
62
63 connect( mCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsScaleMethodWidget::methodChanged );
64 connect( mCombo, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsScaleMethodWidget::updateWarning );
65}
66
68{
69 return mCombo->currentData().value< Qgis::ScaleCalculationMethod >();
70}
71
73{
74 mCombo->setCurrentIndex( mCombo->findData( QVariant::fromValue( method ) ) );
75 updateWarning();
76}
77
78void QgsScaleMethodWidget::updateWarning()
79{
80 switch ( scaleMethod() )
81 {
86 mWarningLabelContainer->hide();
87 break;
88
90 {
91 mWarningLabelContainer->show();
92 const QString warning = QStringLiteral( "<p>%1</p><p>%2</p>" ).arg( tr( "This method will calculate misleading scales when the map extent is not close to the "
93 "equator, however it ensures that the scale remains constant and does not "
94 "change as the map is panned." ),
95 tr( "This setting is valid for maps in a geographic (latitude/longitude) CRS only." ) );
96 mWarningLabel->setToolTip( warning );
97
98 break;
99 }
100 }
101}
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5080
@ HorizontalTop
Calculate horizontally, across top of map.
@ HorizontalMiddle
Calculate horizontally, across midle of map.
@ AtEquator
Always calculate the scale at the equator, regardless of the actual visible map extent....
@ HorizontalAverage
Calculate horizontally, using the average of the top, middle and bottom scales.
@ HorizontalBottom
Calculate horizontally, across bottom of map.
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