|
20 | 20 | #include "qgscomposerlegenditemdialog.h"
|
21 | 21 | #include "qgscomposerlegendlayersdialog.h"
|
22 | 22 | #include "qgscomposeritemwidget.h"
|
| 23 | +#include "qgscomposermap.h" |
23 | 24 | #include <QFontDialog>
|
24 | 25 |
|
25 | 26 | #include "qgsapplegendinterface.h"
|
@@ -88,6 +89,17 @@ void QgsComposerLegendWidget::setGuiElements()
|
88 | 89 | {
|
89 | 90 | mCheckBoxAutoUpdate->setChecked( mLegend->model()->autoUpdate() );
|
90 | 91 | }
|
| 92 | + refreshMapComboBox(); |
| 93 | + |
| 94 | + const QgsComposerMap* map = mLegend->composerMap(); |
| 95 | + if ( map ) |
| 96 | + { |
| 97 | + mMapComboBox->setCurrentIndex( mMapComboBox->findData( map->id() ) ); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + mMapComboBox->setCurrentIndex( mMapComboBox->findData( -1 ) ); |
| 102 | + } |
91 | 103 |
|
92 | 104 | blockAllSignals( false );
|
93 | 105 | }
|
@@ -391,6 +403,43 @@ void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
|
391 | 403 | }
|
392 | 404 | }
|
393 | 405 |
|
| 406 | +void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged( int index ) |
| 407 | +{ |
| 408 | + if ( !mLegend ) |
| 409 | + { |
| 410 | + return; |
| 411 | + } |
| 412 | + |
| 413 | + QVariant itemData = mMapComboBox->itemData( index ); |
| 414 | + if ( itemData.type() == QVariant::Invalid ) |
| 415 | + { |
| 416 | + return; |
| 417 | + } |
| 418 | + |
| 419 | + const QgsComposition* comp = mLegend->composition(); |
| 420 | + if ( !comp ) |
| 421 | + { |
| 422 | + return; |
| 423 | + } |
| 424 | + |
| 425 | + int mapNr = itemData.toInt(); |
| 426 | + if ( mapNr < 0 ) |
| 427 | + { |
| 428 | + mLegend->setComposerMap( 0 ); |
| 429 | + } |
| 430 | + else |
| 431 | + { |
| 432 | + const QgsComposerMap* map = comp->getComposerMapById( mapNr ); |
| 433 | + if ( map ) |
| 434 | + { |
| 435 | + mLegend->beginCommand( tr( "Legend map changed" ) ); |
| 436 | + mLegend->setComposerMap( map ); |
| 437 | + mLegend->update(); |
| 438 | + mLegend->endCommand(); |
| 439 | + } |
| 440 | + } |
| 441 | +} |
| 442 | + |
394 | 443 | void QgsComposerLegendWidget::on_mAddToolButton_clicked()
|
395 | 444 | {
|
396 | 445 | if ( !mLegend )
|
@@ -442,16 +491,21 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
|
442 | 491 | return;
|
443 | 492 | }
|
444 | 493 |
|
445 |
| - QModelIndex currentIndex = mItemTreeView->currentIndex(); |
446 |
| - if ( !currentIndex.isValid() ) |
| 494 | + mLegend->beginCommand( "Legend item removed" ); |
| 495 | + |
| 496 | + QItemSelectionModel* selectionModel = mItemTreeView->selectionModel(); |
| 497 | + if ( !selectionModel ) |
447 | 498 | {
|
448 | 499 | return;
|
449 | 500 | }
|
450 | 501 |
|
451 |
| - QModelIndex parentIndex = currentIndex.parent(); |
| 502 | + QModelIndexList selection = selectionModel->selectedIndexes(); |
| 503 | + for ( int i = selection.size() - 1; i >= 0; --i ) |
| 504 | + { |
| 505 | + QModelIndex parentIndex = selection.at( i ).parent(); |
| 506 | + itemModel->removeRow( selection.at( i ).row(), parentIndex ); |
| 507 | + } |
452 | 508 |
|
453 |
| - mLegend->beginCommand( "Legend item removed" ); |
454 |
| - itemModel->removeRow( currentIndex.row(), parentIndex ); |
455 | 509 | mLegend->adjustBoxSize();
|
456 | 510 | mLegend->update();
|
457 | 511 | mLegend->endCommand();
|
@@ -583,4 +637,47 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
|
583 | 637 | {
|
584 | 638 | mItemTreeView->blockSignals( b );
|
585 | 639 | mCheckBoxAutoUpdate->blockSignals( b );
|
| 640 | + mMapComboBox->blockSignals( b ); |
| 641 | +} |
| 642 | + |
| 643 | +void QgsComposerLegendWidget::refreshMapComboBox() |
| 644 | +{ |
| 645 | + if ( !mLegend ) |
| 646 | + { |
| 647 | + return; |
| 648 | + } |
| 649 | + |
| 650 | + const QgsComposition* composition = mLegend->composition(); |
| 651 | + if ( !composition ) |
| 652 | + { |
| 653 | + return; |
| 654 | + } |
| 655 | + |
| 656 | + //save current entry |
| 657 | + int currentMapId = mMapComboBox->itemData( mMapComboBox->currentIndex() ).toInt(); |
| 658 | + mMapComboBox->clear(); |
| 659 | + |
| 660 | + QList<const QgsComposerMap*> availableMaps = composition->composerMapItems(); |
| 661 | + QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin(); |
| 662 | + for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt ) |
| 663 | + { |
| 664 | + mMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), ( *mapItemIt )->id() ); |
| 665 | + } |
| 666 | + mMapComboBox->addItem( tr( "None" ), -1 ); |
| 667 | + |
| 668 | + //the former entry is not there anymore |
| 669 | + int entry = mMapComboBox->findData( currentMapId ); |
| 670 | + if ( entry == -1 ) |
| 671 | + { |
| 672 | + } |
| 673 | + else |
| 674 | + { |
| 675 | + mMapComboBox->setCurrentIndex( entry ); |
| 676 | + } |
| 677 | +} |
| 678 | + |
| 679 | +void QgsComposerLegendWidget::showEvent( QShowEvent * event ) |
| 680 | +{ |
| 681 | + refreshMapComboBox(); |
| 682 | + QWidget::showEvent( event ); |
586 | 683 | }
|
0 commit comments