QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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] { emit crsDoubleClicked( projectionSelector->crs() ); } );
80
81 connect( mCrsDefinitionWidget, &QgsCrsDefinitionWidget::crsChanged, this, [this]() {
82 if ( !mBlockSignals )
83 {
84 emit crsChanged();
86 }
87 } );
88
89 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::crsSelected, this, [this]() {
90 if ( !mBlockSignals )
91 {
92 mDeferredInvalidCrsSet = false;
93 emit crsChanged();
95 }
96 } );
97
98 connect( projectionSelector, &QgsProjectionSelectionTreeWidget::hasValidSelectionChanged, this, [this]() {
99 if ( !mBlockSignals )
100 {
101 emit crsChanged();
103 }
104 } );
105
106 const QgsSettings settings;
107 mSplitter->restoreState( settings.value( u"Windows/ProjectionSelectorDialog/splitterState"_s ).toByteArray() );
108}
109
111{
112 QgsSettings settings;
113 settings.setValue( u"Windows/ProjectionSelectorDialog/splitterState"_s, mSplitter->saveState() );
114}
115
116void QgsCrsSelectionWidget::setMessage( const QString &message )
117{
118 textEdit->setHtml( u"<head><style>%1</style></head><body>%2</body>"_s.arg( QgsApplication::reportStyleSheet(), message ) );
119 textEdit->show();
120}
121
123{
124 if ( mShowNoCrsOption == show )
125 return;
126
127 mBlockSignals++;
128 mShowNoCrsOption = show;
129 if ( mShowNoCrsOption )
130 {
131 mComboCrsType->insertItem( 0, mNotSetText );
132 }
133 else
134 {
135 mComboCrsType->removeItem( 0 );
136 }
137
138 if ( show && mDeferredInvalidCrsSet )
139 {
140 mComboCrsType->setCurrentIndex( 0 );
141 }
142
143 mBlockSignals--;
144
145 if ( mDeferredInvalidCrsSet )
146 emit crsChanged();
147
148 mDeferredInvalidCrsSet = false;
149
151}
152
154{
155 return mShowNoCrsOption;
156}
157
158void QgsCrsSelectionWidget::setNotSetText( const QString &text, const QString &description )
159{
160 mNotSetText = text;
161
162 if ( mShowNoCrsOption )
163 {
164 mComboCrsType->setItemText( 0, mNotSetText );
165 }
166
167 mLabelNoCrs->setText( description.isEmpty() ? text : description );
168}
169
171{
172 if ( !mComboCrsType->currentData().isValid() )
173 return true;
174 else if ( mDeferredInvalidCrsSet )
175 return false;
176 else
177 {
178 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
179 {
180 case QgsCrsSelectionWidget::CrsType::Predefined:
181 return projectionSelector->hasValidSelection();
182 case QgsCrsSelectionWidget::CrsType::Custom:
183 return mCrsDefinitionWidget->crs().isValid();
184 }
186 }
187}
188
190{
191 return projectionSelector->filters();
192}
193
198
200{
201 if ( !mComboCrsType->currentData().isValid() )
203 else
204 {
205 switch ( static_cast<CrsType>( mComboCrsType->currentData().toInt() ) )
206 {
207 case QgsCrsSelectionWidget::CrsType::Predefined:
208 return projectionSelector->crs();
209 case QgsCrsSelectionWidget::CrsType::Custom:
210 return mCrsDefinitionWidget->crs();
211 }
213 }
214}
215
217{
218 mBlockSignals++;
219 if ( !crs.isValid() )
220 {
221 if ( mShowNoCrsOption )
222 mComboCrsType->setCurrentIndex( 0 );
223 else
224 mDeferredInvalidCrsSet = true;
225 }
226 else
227 {
228 projectionSelector->setCrs( crs );
229 mCrsDefinitionWidget->setCrs( crs );
230 if ( crs.isValid() && crs.authid().isEmpty() )
231 {
232 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Custom ) ) );
233 mStackedWidget->setCurrentWidget( mPageCustom );
234 }
235 else
236 {
237 mComboCrsType->setCurrentIndex( mComboCrsType->findData( static_cast<int>( CrsType::Predefined ) ) );
238 mStackedWidget->setCurrentWidget( mPageDatabase );
239 }
240 }
241 mBlockSignals--;
242
243 emit crsChanged();
245}
246
247void QgsCrsSelectionWidget::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
248{
249 projectionSelector->setOgcWmsCrsFilter( crsFilter );
250}
251
252
253//
254// QgsProjectionSelectionDialog
255//
256
258 : QDialog( parent, fl )
259{
260 QVBoxLayout *vlayout = new QVBoxLayout();
261
262 mCrsWidget = new QgsCrsSelectionWidget( nullptr, filters );
263 vlayout->addWidget( mCrsWidget, 1 );
264
265 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
266 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsProjectionSelectionDialog::accept );
267 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProjectionSelectionDialog::reject );
268 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProjectionSelectionDialog::showHelp );
269
270 vlayout->addWidget( mButtonBox );
271
272 setLayout( vlayout );
273
275
276 //apply selected projection upon double-click on item
277 connect( mCrsWidget, &QgsCrsSelectionWidget::crsDoubleClicked, this, &QgsProjectionSelectionDialog::accept );
278}
279
280void QgsProjectionSelectionDialog::setMessage( const QString &message )
281{
282 mCrsWidget->setMessage( message );
283}
284
286{
288 tr( "This layer appears to have no projection specification." )
289 + ' '
290 + tr(
291 "By default, this layer will now have its projection set to that of the project, "
292 "but you may override this by selecting a different projection below."
293 )
294 );
295}
296
298{
299 mCrsWidget->setShowNoCrs( show );
300}
301
303{
304 return mCrsWidget->showNoCrs();
305}
306
307void QgsProjectionSelectionDialog::setNotSetText( const QString &text, const QString &description )
308{
309 mCrsWidget->setNotSetText( text, description );
310}
311
313{
314 mRequireValidSelection = true;
315 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
316
317 connect( mCrsWidget, &QgsCrsSelectionWidget::hasValidSelectionChanged, this, [this]( bool isValid ) { mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid ); } );
318}
319
321{
322 return mCrsWidget->hasValidSelection();
323}
324
329
334
336{
337 return mCrsWidget->crs();
338}
339
341{
342 mCrsWidget->setCrs( crs );
343
344 if ( mRequireValidSelection )
345 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( hasValidSelection() );
346}
347
348void QgsProjectionSelectionDialog::setOgcWmsCrsFilter( const QSet<QString> &crsFilter )
349{
350 mCrsWidget->setOgcWmsCrsFilter( crsFilter );
351}
352
353void QgsProjectionSelectionDialog::showHelp()
354{
355 QgsHelp::openHelp( u"working_with_projections/working_with_projections.html"_s );
356}
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:7540