📄 qgslegend.cpp
字号:
clear(); //remove all items first mStateOfCheckBoxes.clear(); do { QDomElement childelem = child.toElement(); QString name = childelem.attribute("name"); //test every possibility of element... if(childelem.tagName()=="legendgroup") { QgsLegendGroup* theGroup = new QgsLegendGroup(this, name); childelem.attribute("open") == "true" ? expanded.push_back(theGroup) : collapsed.push_back(theGroup); //set the checkbox of the legend group to the right state blockSignals(true); QString checked = childelem.attribute("checked"); if(checked == "Qt::Checked") { theGroup->setCheckState(0, Qt::Checked); mStateOfCheckBoxes.insert(std::make_pair(theGroup, Qt::Checked)); } else if(checked == "Qt::Unchecked") { theGroup->setCheckState(0, Qt::Unchecked); mStateOfCheckBoxes.insert(std::make_pair(theGroup, Qt::Unchecked)); } else if(checked == "Qt::PartiallyChecked") { theGroup->setCheckState(0, Qt::PartiallyChecked); mStateOfCheckBoxes.insert(std::make_pair(theGroup, Qt::PartiallyChecked)); } blockSignals(false); lastGroup = theGroup; } else if(childelem.tagName()=="legendlayer") { //add the legendlayer to the legend (but no legendlayerfile yet, follows later) //if childelem is in a legendgroup element, add the layer to the group QgsLegendLayer* theLayer; if(child.parentNode().toElement().tagName() == "legendgroup") { theLayer = new QgsLegendLayer(lastGroup, name); } else { theLayer = new QgsLegendLayer(this, name); lastGroup = 0; } childelem.attribute("open") == "true" ? expanded.push_back(theLayer) : collapsed.push_back(theLayer); //set the checkbox of the legend layer to the right state blockSignals(true); QString checked = childelem.attribute("checked", "Qt::Checked"); // Default is to show if(checked == "Qt::Checked") { theLayer->setCheckState(0, Qt::Checked); mStateOfCheckBoxes.insert(std::make_pair(theLayer, Qt::Checked)); } else if(checked == "Qt::Unchecked") { theLayer->setCheckState(0, Qt::Unchecked); mStateOfCheckBoxes.insert(std::make_pair(theLayer, Qt::Unchecked)); } else if(checked == "Qt::PartiallyChecked") { theLayer->setCheckState(0, Qt::PartiallyChecked); mStateOfCheckBoxes.insert(std::make_pair(theLayer, Qt::PartiallyChecked)); } blockSignals(false); lastLayer = theLayer; } else if(childelem.tagName()=="legendlayerfile") { //find out the legendlayer QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(childelem.attribute("layerid")); if(theMapLayer == NULL) //the layer cannot be found (e.g. the file has been moved) { // remove the whole legendlayer if this is the only legendlayerfile if(childelem.previousSibling().isNull() && childelem.nextSibling().isNull()) { collapsed.remove(lastLayer); expanded.remove(lastLayer); delete lastLayer; lastLayer=0; } } else if(lastLayerFileGroup) { QgsLegendLayerFile* theLegendLayerFile = new QgsLegendLayerFile(lastLayerFileGroup, QgsLegendLayerFile::nameFromLayer(theMapLayer), theMapLayer); // load layer's visibility and 'show in overview' flag theLegendLayerFile->setVisible(atoi(childelem.attribute("visible", "1"))); //Default is visible theLegendLayerFile->setInOverview(atoi(childelem.attribute("inOverview"))); // set the check state blockSignals(true); if(theLegendLayerFile->isVisible()) { mStateOfCheckBoxes.insert(std::make_pair(theLegendLayerFile, Qt::Checked)); theLegendLayerFile->setCheckState(0, Qt::Checked); } else { mStateOfCheckBoxes.insert(std::make_pair(theLegendLayerFile, Qt::Unchecked)); theLegendLayerFile->setCheckState(0, Qt::Unchecked); } blockSignals(false); //set the layer type icon if this legendlayerfile is the last in the file group if(child.nextSibling().isNull()) { static_cast<QgsLegendLayer*>(theLegendLayerFile->parent()->parent())->setLayerTypeIcon(); } theLegendLayerFile->updateLegendItem(); refreshLayerSymbology(theMapLayer->getLayerID()); } } else if(childelem.tagName()=="filegroup") { QgsLegendLayerFileGroup* theFileGroup = new QgsLegendLayerFileGroup(lastLayer, "Files"); childelem.attribute("open") == "true" ? expandItem(theFileGroup) : collapseItem(theFileGroup); childelem.attribute("hidden") == "true" ? theFileGroup->setHidden(true) : theFileGroup->setHidden(false); lastLayerFileGroup = theFileGroup; } else if(childelem.tagName() == "propertygroup") { QgsLegendPropertyGroup* thePropertyGroup = new QgsLegendPropertyGroup(lastLayer, "Properties"); childelem.attribute("open") == "true" ? expandItem(thePropertyGroup) : collapseItem(thePropertyGroup); } child = nextDomNode(child); } while(!(child.isNull())); } // Do the tree item expands and collapses. for (int i = 0; i < expanded.size(); ++i) expandItem(expanded[i]); for (int i = 0; i < collapsed.size(); ++i) collapseItem(collapsed[i]); return true;}void QgsLegend::storeInitialPosition(QTreeWidgetItem* li){ if(li == firstItem()) //the item is the first item in the list view { mRestoreInformation = FIRST_ITEM; mRestoreItem = 0; } else if(li->parent() == 0) //li is a toplevel item, but not the first one { mRestoreInformation = YOUNGER_SIBLING; mRestoreItem = ((QgsLegendItem*)(li))->findYoungerSibling(); } else if(li == li->parent()->child(0))//li is not a toplevel item, but the first child { mRestoreInformation = FIRST_CHILD; mRestoreItem = li->parent(); } else { mRestoreInformation = YOUNGER_SIBLING; mRestoreItem = ((QgsLegendItem*)(li))->findYoungerSibling(); } mLayersPriorToMove = layerIDs();}void QgsLegend::resetToInitialPosition(QTreeWidgetItem* li){ QgsLegendItem* formerParent = dynamic_cast<QgsLegendItem*>(li->parent()); //todo: make sure legend layers are updated if(mRestoreInformation == FIRST_ITEM) {#ifdef QGISDEBUG qWarning("FIRST_ITEM");#endif removeItem(li); insertTopLevelItem(0, li); } else if(mRestoreInformation == FIRST_CHILD) {#ifdef QGISDEBUG qWarning("FIRST_CHILD");#endif removeItem(li); if(formerParent) { formerParent->release((QgsLegendItem*)li); } mRestoreItem->insertChild(0, li); ((QgsLegendItem*)mRestoreItem)->receive((QgsLegendItem*)li); } else if(mRestoreInformation == YOUNGER_SIBLING) {#ifdef QGISDEBUG qWarning("YOUNGER_SIBLING");#endif if(formerParent) { formerParent->release((QgsLegendItem*)li); } dynamic_cast<QgsLegendItem*>(li)->moveItem(dynamic_cast<QgsLegendItem*>(mRestoreItem)); if(mRestoreItem->parent()) { ((QgsLegendItem*)(mRestoreItem->parent()))->receive((QgsLegendItem*)li); } }}QgsLegendLayer* QgsLegend::findLegendLayer(const QString& layerKey){ QgsLegendLayer* theLegendLayer = 0; std::list<QgsMapLayer*> theMapLayers; QTreeWidgetItem* theItem = firstItem(); do { theLegendLayer = dynamic_cast<QgsLegendLayer*>(theItem); if(theLegendLayer) //item is a legend layer { theMapLayers = theLegendLayer->mapLayers(); for(std::list<QgsMapLayer*>::iterator it = theMapLayers.begin(); it != theMapLayers.end(); ++it) { if((*it)->getLayerID() == layerKey) { return theLegendLayer; } } } } while((theItem = nextItem(theItem))); 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); int height = rect.height(); int top = rect.top(); int mid = top + (height / 2); if (ycoord > mid) //bottom, remember the y-coordinate increases downwards { return false; } else//top { return true; }}/**Returns the first item in the hierarchy*/QTreeWidgetItem* QgsLegend::firstItem(){ return topLevelItem(0);}/**Returns the next item (next sibling or next item on level above)*/QTreeWidgetItem* QgsLegend::nextItem(QTreeWidgetItem* item){ QgsLegendItem* litem = dynamic_cast<QgsLegendItem*>(item); if(!litem) { return 0; } else if(litem->childCount() > 0) { return litem->child(0); } else if(litem->nextSibling()) { return litem->nextSibling(); } else if(!(litem->parent())) { return 0; } //go to other levels to find the next item else if(litem->parent() && ((QgsLegendItem*)(litem->parent()))->nextSibling()) { return (dynamic_cast<QgsLegendItem*>(litem->parent())->nextSibling()); } else if(litem->parent() && litem->parent()->parent() && ((QgsLegendItem*)(litem->parent()->parent()))->nextSibling()) { return (dynamic_cast<QgsLegendItem*>(litem->parent()->parent())->nextSibling()); } else if(litem->parent() && litem->parent()->parent() && litem->parent()->parent()->parent() &&\ ((QgsLegendItem*)(litem->parent()->parent()->parent()))->nextSibling())//maximum four nesting states in the current legend { return (dynamic_cast<QgsLegendItem*>(litem->parent()->parent()->parent())->nextSibling()); } else { return 0; }}QTreeWidgetItem* QgsLegend::nextSibling(QTreeWidgetItem* item){ QModelIndex thisidx = indexFromItem(item); QModelIndex nextsidx = thisidx.sibling(thisidx.row()+1, thisidx.column()); if(nextsidx.isValid()) { return dynamic_cast<QgsLegendItem*>(itemFromIndex(nextsidx)); } else { return 0; }}QTreeWidgetItem* QgsLegend::previousSibling(QTreeWidgetItem* item){ QModelIndex thisidx = indexFromItem(item); QModelIndex nextsidx = thisidx.sibling(thisidx.row()-1, thisidx.column()); if(nextsidx.isValid()) { return dynamic_cast<QgsLegendItem*>(itemFromIndex(nextsidx)); } else { return 0; }}QDomNode QgsLegend::nextDomNode(const QDomNode& theNode){ if(!theNode.firstChild().isNull()) { return (theNode.firstChild()); } QDomNode currentNode = theNode; do { if(!currentNode.nextSibling().isNull()) { return currentNode.nextSibling(); } currentNode = currentNode.parentNode(); } while(!currentNode.isNull()); QDomNode nullNode; return nullNode;}void QgsLegend::insertItem(QTreeWidgetItem* move, QTreeWidgetItem* into){ QgsLegendItem* movedItem = dynamic_cast<QgsLegendItem*>(move); QgsLegendItem* intoItem = dynamic_cast<QgsLegendItem*>(into); if(movedItem && intoItem) { QgsLegendItem* parentItem = dynamic_cast<QgsLegendItem*>(movedItem->parent()); movedItem->storeAppearanceSettings();//store settings in the moved item and its children removeItem(movedItem); intoItem->insert(movedItem); if(parentItem) { parentItem->release(movedItem); //give the former parent item the possibility to do cleanups } intoItem->receive(movedItem); movedItem->restoreAppearanceSettings();//apply the settings again }}void QgsLegend::moveItem(QTreeWidgetItem* move, QTreeWidgetItem* after){ static_cast<QgsLegendItem*>(move)->storeAppearanceSettings();//store settings in the moved item and its childern if(move->parent()) { move->parent()->takeChild(move->parent()->indexOfChild(move)); } else //move is toplevel item { takeTopLevelItem(indexOfTopLevelItem(move)); } if(after->parent()) { after->parent()->insertChild(after->parent()->indexOfChild(after)+1, move); } else //toplevel item { insertTopLevelItem(indexOfTopLevelItem(after)+1, move); } static_cast<QgsLegendItem*>(move)->restoreAppearanceSettings();//apply the settings again}void QgsLegend::removeItem(QTreeWidgetItem* item){ if(item->parent()) { item->parent()->takeChild(item->parent()->indexOfChild(item)); } else { takeTopLevelItem(indexOfTopLevelItem(item)); }}void QgsLegend::updateMapCanvasLayerSet(){ //std::deque<QString> layers = layerIDs(); QList<QgsMapCanvasLayer> layers; // create list of the layers QTreeWidgetItem* theItem = firstItem(); while (theItem) { QgsLegendItem *li = dynamic_cast<QgsLegendItem*>(theItem); QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(li); if(llf) { QgsMapCanvasLayer& lyr = llf->canvasLayer(); layers.append(lyr); } theItem = nextItem(theItem); } // set layers in canvas mMapCanvas->setLayerSet(layers);}void QgsLegend::updateOverview(){ mMapCanvas->updateOverview();}void QgsLegend::setOverviewAllLayers(bool inOverview){ QTreeWidgetItem* theItem = firstItem(); while(theItem) { QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(theItem); if(llf) { llf->setInOverview(inOverview);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -