⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qgslegend.cpp

📁 一个非常好的GIS开源新版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	}      theItem = nextItem(theItem);    }  updateMapCanvasLayerSet();  updateOverview();}std::deque<QString> QgsLegend::layerIDs(){  std::deque<QString> layers;  QTreeWidgetItem* theItem = firstItem();  while (theItem)    {      QgsLegendItem *li = dynamic_cast<QgsLegendItem*>(theItem);      QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(li);      if(llf)	{	  QgsMapLayer *lyr = llf->layer();	  layers.push_front(lyr->getLayerID());	}      theItem = nextItem(theItem);    }  #ifdef QGISDEBUG  qWarning("QgsLegend::layerIDs()");  for(std::deque<QString>::iterator it = layers.begin(); it != layers.end(); ++it)    {      qWarning(*it);    }#endif  return layers;}void QgsLegend::refreshLayerSymbology(QString key, bool expandItem){  QgsLegendLayer* theLegendLayer = findLegendLayer(key);  if(!theLegendLayer)  {    return;  }    //store the current item  QTreeWidgetItem* theCurrentItem = currentItem();  theLegendLayer->refreshSymbology(key);    //restore the current item again  setCurrentItem(theCurrentItem);  adjustIconSize();  if (expandItem)    {      setItemExpanded(theLegendLayer, true);//make sure the symbology items are visible    }}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,                        QString layerName){  if (legendLayerFile)  {    QTreeWidgetItem* p = legendLayerFile->parent();    if (p)    {      p = p->parent();      if (p)        p->setText(0, layerName);    }  }}void QgsLegend::handleItemChange(QTreeWidgetItem* item, int row){  if(!item)    {      return;    }  closePersistentEditor(item, row);  //if the text of a QgsLegendLayer has changed, change the display names of all its maplayers  QgsLegendLayer* theLegendLayer = dynamic_cast<QgsLegendLayer*>(item); //item is a legend layer  if(theLegendLayer)    {       std::list<QgsMapLayer*> theMapLayers = theLegendLayer->mapLayers();       for(std::list<QgsMapLayer*>::iterator it = theMapLayers.begin(); it != theMapLayers.end(); ++it)	 {	   (*it)->setLayerName(theLegendLayer->text(0));	 }    }  std::map<QTreeWidgetItem*, Qt::CheckState>::iterator it = mStateOfCheckBoxes.find(item);  if (it == mStateOfCheckBoxes.end())    return;      // has the checkState changed?  if (it->second == item->checkState(0))	 return;	    QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(item); //item is a layer file  if(llf)  {    if(llf->layer())		{		  llf->setVisible(item->checkState(0) == Qt::Checked);		}    //update check state of the legend layer    QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(item->parent()->parent());    if(ll)		{		  ll->updateCheckState();		  mStateOfCheckBoxes[ll] = ll->checkState(0);		}    //update check state of the legend group (if any)    if(item->parent()->parent()->parent())		{		  QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup*>(item->parent()->parent()->parent());		  if(lg)		    {		      lg->updateCheckState();		      mStateOfCheckBoxes[lg] = lg->checkState(0);		    }		}    mStateOfCheckBoxes[item] = item->checkState(0);    // Setting the renderFlag to true will trigger a render,    // so only do this if the flag is alread set to true.     if (mMapCanvas->renderFlag())      mMapCanvas->setRenderFlag(true);  }	    std::list<QgsLegendLayerFile*> subfiles;  QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup*>(item); //item is a legend group  if(lg)  {    //set all the child layer files to the new check state    subfiles = lg->legendLayerFiles();    bool renderFlagState = mMapCanvas->renderFlag();    mMapCanvas->setRenderFlag(false);    for(std::list<QgsLegendLayerFile*>::iterator iter = subfiles.begin(); iter != subfiles.end(); ++iter)		{#ifdef QGISDEBUG		  if(item->checkState(0) == Qt::Checked)		    {		      qWarning("item checked");		    }		  else if(item->checkState(0) == Qt::Unchecked)		    {		      qWarning("item unchecked");		    }		  else if(item->checkState(0) == Qt::PartiallyChecked)		    {		      qWarning("item partially checked");		    }#endif		  blockSignals(true);		  (*iter)->setCheckState(0, item->checkState(0));		  blockSignals(false);		  mStateOfCheckBoxes[(*iter)] = item->checkState(0);		  if((*iter)->layer())      {        (*iter)->setVisible(item->checkState(0) == Qt::Checked);      }		}	          //update the check states of all child legend layers    for(int i = 0; i < lg->childCount(); ++i)		{		  static_cast<QgsLegendLayer*>(lg->child(i))->updateCheckState();		  mStateOfCheckBoxes[lg->child(i)] = lg->child(i)->checkState(0);		}    // If it was on, turn it back on, otherwise leave it    // off, as turning it on causes a refresh.    if (renderFlagState)      mMapCanvas->setRenderFlag(true);    mStateOfCheckBoxes[item] = item->checkState(0);  }    QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(item); //item is a legend layer  if(ll)  {    //set all the child layer files to the new check state    subfiles = ll->legendLayerFiles();    bool renderFlagState = mMapCanvas->renderFlag();    mMapCanvas->freeze(true);    mMapCanvas->setRenderFlag(false);    for(std::list<QgsLegendLayerFile*>::iterator iter = subfiles.begin(); iter != subfiles.end(); ++iter)		{		  blockSignals(true);		  (*iter)->setCheckState(0, item->checkState(0));		  blockSignals(false);		  mStateOfCheckBoxes[(*iter)] = item->checkState(0);		  if((*iter)->layer())      {        (*iter)->setVisible(item->checkState(0) == Qt::Checked);      }		}    if(ll->parent())		{		  static_cast<QgsLegendGroup*>(ll->parent())->updateCheckState();		  mStateOfCheckBoxes[ll->parent()] = ll->parent()->checkState(0);		}    // If it was on, turn it back on, otherwise leave it    // off, as turning it on causes a refresh.    if (renderFlagState)      {	mMapCanvas->setRenderFlag(true);      }    mMapCanvas->freeze(false);    //update check state of the legend group    mStateOfCheckBoxes[item] = item->checkState(0);  }      // update layer set  updateMapCanvasLayerSet();}void QgsLegend::openEditor(){  QTreeWidgetItem* theItem = currentItem();  if(theItem)    {      openPersistentEditor(theItem, 0);    }}void QgsLegend::makeToTopLevelItem(){  QgsLegendItem* theItem = dynamic_cast<QgsLegendItem*>(currentItem());  if(theItem)    {      theItem->storeAppearanceSettings();      removeItem(theItem);      addTopLevelItem(theItem);      theItem->restoreAppearanceSettings();    }}void QgsLegend::showLegendLayerFileGroups(){  // Toggle the boolean associated with the checkbox  mShowLegendLayerFiles = !mShowLegendLayerFiles;  QgsLegendLayerFileGroup* theFileGroup = 0;  QTreeWidgetItem* theItem = firstItem();    if(!theItem)    {      return;    }  do    {      // This call seems to fix a bug in Qt4.2 (qgis trac #405) whereby the      // setHidden() call in the if statement below doesn't result in      // correct drawing of the visible file group part of the tree,      // but doing this setHidden() call does result in correct drawing.      theItem->setHidden(false);      theFileGroup = dynamic_cast<QgsLegendLayerFileGroup*>(theItem);      if(theFileGroup)      {        theFileGroup->setHidden(!mShowLegendLayerFiles);      }    }  while((theItem = nextItem(theItem)));}void QgsLegend::legendLayerZoom(){  std::list<QgsLegendLayerFile*> layerFiles;    //find current Layer  QgsLegendLayer* currentLayer=dynamic_cast<QgsLegendLayer*>(currentItem());  if (currentLayer)  {    layerFiles = currentLayer->legendLayerFiles();  }  else  {    QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(currentItem());    if (llf)    {      // user selected legend layer file - use just that one      layerFiles.push_back(llf);    }  }    if(layerFiles.size() == 0)    return;    QgsMapLayer* theLayer;  bool first(true);  QgsRect extent;    for(std::list<QgsLegendLayerFile*>::iterator it= layerFiles.begin(); it != layerFiles.end(); ++it)  {    theLayer = (*it)->layer();    if (!theLayer)      continue;          QgsRect lyrExtent = mMapCanvas->mapRender()->layerExtentToOutputExtent(theLayer, theLayer->extent());        if (!lyrExtent.isFinite())      lyrExtent = theLayer->extent();      if (first)    {      extent = lyrExtent;      first = false;    }    else    {      extent.combineExtentWith(&lyrExtent);    }  }  // Increase bounding box with 5%, so that layer is a bit inside the borders  extent.scale(1.05);   //zoom to bounding box  mMapCanvas->setExtent(extent);  mMapCanvas->refresh();   // notify the project we've made a change  QgsProject::instance()->dirty(true);}void QgsLegend::legendLayerZoomNative(){  QgsLegendItem* citem=dynamic_cast<QgsLegendItem*>(currentItem());  if(!citem)  {      return;  }  QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(citem);  if(!ll)  {    return;  }  QgsRasterLayer *layer =  dynamic_cast<QgsRasterLayer*>(ll->firstMapLayer());   if(layer)  {    QgsDebugMsg("Raster units per pixel  : " + QString::number(layer->rasterUnitsPerPixel()));    QgsDebugMsg("Mupp before             : " + QString::number(mMapCanvas->mupp()));    mMapCanvas->zoom(fabs( layer->rasterUnitsPerPixel() / mMapCanvas->mupp()));    mMapCanvas->refresh();    QgsDebugMsg("Mupp after              : " + QString::number(mMapCanvas->mupp()));    // notify the project we've made a change    QgsProject::instance()->dirty(true);  }}void QgsLegend::legendLayerAttributeTable(){  if(!mMapCanvas || mMapCanvas->isDrawing())    {      return;    }  // try whether it's a legend layer  QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(currentItem());  if (ll)  {    ll->table();    return;  }    // try whether it's a legend layer file  QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(currentItem());  if (llf)  {    llf->table();    return;  }  // nothing selected  QMessageBox::information(this, tr("No Layer Selected"),                           tr("To open an attribute table, you must select a vector layer in the legend"));}void QgsLegend::initPixmaps(){  QString myThemePath = QgsApplication::themePath();  mPixmaps.mOriginalPixmap.load(myThemePath + "/mActionFileSmall.png");  mPixmaps.mInOverviewPixmap.load(myThemePath + "/mActionInOverview.png");  mPixmaps.mEditablePixmap.load(myThemePath + "/mIconEditable.png");  mPixmaps.mProjectionErrorPixmap.load(myThemePath + "/mIconProjectionProblem.png");}void QgsLegend::readProject(const QDomDocument & doc){  QDomNodeList nodes = doc.elementsByTagName("legend");  if (nodes.count())  {    QDomNode node = nodes.item(0);    readXML(node);        // update canvas layers to match the order defined by legend    updateMapCanvasLayerSet();  }  else  {    QgsDebugMsg("Couldn't read legend information from project");  }}  void QgsLegend::writeProject(QDomDocument & doc){  QDomNodeList nl = doc.elementsByTagName("qgis");  if (!nl.count())  {    QgsDebugMsg("Unable to find qgis element in project file");    return;  }  QDomNode qgisNode = nl.item(0);  // there should only be one, so zeroth element ok    QDomElement mapcanvasNode = doc.createElement("legend");  qgisNode.appendChild(mapcanvasNode);  writeXML(mapcanvasNode, doc);}bool QgsLegend::checkLayerOrderUpdate(){  std::deque<QString> layersAfterRelease = layerIDs(); //test if canvas redraw is really necessary  if(layersAfterRelease != mLayersPriorToMove)    {      // z-order has changed - update layer set      updateMapCanvasLayerSet();      return true;    }  return false;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -