QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprojectionselectiondialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgenericprojectionselector.cpp
3 Set user defined CRS using projection selector widget
4 -------------------
5 begin : May 28, 2004
6 copyright : (C) 2004 by Gary E.Sherman
7 email : sherman at mrcc.com
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18#include "qgsapplication.h"
19#include "qgssettings.h"
20
22#include "moc_qgsprojectionselectiondialog.cpp"
23#include "qgshelp.h"
24#include <QDialogButtonBox>
25#include <QApplication>
26#include "qgsgui.h"
27#include <QPushButton>
28
29
30//
31// QgsCrsSelectionWidget
32//
34 : QgsPanelWidget( parent )
35{
36 setupUi( this );
37
38 projectionSelector->setFilters( filters );
39
40 //we will show this only when a message is set
41 textEdit->hide();
42
43 mNotSetText = tr( "No CRS (or unknown/non-Earth projection)" );
44 mLabelNoCrs->setText( tr( "Use this option to treat all coordinates as Cartesian coordinates in an unknown reference system." ) );
45
46 mComboCrsType->addItem( tr( "Predefined CRS" ), static_cast<int>( CrsType::Predefined ) );
47 mComboCrsType->addItem( tr( "Custom CRS" ), static_cast<int>( CrsType::Custom ) );
48
49 mStackedWidget->setCurrentWidget( mPageDatabase );
50 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
51
52 connect( mComboCrsType, qOverload<int>( &QComboBox::currentIndexChanged ), this, [=]( int ) {
53 if ( !mComboCrsType->currentData().isValid() )
54 mStackedWidget->setCurrentWidget( mPageNoCrs );
55 else
56 {
57 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
58 {
59 case QgsCrsSelectionWidget::CrsType::Predefined:
60 mStackedWidget->setCurrentWidget( mPageDatabase );
61 break;
62 case QgsCrsSelectionWidget::CrsType::Custom:
63 mStackedWidget->setCurrentWidget( mPageCustom );
64 break;
65 }
66 }
67
68 if ( !mBlockSignals )
69 {
70 emit crsChanged();
72 }
73 } );
74
75 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::projectionDoubleClicked, this, [=] {
76 emit crsDoubleClicked( projectionSelector->crs() );
77 } );
78
79 connect( mCrsDefinitionWidget, &QgsCrsDefinitionWidget::crsChanged, this, [=]() {
80 if ( !mBlockSignals )
81 {
82 emit crsChanged();
84 }
85 } );
86
87 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::crsSelected, this, [=]() {
88 if ( !mBlockSignals )
89 {
90 mDeferredInvalidCrsSet = false;
91 emit crsChanged();
93 }
94 } );
95
96 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::hasValidSelectionChanged, this, [=]() {
97 if ( !mBlockSignals )
98 {
99 emit crsChanged();
101 }
102 } );
103
104 const QgsSettings settings;
105 mSplitter->restoreState( settings.value( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ) ).toByteArray() );
106}
107
109{
110 QgsSettings settings;
111 settings.setValue( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ), mSplitter->saveState() );
112}
113
114void QgsCrsSelectionWidget::setMessage( const QString &message )
115{
116 textEdit->setHtml( QStringLiteral( "<head><style>%1</style></head><body>%2</body>" ).arg( QgsApplication::reportStyleSheet(), message ) );
117 textEdit->show();
118}
119
121{
122 if ( mShowNoCrsOption == show )
123 return;
124
125 mBlockSignals++;
126 mShowNoCrsOption = show;
127 if ( mShowNoCrsOption )
128 {
129 mComboCrsType->insertItem( 0, mNotSetText );
130 }
131 else
132 {
133 mComboCrsType->removeItem( 0 );
134 }
135
136 if ( show && mDeferredInvalidCrsSet )
137 {
138 mComboCrsType->setCurrentIndex( 0 );
139 }
140
141 mBlockSignals--;
142
143 if ( mDeferredInvalidCrsSet )
144 emit crsChanged();
145
146 mDeferredInvalidCrsSet = false;
147
149}
150
152{
153 return mShowNoCrsOption;
154}
155
156void QgsCrsSelectionWidget::setNotSetText( const QString &text, const QString &description )
157{
158 mNotSetText = text;
159
160 if ( mShowNoCrsOption )
161 {
162 mComboCrsType->setItemText( 0, mNotSetText );
163 }
164
165 mLabelNoCrs->setText( description.isEmpty() ? text : description );
166}
167
169{
170 if ( !mComboCrsType->currentData().isValid() )
171 return true;
172 else if ( mDeferredInvalidCrsSet )
173 return false;
174 else
175 {
176 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
177 {
178 case QgsCrsSelectionWidget::CrsType::Predefined:
179 return projectionSelector->hasValidSelection();
180 case QgsCrsSelectionWidget::CrsType::Custom:
181 return mCrsDefinitionWidget->crs().isValid();
182 }
184 }
185}
186
188{
189 return projectionSelector->filters();
190}
191
193{
194 projectionSelector->setFilters( filters );
195}
196
198{
199 if ( !mComboCrsType->currentData().isValid() )
201 else
202 {
203 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
204 {
205 case QgsCrsSelectionWidget::CrsType::Predefined:
206 return projectionSelector->crs();
207 case QgsCrsSelectionWidget::CrsType::Custom:
208 return mCrsDefinitionWidget->crs();
209 }
211 }
212}
213
215{
216 mBlockSignals++;
217 if ( !crs.isValid() )
218 {
219 if ( mShowNoCrsOption )
220 mComboCrsType->setCurrentIndex( 0 );
221 else
222 mDeferredInvalidCrsSet = true;
223 }
224 else
225 {
226 projectionSelector->setCrs( crs );
227 mCrsDefinitionWidget->setCrs( crs );
228 if ( crs.isValid() && crs.authid().isEmpty() )
229 {
230 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Custom ) ) );
231 mStackedWidget->setCurrentWidget( mPageCustom );
232 }
233 else
234 {
235 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
236 mStackedWidget->setCurrentWidget( mPageDatabase );
237 }
238 }
239 mBlockSignals--;
240
241 emit crsChanged();
243}
244
245void QgsCrsSelectionWidget::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
246{
247 projectionSelector->setOgcWmsCrsFilter( crsFilter );
248}
249
250
251//
252// QgsProjectionSelectionDialog
253//
254
256 : QDialog( parent, fl )
257{
258 QVBoxLayout *vlayout = new QVBoxLayout();
259
260 mCrsWidget = new QgsCrsSelectionWidget( nullptr, filters );
261 vlayout->addWidget( mCrsWidget, 1 );
262
263 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
264 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectionSelectionDialog::accept );
265 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProjectionSelectionDialog::reject );
266 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectionSelectionDialog::showHelp );
267
268 vlayout->addWidget( mButtonBox );
269
270 setLayout( vlayout );
271
273
274 //apply selected projection upon double-click on item
275 connect( mCrsWidget, &QgsCrsSelectionWidget::crsDoubleClicked, this, &QgsProjectionSelectionDialog::accept );
276}
277
278void QgsProjectionSelectionDialog::setMessage( const QString &message )
279{
280 mCrsWidget->setMessage( message );
281}
282
284{
285 setMessage( tr( "This layer appears to have no projection specification." ) + ' ' + tr( "By default, this layer will now have its projection set to that of the project, "
286 "but you may override this by selecting a different projection below." ) );
287}
288
290{
291 mCrsWidget->setShowNoCrs( show );
292}
293
295{
296 return mCrsWidget->showNoCrs();
297}
298
299void QgsProjectionSelectionDialog::setNotSetText( const QString &text, const QString &description )
300{
301 mCrsWidget->setNotSetText( text, description );
302}
303
305{
306 mRequireValidSelection = true;
307 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
308
309 connect( mCrsWidget, &QgsCrsSelectionWidget::hasValidSelectionChanged, this, [=]( bool isValid ) {
310 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
311 } );
312}
313
315{
316 return mCrsWidget->hasValidSelection();
317}
318
323
328
330{
331 return mCrsWidget->crs();
332}
333
335{
336 mCrsWidget->setCrs( crs );
337
338 if ( mRequireValidSelection )
339 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
340}
341
342void QgsProjectionSelectionDialog::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
343{
344 mCrsWidget->setOgcWmsCrsFilter( crsFilter );
345}
346
347void QgsProjectionSelectionDialog::showHelp()
348{
349 QgsHelp::openHelp( QStringLiteral( "working_with_projections/working_with_projections.html" ) );
350}
static QString reportStyleSheet(QgsApplication::StyleSheetType styleSheetType=QgsApplication::StyleSheetType::Qt)
Returns a css style sheet for reports, the styleSheetType argument determines what type of stylesheet...
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
void crsChanged()
Emitted when the CRS defined in the widget is changed.
A generic widget allowing users to pick a Coordinate Reference System (or define their own).
bool showNoCrs() const
Returns whether the "no/invalid" CRS option is shown.
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
void hasValidSelectionChanged(bool isValid)
Emitted when the widget has a valid selection or not.
QgsCrsSelectionWidget(QWidget *parent=nullptr, QgsCoordinateReferenceSystemProxyModel::Filters filters=QgsCoordinateReferenceSystemProxyModel::FilterHorizontal|QgsCoordinateReferenceSystemProxyModel::FilterCompound)
Constructor for QgsCrsSelectionWidget, with the specified parent widget.
QgsCoordinateReferenceSystemProxyModel::Filters filters() const
Returns the filters set on the available CRS.
void setFilters(QgsCoordinateReferenceSystemProxyModel::Filters filters)
Sets filters for the available CRS.
void crsDoubleClicked(const QgsCoordinateReferenceSystem &crs)
Emitted when a CRS entry in the widget is double-clicked.
void setShowNoCrs(bool show)
Sets whether a "no/invalid" CRS option should be shown.
void crsChanged()
Emitted when the CRS defined in the widget is changed.
void setMessage(const QString &message)
Sets a message to show in the dialog.
bool hasValidSelection() const
Returns true if the widget has a valid CRS defined.
void setNotSetText(const QString &text, const QString &description=QString())
Sets the text to show for the not set option.
void setOgcWmsCrsFilter(const QSet< QString > &crsFilter)
filters this dialog by the given CRSs
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs to show within the widget.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Base class for any widget that can be shown as a inline panel.
void showNoCrsForLayerMessage()
When called, the dialog will show a default "layer has no CRS set" message above the projection selec...
void setShowNoProjection(bool show)
Sets whether a "no/invalid" projection option should be shown.
bool showNoProjection() const
Returns whether the "no/invalid" projection option is shown.
QgsCoordinateReferenceSystemProxyModel::Filters filters() const
Returns the filters set on the available CRS.
bool hasValidSelection() const
Returns true if the dialog has a valid CRS defined.
void setNotSetText(const QString &text, const QString &description=QString())
Sets the text to show for the not set option.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the initial crs to show within the dialog.
void setMessage(const QString &message)
Sets a message to show in the dialog.
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
QgsProjectionSelectionDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QgsCoordinateReferenceSystemProxyModel::Filters filters=QgsCoordinateReferenceSystemProxyModel::FilterHorizontal|QgsCoordinateReferenceSystemProxyModel::FilterCompound)
Constructor for QgsProjectionSelectionDialog.
void setRequireValidSelection()
Sets the dialog to require a valid selection only, preventing users from accepting the dialog if no s...
void setOgcWmsCrsFilter(const QSet< QString > &crsFilter)
filters this dialog by the given CRSs
void setFilters(QgsCoordinateReferenceSystemProxyModel::Filters filters)
Sets filters for the available CRS.
void crsSelected()
Emitted when a projection is selected in the widget.
void projectionDoubleClicked()
Emitted when a projection is double clicked in the list.
void hasValidSelectionChanged(bool isValid)
Emitted when the selection in the tree is changed from a valid selection to an invalid selection,...
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define BUILTIN_UNREACHABLE
Definition qgis.h:6678
const QgsCoordinateReferenceSystem & crs