Skip to content

Commit

Permalink
First implementation step of having checkboxes in the qt4 legend (leg…
Browse files Browse the repository at this point in the history
…endlayer and legendlayerfile)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4335 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Dec 14, 2005
1 parent 3acf13a commit 9baf4e6
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 25 deletions.
108 changes: 87 additions & 21 deletions src/legend/qgslegend.cpp
Expand Up @@ -55,6 +55,11 @@ QgsLegend::QgsLegend(QgisApp* app, QWidget * parent, const char *name)
connect( this, SIGNAL(doubleClicked(QTreeWidgetItem *, const QPoint &, int)),
this, SLOT(handleDoubleClickEvent(QTreeWidgetItem*)));

//just for a test

connect( this, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
this, SLOT(handleItemChange(QTreeWidgetItem*, int)));

setSortingEnabled(false);
setDragEnabled(false);
setAutoScroll(true);
Expand Down Expand Up @@ -86,7 +91,8 @@ void QgsLegend::updateLegendItem( QTreeWidgetItem * li )

void QgsLegend::removeAll()
{
clear();
mStateOfCheckBoxes.clear();
clear();
}

void QgsLegend::removeLayer(QString layer_key)
Expand All @@ -105,8 +111,10 @@ void QgsLegend::removeLayer(QString layer_key)
{
if (llf->layer()&&llf->layer()->getLayerID() == layer_key)
{
removeItem(llf);
break;
//remove the map entry for the checkbox
mStateOfCheckBoxes.erase(llf);
removeItem(llf);
break;
}
}
}
Expand All @@ -118,22 +126,6 @@ void QgsLegend::removeLayer(QString layer_key)

void QgsLegend::mousePressEvent(QMouseEvent * e)
{
#ifdef QGISDEBUG
qWarning("this message comes from within QgsLegend::contentsMousePressEvent");
#endif

#if 0
//check, if a checkbox has been pressed
QWidget* w = childAt(e->pos());
QCheckBox* cb = dynamic_cast<QCheckBox*>(w);
if(cb)
{
#ifdef QGISDEBUG
qWarning("A checkbox has been pressed");
#endif
}
#endif

if (e->button() == Qt::LeftButton)
{
mLastPressPos = e->pos();
Expand All @@ -148,7 +140,6 @@ void QgsLegend::mousePressEvent(QMouseEvent * e)
QTreeWidget::mousePressEvent(e);
} // contentsMousePressEvent


void QgsLegend::mouseMoveEvent(QMouseEvent * e)
{
if(mMousePressedFlag)
Expand Down Expand Up @@ -266,7 +257,6 @@ void QgsLegend::mouseMoveEvent(QMouseEvent * e)
//QTreeWidget::mouseMoveEvent(e);
}


void QgsLegend::mouseReleaseEvent(QMouseEvent * e)
{
QTreeWidget::mouseReleaseEvent(e);
Expand Down Expand Up @@ -432,6 +422,7 @@ void QgsLegend::handleRightClickEvent(QTreeWidgetItem* item, const QPoint& posit
{
Q3PopupMenu pm;
pm.insertItem(QIcon(QPixmap(iconsPath+QString("remove.png"))), tr("&Remove"), this, SLOT(legendLayerRemove()));
pm.insertItem(tr("Re&name"), this, SLOT(openEditor()));
pm.insertItem(tr("&Properties"), this, SLOT(legendLayerShowProperties()));
pm.insertItem(QIcon(QPixmap(iconsPath+QString("inoverview.png"))), tr("&Add to overview"), this, SLOT(legendLayerAddToOverview()));
pm.insertItem(QIcon(QPixmap(iconsPath+QString("remove_from_overview.png"))), tr("&Remove from overview"), this, SLOT(legendLayerRemoveFromOverview()));
Expand All @@ -444,6 +435,7 @@ void QgsLegend::handleRightClickEvent(QTreeWidgetItem* item, const QPoint& posit
{
Q3PopupMenu pm;
pm.insertItem(tr("&Remove"), this, SLOT(legendGroupRemove()));
pm.insertItem(tr("Re&name"), this, SLOT(openEditor()));
pm.insertItem(QIcon(QPixmap(iconsPath+QString("folder_new.png"))), tr("&Add group"), this, SLOT(addGroup()));
pm.insertItem(QIcon(QPixmap(iconsPath+QString("expand_tree.png"))), tr("&Expand all"), this, SLOT(expandAll()));
pm.insertItem(QIcon(QPixmap(iconsPath+QString("collapse_tree.png"))), tr("&Collapse all"), this, SLOT(collapseAll()));
Expand Down Expand Up @@ -472,8 +464,10 @@ int QgsLegend::getItemPos(QTreeWidgetItem* item)
void QgsLegend::addLayer( QgsMapLayer * layer )
{
QgsLegendLayer * llayer = new QgsLegendLayer(QString(layer->name()));
mStateOfCheckBoxes.insert(std::make_pair(llayer, Qt::Checked)); //insert the check state into the map to query for changes later
QgsLegendLayerFileGroup * llfgroup = new QgsLegendLayerFileGroup(llayer,QString("Files"));
QgsLegendLayerFile * llfile = new QgsLegendLayerFile(llfgroup, QgsLegendLayerFile::nameFromLayer(layer), layer);
mStateOfCheckBoxes.insert(std::make_pair(llfile, Qt::Checked)); //insert the check state into the map to query for changes later
QgsLegendSymbologyGroup * lsgroup = new QgsLegendSymbologyGroup(llayer,QString("Symbology"));
layer->setLegendSymbologyGroupParent(lsgroup);
QgsLegendPropertyGroup * lpgroup = new QgsLegendPropertyGroup(llayer,QString("Properties"));
Expand Down Expand Up @@ -542,7 +536,16 @@ void QgsLegend::legendLayerRemove()
{
return;
}

std::list<QgsMapLayer*> maplayers = ll->mapLayers();
mStateOfCheckBoxes.erase(ll);

//todo: also remove the entries for the QgsLegendLayerFiles from the map
std::list<QgsLegendLayerFile*> llfiles = ll->legendLayerFiles();
for(std::list<QgsLegendLayerFile*>::iterator it = llfiles.begin(); it != llfiles.end(); ++it)
{
mStateOfCheckBoxes.erase(*it);
}

for(std::list<QgsMapLayer*>::iterator it = maplayers.begin(); it!=maplayers.end(); ++it)
{
Expand Down Expand Up @@ -1165,6 +1168,10 @@ QTreeWidgetItem* QgsLegend::nextItem(QTreeWidgetItem* item)
{
return litem->nextSibling();
}
else if(!(litem->parent()))
{
return 0;
}
else if(dynamic_cast<QgsLegendItem*>(litem->parent())->nextSibling())
{
return (dynamic_cast<QgsLegendItem*>(litem->parent())->nextSibling());//todo: make this for deeper trees
Expand Down Expand Up @@ -1261,3 +1268,62 @@ std::deque<QString> QgsLegend::layerIDs()

return layers;
}

void QgsLegend::handleItemChange(QTreeWidgetItem* item, int row)
{
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(item);
if(llf)
{
#ifdef QGISDEBUG
qWarning("item is a QgsLegendLayerFile*");
#endif
std::map<QTreeWidgetItem*, Qt::CheckState>::iterator it = mStateOfCheckBoxes.find(item);
if(it != mStateOfCheckBoxes.end())
{
if(it->second != item->checkState(0)) //the checkState has changed
{
QgsMapLayer* theLayer = llf->layer();
if(theLayer)
{
bool checked = (item->checkState(0) == Qt::Checked);
theLayer->setVisible(checked);
}
mStateOfCheckBoxes[item] = item->checkState(0);
}
}
return;
}

QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(item);
if(ll)
{
#ifdef QGISDEBUG
qWarning("item is a QgsLegendLayer");
#endif
bool checked = (item->checkState(0) == Qt::Checked);
std::list<QgsLegendLayerFile*> llflist = ll->legendLayerFiles();
mMapCanvas->setRenderFlag(false);
for(std::list<QgsLegendLayerFile*>::iterator it = llflist.begin(); it != llflist.end(); ++it)
{
if(checked)
{
(*it)->setCheckState(0, Qt::Checked);
}
else
{
(*it)->setCheckState(0, Qt::Unchecked);
}
}
mMapCanvas->setRenderFlag(true);

}
}

void QgsLegend::openEditor()
{
QTreeWidgetItem* theItem = currentItem();
if(theItem)
{
openPersistentEditor(theItem, 0);
}
}
10 changes: 7 additions & 3 deletions src/legend/qgslegend.h
Expand Up @@ -28,7 +28,6 @@ class QgisApp;
class QgsLegendItem;
class QgsMapLayer;
class QgsMapCanvas;
class QCheckBox;
class QDomDocument;
class QDomNode;
class QMouseEvent;
Expand Down Expand Up @@ -230,6 +229,10 @@ this item may be moved back to the original position with resetToInitialPosition
void expandAll();
/**Sets all listview items to closed*/
void collapseAll();
/**Just for a test*/
void handleItemChange(QTreeWidgetItem* item, int row);
/**Calls openPersistentEditor for the current item*/
void openEditor();

private:

Expand Down Expand Up @@ -292,8 +295,9 @@ this item may be moved back to the original position with resetToInitialPosition
/**Pointer to the main canvas. Used for requiring repaints in case of legend changes*/
QgsMapCanvas* mMapCanvas;

/**Moves a checkbox to a position next to its listview*/
void placeCheckBox(QTreeWidgetItem* litem, QCheckBox* cbox);
/**Map that keeps track of which checkboxes are in which check state. This is necessary because QTreeView does not emit
a signal for check state changes*/
std::map<QTreeWidgetItem*, Qt::CheckState> mStateOfCheckBoxes;

signals:
void zOrderChanged(QgsLegend * lv);
Expand Down
2 changes: 2 additions & 0 deletions src/legend/qgslegendgroup.cpp
Expand Up @@ -30,6 +30,7 @@ QgsLegendGroup::QgsLegendGroup(QTreeWidgetItem * theItem ,QString theName)
#else
QString pkgDataPath(PKGDATAPATH);
#endif
setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
QIcon myIcon(pkgDataPath+QString("/images/icons/folder.png"));
setIcon(0, myIcon);
}
Expand All @@ -42,6 +43,7 @@ QgsLegendGroup::QgsLegendGroup(QTreeWidget* theListView, QString theString)
#else
QString pkgDataPath(PKGDATAPATH);
#endif
setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
QIcon myIcon(pkgDataPath+QString("/images/icons/folder.png"));
setIcon(0, myIcon);
}
Expand Down
41 changes: 41 additions & 0 deletions src/legend/qgslegendlayer.cpp
Expand Up @@ -33,7 +33,9 @@ QgsLegendLayer::QgsLegendLayer(QTreeWidgetItem* parent,QString name)
#else
QString pkgDataPath(PKGDATAPATH);
#endif
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
QIcon myIcon(pkgDataPath+QString("/images/icons/layer.png"));
setCheckState (0, Qt::Checked);
setText(0, name);
setIcon(0, myIcon);
}
Expand All @@ -46,6 +48,8 @@ QgsLegendLayer::QgsLegendLayer(QTreeWidget* parent, QString name): QObject(), Qg
#else
QString pkgDataPath(PKGDATAPATH);
#endif
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState (0, Qt::Checked);
QIcon myIcon(pkgDataPath+QString("/images/icons/layer.png"));
setText(0, name);
setIcon(0, myIcon);
Expand All @@ -59,6 +63,8 @@ QgsLegendLayer::QgsLegendLayer(QString name): QObject(), QgsLegendItem()
#else
QString pkgDataPath(PKGDATAPATH);
#endif
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState (0, Qt::Checked);
QIcon myIcon(pkgDataPath+QString("/images/icons/layer.png"));
setText(0, name);
setIcon(0, myIcon);
Expand Down Expand Up @@ -141,6 +147,14 @@ QgsMapLayer* QgsLegendLayer::firstMapLayer()
std::list<QgsMapLayer*> QgsLegendLayer::mapLayers()
{
std::list<QgsMapLayer*> list;
std::list<QgsLegendLayerFile*> llist = legendLayerFiles();
for(std::list<QgsLegendLayerFile*>::iterator it = llist.begin(); it != llist.end(); ++it)
{
list.push_back((*it)->layer());
}
return list;

#if 0
QTreeWidgetItem* llfgroup = QTreeWidgetItem::child(0); //the legend layer file group
if(!llfgroup)
{
Expand All @@ -162,4 +176,31 @@ std::list<QgsMapLayer*> QgsLegendLayer::mapLayers()
}
while(llf = llf->nextSibling());
return list;
#endif
}

std::list<QgsLegendLayerFile*> QgsLegendLayer::legendLayerFiles()
{
std::list<QgsLegendLayerFile*> list;
QTreeWidgetItem* llfgroup = QTreeWidgetItem::child(0); //the legend layer file group
if(!llfgroup)
{
return list;
}
QgsLegendItem* llf = dynamic_cast<QgsLegendItem*>(llfgroup->child(0));
if(!llf)
{
return list;
}
QgsLegendLayerFile* legendlayerfile = 0;
do
{
legendlayerfile = dynamic_cast<QgsLegendLayerFile*>(llf);
if(legendlayerfile)
{
list.push_back(legendlayerfile);
}
}
while(llf = llf->nextSibling());
return list;
}
3 changes: 3 additions & 0 deletions src/legend/qgslegendlayer.h
Expand Up @@ -24,6 +24,7 @@
#include <qgslegenditem.h>

class QgsLegendLayer;
class QgsLegendLayerFile;
class QgsLegendPropertyGroup;
class QgsMapLayer;
class QTreeWidget;
Expand All @@ -49,6 +50,8 @@ class QgsLegendLayer : public QgsLegendItem, public QObject //for signal/ slot
QgsMapLayer* firstMapLayer();
/**Returns the map layers associated with the QgsLegendLayerFiles*/
std::list<QgsMapLayer*> mapLayers();
/**Returns the legend layer file items associated with this legend layer*/
std::list<QgsLegendLayerFile*> legendLayerFiles();
};

#endif
2 changes: 1 addition & 1 deletion src/legend/qgslegendlayerfile.cpp
Expand Up @@ -42,7 +42,7 @@ QgsLegendLayerFile::QgsLegendLayerFile(QTreeWidgetItem * theLegendItem, QString
p.drawPixmap(0,0,inOverviewPixmap);
}
QIcon originalIcon(originalPixmap);
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState (0, Qt::Checked );
setText(0, theString);
setIcon(0, originalIcon);
Expand Down

0 comments on commit 9baf4e6

Please sign in to comment.