Skip to content

Commit 191d62b

Browse files
author
mhugent
committed
added dynamic changing of icon size to the legend for displaying large point symbols
git-svn-id: http://svn.osgeo.org/qgis/trunk@5295 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cc5ae8e commit 191d62b

15 files changed

+139
-32
lines changed

src/core/qgsrenderer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ QgsRenderer::QgsRenderer()
1313

1414
}
1515

16-
void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const
16+
void QgsRenderer::refreshLegend(std::list< std::pair<QString, QPixmap> >* symbologyList) const
1717
{
1818
if(symbologyList)
1919
{
@@ -36,8 +36,6 @@ void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbolo
3636
{
3737
pix = (*it)->getPolygonSymbolAsPixmap();
3838
}
39-
40-
QIcon* theIcon = new QIcon(pix);
4139

4240
QString values;
4341
lw = (*it)->lowerValue();
@@ -57,7 +55,7 @@ void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbolo
5755
values += " ";
5856
values += label;
5957
}
60-
symbologyList->push_back(std::make_pair(values, theIcon));
58+
symbologyList->push_back(std::make_pair(values, pix));
6159
}
6260
}
6361
}

src/core/qgsrenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class QgsRenderer
7575
virtual const std::list<QgsSymbol*> symbols() const=0;
7676
/**Deletes the child items of the legendparent and add new ones according to the
7777
QgsSymbols contained in this renderer*/
78-
virtual void refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const;
78+
virtual void refreshLegend(std::list< std::pair<QString, QPixmap> >* symbologyList) const;
7979
/**Returns a copy of the renderer (a deep copy on the heap)*/
8080
virtual QgsRenderer* clone() const=0;
8181
/**Color to draw selected features - static so we can change it in proj props and automatically

src/gui/qgsvectorlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@ void QgsVectorLayer::refreshLegend()
26852685
{
26862686
if(mLegend && m_renderer)
26872687
{
2688-
std::list< std::pair<QString, QIcon*> > itemList;
2688+
std::list< std::pair<QString, QPixmap> > itemList;
26892689
m_renderer->refreshLegend(&itemList);
26902690
if(m_renderer->needsAttributes()) //create an item for each classification field (only one for most renderers)
26912691
{
@@ -2694,7 +2694,7 @@ void QgsVectorLayer::refreshLegend()
26942694
{
26952695
const QgsField theField = (dataProvider->fields())[*it];
26962696
QString classfieldname = theField.name();
2697-
itemList.push_front(std::make_pair(classfieldname, (QIcon*)0));
2697+
itemList.push_front(std::make_pair(classfieldname, QPixmap()));
26982698
}
26992699
}
27002700
mLegend->changeSymbologySettings(getLayerID(), &itemList);

src/legend/qgslegend.cpp

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const int AUTOSCROLL_MARGIN = 16;
5454
set mItemBeingMoved pointer to 0 to prevent SuSE 9.0 crash
5555
*/
5656
QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
57-
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0), mShowLegendLayerFiles(false)
57+
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0), mShowLegendLayerFiles(false), mMinimumIconSize(20, 20)
5858
{
5959
connect( this, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
6060
this, SLOT(handleItemChange(QTreeWidgetItem*, int)));
@@ -69,7 +69,6 @@ QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
6969
QFont f("Arial", 10, QFont::Normal);
7070
setFont(f);
7171
setBackgroundColor(QColor(192, 192, 192));
72-
//setIconSize(QSize(30, 30));
7372
setColumnCount(1);
7473
QStringList myList("Layers");
7574
setHeaderLabels(myList);
@@ -1043,6 +1042,36 @@ QgsLegendLayer* QgsLegend::findLegendLayer(const QString& layerKey)
10431042
return 0;
10441043
}
10451044

1045+
void QgsLegend::adjustIconSize()
1046+
{
1047+
if(mPixmapWidthValues.size() > 0 && mPixmapHeightValues.size() > 0)
1048+
{
1049+
std::multiset<int>::const_reverse_iterator width_it = mPixmapWidthValues.rbegin();
1050+
std::multiset<int>::const_reverse_iterator height_it = mPixmapHeightValues.rbegin();
1051+
int maxWidth = *width_it;
1052+
int maxHeight = *height_it;
1053+
1054+
QSize currentIconSize = iconSize();
1055+
if(maxWidth == currentIconSize.width() && maxHeight == currentIconSize.height())
1056+
{
1057+
//no resizing necessary
1058+
return;
1059+
}
1060+
1061+
//keep the minimum size
1062+
if(maxWidth < mMinimumIconSize.width())
1063+
{
1064+
maxWidth = mMinimumIconSize.width();
1065+
}
1066+
if(maxHeight < mMinimumIconSize.height())
1067+
{
1068+
maxHeight = mMinimumIconSize.height();
1069+
}
1070+
1071+
setIconSize(QSize(maxWidth, maxHeight));
1072+
}
1073+
}
1074+
10461075
bool QgsLegend::yCoordAboveCenter(QgsLegendItem* it, int ycoord)
10471076
{
10481077
QRect rect = visualItemRect(it);
@@ -1232,7 +1261,7 @@ std::deque<QString> QgsLegend::layerIDs()
12321261
return layers;
12331262
}
12341263

1235-
void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems)
1264+
void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QPixmap> >* newSymbologyItems)
12361265
{
12371266
QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(key);
12381267
if(!theMapLayer)
@@ -1255,21 +1284,25 @@ void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std
12551284
theSymbologyItem = dynamic_cast<QgsLegendSymbologyItem*>((theLegendLayer)->child(i));
12561285
if(theSymbologyItem)
12571286
{
1258-
theLegendLayer->takeChild(i);
1287+
removePixmapWidthValue(theSymbologyItem->pixmapWidth());
1288+
removePixmapHeightValue(theSymbologyItem->pixmapHeight());
1289+
delete (theLegendLayer->takeChild(i));
12591290
}
12601291
}
12611292

12621293
//add the new symbology items
12631294
if(newSymbologyItems)
12641295
{
12651296
int childposition = 0; //position to insert the items
1266-
for(std::list< std::pair<QString, QIcon*> >::const_iterator it= newSymbologyItems->begin(); it != newSymbologyItems->end(); ++it)
1297+
for(std::list< std::pair<QString, QPixmap> >::const_iterator it= newSymbologyItems->begin(); it != newSymbologyItems->end(); ++it)
12671298
{
1268-
QgsLegendSymbologyItem* theItem = new QgsLegendSymbologyItem();
1299+
QgsLegendSymbologyItem* theItem = new QgsLegendSymbologyItem(it->second.width(), it->second.height());
12691300
theItem->setText(0, it->first);
1270-
theItem->setIcon(0, *(it->second));
1301+
theItem->setIcon(0, QIcon(it->second));
12711302
theLegendLayer->insertChild(childposition, theItem);
1272-
delete (it->second);//free the memory for the QIcon*
1303+
//add the width and height values to the multisets
1304+
addPixmapWidthValue(theItem->pixmapWidth());
1305+
addPixmapHeightValue(theItem->pixmapHeight());
12731306
++childposition;
12741307
}
12751308
}
@@ -1279,6 +1312,37 @@ void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std
12791312

12801313
//restore the current item again
12811314
setCurrentItem(theCurrentItem);
1315+
adjustIconSize();
1316+
}
1317+
1318+
void QgsLegend::addPixmapWidthValue(int width)
1319+
{
1320+
mPixmapWidthValues.insert(width);
1321+
}
1322+
1323+
void QgsLegend::addPixmapHeightValue(int height)
1324+
{
1325+
mPixmapHeightValues.insert(height);
1326+
}
1327+
1328+
void QgsLegend::removePixmapWidthValue(int width)
1329+
{
1330+
std::multiset<int>::iterator it = mPixmapWidthValues.find(width);
1331+
if (it != mPixmapWidthValues.end())
1332+
{
1333+
mPixmapWidthValues.erase(it);
1334+
}
1335+
//todo: adapt the icon size if width is the largest value and the size of the next element is higher than the minimum
1336+
}
1337+
1338+
void QgsLegend::removePixmapHeightValue(int height)
1339+
{
1340+
std::multiset<int>::iterator it = mPixmapHeightValues.find(height);
1341+
if (it != mPixmapHeightValues.end())
1342+
{
1343+
mPixmapHeightValues.erase(height);
1344+
}
1345+
//todo: adapt the icon size if height is the largest value and the size of the next element is higher than the minimum
12821346
}
12831347

12841348
void QgsLegend::setName(QgsLegendLayerFile* legendLayerFile,

src/legend/qgslegend.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <deque>
2424
#include <map>
25+
#include <set>
2526
#include <QTreeWidget>
2627

2728
class QgisApp;
@@ -144,12 +145,25 @@ class QgsLegend : public QTreeWidget
144145

145146
/**Removes the symbology items of a layer and adds new ones. If other files are in the same legend layer, the new symbology settings are copied.
146147
Note: the QIcon* are deleted and therefore need to be allocated by calling functions using operator new*/
147-
void changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems);
148+
void changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QPixmap> >* newSymbologyItems);
149+
150+
/**Adds an entry to mPixmapWidthValues*/
151+
void addPixmapWidthValue(int width);
152+
153+
/**Adds an entry to mPixmapHeightValues*/
154+
void addPixmapHeightValue(int height);
155+
156+
/**Removes an entry from mPixmapWidthValues*/
157+
void removePixmapWidthValue(int width);
158+
159+
/**Removes an entry from mPixmapHeightValues*/
160+
void removePixmapHeightValue(int height);
148161

149162
/** Sets the name of the QgsLegendLayer that is the parent of
150163
the given QgsLegendLayerFile */
151164
void setName(QgsLegendLayerFile* w, QString layerName);
152165

166+
153167
public slots:
154168

155169
/*!Adds a new layer group with the maplayer to the canvas*/
@@ -227,6 +241,8 @@ this item may be moved back to the original position with resetToInitialPosition
227241
/**Returns the legend layer to which a map layer gelongs*/
228242
QgsLegendLayer* findLegendLayer(const QString& layerKey);
229243

244+
/**Checks mPixmapWidthValues and mPixmapHeightValues and sets a new icon size if necessary*/
245+
void adjustIconSize();
230246

231247
private slots:
232248

@@ -329,6 +345,17 @@ this item may be moved back to the original position with resetToInitialPosition
329345
a signal for check state changes*/
330346
std::map<QTreeWidgetItem*, Qt::CheckState> mStateOfCheckBoxes;
331347

348+
/**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change
349+
the global IconWidth when items are added or removed*/
350+
std::multiset<int> mPixmapWidthValues;
351+
352+
/**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change
353+
the global IconWidth when items are added or removed*/
354+
std::multiset<int> mPixmapHeightValues;
355+
356+
/**QgsLegend does not set the icon with/height to values lower than the minimum icon size*/
357+
QSize mMinimumIconSize;
358+
332359
signals:
333360
void zOrderChanged(QgsLegend * lv);
334361

src/legend/qgslegenditem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ void QgsLegendItem::restoreAppearanceSettings()
121121
static_cast<QgsLegendItem*>(child(i))->restoreAppearanceSettings();
122122
}
123123
}
124+
125+
QgsLegend* QgsLegendItem::legend() const
126+
{
127+
QTreeWidget* treeWidgetPtr = treeWidget();
128+
QgsLegend* legendPtr = dynamic_cast<QgsLegend*>(treeWidgetPtr);
129+
return legendPtr;
130+
}

src/legend/qgslegenditem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QTreeWidget>
2424
#include <QTreeWidgetItem>
2525

26+
class QgsLegend;
2627
class QgsLegendGroup;
2728
class QgsLegendLayer;
2829
class QgsLegendPropertyGroup;
@@ -39,7 +40,7 @@ class QgsLegendItem : public QTreeWidgetItem
3940
QgsLegendItem(QTreeWidgetItem*, QString);
4041
QgsLegendItem (QTreeWidget*,QString);
4142
QgsLegendItem();
42-
~QgsLegendItem();
43+
virtual ~QgsLegendItem();
4344

4445
enum LEGEND_ITEM_TYPE
4546
{
@@ -95,6 +96,8 @@ class QgsLegendItem : public QTreeWidgetItem
9596
void storeAppearanceSettings();
9697
/**Restore appearanc settings (expanded and hidden) e.g. after being inserted into a new place in the tree widget*/
9798
void restoreAppearanceSettings();
99+
/**Returns a pointer to the legend widget*/
100+
QgsLegend* legend() const;
98101
protected:
99102
bool mLeafNodeFlag;
100103
LEGEND_ITEM_TYPE mType;

src/legend/qgslegendsymbologyitem.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@
1717
* Free Software Foundation, Inc., *
1818
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
1919
***************************************************************************/
20+
#include "qgslegend.h"
2021
#include "qgslegendsymbologyitem.h"
2122

22-
QgsLegendSymbologyItem::QgsLegendSymbologyItem(QTreeWidgetItem * theItem,QString theString)
23-
: QgsLegendItem(theItem, theString)
23+
QgsLegendSymbologyItem::QgsLegendSymbologyItem(QTreeWidgetItem * theItem,QString theString, int pixmapWidth, int pixmapHeight)
24+
: QgsLegendItem(theItem, theString), mPixmapWidth(pixmapWidth), mPixmapHeight(pixmapHeight)
2425
{
2526
mType = LEGEND_SYMBOL_ITEM;
2627
}
2728

28-
QgsLegendSymbologyItem::QgsLegendSymbologyItem(): QgsLegendItem()
29+
QgsLegendSymbologyItem::QgsLegendSymbologyItem(int pixmapWidth, int pixmapHeight): QgsLegendItem(), mPixmapWidth(pixmapWidth), mPixmapHeight(pixmapHeight)
2930
{
3031
mType = LEGEND_SYMBOL_ITEM;
3132
}
3233

3334
QgsLegendSymbologyItem::~QgsLegendSymbologyItem()
34-
{
35+
{
3536
}
3637

3738
QgsLegendItem::DRAG_ACTION QgsLegendSymbologyItem::accept(LEGEND_ITEM_TYPE type)

src/legend/qgslegendsymbologyitem.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@
2828
class QgsLegendSymbologyItem : public QgsLegendItem
2929
{
3030
public:
31-
QgsLegendSymbologyItem(QTreeWidgetItem* theItem, QString theString);
32-
QgsLegendSymbologyItem();
31+
QgsLegendSymbologyItem(QTreeWidgetItem* theItem, QString theString, int pixmapWidth, int pixmapHeight);
32+
QgsLegendSymbologyItem(int pixmapWidth, int pixmapHeight);
3333
~QgsLegendSymbologyItem();
3434
bool isLeafNode() {return true;}
3535
DRAG_ACTION accept(LEGEND_ITEM_TYPE type);
3636
QgsLegendItem::DRAG_ACTION accept(const QgsLegendItem* li) const;
37+
int pixmapWidth() const {return mPixmapWidth;}
38+
int pixmapHeight() const {return mPixmapHeight;}
39+
protected:
40+
int mPixmapWidth;
41+
int mPixmapHeight;
3742
};
3843

3944
#endif

src/legend/qgslegendvectorsymbologyitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
#include "qgslegendvectorsymbologyitem.h"
1919

20-
QgsLegendVectorSymbologyItem::QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1)
21-
: QgsLegendSymbologyItem(theItem, col1)
20+
QgsLegendVectorSymbologyItem::QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1, int pixmapWidth, int pixmapHeight)
21+
: QgsLegendSymbologyItem(theItem, col1, pixmapWidth, pixmapHeight)
2222
{
2323
mType = LEGEND_VECTOR_SYMBOL_ITEM;
2424
}

src/legend/qgslegendvectorsymbologyitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class QgsSymbol;
2626
class QgsLegendVectorSymbologyItem: public QgsLegendSymbologyItem
2727
{
2828
public:
29-
QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1);
29+
QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1, int pixmapWidth, int pixmapHeight);
3030
/**Add a symbol to the list such that it will be updated if necessary*/
3131
void addSymbol(QgsSymbol* s);
3232
/**Brings up a single symbol dialog. The changes are then applied to all entries of mSymbols*/

src/raster/qgsrasterlayer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,9 +5059,8 @@ void QgsRasterLayer::refreshLegend()
50595059
{
50605060
if(mLegend)
50615061
{
5062-
std::list< std::pair<QString, QIcon*> > itemList;
5063-
QIcon* theIcon = new QIcon(getLegendQPixmap(true));
5064-
itemList.push_back(std::make_pair("", theIcon));
5062+
std::list< std::pair<QString, QPixmap> > itemList;
5063+
itemList.push_back(std::make_pair("", getLegendQPixmap(true)));
50655064
mLegend->changeSymbologySettings(getLayerID(), &itemList);
50665065
}
50675066
}

tests/src/core/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ GLOBALLDADD = $(QT_LDADD) \
5858
../../../src/core/libqgis_core.la \
5959
../../../src/raster/libqgis_raster.la \
6060
../../../src/legend/libqgis_legend.la \
61-
../../../src/gui/libqgis_gui.la
61+
../../../src/gui/libqgis_gui.la \
62+
../../../src/composer/libqgis_composer.la
6263
GLOBALCXXFLAGS = $(CXXFLAGS) \
6364
$(EXTRA_CXXFLAGS) \
6465
$(GDAL_CFLAGS) \

tests/src/gui/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ GLOBALLDADD = $(QT_LDADD) \
9494
../../../src/core/libqgis_core.la \
9595
../../../src/raster/libqgis_raster.la \
9696
../../../src/legend/libqgis_legend.la \
97-
../../../src/gui/libqgis_gui.la
97+
../../../src/gui/libqgis_gui.la \
98+
../../../src/composer/libqgis_composer.la
9899
GLOBALCXXFLAGS = $(CXXFLAGS) \
99100
$(EXTRA_CXXFLAGS) \
100101
$(GDAL_CFLAGS) \

tests/src/raster/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ GLOBALLDADD = $(QT_LDADD) \
2020
../../../src/core/libqgis_core.la \
2121
../../../src/raster/libqgis_raster.la \
2222
../../../src/legend/libqgis_legend.la \
23-
../../../src/gui/libqgis_gui.la
23+
../../../src/gui/libqgis_gui.la \
24+
../../../src/composer/libqgis_composer.la
2425
GLOBALCXXFLAGS = $(CXXFLAGS) \
2526
$(EXTRA_CXXFLAGS) \
2627
$(GDAL_CFLAGS) \

0 commit comments

Comments
 (0)