1717
1818#include " qgscomposerpicturewidget.h"
1919#include " qgsapplication.h"
20+ #include " qgscomposermap.h"
2021#include " qgscomposerpicture.h"
2122#include " qgscomposeritemwidget.h"
2223#include < QDoubleValidator>
@@ -34,18 +35,16 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture
3435
3536 // add widget for general composer item properties
3637 QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget ( this , picture );
37- gridLayout->addWidget ( itemPropertiesWidget, 6 , 0 , 1 , 4 );
38+ gridLayout->addWidget ( itemPropertiesWidget, 8 , 0 , 1 , 4 );
3839
3940 mWidthLineEdit ->setValidator ( new QDoubleValidator ( this ) );
4041 mHeightLineEdit ->setValidator ( new QDoubleValidator ( this ) );
41-
4242 setGuiElementValues ();
4343
4444 mPreviewListWidget ->setIconSize ( QSize ( 30 , 30 ) );
4545
4646 // add preview icons
4747 addStandardDirectoriesToPreview ();
48-
4948 connect ( mPicture , SIGNAL ( settingsChanged () ), this , SLOT ( setGuiElementValues () ) );
5049}
5150
@@ -198,6 +197,118 @@ void QgsComposerPictureWidget::on_mRemoveDirectoryButton_clicked()
198197 }
199198}
200199
200+ void QgsComposerPictureWidget::on_mRotationFromComposerMapCheckBox_stateChanged ( int state )
201+ {
202+ if ( !mPicture )
203+ {
204+ return ;
205+ }
206+
207+ if ( state == Qt::Unchecked )
208+ {
209+ mPicture ->setRotationMap ( -1 );
210+ mRotationSpinBox ->setEnabled ( true );
211+ mComposerMapComboBox ->setEnabled ( false );
212+ }
213+ else
214+ {
215+ int currentItemIndex = mComposerMapComboBox ->currentIndex ();
216+ if ( currentItemIndex == -1 )
217+ {
218+ return ;
219+ }
220+ int composerId = mComposerMapComboBox ->itemData ( currentItemIndex, Qt::UserRole ).toInt ();
221+ mPicture ->setRotationMap ( composerId );
222+ mRotationSpinBox ->setEnabled ( false );
223+ mComposerMapComboBox ->setEnabled ( true );
224+ }
225+ }
226+
227+ void QgsComposerPictureWidget::showEvent ( QShowEvent * event )
228+ {
229+ refreshMapComboBox ();
230+ QWidget::showEvent ( event );
231+ }
232+
233+ void QgsComposerPictureWidget::on_mComposerMapComboBox_activated ( const QString & text )
234+ {
235+ if ( !mPicture || text.isEmpty () || !mPicture ->useRotationMap () )
236+ {
237+ return ;
238+ }
239+
240+ // get composition
241+ const QgsComposition* composition = mPicture ->composition ();
242+ if ( !composition )
243+ {
244+ return ;
245+ }
246+
247+ // extract id
248+ int id;
249+ bool conversionOk;
250+ QStringList textSplit = text.split ( " " );
251+ if ( textSplit.size () < 1 )
252+ {
253+ return ;
254+ }
255+
256+ QString idString = textSplit.at ( textSplit.size () - 1 );
257+ id = idString.toInt ( &conversionOk );
258+
259+ if ( !conversionOk )
260+ {
261+ return ;
262+ }
263+
264+ const QgsComposerMap* composerMap = composition->getComposerMapById ( id );
265+ if ( !composerMap )
266+ {
267+ return ;
268+ }
269+ mPicture ->setRotationMap ( id );
270+ mPicture ->update ();
271+ }
272+
273+ void QgsComposerPictureWidget::refreshMapComboBox ()
274+ {
275+ mComposerMapComboBox ->blockSignals ( true );
276+ // save the current entry in case it is still present after refresh
277+ QString saveCurrentComboText = mComposerMapComboBox ->currentText ();
278+
279+ mComposerMapComboBox ->clear ();
280+
281+ if ( mPicture )
282+ {
283+ // insert available maps into mMapComboBox
284+ const QgsComposition* composition = mPicture ->composition ();
285+ if ( composition )
286+ {
287+ QList<const QgsComposerMap*> availableMaps = composition->composerMapItems ();
288+ QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin ();
289+ for ( ; mapItemIt != availableMaps.constEnd (); ++mapItemIt )
290+ {
291+ mComposerMapComboBox ->addItem ( tr ( " Map %1" ).arg (( *mapItemIt )->id () ), ( *mapItemIt )->id () );
292+ }
293+ }
294+ }
295+
296+ if ( !saveCurrentComboText.isEmpty () )
297+ {
298+ if ( mComposerMapComboBox ->findText ( saveCurrentComboText ) == -1 )
299+ {
300+ // the former entry is no longer present. Inform the scalebar about the changed composer map
301+ on_mComposerMapComboBox_activated ( mComposerMapComboBox ->currentText () );
302+ }
303+ else
304+ {
305+ // the former entry is still present. Make it the current entry again
306+ mComposerMapComboBox ->setCurrentIndex ( mComposerMapComboBox ->findText ( saveCurrentComboText ) );
307+ }
308+ }
309+ mComposerMapComboBox ->blockSignals ( false );
310+ }
311+
201312void QgsComposerPictureWidget::setGuiElementValues ()
202313{
203314 // set initial gui values
@@ -207,17 +318,43 @@ void QgsComposerPictureWidget::setGuiElementValues()
207318 mHeightLineEdit ->blockSignals ( true );
208319 mRotationSpinBox ->blockSignals ( true );
209320 mPictureLineEdit ->blockSignals ( true );
321+ mComposerMapComboBox ->blockSignals ( true );
322+ mRotationFromComposerMapCheckBox ->blockSignals ( true );
210323
211324 mPictureLineEdit ->setText ( mPicture ->pictureFile () );
212325 QRectF pictureRect = mPicture ->rect ();
213326 mWidthLineEdit ->setText ( QString::number ( pictureRect.width () ) );
214327 mHeightLineEdit ->setText ( QString::number ( pictureRect.height () ) );
215328 mRotationSpinBox ->setValue ( mPicture ->rotation () );
216329
330+ refreshMapComboBox ();
331+
332+ if ( mPicture ->useRotationMap () )
333+ {
334+ mRotationFromComposerMapCheckBox ->setCheckState ( Qt::Checked );
335+ mRotationSpinBox ->setEnabled ( false );
336+ mComposerMapComboBox ->setEnabled ( true );
337+ QString mapText = tr ( " Map %1" ).arg ( mPicture ->rotationMap () );
338+ int itemId = mComposerMapComboBox ->findText ( mapText );
339+ if ( itemId >= 0 )
340+ {
341+ mComposerMapComboBox ->setCurrentIndex ( itemId );
342+ }
343+ }
344+ else
345+ {
346+ mRotationFromComposerMapCheckBox ->setCheckState ( Qt::Unchecked );
347+ mRotationSpinBox ->setEnabled ( true );
348+ mComposerMapComboBox ->setEnabled ( false );
349+ }
350+
351+
352+ mRotationFromComposerMapCheckBox ->blockSignals ( false );
217353 mWidthLineEdit ->blockSignals ( false );
218354 mHeightLineEdit ->blockSignals ( false );
219355 mRotationSpinBox ->blockSignals ( false );
220356 mPictureLineEdit ->blockSignals ( false );
357+ mComposerMapComboBox ->blockSignals ( false );
221358 }
222359}
223360
@@ -302,7 +439,8 @@ void QgsComposerPictureWidget::addStandardDirectoriesToPreview()
302439{
303440 // list all directories in $prefix/share/qgis/svg
304441 QStringList svgPaths = QgsApplication::svgPaths ();
305- for (int i=0 ; i<svgPaths.size (); i++) {
442+ for ( int i = 0 ; i < svgPaths.size (); i++ )
443+ {
306444 QDir svgDirectory ( svgPaths[i] );
307445 if ( !svgDirectory.exists () || !svgDirectory.isReadable () )
308446 {
@@ -315,7 +453,7 @@ void QgsComposerPictureWidget::addStandardDirectoriesToPreview()
315453 {
316454 if ( addDirectoryToPreview ( dirIt->absoluteFilePath () ) == 0 )
317455 {
318- mSearchDirectoriesComboBox ->addItem ( dirIt->absoluteFilePath () );
456+ mSearchDirectoriesComboBox ->addItem ( dirIt->absoluteFilePath () );
319457 }
320458 }
321459 }
0 commit comments