QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
29#include "moc_qgsprojectionselectiondialog.cpp"
30
31//
32// QgsCrsSelectionWidget
33//
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, [this]( int ) {
54 if ( !mComboCrsType->currentData().isValid() )
55 mStackedWidget->setCurrentWidget( mPageNoCrs );
56 else
57 {
58 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
59 {
60 case QgsCrsSelectionWidget::CrsType::Predefined:
61 mStackedWidget->setCurrentWidget( mPageDatabase );
62 break;
63 case QgsCrsSelectionWidget::CrsType::Custom:
64 mStackedWidget->setCurrentWidget( mPageCustom );
65 break;
66 }
67 }
68
69 if ( !mBlockSignals )
70 {
71 emit crsChanged();
73 }
74 } );
75
76 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::projectionDoubleClicked, this, [this] {
77 emit crsDoubleClicked( projectionSelector->crs() );
78 } );
79
80 connect( mCrsDefinitionWidget, &QgsCrsDefinitionWidget::crsChanged, this, [this]() {
81 if ( !mBlockSignals )
82 {
83 emit crsChanged();
85 }
86 } );
87
88 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::crsSelected, this, [this]() {
89 if ( !mBlockSignals )
90 {
91 mDeferredInvalidCrsSet = false;
92 emit crsChanged();
94 }
95 } );
96
97 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::hasValidSelectionChanged, this, [this]() {
98 if ( !mBlockSignals )
99 {
100 emit crsChanged();
102 }
103 } );
104
105 const QgsSettings settings;
106 mSplitter->restoreState( settings.value( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ) ).toByteArray() );
107}
108
110{
111 QgsSettings settings;
112 settings.setValue( QStringLiteral( "Windows/ProjectionSelectorDialog/splitterState" ), mSplitter->saveState() );
113}
114
115void QgsCrsSelectionWidget::setMessage( const QString &message )
116{
117 textEdit->setHtml( QStringLiteral( "<head><style>%1</style></head><body>%2</body>" ).arg( QgsApplication::reportStyleSheet(), message ) );
118 textEdit->show();
119}
120
122{
123 if ( mShowNoCrsOption == show )
124 return;
125
126 mBlockSignals++;
127 mShowNoCrsOption = show;
128 if ( mShowNoCrsOption )
129 {
130 mComboCrsType->insertItem( 0, mNotSetText );
131 }
132 else
133 {
134 mComboCrsType->removeItem( 0 );
135 }
136
137 if ( show && mDeferredInvalidCrsSet )
138 {
139 mComboCrsType->setCurrentIndex( 0 );
140 }
141
142 mBlockSignals--;
143
144 if ( mDeferredInvalidCrsSet )
145 emit crsChanged();
146
147 mDeferredInvalidCrsSet = false;
148
150}
151
153{
154 return mShowNoCrsOption;
155}
156
157void QgsCrsSelectionWidget::setNotSetText( const QString &text, const QString &description )
158{
159 mNotSetText = text;
160
161 if ( mShowNoCrsOption )
162 {
163 mComboCrsType->setItemText( 0, mNotSetText );
164 }
165
166 mLabelNoCrs->setText( description.isEmpty() ? text : description );
167}
168
170{
171 if ( !mComboCrsType->currentData().isValid() )
172 return true;
173 else if ( mDeferredInvalidCrsSet )
174 return false;
175 else
176 {
177 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
178 {
179 case QgsCrsSelectionWidget::CrsType::Predefined:
180 return projectionSelector->hasValidSelection();
181 case QgsCrsSelectionWidget::CrsType::Custom:
182 return mCrsDefinitionWidget->crs().isValid();
183 }
185 }
186}
187
189{
190 return projectionSelector->filters();
191}
192
197
199{
200 if ( !mComboCrsType->currentData().isValid() )
202 else
203 {
204 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
205 {
206 case QgsCrsSelectionWidget::CrsType::Predefined:
207 return projectionSelector->crs();
208 case QgsCrsSelectionWidget::CrsType::Custom:
209 return mCrsDefinitionWidget->crs();
210 }
212 }
213}
214
216{
217 mBlockSignals++;
218 if ( !crs.isValid() )
219 {
220 if ( mShowNoCrsOption )
221 mComboCrsType->setCurrentIndex( 0 );
222 else
223 mDeferredInvalidCrsSet = true;
224 }
225 else
226 {
227 projectionSelector->setCrs( crs );
228 mCrsDefinitionWidget->setCrs( crs );
229 if ( crs.isValid() && crs.authid().isEmpty() )
230 {
231 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Custom ) ) );
232 mStackedWidget->setCurrentWidget( mPageCustom );
233 }
234 else
235 {
236 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
237 mStackedWidget->setCurrentWidget( mPageDatabase );
238 }
239 }
240 mBlockSignals--;
241
242 emit crsChanged();
244}
245
246void QgsCrsSelectionWidget::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
247{
248 projectionSelector->setOgcWmsCrsFilter( crsFilter );
249}
250
251
252//
253// QgsProjectionSelectionDialog
254//
255
257 : QDialog( parent, fl )
258{
259 QVBoxLayout *vlayout = new QVBoxLayout();
260
261 mCrsWidget = new QgsCrsSelectionWidget( nullptr, filters );
262 vlayout->addWidget( mCrsWidget, 1 );
263
264 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
265 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectionSelectionDialog::accept );
266 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProjectionSelectionDialog::reject );
267 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectionSelectionDialog::showHelp );
268
269 vlayout->addWidget( mButtonBox );
270
271 setLayout( vlayout );
272
274
275 //apply selected projection upon double-click on item
276 connect( mCrsWidget, &QgsCrsSelectionWidget::crsDoubleClicked, this, &QgsProjectionSelectionDialog::accept );
277}
278
279void QgsProjectionSelectionDialog::setMessage( const QString &message )
280{
281 mCrsWidget->setMessage( message );
282}
283
285{
286 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, "
287 "but you may override this by selecting a different projection below." ) );
288}
289
291{
292 mCrsWidget->setShowNoCrs( show );
293}
294
296{
297 return mCrsWidget->showNoCrs();
298}
299
300void QgsProjectionSelectionDialog::setNotSetText( const QString &text, const QString &description )
301{
302 mCrsWidget->setNotSetText( text, description );
303}
304
306{
307 mRequireValidSelection = true;
308 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
309
310 connect( mCrsWidget, &QgsCrsSelectionWidget::hasValidSelectionChanged, this, [this]( bool isValid ) {
311 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
312 } );
313}
314
316{
317 return mCrsWidget->hasValidSelection();
318}
319
324
329
331{
332 return mCrsWidget->crs();
333}
334
336{
337 mCrsWidget->setCrs( crs );
338
339 if ( mRequireValidSelection )
340 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
341}
342
343void QgsProjectionSelectionDialog::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
344{
345 mCrsWidget->setOgcWmsCrsFilter( crsFilter );
346}
347
348void QgsProjectionSelectionDialog::showHelp()
349{
350 QgsHelp::openHelp( QStringLiteral( "working_with_projections/working_with_projections.html" ) );
351}
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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
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:65
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:7208