16 #include <QHBoxLayout> 29 QHBoxLayout *layout =
new QHBoxLayout();
30 layout->setContentsMargins( 0, 0, 0, 0 );
31 layout->setSpacing( 6 );
34 mCrsComboBox =
new QComboBox(
this );
36 mCrsComboBox->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
39 addProjectCrsOption();
47 addDefaultCrsOption();
52 layout->addWidget( mCrsComboBox );
54 mButton =
new QToolButton(
this );
56 mButton->setToolTip( tr(
"Select CRS" ) );
57 layout->addWidget( mButton );
59 setFocusPolicy( Qt::StrongFocus );
60 setFocusProxy( mButton );
63 connect( mCrsComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsProjectionSelectionWidget::comboIndexChanged );
68 switch ( (
CrsOption )mCrsComboBox->currentData().toInt() )
80 long srsid = mCrsComboBox->currentData( Qt::UserRole + 1 ).toLongLong();
92 int optionIndex = mCrsComboBox->findData( option );
94 if ( visible && optionIndex < 0 )
106 addProjectCrsOption();
111 addDefaultCrsOption();
116 addCurrentCrsOption();
136 else if ( !visible && optionIndex >= 0 )
139 mCrsComboBox->removeItem( optionIndex );
151 int optionIndex = mCrsComboBox->findData(
CrsNotSet );
152 if ( optionIndex >= 0 )
154 mCrsComboBox->setItemText( optionIndex, mNotSetText );
165 int optionIndex = mCrsComboBox->findData( option );
166 return optionIndex >= 0;
176 if (
optionVisible( QgsProjectionSelectionWidget::CrsOption::CrsNotSet ) )
183 mCrsComboBox->blockSignals(
true );
185 mCrsComboBox->blockSignals(
false );
192 QApplication::restoreOverrideCursor();
196 void QgsProjectionSelectionWidget::addNotSetOption()
203 void QgsProjectionSelectionWidget::comboIndexChanged(
int idx )
205 switch ( (
CrsOption )mCrsComboBox->itemData( idx ).toInt() )
221 long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
241 mCrsComboBox->blockSignals(
true );
243 mCrsComboBox->blockSignals(
false );
248 if ( crsNotSetIndex >= 0 )
250 mCrsComboBox->blockSignals(
true );
251 mCrsComboBox->setCurrentIndex( crsNotSetIndex );
252 mCrsComboBox->blockSignals(
false );
272 if ( layerItemIndex > -1 )
274 mCrsComboBox->setItemText( layerItemIndex, tr(
"Layer CRS: %1 - %2" ).arg( crs.
authid(), crs.
description() ) );
283 if ( layerItemIndex > -1 )
285 mCrsComboBox->removeItem( layerItemIndex );
291 void QgsProjectionSelectionWidget::addProjectCrsOption()
299 void QgsProjectionSelectionWidget::addDefaultCrsOption()
304 void QgsProjectionSelectionWidget::addCurrentCrsOption()
316 return tr(
"invalid projection" );
319 void QgsProjectionSelectionWidget::addRecentCrs()
323 const auto constRecentProjections = recentProjections;
324 for (
const QString &projection : constRecentProjections )
326 long srsid = projection.toLong();
329 if ( crsIsShown( srsid ) )
339 mCrsComboBox->setItemData( mCrsComboBox->count() - 1, QVariant( (
long long )srsid ), Qt::UserRole + 1 );
349 bool QgsProjectionSelectionWidget::crsIsShown(
const long srsid )
const 351 return srsid == mLayerCrs.
srsid() || srsid == mDefaultCrs.
srsid() || srsid == mProjectCrs.
srsid();
354 int QgsProjectionSelectionWidget::firstRecentCrsIndex()
const 356 for (
int i = 0; i < mCrsComboBox->count(); ++i )
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setMessage(const QString &message)
Sets a message to show in the dialog.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void setShowNoProjection(bool show)
Sets whether a "no/invalid" projection option should be shown.
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
A generic dialog to prompt the user for a Coordinate Reference System.
static QStringList recentProjections()
Returns a list of recently used projections.
QgsCoordinateReferenceSystem crs
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the initial crs to show within the dialog.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class represents a coordinate reference system (CRS).
long srsid() const
Returns the internal CRS ID, if available.
QString authid() const
Returns the authority identifier for the CRS.
static QgsCoordinateReferenceSystem fromSrsId(long srsId)
Creates a CRS from a specified QGIS SRS ID.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.