📄 qgslegendlayer.cpp
字号:
}void QgsLegendLayer::vectorLayerSymbology(const QgsVectorLayer* layer){ SymbologyList itemList; //add the new items QString lw, uv, label; const QgsRenderer* renderer = layer->renderer(); const QList<QgsSymbol*> sym = renderer->symbols(); for(QList<QgsSymbol*>::const_iterator it=sym.begin(); it!=sym.end(); ++it) { QImage img; if((*it)->type() == QGis::Point) { img = (*it)->getPointSymbolAsImage(); } else if((*it)->type() == QGis::Line) { img = (*it)->getLineSymbolAsImage(); } else //polygon { img = (*it)->getPolygonSymbolAsImage(); } QString values; lw = (*it)->lowerValue(); if(!lw.isEmpty()) { values += lw; } uv = (*it)->upperValue(); if(!uv.isEmpty()) { values += " - "; values += uv; } label = (*it)->label(); if(!label.isEmpty()) { values += " "; values += label; } QPixmap pix = QPixmap::fromImage(img); // convert to pixmap itemList.push_back(std::make_pair(values, pix)); } if(renderer->needsAttributes()) //create an item for each classification field (only one for most renderers) { QgsAttributeList classfieldlist = renderer->classificationAttributes(); const QgsFieldMap& fields = layer->getDataProvider()->fields(); for(QgsAttributeList::iterator it = classfieldlist.begin(); it!=classfieldlist.end(); ++it) { const QgsField& theField = fields[*it]; QString classfieldname = theField.name(); itemList.push_front(std::make_pair(classfieldname, QPixmap())); } } changeSymbologySettings(layer, itemList);}void QgsLegendLayer::rasterLayerSymbology(QgsRasterLayer* layer){ SymbologyList itemList; QPixmap legendpixmap = layer->getLegendQPixmap(true).scaled(20, 20, Qt::KeepAspectRatio); itemList.push_back(std::make_pair("", legendpixmap)); changeSymbologySettings(layer, itemList);}void QgsLegendLayer::updateIcon(){ QPixmap newIcon(getOriginalPixmap()); QgsMapLayer* theLayer = firstMapLayer(); QgsLegendLayerFile* theFile = firstLayerFile(); if(mapLayers().size() == 1) { //overview if(theFile->isInOverview()) { // Overlay the overview icon on the default icon QPixmap myPixmap(QgsApplication::themePath()+"mIconOverview.png"); QPainter p(&newIcon); p.drawPixmap(0,0,myPixmap); p.end(); } //editable if(theLayer->isEditable()) { // Overlay the editable icon on the default icon QPixmap myPixmap(QgsApplication::themePath()+"mIconEditable.png"); QPainter p(&newIcon); p.drawPixmap(0,0,myPixmap); p.end(); } } QIcon theIcon(newIcon); setIcon(0, theIcon);}QPixmap QgsLegendLayer::getOriginalPixmap() const{ QgsMapLayer* firstLayer = firstMapLayer(); if(firstLayer) { QString myThemePath = QgsApplication::themePath(); QString myPath; if (firstLayer->type() == QgsMapLayer::VECTOR) { QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>(firstLayer); switch(vlayer->vectorType()) { case QGis::Point: myPath = myThemePath+"/mIconPointLayer.png"; break; case QGis::Line: myPath = myThemePath+"/mIconLineLayer.png"; break; case QGis::Polygon: myPath = myThemePath+"/mIconPolygonLayer.png"; break; default: myPath = myThemePath+"/mIconLayer.png"; } } else // RASTER { myPath = myThemePath+"/mIconLayer.png"; } QFileInfo file(myPath); if(file.exists()) { return QPixmap(file.absoluteFilePath()); } } QPixmap emptyPixmap; return emptyPixmap;}void QgsLegendLayer::addToPopupMenu(QMenu& theMenu, QAction* toggleEditingAction){ QString iconsPath = QgsApplication::themePath(); std::list<QgsLegendLayerFile*> files = legendLayerFiles(); QgsMapLayer* firstLayer = NULL; if (files.size() > 0) { firstLayer = files.front()->layer(); } // zoom to layer extent theMenu.addAction(QIcon(iconsPath+QString("/mActionZoomToLayer.png")), tr("&Zoom to layer extent"), legend(), SLOT(legendLayerZoom())); if (firstLayer && firstLayer->type() == QgsMapLayer::RASTER) { theMenu.addAction(tr("&Zoom to best scale (100%)"), legend(), SLOT(legendLayerZoomNative())); } // show in overview QAction* showInOverviewAction = theMenu.addAction(tr("&Show in overview"), this, SLOT(showInOverview())); showInOverviewAction->setCheckable(true); // doesn't support tristate showInOverviewAction->setChecked(isInOverview()); // remove from canvas theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionRemove.png"))), tr("&Remove"), legend(), SLOT(legendLayerRemove())); theMenu.addSeparator(); if (firstLayer && firstLayer->type() == QgsMapLayer::VECTOR) { // attribute table QAction* tableAction = theMenu.addAction(tr("&Open attribute table"), this, SLOT(table())); if (files.size() != 1) { tableAction->setEnabled(false); } // allow editing if(toggleEditingAction) { theMenu.addAction(toggleEditingAction); } QgsVectorLayer* theVectorLayer = dynamic_cast<QgsVectorLayer*>(firstLayer); if (files.size() != 1) { toggleEditingAction->setEnabled(false); } if (theVectorLayer) { toggleEditingAction->setChecked(theVectorLayer->isEditable()); } // save as shapefile QAction* saveShpAction = theMenu.addAction(tr("Save as shapefile..."), this, SLOT(saveAsShapefile())); if (files.size() != 1) { saveShpAction->setEnabled(false); } // save selection as shapefile QAction* saveSelectionAction = theMenu.addAction(tr("Save selection as shapefile..."), this, SLOT(saveSelectionAsShapefile())); if (files.size() != 1 || theVectorLayer->selectedFeatureCount() == 0) { saveSelectionAction->setEnabled(false); } theMenu.addSeparator(); } QAction* propertiesAction = theMenu.addAction(tr("&Properties"), legend(), SLOT(legendLayerShowProperties())); if (files.size() != 1) { propertiesAction->setEnabled(false); }}bool QgsLegendLayer::isInOverview(){ // QAction doesn't support tristate checkboxes // returns true if just some are in overview bool hasVisible = false; // find out whether we're showing or hiding them std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles(); for (std::list<QgsLegendLayerFile*>::iterator it = maplayers.begin(); it!=maplayers.end(); ++it) { if (*it && (*it)->isInOverview()) { hasVisible = true; break; } } return hasVisible;}void QgsLegendLayer::showInOverview(){ std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles(); bool showLayers = ( ! isInOverview() ); // set overview visibility for (std::list<QgsLegendLayerFile*>::iterator it = maplayers.begin(); it!=maplayers.end(); ++it) { if (*it) (*it)->setInOverview(showLayers); } legend()->updateMapCanvasLayerSet(); legend()->updateOverview();}void QgsLegendLayer::table(){ std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles(); if (maplayers.size() > 1) { QMessageBox::information(0, tr("More layers"), tr("This item contains more layer files. Displaying more layers in table is not supported.")); } else if (maplayers.size() == 1) { maplayers.front()->table(); }}void QgsLegendLayer::saveAsShapefile(){ std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles(); if (maplayers.size() == 1) { maplayers.front()->saveAsShapefile(); }}void QgsLegendLayer::saveSelectionAsShapefile(){ std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles(); if (maplayers.size() == 1) { maplayers.front()->saveSelectionAsShapefile(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -