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;
173 if ( !mMessage.isEmpty() )
177 if (
optionVisible( QgsProjectionSelectionWidget::CrsOption::CrsNotSet ) )
184 mCrsComboBox->blockSignals(
true );
186 mCrsComboBox->blockSignals(
false );
193 QApplication::restoreOverrideCursor();
197 void QgsProjectionSelectionWidget::addNotSetOption()
204 void QgsProjectionSelectionWidget::comboIndexChanged(
int idx )
206 switch ( (
CrsOption )mCrsComboBox->itemData( idx ).toInt() )
222 long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
242 mCrsComboBox->blockSignals(
true );
244 mCrsComboBox->blockSignals(
false );
249 if ( crsNotSetIndex >= 0 )
251 mCrsComboBox->blockSignals(
true );
252 mCrsComboBox->setCurrentIndex( crsNotSetIndex );
253 mCrsComboBox->blockSignals(
false );
273 if ( layerItemIndex > -1 )
275 mCrsComboBox->setItemText( layerItemIndex, tr(
"Layer CRS: %1 - %2" ).arg( crs.
authid(), crs.
description() ) );
284 if ( layerItemIndex > -1 )
286 mCrsComboBox->removeItem( layerItemIndex );
292 void QgsProjectionSelectionWidget::addProjectCrsOption()
300 void QgsProjectionSelectionWidget::addDefaultCrsOption()
305 void QgsProjectionSelectionWidget::addCurrentCrsOption()
317 return tr(
"invalid projection" );
320 void QgsProjectionSelectionWidget::addRecentCrs()
324 const auto constRecentProjections = recentProjections;
325 for (
const QString &projection : constRecentProjections )
327 long srsid = projection.toLong();
330 if ( crsIsShown( srsid ) )
340 mCrsComboBox->setItemData( mCrsComboBox->count() - 1, QVariant( (
long long )srsid ), Qt::UserRole + 1 );
350 bool QgsProjectionSelectionWidget::crsIsShown(
const long srsid )
const 352 return srsid == mLayerCrs.
srsid() || srsid == mDefaultCrs.
srsid() || srsid == mProjectCrs.
srsid();
355 int QgsProjectionSelectionWidget::firstRecentCrsIndex()
const 357 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.