Skip to content

Commit cc6eccc

Browse files
author
mhugent
committed
Drag and drop support for composer legend model
git-svn-id: http://svn.osgeo.org/qgis/trunk@13491 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent daff22f commit cc6eccc

File tree

8 files changed

+290
-121
lines changed

8 files changed

+290
-121
lines changed

src/app/composer/qgscomposer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,10 @@ void QgsComposer::addComposerLegend( QgsComposerLegend* legend )
13491349
}
13501350

13511351
QgsComposerLegendWidget* lWidget = new QgsComposerLegendWidget( legend );
1352+
if ( sender() ) //only update if created from GUI (not after XML read)
1353+
{
1354+
lWidget->updateLegend();
1355+
}
13521356
mItemWidgetMap.insert( legend, lWidget );
13531357
}
13541358

src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): m
3939
mItemTreeView->setModel( legend->model() );
4040
}
4141

42-
updateLegend();
43-
4442
mItemTreeView->setDragEnabled( true );
4543
mItemTreeView->setAcceptDrops( true );
4644
mItemTreeView->setDropIndicatorShown( true );
47-
//only available in 4.6
48-
//mItemTreeView->setDefaultDropAction( Qt::MoveAction );
4945
mItemTreeView->setDragDropMode( QAbstractItemView::InternalMove );
5046

5147
setGuiElements();

src/app/composer/qgscomposerlegendwidget.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
3434
QgsComposerLegendWidget( QgsComposerLegend* legend );
3535
~QgsComposerLegendWidget();
3636

37+
/**Updates the legend layers and groups*/
38+
void updateLegend();
39+
3740
public slots:
3841

3942
void on_mTitleLineEdit_textChanged( const QString& text );
@@ -61,8 +64,6 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
6164
QgsComposerLegendWidget();
6265
/**Sets GUI according to state of mLegend*/
6366
void setGuiElements();
64-
/**Updates the legend layers and groups*/
65-
void updateLegend();
6667

6768
QgsComposerLegend* mLegend;
6869
};

src/core/composer/qgscomposerlegend.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
604604
if ( modelNodeList.size() > 0 )
605605
{
606606
QDomElement modelElem = modelNodeList.at( 0 ).toElement();
607-
mLegendModel.clear();
608607
mLegendModel.readXML( modelElem, doc );
609608
}
610609

src/core/composer/qgscomposerlegenditem.cpp

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposerlegenditem.h"
19+
#include "qgsmaplayerregistry.h"
1920
#include "qgssymbol.h"
2021
#include "qgssymbolv2.h"
2122
#include "qgssymbollayerv2utils.h"
23+
#include "qgsvectorlayer.h"
2224
#include <QDomDocument>
2325
#include <QDomElement>
2426

@@ -79,7 +81,6 @@ void QgsComposerSymbolItem::setSymbol( QgsSymbol* s )
7981

8082
QStandardItem* QgsComposerSymbolItem::clone() const
8183
{
82-
qWarning( "QgsComposerSymbolItem::clone" );
8384
QgsComposerSymbolItem* cloneItem = new QgsComposerSymbolItem();
8485
*cloneItem = *this;
8586
if ( mSymbol )
@@ -97,12 +98,48 @@ void QgsComposerSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) con
9798
mSymbol->writeXML( vectorClassElem, doc, 0 );
9899
}
99100
vectorClassElem.setAttribute( "text", text() );
101+
vectorClassElem.setAttribute( "layerId", mLayerID );
102+
100103
elem.appendChild( vectorClassElem );
101104
}
102105

103106
void QgsComposerSymbolItem::readXML( const QDomElement& itemElem )
104107
{
105-
//soon...
108+
if ( itemElem.isNull() )
109+
{
110+
return;
111+
}
112+
setText( itemElem.attribute( "text", "" ) );
113+
setLayerID( itemElem.attribute( "layerId", "" ) );
114+
115+
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mLayerID ) );
116+
if ( vLayer )
117+
{
118+
QDomElement symbolElem = itemElem.firstChildElement( "symbol" );
119+
if ( !symbolElem.isNull() )
120+
{
121+
QgsSymbol* symbol = new QgsSymbol( vLayer->geometryType() );
122+
symbol->readXML( symbolElem, vLayer );
123+
setSymbol( symbol );
124+
125+
//add icon
126+
switch ( symbol->type() )
127+
{
128+
case QGis::Point:
129+
setIcon( QIcon( QPixmap::fromImage( symbol->getPointSymbolAsImage() ) ) );
130+
break;
131+
case QGis::Line:
132+
setIcon( QIcon( QPixmap::fromImage( symbol->getLineSymbolAsImage() ) ) );
133+
break;
134+
case QGis::Polygon:
135+
setIcon( QIcon( QPixmap::fromImage( symbol->getPolygonSymbolAsImage() ) ) );
136+
break;
137+
case QGis::UnknownGeometry:
138+
// should not occur
139+
break;
140+
}
141+
}
142+
}
106143
}
107144

108145
////////////////QgsComposerSymbolV2Item
@@ -153,7 +190,28 @@ void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) c
153190

154191
void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem )
155192
{
156-
//soon...
193+
if ( itemElem.isNull() )
194+
{
195+
return;
196+
}
197+
198+
setText( itemElem.attribute( "text", "" ) );
199+
QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
200+
if ( !symbolsElem.isNull() )
201+
{
202+
QgsSymbolV2Map loadSymbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
203+
//we assume there is only one symbol in the map...
204+
QgsSymbolV2Map::iterator mapIt = loadSymbolMap.begin();
205+
if ( mapIt != loadSymbolMap.end() )
206+
{
207+
QgsSymbolV2* symbolNg = mapIt.value();
208+
if ( symbolNg )
209+
{
210+
setSymbolV2( symbolNg );
211+
setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolNg, QSize( 30, 30 ) ) );
212+
}
213+
}
214+
}
157215
}
158216

159217
void QgsComposerSymbolV2Item::setSymbolV2( QgsSymbolV2* s )
@@ -195,7 +253,45 @@ void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) cons
195253

196254
void QgsComposerLayerItem::readXML( const QDomElement& itemElem )
197255
{
198-
//soon...
256+
if ( itemElem.isNull() )
257+
{
258+
return;
259+
}
260+
setText( itemElem.attribute( "text", "" ) );
261+
setLayerID( itemElem.attribute( "layerId", "" ) );
262+
263+
//now call readXML for all the child items
264+
QDomNodeList childList = itemElem.childNodes();
265+
QDomNode currentNode;
266+
QDomElement currentElem;
267+
QgsComposerLegendItem* currentChildItem = 0;
268+
269+
int nChildItems = childList.count();
270+
for ( int i = 0; i < nChildItems; ++i )
271+
{
272+
currentNode = childList.at( i );
273+
if ( !currentNode.isElement() )
274+
{
275+
continue;
276+
}
277+
278+
currentElem = currentNode.toElement();
279+
QString elemTag = currentElem.tagName();
280+
if ( elemTag == "VectorClassificationItem" )
281+
{
282+
currentChildItem = new QgsComposerSymbolItem();
283+
}
284+
else if ( elemTag == "VectorClassificationItemNg" )
285+
{
286+
currentChildItem = new QgsComposerSymbolV2Item();
287+
}
288+
else
289+
{
290+
continue; //unsupported child type
291+
}
292+
currentChildItem->readXML( currentElem );
293+
appendRow( currentChildItem );
294+
}
199295
}
200296

201297
////////////////////QgsComposerGroupItem
@@ -229,5 +325,43 @@ void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) cons
229325

230326
void QgsComposerGroupItem::readXML( const QDomElement& itemElem )
231327
{
232-
//soon...
328+
if ( itemElem.isNull() )
329+
{
330+
return;
331+
}
332+
setText( itemElem.attribute( "text", "" ) );
333+
334+
//now call readXML for all the child items
335+
QDomNodeList childList = itemElem.childNodes();
336+
QDomNode currentNode;
337+
QDomElement currentElem;
338+
QgsComposerLegendItem* currentChildItem = 0;
339+
340+
int nChildItems = childList.count();
341+
for ( int i = 0; i < nChildItems; ++i )
342+
{
343+
currentNode = childList.at( i );
344+
if ( !currentNode.isElement() )
345+
{
346+
continue;
347+
}
348+
349+
currentElem = currentNode.toElement();
350+
QString elemTag = currentElem.tagName();
351+
352+
if ( elemTag == "GroupItem" )
353+
{
354+
currentChildItem = new QgsComposerGroupItem();
355+
}
356+
else if ( elemTag == "LayerItem" )
357+
{
358+
currentChildItem = new QgsComposerLayerItem();
359+
}
360+
else
361+
{
362+
continue; //unsupported child item type
363+
}
364+
currentChildItem->readXML( currentElem );
365+
appendRow( currentChildItem );
366+
}
233367
}

src/core/composer/qgscomposerlegenditem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ class CORE_EXPORT QgsComposerSymbolItem: public QgsComposerLegendItem
6969
void setSymbol( QgsSymbol* s );
7070
QgsSymbol* symbol() {return mSymbol;}
7171

72+
void setLayerID( const QString& id ) { mLayerID = id; }
73+
QString layerID() const { return mLayerID; }
74+
7275
ItemType itemType() const { return SymbologyItem; }
7376

7477
private:
7578
QgsSymbol* mSymbol;
79+
QString mLayerID; //this is needed to read the symbol from XML
7680
};
7781

7882
class QgsSymbolV2;

0 commit comments

Comments
 (0)