19
19
#include " qgsprojectionselectionwidget.h"
20
20
#include " qgsapplication.h"
21
21
#include " qgsgenericprojectionselector.h"
22
+ #include " qgsproject.h"
23
+ #include < QSettings>
22
24
23
25
QgsProjectionSelectionWidget::QgsProjectionSelectionWidget ( QWidget *parent ) :
24
26
QWidget( parent )
@@ -30,9 +32,29 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
30
32
layout->setSpacing ( 0 );
31
33
setLayout ( layout );
32
34
33
- mCrsLineEdit = new QLineEdit ( tr ( " invalid projection" ), this );
34
- mCrsLineEdit ->setReadOnly ( true );
35
- layout->addWidget ( mCrsLineEdit );
35
+ mCrsComboBox = new QComboBox ( this );
36
+ mCrsComboBox ->addItem ( tr ( " invalid projection" ), QgsProjectionSelectionWidget::CurrentCrs );
37
+
38
+ if ( QgsProject::instance ()->readNumEntry ( " SpatialRefSys" , " /ProjectionsEnabled" , 0 ) )
39
+ {
40
+ // only show project CRS if OTF reprojection is enabled - otherwise the
41
+ // CRS stored in the project can be misleading
42
+ QString projectCrsString = QgsProject::instance ()->readEntry ( " SpatialRefSys" , " /ProjectCrs" );
43
+ mProjectCrs .createFromOgcWmsCrs ( projectCrsString );
44
+ addProjectCrsOption ();
45
+ }
46
+
47
+ QSettings settings;
48
+ QString defCrsString = settings.value ( " /Projections/projectDefaultCrs" , GEO_EPSG_CRS_AUTHID ).toString ();
49
+ mDefaultCrs .createFromOgcWmsCrs ( defCrsString );
50
+ if ( mDefaultCrs .authid () != mProjectCrs .authid () )
51
+ {
52
+ // only show default CRS option if it's different to the project CRS, avoids
53
+ // needlessly cluttering the widget
54
+ addDefaultCrsOption ();
55
+ }
56
+
57
+ layout->addWidget ( mCrsComboBox );
36
58
37
59
mButton = new QToolButton ( this );
38
60
mButton ->setIcon ( QgsApplication::getThemeIcon ( " mActionSetProjection.svg" ) );
@@ -43,6 +65,56 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
43
65
setFocusProxy ( mButton );
44
66
45
67
connect ( mButton , SIGNAL ( clicked () ), this , SLOT ( selectCrs () ) );
68
+ connect ( mCrsComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( comboIndexChanged ( int ) ) );
69
+ }
70
+
71
+ QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs () const
72
+ {
73
+ switch (( CrsOption )mCrsComboBox ->itemData ( mCrsComboBox ->currentIndex () ).toInt () )
74
+ {
75
+ case QgsProjectionSelectionWidget::LayerCrs:
76
+ return mLayerCrs ;
77
+ case QgsProjectionSelectionWidget::ProjectCrs:
78
+ return mProjectCrs ;
79
+ case QgsProjectionSelectionWidget::DefaultCrs:
80
+ return mDefaultCrs ;
81
+ case QgsProjectionSelectionWidget::CurrentCrs:
82
+ return mCrs ;
83
+ }
84
+ return mCrs ;
85
+ }
86
+
87
+ void QgsProjectionSelectionWidget::setOptionVisible ( const QgsProjectionSelectionWidget::CrsOption option, const bool visible )
88
+ {
89
+ int optionIndex = mCrsComboBox ->findData ( option );
90
+
91
+ if ( visible && optionIndex < 0 )
92
+ {
93
+ // add missing CRS option
94
+ switch ( option )
95
+ {
96
+ case QgsProjectionSelectionWidget::LayerCrs:
97
+ {
98
+ setLayerCrs ( mLayerCrs );
99
+ return ;
100
+ }
101
+ case QgsProjectionSelectionWidget::ProjectCrs:
102
+ {
103
+ addProjectCrsOption ();
104
+ return ;
105
+ }
106
+ case QgsProjectionSelectionWidget::DefaultCrs:
107
+ {
108
+ addDefaultCrsOption ();
109
+ return ;
110
+ }
111
+ }
112
+ }
113
+ else if ( !visible && optionIndex >= 0 )
114
+ {
115
+ // remove CRS option
116
+ mCrsComboBox ->removeItem ( optionIndex );
117
+ }
46
118
}
47
119
48
120
void QgsProjectionSelectionWidget::selectCrs ()
@@ -55,6 +127,9 @@ void QgsProjectionSelectionWidget::selectCrs()
55
127
56
128
if ( mDialog ->exec () )
57
129
{
130
+ mCrsComboBox ->blockSignals ( true );
131
+ mCrsComboBox ->setCurrentIndex ( mCrsComboBox ->findData ( QgsProjectionSelectionWidget::CurrentCrs ) );
132
+ mCrsComboBox ->blockSignals ( false );
58
133
QgsCoordinateReferenceSystem crs;
59
134
crs.createFromOgcWmsCrs ( mDialog ->selectedAuthId () );
60
135
setCrs ( crs );
@@ -66,16 +141,76 @@ void QgsProjectionSelectionWidget::selectCrs()
66
141
}
67
142
}
68
143
144
+ void QgsProjectionSelectionWidget::comboIndexChanged ( int idx )
145
+ {
146
+ switch (( CrsOption )mCrsComboBox ->itemData ( idx ).toInt () )
147
+ {
148
+ case QgsProjectionSelectionWidget::LayerCrs:
149
+ emit crsChanged ( mLayerCrs );
150
+ break ;
151
+ case QgsProjectionSelectionWidget::ProjectCrs:
152
+ emit crsChanged ( mProjectCrs );
153
+ break ;
154
+ case QgsProjectionSelectionWidget::CurrentCrs:
155
+ emit crsChanged ( mCrs );
156
+ break ;
157
+ case QgsProjectionSelectionWidget::DefaultCrs:
158
+ emit crsChanged ( mDefaultCrs );
159
+ break ;
160
+ }
161
+ }
69
162
70
163
void QgsProjectionSelectionWidget::setCrs ( const QgsCoordinateReferenceSystem& crs )
71
164
{
72
165
if ( crs.isValid () )
73
166
{
74
- mCrsLineEdit ->setText ( crs.authid () + " - " + crs.description () );
167
+ mCrsComboBox ->setItemText ( mCrsComboBox ->findData ( QgsProjectionSelectionWidget::CurrentCrs ),
168
+ tr ( " Selected CRS (%1, %2)" ).arg ( crs.authid () ).arg ( crs.description () ) );
169
+ mCrsComboBox ->blockSignals ( true );
170
+ mCrsComboBox ->setCurrentIndex ( mCrsComboBox ->findData ( QgsProjectionSelectionWidget::CurrentCrs ) );
171
+ mCrsComboBox ->blockSignals ( false );
75
172
}
76
173
else
77
174
{
78
- mCrsLineEdit ->setText ( tr ( " invalid projection" ) );
175
+ mCrsComboBox ->setItemText ( mCrsComboBox ->findData ( QgsProjectionSelectionWidget::CurrentCrs ),
176
+ tr ( " invalid projection" ) );
79
177
}
80
178
mCrs = crs;
81
179
}
180
+
181
+ void QgsProjectionSelectionWidget::setLayerCrs ( const QgsCoordinateReferenceSystem &crs )
182
+ {
183
+ int layerItemIndex = mCrsComboBox ->findData ( QgsProjectionSelectionWidget::LayerCrs );
184
+ if ( crs.isValid () )
185
+ {
186
+ if ( layerItemIndex > -1 )
187
+ {
188
+ mCrsComboBox ->setItemText ( layerItemIndex, tr ( " Layer CRS (%1, %2)" ).arg ( crs.authid () ).arg ( crs.description () ) );
189
+ }
190
+ else
191
+ {
192
+ mCrsComboBox ->addItem ( tr ( " Layer CRS (%1, %2)" ).arg ( crs.authid () ).arg ( crs.description () ), QgsProjectionSelectionWidget::LayerCrs );
193
+ }
194
+ }
195
+ else
196
+ {
197
+ if ( layerItemIndex > -1 )
198
+ {
199
+ mCrsComboBox ->removeItem ( layerItemIndex );
200
+ }
201
+ }
202
+ mLayerCrs = crs;
203
+ }
204
+
205
+ void QgsProjectionSelectionWidget::addProjectCrsOption ()
206
+ {
207
+ if ( mProjectCrs .isValid () )
208
+ {
209
+ mCrsComboBox ->addItem ( tr ( " Project CRS (%1 - %2)" ).arg ( mProjectCrs .authid () ).arg ( mProjectCrs .description () ), QgsProjectionSelectionWidget::ProjectCrs );
210
+ }
211
+ }
212
+
213
+ void QgsProjectionSelectionWidget::addDefaultCrsOption ()
214
+ {
215
+ mCrsComboBox ->addItem ( tr ( " Default CRS (%1 - %2)" ).arg ( mDefaultCrs .authid () ).arg ( mDefaultCrs .description () ), QgsProjectionSelectionWidget::DefaultCrs );
216
+ }
0 commit comments