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