Skip to content

Commit 92df029

Browse files
committed
resolve another conflict
1 parent 86e63db commit 92df029

7 files changed

+195
-17
lines changed

src/app/composer/qgscomposerlegendwidget.cpp

+102-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgscomposerlegenditemdialog.h"
2121
#include "qgscomposerlegendlayersdialog.h"
2222
#include "qgscomposeritemwidget.h"
23+
#include "qgscomposermap.h"
2324
#include <QFontDialog>
2425

2526
#include "qgsapplegendinterface.h"
@@ -88,6 +89,17 @@ void QgsComposerLegendWidget::setGuiElements()
8889
{
8990
mCheckBoxAutoUpdate->setChecked( mLegend->model()->autoUpdate() );
9091
}
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+
}
91103

92104
blockAllSignals( false );
93105
}
@@ -391,6 +403,43 @@ void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
391403
}
392404
}
393405

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+
394443
void QgsComposerLegendWidget::on_mAddToolButton_clicked()
395444
{
396445
if ( !mLegend )
@@ -442,16 +491,21 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
442491
return;
443492
}
444493

445-
QModelIndex currentIndex = mItemTreeView->currentIndex();
446-
if ( !currentIndex.isValid() )
494+
mLegend->beginCommand( "Legend item removed" );
495+
496+
QItemSelectionModel* selectionModel = mItemTreeView->selectionModel();
497+
if ( !selectionModel )
447498
{
448499
return;
449500
}
450501

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+
}
452508

453-
mLegend->beginCommand( "Legend item removed" );
454-
itemModel->removeRow( currentIndex.row(), parentIndex );
455509
mLegend->adjustBoxSize();
456510
mLegend->update();
457511
mLegend->endCommand();
@@ -583,4 +637,47 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
583637
{
584638
mItemTreeView->blockSignals( b );
585639
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 );
586683
}

src/app/composer/qgscomposerlegendwidget.h

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
5151
void on_mItemFontButton_clicked();
5252
void on_mBoxSpaceSpinBox_valueChanged( double d );
5353
void on_mCheckBoxAutoUpdate_stateChanged( int state );
54+
void on_mMapComboBox_currentIndexChanged( int index );
5455

5556
//item manipulation
5657
void on_mMoveDownToolButton_clicked();
@@ -62,13 +63,17 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
6263
void on_mUpdateAllPushButton_clicked();
6364
void on_mAddGroupButton_clicked();
6465

66+
protected:
67+
void showEvent( QShowEvent * event );
68+
6569
private slots:
6670
/**Sets GUI according to state of mLegend*/
6771
void setGuiElements();
6872

6973
private:
7074
QgsComposerLegendWidget();
7175
void blockAllSignals( bool b );
76+
void refreshMapComboBox();
7277

7378

7479
QgsComposerLegend* mLegend;

src/core/composer/qgscomposerlegend.cpp

+57-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposerlegend.h"
1919
#include "qgscomposerlegenditem.h"
20+
#include "qgscomposermap.h"
2021
#include "qgsmaplayer.h"
2122
#include "qgsmaplayerregistry.h"
2223
#include "qgsmaprenderer.h"
@@ -33,7 +34,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
3334
, mBoxSpace( 2 )
3435
, mLayerSpace( 2 )
3536
, mSymbolSpace( 2 )
36-
, mIconLabelSpace( 2 )
37+
, mIconLabelSpace( 2 ), mComposerMap( 0 )
3738
{
3839
//QStringList idList = layerIdList();
3940
//mLegendModel.setLayerSet( idList );
@@ -50,7 +51,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
5051
connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
5152
}
5253

53-
QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 )
54+
QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 ), mComposerMap( 0 )
5455
{
5556

5657
}
@@ -390,23 +391,49 @@ void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double curren
390391
rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
391392
}
392393

394+
//consider relation to composer map for symbol sizes in mm
395+
bool sizeInMapUnits = s->outputUnit() == QgsSymbolV2::MapUnit;
396+
double mmPerMapUnit = 1;
397+
if ( mComposerMap )
398+
{
399+
mmPerMapUnit = mComposerMap->mapUnitsToMM();
400+
}
401+
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
402+
393403
//Consider symbol size for point markers
394404
double height = mSymbolHeight;
395405
double width = mSymbolWidth;
396-
if ( s->type() == QgsSymbolV2::Marker )
406+
double size = 0;
407+
408+
if ( markerSymbol )
397409
{
398-
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
399-
if ( markerSymbol )
410+
size = markerSymbol->size();
411+
height = size;
412+
width = size;
413+
if ( mComposerMap && sizeInMapUnits )
400414
{
401-
height = markerSymbol->size();
402-
width = markerSymbol->size();
415+
height *= mmPerMapUnit;
416+
width *= mmPerMapUnit;
417+
markerSymbol->setSize( width );
403418
}
404419
}
405420

406421
p->save();
407422
p->translate( currentXPosition, currentYCoord );
408423
p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
424+
425+
if ( markerSymbol && sizeInMapUnits )
426+
{
427+
s->setOutputUnit( QgsSymbolV2::MM );
428+
}
409429
s->drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) );
430+
431+
if ( markerSymbol && sizeInMapUnits )
432+
{
433+
s->setOutputUnit( QgsSymbolV2::MapUnit );
434+
markerSymbol->setSize( size );
435+
}
436+
410437
p->restore();
411438
currentXPosition += width;
412439
symbolHeight = height;
@@ -605,6 +632,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
605632
composerLegendElem.setAttribute( "symbolWidth", mSymbolWidth );
606633
composerLegendElem.setAttribute( "symbolHeight", mSymbolHeight );
607634

635+
if ( mComposerMap )
636+
{
637+
composerLegendElem.setAttribute( "map", mComposerMap->id() );
638+
}
639+
608640
//write model properties
609641
mLegendModel.writeXML( composerLegendElem, doc );
610642

@@ -655,6 +687,12 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
655687
mSymbolWidth = itemElem.attribute( "symbolWidth", "7.0" ).toDouble();
656688
mSymbolHeight = itemElem.attribute( "symbolHeight", "14.0" ).toDouble();
657689

690+
//composer map
691+
if ( !itemElem.attribute( "map" ).isEmpty() )
692+
{
693+
mComposerMap = mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() );
694+
}
695+
658696
//read model properties
659697
QDomNodeList modelNodeList = itemElem.elementsByTagName( "Model" );
660698
if ( modelNodeList.size() > 0 )
@@ -674,3 +712,15 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
674712
emit itemChanged();
675713
return true;
676714
}
715+
716+
void QgsComposerLegend::setComposerMap( const QgsComposerMap* map )
717+
{
718+
mComposerMap = map;
719+
QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
720+
}
721+
722+
void QgsComposerLegend::invalidateCurrentMap()
723+
{
724+
disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
725+
mComposerMap = 0;
726+
}

src/core/composer/qgscomposerlegend.h

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class QgsSymbol;
2525
class QgsSymbolV2;
2626
class QgsComposerGroupItem;
2727
class QgsComposerLayerItem;
28+
class QgsComposerMap;
2829

2930
/** \ingroup MapComposer
3031
* A legend that can be placed onto a map composition
@@ -86,6 +87,9 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
8687
double symbolHeight() const {return mSymbolHeight;}
8788
void setSymbolHeight( double h ) {mSymbolHeight = h;}
8889

90+
void setComposerMap( const QgsComposerMap* map );
91+
const QgsComposerMap* composerMap() const { return mComposerMap; }
92+
8993
/**Updates the model and all legend entries*/
9094
void updateLegend();
9195

@@ -104,6 +108,8 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
104108
public slots:
105109
/**Data changed*/
106110
void synchronizeWithModel();
111+
/**Sets mCompositionMap to 0 if the map is deleted*/
112+
void invalidateCurrentMap();
107113

108114
protected:
109115
QString mTitle;
@@ -129,6 +135,9 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
129135

130136
QgsLegendModel mLegendModel;
131137

138+
/**Reference to map (because symbols are sometimes in map units)*/
139+
const QgsComposerMap* mComposerMap;
140+
132141

133142
private:
134143
QgsComposerLegend(); //forbidden

src/core/composer/qgscomposermap.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
260260
void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
261261
bool drawCanvasItems() const { return mDrawCanvasItems; }
262262

263+
/**Returns the conversion factor map units -> mm*/
264+
double mapUnitsToMM() const;
265+
263266
signals:
264267
void extentChanged();
265268

@@ -390,8 +393,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
390393
void mapPolygon( QPolygonF& poly ) const;
391394
/**Calculates the extent to request and the yShift of the top-left point in case of rotation.*/
392395
void requestedExtent( QgsRectangle& extent ) const;
393-
/**Returns the conversion factor map units -> mm*/
394-
double mapUnitsToMM() const;
395396
/**Scales a composer map shift (in MM) and rotates it by mRotation
396397
@param xShift in: shift in x direction (in item units), out: xShift in map units
397398
@param yShift in: shift in y direction (in item units), out: yShift in map units*/

src/gui/qgscomposerview.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ void QgsComposerView::addComposerMap( QgsComposerMap* map )
544544

545545
void QgsComposerView::addComposerScaleBar( QgsComposerScaleBar* scaleBar )
546546
{
547-
//take first available map...
547+
//take first available map
548548
QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
549549
if ( mapItemList.size() > 0 )
550550
{
@@ -561,6 +561,12 @@ void QgsComposerView::addComposerScaleBar( QgsComposerScaleBar* scaleBar )
561561

562562
void QgsComposerView::addComposerLegend( QgsComposerLegend* legend )
563563
{
564+
//take first available map
565+
QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
566+
if ( mapItemList.size() > 0 )
567+
{
568+
legend->setComposerMap( mapItemList.at( 0 ) );
569+
}
564570
scene()->addItem( legend );
565571
emit composerLegendAdded( legend );
566572
scene()->clearSelection();

0 commit comments

Comments
 (0)