QGIS API Documentation 3.99.0-Master (d270888f95f)
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 ***************************************************************************/
19
20#include "qgsapplication.h"
21#include "qgsgui.h"
22#include "qgshelp.h"
23#include "qgssettings.h"
24
25#include <QApplication>
26#include <QDialogButtonBox>
27#include <QPushButton>
28#include <QString>
29
30#include "moc_qgsprojectionselectiondialog.cpp"
31
32using namespace Qt::StringLiterals;
33
34//
35// QgsCrsSelectionWidget
36//
38 : QgsPanelWidget( parent )
39{
40 setupUi( this );
41
42 projectionSelector->setFilters( filters );
43
44 //we will show this only when a message is set
45 textEdit->hide();
46
47 mNotSetText = tr( "No CRS (or unknown/non-Earth projection)" );
48 mLabelNoCrs->setText( tr( "Use this option to treat all coordinates as Cartesian coordinates in an unknown reference system." ) );
49
50 mComboCrsType->addItem( tr( "Predefined CRS" ), static_cast<int>( CrsType::Predefined ) );
51 mComboCrsType->addItem( tr( "Custom CRS" ), static_cast<int>( CrsType::Custom ) );
52
53 mStackedWidget->setCurrentWidget( mPageDatabase );
54 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
55
56 connect( mComboCrsType, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
57 if ( !mComboCrsType->currentData().isValid() )
58 mStackedWidget->setCurrentWidget( mPageNoCrs );
59 else
60 {
61 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
62 {
63 case QgsCrsSelectionWidget::CrsType::Predefined:
64 mStackedWidget->setCurrentWidget( mPageDatabase );
65 break;
66 case QgsCrsSelectionWidget::CrsType::Custom:
67 mStackedWidget->setCurrentWidget( mPageCustom );
68 break;
69 }
70 }
71
72 if ( !mBlockSignals )
73 {
74 emit crsChanged();
76 }
77 } );
78
79 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::projectionDoubleClicked, this, [this] {
80 emit crsDoubleClicked( projectionSelector->crs() );
81 } );
82
83 connect( mCrsDefinitionWidget, &QgsCrsDefinitionWidget::crsChanged, this, [this]() {
84 if ( !mBlockSignals )
85 {
86 emit crsChanged();
88 }
89 } );
90
91 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::crsSelected, this, [this]() {
92 if ( !mBlockSignals )
93 {
94 mDeferredInvalidCrsSet = false;
95 emit crsChanged();
97 }
98 } );
99
100 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::hasValidSelectionChanged, this, [this]() {
101 if ( !mBlockSignals )
102 {
103 emit crsChanged();
105 }
106 } );
107
108 const QgsSettings settings;
109 mSplitter->restoreState( settings.value( u"Windows/ProjectionSelectorDialog/splitterState"_s ).toByteArray() );
110}
111
113{
114 QgsSettings settings;
115 settings.setValue( u"Windows/ProjectionSelectorDialog/splitterState"_s, mSplitter->saveState() );
116}
117
118void QgsCrsSelectionWidget::setMessage( const QString &message )
119{
120 textEdit->setHtml( u"<head><style>%1</style></head><body>%2</body>"_s.arg( QgsApplication::reportStyleSheet(), message ) );
121 textEdit->show();
122}
123
125{
126 if ( mShowNoCrsOption == show )
127 return;
128
129 mBlockSignals++;
130 mShowNoCrsOption = show;
131 if ( mShowNoCrsOption )
132 {
133 mComboCrsType->insertItem( 0, mNotSetText );
134 }
135 else
136 {
137 mComboCrsType->removeItem( 0 );
138 }
139
140 if ( show && mDeferredInvalidCrsSet )
141 {
142 mComboCrsType->setCurrentIndex( 0 );
143 }
144
145 mBlockSignals--;
146
147 if ( mDeferredInvalidCrsSet )
148 emit crsChanged();
149
150 mDeferredInvalidCrsSet = false;
151
153}
154
156{
157 return mShowNoCrsOption;
158}
159
160void QgsCrsSelectionWidget::setNotSetText( const QString &text, const QString &description )
161{
162 mNotSetText = text;
163
164 if ( mShowNoCrsOption )
165 {
166 mComboCrsType->setItemText( 0, mNotSetText );
167 }
168
169 mLabelNoCrs->setText( description.isEmpty() ? text : description );
170}
171
173{
174 if ( !mComboCrsType->currentData().isValid() )
175 return true;
176 else if ( mDeferredInvalidCrsSet )
177 return false;
178 else
179 {
180 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
181 {
182 case QgsCrsSelectionWidget::CrsType::Predefined:
183 return projectionSelector->hasValidSelection();
184 case QgsCrsSelectionWidget::CrsType::Custom:
185 return mCrsDefinitionWidget->crs().isValid();
186 }
188 }
189}
190
192{
193 return projectionSelector->filters();
194}
195
200
202{
203 if ( !mComboCrsType->currentData().isValid() )
205 else
206 {
207 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
208 {
209 case QgsCrsSelectionWidget::CrsType::Predefined:
210 return projectionSelector->crs();
211 case QgsCrsSelectionWidget::CrsType::Custom:
212 return mCrsDefinitionWidget->crs();
213 }
215 }
216}
217
219{
220 mBlockSignals++;
221 if ( !crs.isValid() )
222 {
223 if ( mShowNoCrsOption )
224 mComboCrsType->setCurrentIndex( 0 );
225 else
226 mDeferredInvalidCrsSet = true;
227 }
228 else
229 {
230 projectionSelector->setCrs( crs );
231 mCrsDefinitionWidget->setCrs( crs );
232 if ( crs.isValid() && crs.authid().isEmpty() )
233 {
234 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Custom ) ) );
235 mStackedWidget->setCurrentWidget( mPageCustom );
236 }
237 else
238 {
239 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
240 mStackedWidget->setCurrentWidget( mPageDatabase );
241 }
242 }
243 mBlockSignals--;
244
245 emit crsChanged();
247}
248
249void QgsCrsSelectionWidget::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
250{
251 projectionSelector->setOgcWmsCrsFilter( crsFilter );
252}
253
254
255//
256// QgsProjectionSelectionDialog
257//
258
260 : QDialog( parent, fl )
261{
262 QVBoxLayout *vlayout = new QVBoxLayout();
263
264 mCrsWidget = new QgsCrsSelectionWidget( nullptr, filters );
265 vlayout->addWidget( mCrsWidget, 1 );
266
267 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
268 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectionSelectionDialog::accept );
269 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProjectionSelectionDialog::reject );
270 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectionSelectionDialog::showHelp );
271
272 vlayout->addWidget( mButtonBox );
273
274 setLayout( vlayout );
275
277
278 //apply selected projection upon double-click on item
279 connect( mCrsWidget, &QgsCrsSelectionWidget::crsDoubleClicked, this, &QgsProjectionSelectionDialog::accept );
280}
281
282void QgsProjectionSelectionDialog::setMessage( const QString &message )
283{
284 mCrsWidget->setMessage( message );
285}
286
288{
289 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, "
290 "but you may override this by selecting a different projection below." ) );
291}
292
294{
295 mCrsWidget->setShowNoCrs( show );
296}
297
299{
300 return mCrsWidget->showNoCrs();
301}
302
303void QgsProjectionSelectionDialog::setNotSetText( const QString &text, const QString &description )
304{
305 mCrsWidget->setNotSetText( text, description );
306}
307
309{
310 mRequireValidSelection = true;
311 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
312
313 connect( mCrsWidget, &QgsCrsSelectionWidget::hasValidSelectionChanged, this, [this]( bool isValid ) {
314 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
315 } );
316}
317
319{
320 return mCrsWidget->hasValidSelection();
321}
322
327
332
334{
335 return mCrsWidget->crs();
336}
337
339{
340 mCrsWidget->setCrs( crs );
341
342 if ( mRequireValidSelection )
343 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
344}
345
346void QgsProjectionSelectionDialog::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
347{
348 mCrsWidget->setOgcWmsCrsFilter( crsFilter );
349}
350
351void QgsProjectionSelectionDialog::showHelp()
352{
353 QgsHelp::openHelp( u"working_with_projections/working_with_projections.html"_s );
354}
static QString reportStyleSheet(QgsApplication::StyleSheetType styleSheetType=QgsApplication::StyleSheetType::Qt)
Returns a css style sheet for reports, the styleSheetType argument determines what type of stylesheet...
Represents a coordinate reference system (CRS).
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:224
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an 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,...
Stores settings for use within QGIS.
Definition qgssettings.h:68
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:7489