Skip to content

Commit

Permalink
added dynamic changing of icon size to the legend for displaying larg…
Browse files Browse the repository at this point in the history
…e point symbols

git-svn-id: http://svn.osgeo.org/qgis/trunk@5295 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 17, 2006
1 parent cc5ae8e commit 191d62b
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 32 deletions.
6 changes: 2 additions & 4 deletions src/core/qgsrenderer.cpp
Expand Up @@ -13,7 +13,7 @@ QgsRenderer::QgsRenderer()

}

void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const
void QgsRenderer::refreshLegend(std::list< std::pair<QString, QPixmap> >* symbologyList) const
{
if(symbologyList)
{
Expand All @@ -36,8 +36,6 @@ void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbolo
{
pix = (*it)->getPolygonSymbolAsPixmap();
}

QIcon* theIcon = new QIcon(pix);

QString values;
lw = (*it)->lowerValue();
Expand All @@ -57,7 +55,7 @@ void QgsRenderer::refreshLegend(std::list< std::pair<QString, QIcon*> >* symbolo
values += " ";
values += label;
}
symbologyList->push_back(std::make_pair(values, theIcon));
symbologyList->push_back(std::make_pair(values, pix));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrenderer.h
Expand Up @@ -75,7 +75,7 @@ class QgsRenderer
virtual const std::list<QgsSymbol*> symbols() const=0;
/**Deletes the child items of the legendparent and add new ones according to the
QgsSymbols contained in this renderer*/
virtual void refreshLegend(std::list< std::pair<QString, QIcon*> >* symbologyList) const;
virtual void refreshLegend(std::list< std::pair<QString, QPixmap> >* symbologyList) const;
/**Returns a copy of the renderer (a deep copy on the heap)*/
virtual QgsRenderer* clone() const=0;
/**Color to draw selected features - static so we can change it in proj props and automatically
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsvectorlayer.cpp
Expand Up @@ -2685,7 +2685,7 @@ void QgsVectorLayer::refreshLegend()
{
if(mLegend && m_renderer)
{
std::list< std::pair<QString, QIcon*> > itemList;
std::list< std::pair<QString, QPixmap> > itemList;
m_renderer->refreshLegend(&itemList);
if(m_renderer->needsAttributes()) //create an item for each classification field (only one for most renderers)
{
Expand All @@ -2694,7 +2694,7 @@ void QgsVectorLayer::refreshLegend()
{
const QgsField theField = (dataProvider->fields())[*it];
QString classfieldname = theField.name();
itemList.push_front(std::make_pair(classfieldname, (QIcon*)0));
itemList.push_front(std::make_pair(classfieldname, QPixmap()));
}
}
mLegend->changeSymbologySettings(getLayerID(), &itemList);
Expand Down
80 changes: 72 additions & 8 deletions src/legend/qgslegend.cpp
Expand Up @@ -54,7 +54,7 @@ const int AUTOSCROLL_MARGIN = 16;
set mItemBeingMoved pointer to 0 to prevent SuSE 9.0 crash
*/
QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0), mShowLegendLayerFiles(false)
: QTreeWidget(parent), mApp(app), mMousePressedFlag(false), mItemBeingMoved(0), mMapCanvas(0), mShowLegendLayerFiles(false), mMinimumIconSize(20, 20)
{
connect( this, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
this, SLOT(handleItemChange(QTreeWidgetItem*, int)));
Expand All @@ -69,7 +69,6 @@ QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
QFont f("Arial", 10, QFont::Normal);
setFont(f);
setBackgroundColor(QColor(192, 192, 192));
//setIconSize(QSize(30, 30));
setColumnCount(1);
QStringList myList("Layers");
setHeaderLabels(myList);
Expand Down Expand Up @@ -1043,6 +1042,36 @@ QgsLegendLayer* QgsLegend::findLegendLayer(const QString& layerKey)
return 0;
}

void QgsLegend::adjustIconSize()
{
if(mPixmapWidthValues.size() > 0 && mPixmapHeightValues.size() > 0)
{
std::multiset<int>::const_reverse_iterator width_it = mPixmapWidthValues.rbegin();
std::multiset<int>::const_reverse_iterator height_it = mPixmapHeightValues.rbegin();
int maxWidth = *width_it;
int maxHeight = *height_it;

QSize currentIconSize = iconSize();
if(maxWidth == currentIconSize.width() && maxHeight == currentIconSize.height())
{
//no resizing necessary
return;
}

//keep the minimum size
if(maxWidth < mMinimumIconSize.width())
{
maxWidth = mMinimumIconSize.width();
}
if(maxHeight < mMinimumIconSize.height())
{
maxHeight = mMinimumIconSize.height();
}

setIconSize(QSize(maxWidth, maxHeight));
}
}

bool QgsLegend::yCoordAboveCenter(QgsLegendItem* it, int ycoord)
{
QRect rect = visualItemRect(it);
Expand Down Expand Up @@ -1232,7 +1261,7 @@ std::deque<QString> QgsLegend::layerIDs()
return layers;
}

void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems)
void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QPixmap> >* newSymbologyItems)
{
QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(key);
if(!theMapLayer)
Expand All @@ -1255,21 +1284,25 @@ void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std
theSymbologyItem = dynamic_cast<QgsLegendSymbologyItem*>((theLegendLayer)->child(i));
if(theSymbologyItem)
{
theLegendLayer->takeChild(i);
removePixmapWidthValue(theSymbologyItem->pixmapWidth());
removePixmapHeightValue(theSymbologyItem->pixmapHeight());
delete (theLegendLayer->takeChild(i));
}
}

//add the new symbology items
if(newSymbologyItems)
{
int childposition = 0; //position to insert the items
for(std::list< std::pair<QString, QIcon*> >::const_iterator it= newSymbologyItems->begin(); it != newSymbologyItems->end(); ++it)
for(std::list< std::pair<QString, QPixmap> >::const_iterator it= newSymbologyItems->begin(); it != newSymbologyItems->end(); ++it)
{
QgsLegendSymbologyItem* theItem = new QgsLegendSymbologyItem();
QgsLegendSymbologyItem* theItem = new QgsLegendSymbologyItem(it->second.width(), it->second.height());
theItem->setText(0, it->first);
theItem->setIcon(0, *(it->second));
theItem->setIcon(0, QIcon(it->second));
theLegendLayer->insertChild(childposition, theItem);
delete (it->second);//free the memory for the QIcon*
//add the width and height values to the multisets
addPixmapWidthValue(theItem->pixmapWidth());
addPixmapHeightValue(theItem->pixmapHeight());
++childposition;
}
}
Expand All @@ -1279,6 +1312,37 @@ void QgsLegend::changeSymbologySettings(const QString& key, const std::list< std

//restore the current item again
setCurrentItem(theCurrentItem);
adjustIconSize();
}

void QgsLegend::addPixmapWidthValue(int width)
{
mPixmapWidthValues.insert(width);
}

void QgsLegend::addPixmapHeightValue(int height)
{
mPixmapHeightValues.insert(height);
}

void QgsLegend::removePixmapWidthValue(int width)
{
std::multiset<int>::iterator it = mPixmapWidthValues.find(width);
if (it != mPixmapWidthValues.end())
{
mPixmapWidthValues.erase(it);
}
//todo: adapt the icon size if width is the largest value and the size of the next element is higher than the minimum
}

void QgsLegend::removePixmapHeightValue(int height)
{
std::multiset<int>::iterator it = mPixmapHeightValues.find(height);
if (it != mPixmapHeightValues.end())
{
mPixmapHeightValues.erase(height);
}
//todo: adapt the icon size if height is the largest value and the size of the next element is higher than the minimum
}

void QgsLegend::setName(QgsLegendLayerFile* legendLayerFile,
Expand Down
29 changes: 28 additions & 1 deletion src/legend/qgslegend.h
Expand Up @@ -22,6 +22,7 @@

#include <deque>
#include <map>
#include <set>
#include <QTreeWidget>

class QgisApp;
Expand Down Expand Up @@ -144,12 +145,25 @@ class QgsLegend : public QTreeWidget

/**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.
Note: the QIcon* are deleted and therefore need to be allocated by calling functions using operator new*/
void changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QIcon*> >* newSymbologyItems);
void changeSymbologySettings(const QString& key, const std::list< std::pair<QString, QPixmap> >* newSymbologyItems);

/**Adds an entry to mPixmapWidthValues*/
void addPixmapWidthValue(int width);

/**Adds an entry to mPixmapHeightValues*/
void addPixmapHeightValue(int height);

/**Removes an entry from mPixmapWidthValues*/
void removePixmapWidthValue(int width);

/**Removes an entry from mPixmapHeightValues*/
void removePixmapHeightValue(int height);

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


public slots:

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

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

private slots:

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

/**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change
the global IconWidth when items are added or removed*/
std::multiset<int> mPixmapWidthValues;

/**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change
the global IconWidth when items are added or removed*/
std::multiset<int> mPixmapHeightValues;

/**QgsLegend does not set the icon with/height to values lower than the minimum icon size*/
QSize mMinimumIconSize;

signals:
void zOrderChanged(QgsLegend * lv);

Expand Down
7 changes: 7 additions & 0 deletions src/legend/qgslegenditem.cpp
Expand Up @@ -121,3 +121,10 @@ void QgsLegendItem::restoreAppearanceSettings()
static_cast<QgsLegendItem*>(child(i))->restoreAppearanceSettings();
}
}

QgsLegend* QgsLegendItem::legend() const
{
QTreeWidget* treeWidgetPtr = treeWidget();
QgsLegend* legendPtr = dynamic_cast<QgsLegend*>(treeWidgetPtr);
return legendPtr;
}
5 changes: 4 additions & 1 deletion src/legend/qgslegenditem.h
Expand Up @@ -23,6 +23,7 @@
#include <QTreeWidget>
#include <QTreeWidgetItem>

class QgsLegend;
class QgsLegendGroup;
class QgsLegendLayer;
class QgsLegendPropertyGroup;
Expand All @@ -39,7 +40,7 @@ class QgsLegendItem : public QTreeWidgetItem
QgsLegendItem(QTreeWidgetItem*, QString);
QgsLegendItem (QTreeWidget*,QString);
QgsLegendItem();
~QgsLegendItem();
virtual ~QgsLegendItem();

enum LEGEND_ITEM_TYPE
{
Expand Down Expand Up @@ -95,6 +96,8 @@ class QgsLegendItem : public QTreeWidgetItem
void storeAppearanceSettings();
/**Restore appearanc settings (expanded and hidden) e.g. after being inserted into a new place in the tree widget*/
void restoreAppearanceSettings();
/**Returns a pointer to the legend widget*/
QgsLegend* legend() const;
protected:
bool mLeafNodeFlag;
LEGEND_ITEM_TYPE mType;
Expand Down
9 changes: 5 additions & 4 deletions src/legend/qgslegendsymbologyitem.cpp
Expand Up @@ -17,21 +17,22 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "qgslegend.h"
#include "qgslegendsymbologyitem.h"

QgsLegendSymbologyItem::QgsLegendSymbologyItem(QTreeWidgetItem * theItem,QString theString)
: QgsLegendItem(theItem, theString)
QgsLegendSymbologyItem::QgsLegendSymbologyItem(QTreeWidgetItem * theItem,QString theString, int pixmapWidth, int pixmapHeight)
: QgsLegendItem(theItem, theString), mPixmapWidth(pixmapWidth), mPixmapHeight(pixmapHeight)
{
mType = LEGEND_SYMBOL_ITEM;
}

QgsLegendSymbologyItem::QgsLegendSymbologyItem(): QgsLegendItem()
QgsLegendSymbologyItem::QgsLegendSymbologyItem(int pixmapWidth, int pixmapHeight): QgsLegendItem(), mPixmapWidth(pixmapWidth), mPixmapHeight(pixmapHeight)
{
mType = LEGEND_SYMBOL_ITEM;
}

QgsLegendSymbologyItem::~QgsLegendSymbologyItem()
{
{
}

QgsLegendItem::DRAG_ACTION QgsLegendSymbologyItem::accept(LEGEND_ITEM_TYPE type)
Expand Down
9 changes: 7 additions & 2 deletions src/legend/qgslegendsymbologyitem.h
Expand Up @@ -28,12 +28,17 @@
class QgsLegendSymbologyItem : public QgsLegendItem
{
public:
QgsLegendSymbologyItem(QTreeWidgetItem* theItem, QString theString);
QgsLegendSymbologyItem();
QgsLegendSymbologyItem(QTreeWidgetItem* theItem, QString theString, int pixmapWidth, int pixmapHeight);
QgsLegendSymbologyItem(int pixmapWidth, int pixmapHeight);
~QgsLegendSymbologyItem();
bool isLeafNode() {return true;}
DRAG_ACTION accept(LEGEND_ITEM_TYPE type);
QgsLegendItem::DRAG_ACTION accept(const QgsLegendItem* li) const;
int pixmapWidth() const {return mPixmapWidth;}
int pixmapHeight() const {return mPixmapHeight;}
protected:
int mPixmapWidth;
int mPixmapHeight;
};

#endif
4 changes: 2 additions & 2 deletions src/legend/qgslegendvectorsymbologyitem.cpp
Expand Up @@ -17,8 +17,8 @@

#include "qgslegendvectorsymbologyitem.h"

QgsLegendVectorSymbologyItem::QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1)
: QgsLegendSymbologyItem(theItem, col1)
QgsLegendVectorSymbologyItem::QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1, int pixmapWidth, int pixmapHeight)
: QgsLegendSymbologyItem(theItem, col1, pixmapWidth, pixmapHeight)
{
mType = LEGEND_VECTOR_SYMBOL_ITEM;
}
Expand Down
2 changes: 1 addition & 1 deletion src/legend/qgslegendvectorsymbologyitem.h
Expand Up @@ -26,7 +26,7 @@ class QgsSymbol;
class QgsLegendVectorSymbologyItem: public QgsLegendSymbologyItem
{
public:
QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1);
QgsLegendVectorSymbologyItem(QTreeWidgetItem * theItem, QString col1, int pixmapWidth, int pixmapHeight);
/**Add a symbol to the list such that it will be updated if necessary*/
void addSymbol(QgsSymbol* s);
/**Brings up a single symbol dialog. The changes are then applied to all entries of mSymbols*/
Expand Down
5 changes: 2 additions & 3 deletions src/raster/qgsrasterlayer.cpp
Expand Up @@ -5059,9 +5059,8 @@ void QgsRasterLayer::refreshLegend()
{
if(mLegend)
{
std::list< std::pair<QString, QIcon*> > itemList;
QIcon* theIcon = new QIcon(getLegendQPixmap(true));
itemList.push_back(std::make_pair("", theIcon));
std::list< std::pair<QString, QPixmap> > itemList;
itemList.push_back(std::make_pair("", getLegendQPixmap(true)));
mLegend->changeSymbologySettings(getLayerID(), &itemList);
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/src/core/Makefile.am
Expand Up @@ -58,7 +58,8 @@ GLOBALLDADD = $(QT_LDADD) \
../../../src/core/libqgis_core.la \
../../../src/raster/libqgis_raster.la \
../../../src/legend/libqgis_legend.la \
../../../src/gui/libqgis_gui.la
../../../src/gui/libqgis_gui.la \
../../../src/composer/libqgis_composer.la
GLOBALCXXFLAGS = $(CXXFLAGS) \
$(EXTRA_CXXFLAGS) \
$(GDAL_CFLAGS) \
Expand Down
3 changes: 2 additions & 1 deletion tests/src/gui/Makefile.am
Expand Up @@ -94,7 +94,8 @@ GLOBALLDADD = $(QT_LDADD) \
../../../src/core/libqgis_core.la \
../../../src/raster/libqgis_raster.la \
../../../src/legend/libqgis_legend.la \
../../../src/gui/libqgis_gui.la
../../../src/gui/libqgis_gui.la \
../../../src/composer/libqgis_composer.la
GLOBALCXXFLAGS = $(CXXFLAGS) \
$(EXTRA_CXXFLAGS) \
$(GDAL_CFLAGS) \
Expand Down
3 changes: 2 additions & 1 deletion tests/src/raster/Makefile.am
Expand Up @@ -20,7 +20,8 @@ GLOBALLDADD = $(QT_LDADD) \
../../../src/core/libqgis_core.la \
../../../src/raster/libqgis_raster.la \
../../../src/legend/libqgis_legend.la \
../../../src/gui/libqgis_gui.la
../../../src/gui/libqgis_gui.la \
../../../src/composer/libqgis_composer.la
GLOBALCXXFLAGS = $(CXXFLAGS) \
$(EXTRA_CXXFLAGS) \
$(GDAL_CFLAGS) \
Expand Down

0 comments on commit 191d62b

Please sign in to comment.