QGIS API Documentation 3.41.0-Master (3440c17df1d)
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//
35 : QgsPanelWidget( parent )
36{
37 setupUi( this );
38
39 projectionSelector->setFilters( filters );
40
41 //we will show this only when a message is set
42 textEdit->hide();
43
44 mNotSetText = tr( "No CRS (or unknown/non-Earth projection)" );
45 mLabelNoCrs->setText( tr( "Use this option to treat all coordinates as Cartesian coordinates in an unknown reference system." ) );
46
47 mComboCrsType->addItem( tr( "Predefined CRS" ), static_cast< int >( CrsType::Predefined ) );
48 mComboCrsType->addItem( tr( "Custom CRS" ), static_cast< int >( CrsType::Custom ) );
49
50 mStackedWidget->setCurrentWidget( mPageDatabase );
51 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast< int >( CrsType::Predefined ) ) );
52
53 connect( mComboCrsType, qOverload< int >( &QComboBox::currentIndexChanged ), this, [ = ]( int )
54 {
55 if ( !mComboCrsType->currentData().isValid() )
56 mStackedWidget->setCurrentWidget( mPageNoCrs );
57 else
58 {
59 switch ( static_cast< CrsType >( mComboCrsType->currentData().toInt() ) )
60 {
61 case QgsCrsSelectionWidget::CrsType::Predefined:
62 mStackedWidget->setCurrentWidget( mPageDatabase );
63 break;
64 case QgsCrsSelectionWidget::CrsType::Custom:
65 mStackedWidget->setCurrentWidget( mPageCustom );
66 break;
67 }
68 }
69
70 if ( !mBlockSignals )
71 {
72 emit crsChanged();
74 }
75 } );
76
77 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::projectionDoubleClicked, this, [ = ]
78 {
79 emit crsDoubleClicked( projectionSelector->crs() );
80 } );
81
82 connect( mCrsDefinitionWidget, &QgsCrsDefinitionWidget::crsChanged, this, [ = ]()
83 {
84 if ( !mBlockSignals )
85 {
86 emit crsChanged();
88 }
89 } );
90
91 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::crsSelected, this, [ = ]()
92 {
93 if ( !mBlockSignals )
94 {
95 mDeferredInvalidCrsSet = false;
96 emit crsChanged();
98 }
99 } );
100
101 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::hasValidSelectionChanged, this, [ = ]()
102 {
103 if ( !mBlockSignals )
104 {
105 emit crsChanged();
107 }
108 } );
109
110 const QgsSettings settings;
111 mSplitter->restoreState( settings.value( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ) ).toByteArray() );
112}
113
115{
116 QgsSettings settings;
117 settings.setValue( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ), mSplitter->saveState() );
118}
119
120void QgsCrsSelectionWidget::setMessage( const QString &message )
121{
122 textEdit->setHtml( QStringLiteral( "<head><style>%1</style></head><body>%2</body>" ).arg( QgsApplication::reportStyleSheet(),
123 message ) );
124 textEdit->show();
125}
126
128{
129 if ( mShowNoCrsOption == show )
130 return;
131
132 mBlockSignals++;
133 mShowNoCrsOption = show;
134 if ( mShowNoCrsOption )
135 {
136 mComboCrsType->insertItem( 0, mNotSetText );
137 }
138 else
139 {
140 mComboCrsType->removeItem( 0 );
141 }
142
143 if ( show && mDeferredInvalidCrsSet )
144 {
145 mComboCrsType->setCurrentIndex( 0 );
146 }
147
148 mBlockSignals--;
149
150 if ( mDeferredInvalidCrsSet )
151 emit crsChanged();
152
153 mDeferredInvalidCrsSet = false;
154
156}
157
159{
160 return mShowNoCrsOption;
161}
162
163void QgsCrsSelectionWidget::setNotSetText( const QString &text, const QString &description )
164{
165 mNotSetText = text;
166
167 if ( mShowNoCrsOption )
168 {
169 mComboCrsType->setItemText( 0, mNotSetText );
170 }
171
172 mLabelNoCrs->setText( description.isEmpty() ? text : description );
173}
174
176{
177 if ( !mComboCrsType->currentData().isValid() )
178 return true;
179 else if ( mDeferredInvalidCrsSet )
180 return false;
181 else
182 {
183 switch ( static_cast< CrsType >( mComboCrsType->currentData().toInt() ) )
184 {
185 case QgsCrsSelectionWidget::CrsType::Predefined:
186 return projectionSelector->hasValidSelection();
187 case QgsCrsSelectionWidget::CrsType::Custom:
188 return mCrsDefinitionWidget->crs().isValid();
189 }
191 }
192}
193
195{
196 return projectionSelector->filters();
197}
198
200{
201 projectionSelector->setFilters( filters );
202}
203
205{
206 if ( !mComboCrsType->currentData().isValid() )
208 else
209 {
210 switch ( static_cast< CrsType >( mComboCrsType->currentData().toInt() ) )
211 {
212 case QgsCrsSelectionWidget::CrsType::Predefined:
213 return projectionSelector->crs();
214 case QgsCrsSelectionWidget::CrsType::Custom:
215 return mCrsDefinitionWidget->crs();
216 }
218 }
219}
220
222{
223 mBlockSignals++;
224 if ( !crs.isValid() )
225 {
226 if ( mShowNoCrsOption )
227 mComboCrsType->setCurrentIndex( 0 );
228 else
229 mDeferredInvalidCrsSet = true;
230 }
231 else
232 {
233 projectionSelector->setCrs( crs );
234 mCrsDefinitionWidget->setCrs( crs );
235 if ( crs.isValid() && crs.authid().isEmpty() )
236 {
237 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast< int>( CrsType::Custom ) ) );
238 mStackedWidget->setCurrentWidget( mPageCustom );
239 }
240 else
241 {
242 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast< int>( CrsType::Predefined ) ) );
243 mStackedWidget->setCurrentWidget( mPageDatabase );
244 }
245 }
246 mBlockSignals--;
247
248 emit crsChanged();
250}
251
252void QgsCrsSelectionWidget::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
253{
254 projectionSelector->setOgcWmsCrsFilter( crsFilter );
255}
256
257
258
259//
260// QgsProjectionSelectionDialog
261//
262
264 Qt::WindowFlags fl, QgsCoordinateReferenceSystemProxyModel::Filters filters )
265 : QDialog( parent, fl )
266{
267 QVBoxLayout *vlayout = new QVBoxLayout();
268
269 mCrsWidget = new QgsCrsSelectionWidget( nullptr, filters );
270 vlayout->addWidget( mCrsWidget, 1 );
271
272 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
273 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectionSelectionDialog::accept );
274 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProjectionSelectionDialog::reject );
275 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectionSelectionDialog::showHelp );
276
277 vlayout->addWidget( mButtonBox );
278
279 setLayout( vlayout );
280
282
283 //apply selected projection upon double-click on item
284 connect( mCrsWidget, &QgsCrsSelectionWidget::crsDoubleClicked, this, &QgsProjectionSelectionDialog::accept );
285}
286
287void QgsProjectionSelectionDialog::setMessage( const QString &message )
288{
289 mCrsWidget->setMessage( message );
290}
291
293{
294 setMessage( tr( "This layer appears to have no projection specification." )
295 + ' '
296 + tr( "By default, this layer will now have its projection set to that of the project, "
297 "but you may override this by selecting a different projection below." ) );
298}
299
301{
302 mCrsWidget->setShowNoCrs( show );
303}
304
306{
307 return mCrsWidget->showNoCrs();
308}
309
310void QgsProjectionSelectionDialog::setNotSetText( const QString &text, const QString &description )
311{
312 mCrsWidget->setNotSetText( text, description );
313}
314
316{
317 mRequireValidSelection = true;
318 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
319
320 connect( mCrsWidget, &QgsCrsSelectionWidget::hasValidSelectionChanged, this, [ = ]( bool isValid )
321 {
322 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
323 } );
324}
325
327{
328 return mCrsWidget->hasValidSelection();
329}
330
335
340
342{
343 return mCrsWidget->crs();
344}
345
347{
348 mCrsWidget->setCrs( crs );
349
350 if ( mRequireValidSelection )
351 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
352}
353
354void QgsProjectionSelectionDialog::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
355{
356 mCrsWidget->setOgcWmsCrsFilter( crsFilter );
357}
358
359void QgsProjectionSelectionDialog::showHelp()
360{
361 QgsHelp::openHelp( QStringLiteral( "working_with_projections/working_with_projections.html" ) );
362}
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:209
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:6612
const QgsCoordinateReferenceSystem & crs