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();
241 currentCrsOptionText( crs ) );
242 mCrsComboBox->blockSignals(
true );
244 mCrsComboBox->blockSignals(
false );
249 if ( crsNotSetIndex >= 0 )
251 mCrsComboBox->blockSignals(
true );
252 mCrsComboBox->setCurrentIndex( crsNotSetIndex );
253 mCrsComboBox->blockSignals(
false );
258 currentCrsOptionText( crs ) );
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 Q_FOREACH (
const QString &projection, recentProjections )
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 )
QgsCoordinateReferenceSystem crs() const
Returns the CRS currently selected in the widget.
This class is a composition of two QSettings instances:
long srsid() const
Returns the internal CRS ID, if available.
void setMessage(const QString &message)
Sets a message to show in the dialog.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
A generic dialog to prompt the user for a Coordinate Reference System.
static QStringList recentProjections()
Returns a list of recently used projections.
QgsCoordinateReferenceSystem crs
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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
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).
QString authid() const
Returns the authority identifier for the CRS.
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
static QgsCoordinateReferenceSystem fromSrsId(long srsId)
Creates a CRS from a specified QGIS SRS ID.