Skip to content

Commit

Permalink
Added saving layer's selection as a shapefile. Can be invoked from le…
Browse files Browse the repository at this point in the history
…gend.

git-svn-id: http://svn.osgeo.org/qgis/trunk@6940 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 10, 2007
1 parent 7ef07b3 commit 213e4d4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsvectorfilewriter.sip
Expand Up @@ -27,7 +27,8 @@ public:
/** Write contents of vector layer to a shapefile */
static WriterError writeAsShapefile(QgsVectorLayer* layer,
const QString& shapefileName,
const QString& fileEncoding);
const QString& fileEncoding,
bool onlySelected = FALSE);


/** create shapefile and initialize it */
Expand Down
20 changes: 18 additions & 2 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -513,10 +513,17 @@ void QgsLegendLayer::addToPopupMenu(QMenu& theMenu)
}

// save as shapefile
theMenu.addAction(tr("Save as shapefile..."), this, SLOT(saveAsShapefile()));
QAction* saveShpAction = theMenu.addAction(tr("Save as shapefile..."), this, SLOT(saveAsShapefile()));
if (files.size() != 1)
{
tableAction->setEnabled(false);
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();
Expand Down Expand Up @@ -598,3 +605,12 @@ void QgsLegendLayer::saveAsShapefile()
maplayers.front()->saveAsShapefile();
}
}

void QgsLegendLayer::saveSelectionAsShapefile()
{
std::list<QgsLegendLayerFile*> maplayers = legendLayerFiles();
if (maplayers.size() == 1)
{
maplayers.front()->saveSelectionAsShapefile();
}
}
1 change: 1 addition & 0 deletions src/app/legend/qgslegendlayer.h
Expand Up @@ -91,6 +91,7 @@ class QgsLegendLayer : public QgsLegendItem
void toggleEditing();

void saveAsShapefile();
void saveSelectionAsShapefile();

protected:

Expand Down
22 changes: 21 additions & 1 deletion src/app/legend/qgslegendlayerfile.cpp
Expand Up @@ -293,6 +293,16 @@ void QgsLegendLayerFile::closeTable(bool onlyGeometryWasChanged)
}

void QgsLegendLayerFile::saveAsShapefile()
{
saveAsShapefileGeneral(FALSE);
}

void QgsLegendLayerFile::saveSelectionAsShapefile()
{
saveAsShapefileGeneral(TRUE);
}

void QgsLegendLayerFile::saveAsShapefileGeneral(bool saveOnlySelection)
{
if (mLyr.layer()->type() != QgsMapLayer::VECTOR)
return;
Expand Down Expand Up @@ -333,8 +343,12 @@ void QgsLegendLayerFile::saveAsShapefile()
shapefileName += ".shp";
}

QApplication::setOverrideCursor(Qt::waitCursor);

QgsVectorFileWriter::WriterError error;
error = QgsVectorFileWriter::writeAsShapefile(vlayer, shapefileName, encoding);
error = QgsVectorFileWriter::writeAsShapefile(vlayer, shapefileName, encoding, saveOnlySelection);

QApplication::restoreOverrideCursor();

switch (error)
{
Expand Down Expand Up @@ -460,6 +474,12 @@ void QgsLegendLayerFile::addToPopupMenu(QMenu& theMenu)
// save as shapefile
theMenu.addAction(tr("Save as shapefile..."), this, SLOT(saveAsShapefile()));

QAction* saveSelectionAction = theMenu.addAction(tr("Save selection as shapefile..."), this, SLOT(saveSelectionAsShapefile()));
if (vlayer->selectedFeatureCount() == 0)
{
saveSelectionAction->setEnabled(false);
}

theMenu.addSeparator();
}
else if (lyr->type() == QgsMapLayer::RASTER)
Expand Down
6 changes: 6 additions & 0 deletions src/app/legend/qgslegendlayerfile.h
Expand Up @@ -83,6 +83,9 @@ class QgsLegendLayerFile : public QgsLegendItem
/**Save as shapefile*/
void saveAsShapefile();

/**Save selection as shapefile*/
void saveSelectionAsShapefile();

/**Toggle editing for layer*/
void toggleEditing();

Expand All @@ -94,6 +97,9 @@ class QgsLegendLayerFile : public QgsLegendItem

protected:

/**Save as shapefile (called from saveAsShapefile and saveSelectionAsShapefile)*/
void saveAsShapefileGeneral(bool saveOnlySelection);

/** layer identified by its layer id */
QgsMapCanvasLayer mLyr;

Expand Down
9 changes: 7 additions & 2 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -292,7 +292,8 @@ QgsVectorFileWriter::~QgsVectorFileWriter()
QgsVectorFileWriter::WriterError
QgsVectorFileWriter::writeAsShapefile(QgsVectorLayer* layer,
const QString& shapefileName,
const QString& fileEncoding)
const QString& fileEncoding,
bool onlySelected)
{

QgsVectorDataProvider* provider = layer->getDataProvider();
Expand All @@ -313,14 +314,18 @@ QgsVectorFileWriter::WriterError

provider->select(allAttr, QgsRect(), true);

const QgsFeatureIds& ids = layer->selectedFeaturesIds();

// write all features
while (provider->getNextFeature(fet))
{
if (onlySelected && !ids.contains(fet.featureId()))
continue;

writer->addFeature(fet);
}

delete writer;

return NoError;
}

3 changes: 2 additions & 1 deletion src/core/qgsvectorfilewriter.h
Expand Up @@ -53,7 +53,8 @@ class CORE_EXPORT QgsVectorFileWriter
/** Write contents of vector layer to a shapefile */
static WriterError writeAsShapefile(QgsVectorLayer* layer,
const QString& shapefileName,
const QString& fileEncoding);
const QString& fileEncoding,
bool onlySelected = FALSE);


/** create shapefile and initialize it */
Expand Down

0 comments on commit 213e4d4

Please sign in to comment.