|
36 | 36 | #include "qgsvectorfilewriter.h"
|
37 | 37 | #include "qgsgenericprojectionselector.h"
|
38 | 38 | #include "qgsattributetabledialog.h"
|
39 |
| -#include "qgsencodingfiledialog.h" |
| 39 | +#include "ogr/qgsvectorlayersaveasdialog.h" |
40 | 40 |
|
41 | 41 | #include "qgsrendererv2.h"
|
42 | 42 | #include "qgssymbolv2.h"
|
|
53 | 53 | #include <QMessageBox>
|
54 | 54 | #include <QPainter>
|
55 | 55 | #include <QSettings>
|
| 56 | +#include <QFileDialog> |
56 | 57 |
|
57 | 58 |
|
58 | 59 | QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
|
@@ -450,16 +451,6 @@ void QgsLegendLayer::table()
|
450 | 451 | // the dialog will be deleted by itself on close
|
451 | 452 | }
|
452 | 453 |
|
453 |
| -void QgsLegendLayer::saveAsShapefile() |
454 |
| -{ |
455 |
| - saveAsShapefileGeneral( FALSE ); |
456 |
| -} |
457 |
| - |
458 |
| -void QgsLegendLayer::saveSelectionAsShapefile() |
459 |
| -{ |
460 |
| - saveAsShapefileGeneral( TRUE ); |
461 |
| -} |
462 |
| - |
463 | 454 | void QgsLegendLayer::saveAsVectorFile()
|
464 | 455 | {
|
465 | 456 | saveAsVectorFileGeneral( false );
|
@@ -504,177 +495,73 @@ void QgsLegendLayer::showInOverview()
|
504 | 495 |
|
505 | 496 | void QgsLegendLayer::saveAsVectorFileGeneral( bool saveOnlySelection )
|
506 | 497 | {
|
| 498 | + QgsCoordinateReferenceSystem destCRS; |
| 499 | + |
507 | 500 | if ( mLyr.layer()->type() != QgsMapLayer::VectorLayer )
|
508 | 501 | return;
|
509 | 502 |
|
510 | 503 | QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( mLyr.layer() );
|
511 | 504 |
|
512 |
| - //get output name and format |
513 |
| - QSettings settings; |
514 |
| - QString filter = QString( "Shapefiles (*.shp)" ); |
515 |
| - QString dirName = settings.value( "/UI/lastVectorfileDir", "." ).toString(); |
516 |
| - QString filterString = QgsVectorFileWriter::fileFilterString(); |
517 |
| - QString selectedFilter = settings.value( "/UI/lastVectorFilter", "[OGR] ESRI Shapefiles (*.shp *.SHP)" ).toString(); |
518 |
| - QString outputFile = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), dirName, filterString, &selectedFilter ); |
519 |
| - if ( outputFile.isNull() ) |
520 |
| - { |
521 |
| - return; //cancelled |
522 |
| - } |
523 |
| - |
524 |
| - settings.setValue( "/UI/lastVectorfileDir", QFileInfo( outputFile ).absolutePath() ); |
525 |
| - settings.setValue( "/UI/lastVectorFilter", selectedFilter ); |
| 505 | + QgsVectorLayerSaveAsDialog *dialog = new QgsVectorLayerSaveAsDialog( QgisApp::instance() ); |
526 | 506 |
|
527 |
| - QMap< QString, QString> filterDriverMap = QgsVectorFileWriter::supportedFiltersAndFormats(); |
528 |
| - QMap< QString, QString>::const_iterator it = filterDriverMap.find( selectedFilter + ";;" ); |
529 |
| - if ( it == filterDriverMap.constEnd() ) |
530 |
| - { |
531 |
| - return; //unknown format |
532 |
| - } |
533 |
| - |
534 |
| - QString driverKey = *it; |
535 |
| - |
536 |
| - //output CRS |
537 |
| - QgsCoordinateReferenceSystem destCRS = vlayer->srs(); |
538 |
| - // Find out if we have projections enabled or not |
539 |
| - if ( QgisApp::instance()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled() ) |
540 |
| - { |
541 |
| - destCRS = QgisApp::instance()->mapCanvas()->mapRenderer()->destinationSrs(); |
542 |
| - } |
543 |
| - |
544 |
| - QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector(); |
545 |
| - mySelector->setSelectedCrsId( destCRS.srsid() ); |
546 |
| - mySelector->setMessage( tr( "Select the coordinate reference system for the saved shapefile. " |
547 |
| - "The data points will be transformed from the layer coordinate reference system." ) ); |
548 |
| - |
549 |
| - if ( mySelector->exec() ) |
550 |
| - { |
551 |
| - QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId ); |
552 |
| - destCRS = srs; |
553 |
| - // destCRS->createFromId(mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId) |
554 |
| - } |
555 |
| - else |
| 507 | + if( dialog->exec() == QDialog::Accepted ) |
556 | 508 | {
|
557 |
| - // Aborted CS selection, don't save. |
558 |
| - delete mySelector; |
559 |
| - return; |
560 |
| - } |
561 |
| - delete mySelector; |
| 509 | + QString encoding = dialog->encoding(); |
| 510 | + QString vectorFilename = dialog->filename(); |
| 511 | + QString format = dialog->format(); |
| 512 | + |
| 513 | + if( dialog->crs() < 0 ) |
| 514 | + { |
| 515 | + // Find out if we have projections enabled or not |
| 516 | + if ( QgisApp::instance()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled() ) |
| 517 | + { |
| 518 | + destCRS = QgisApp::instance()->mapCanvas()->mapRenderer()->destinationSrs(); |
| 519 | + } |
| 520 | + else |
| 521 | + { |
| 522 | + destCRS = vlayer->srs(); |
| 523 | + } |
| 524 | + } |
| 525 | + else |
| 526 | + { |
| 527 | + destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId ); |
| 528 | + } |
562 | 529 |
|
563 |
| - // overwrite the file - user will already have been prompted |
564 |
| - // to verify they want to overwrite by the file dialog above |
565 |
| - // might not even exists in the given case. |
566 |
| - if ( driverKey == "ESRI Shapefile" ) |
567 |
| - { |
| 530 | + // overwrite the file - user will already have been prompted |
| 531 | + // to verify they want to overwrite by the file dialog above |
| 532 | + // might not even exists in the given case. |
568 | 533 | // add the extension if not present
|
569 |
| - if ( !outputFile.endsWith( ".shp", Qt::CaseInsensitive ) ) |
| 534 | + if( format == "ESRI Shapefile" ) |
570 | 535 | {
|
571 |
| - outputFile += ".shp"; |
| 536 | + if ( !vectorFilename.endsWith( ".shp", Qt::CaseInsensitive ) ) |
| 537 | + { |
| 538 | + vectorFilename += ".shp"; |
| 539 | + } |
| 540 | + QgsVectorFileWriter::deleteShapeFile( vectorFilename ); |
| 541 | + } |
| 542 | + |
| 543 | + // ok if the file existed it should be deleted now so we can continue... |
| 544 | + QApplication::setOverrideCursor( Qt::WaitCursor ); |
| 545 | + |
| 546 | + QgsVectorFileWriter::WriterError error; |
| 547 | + QString errorMessage; |
| 548 | + error = QgsVectorFileWriter::writeAsVectorFormat( vlayer, vectorFilename, encoding, &destCRS, format, saveOnlySelection, &errorMessage ); |
| 549 | + |
| 550 | + QApplication::restoreOverrideCursor(); |
| 551 | + |
| 552 | + if ( error == QgsVectorFileWriter::NoError ) |
| 553 | + { |
| 554 | + QMessageBox::information( 0, tr( "Saving done" ), tr( "Export to vector file has been completed" ) ); |
| 555 | + } |
| 556 | + else |
| 557 | + { |
| 558 | + QMessageBox::warning( 0, tr( "Save error" ), tr( "Export to vector file failed.\nError: %1").arg( errorMessage ) ); |
572 | 559 | }
|
573 |
| - QgsVectorFileWriter::deleteShapeFile( outputFile ); |
574 |
| - } |
575 |
| - |
576 |
| - QString errorMessage; |
577 |
| - QgsVectorFileWriter::WriterError error; |
578 |
| - error = QgsVectorFileWriter::writeAsVectorFormat( vlayer, outputFile, "utf-8", &destCRS, driverKey, saveOnlySelection, &errorMessage ); |
579 |
| -} |
580 |
| - |
581 |
| - |
582 |
| -void QgsLegendLayer::saveAsShapefileGeneral( bool saveOnlySelection ) |
583 |
| -{ |
584 |
| - QgsCoordinateReferenceSystem destCRS; |
585 |
| - |
586 |
| - if ( mLyr.layer()->type() != QgsMapLayer::VectorLayer ) |
587 |
| - return; |
588 |
| - |
589 |
| - QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( mLyr.layer() ); |
590 |
| - |
591 |
| - // get a name for the shapefile |
592 |
| - // Get a file to process, starting at the current directory |
593 |
| - QSettings settings; |
594 |
| - QString filter = QString( "Shapefiles (*.shp)" ); |
595 |
| - QString dirName = settings.value( "/UI/lastShapefileDir", "." ).toString(); |
596 |
| - |
597 |
| - QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( 0, |
598 |
| - tr( "Save layer as..." ), |
599 |
| - dirName, |
600 |
| - filter, |
601 |
| - QString( "" ) ); |
602 |
| - openFileDialog->setAcceptMode( QFileDialog::AcceptSave ); |
603 |
| - |
604 |
| - // allow for selection of more than one file |
605 |
| - //openFileDialog->setMode(QFileDialog::AnyFile); |
606 |
| - |
607 |
| - if ( openFileDialog->exec() != QDialog::Accepted ) |
608 |
| - return; |
609 |
| - |
610 |
| - |
611 |
| - QString encoding = openFileDialog->encoding(); |
612 |
| - QString shapefileName = openFileDialog->selectedFiles().first(); |
613 |
| - settings.setValue( "/UI/lastShapefileDir", QFileInfo( shapefileName ).absolutePath() ); |
614 |
| - |
615 |
| - |
616 |
| - if ( shapefileName.isNull() ) |
617 |
| - return; |
618 |
| - |
619 |
| - // add the extension if not present |
620 |
| - if ( !shapefileName.endsWith( ".shp", Qt::CaseInsensitive ) ) |
621 |
| - { |
622 |
| - shapefileName += ".shp"; |
623 |
| - } |
624 |
| - |
625 |
| - destCRS = vlayer->srs(); |
626 |
| - // Find out if we have projections enabled or not |
627 |
| - if ( QgisApp::instance()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled() ) |
628 |
| - { |
629 |
| - destCRS = QgisApp::instance()->mapCanvas()->mapRenderer()->destinationSrs(); |
630 |
| - } |
631 |
| - |
632 |
| - QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector(); |
633 |
| - mySelector->setSelectedCrsId( destCRS.srsid() ); |
634 |
| - mySelector->setMessage( tr( "Select the coordinate reference system for the saved shapefile. " |
635 |
| - "The data points will be transformed from the layer coordinate reference system." ) ); |
636 |
| - |
637 |
| - if ( mySelector->exec() ) |
638 |
| - { |
639 |
| - QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId ); |
640 |
| - destCRS = srs; |
641 |
| - // destCRS->createFromId(mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId) |
642 |
| - } |
643 |
| - else |
644 |
| - { |
645 |
| - // Aborted CS selection, don't save. |
646 |
| - delete mySelector; |
647 |
| - return; |
648 | 560 | }
|
649 | 561 |
|
650 |
| - delete mySelector; |
651 |
| - |
652 |
| - // overwrite the file - user will already have been prompted |
653 |
| - // to verify they want to overwrite by the file dialog above |
654 |
| - // might not even exists in the given case. |
655 |
| - QgsVectorFileWriter::deleteShapeFile( shapefileName ); |
656 |
| - |
657 |
| - // ok if the file existed it should be deleted now so we can continue... |
658 |
| - QApplication::setOverrideCursor( Qt::WaitCursor ); |
659 |
| - |
660 |
| - QgsVectorFileWriter::WriterError error; |
661 |
| - QString errorMessage; |
662 |
| - error = QgsVectorFileWriter::writeAsShapefile( vlayer, shapefileName, encoding, &destCRS, saveOnlySelection, &errorMessage ); |
663 |
| - |
664 |
| - QApplication::restoreOverrideCursor(); |
665 |
| - |
666 |
| - if ( error == QgsVectorFileWriter::NoError ) |
667 |
| - { |
668 |
| - QMessageBox::information( 0, tr( "Saving done" ), tr( "Export to Shapefile has been completed" ) ); |
669 |
| - } |
670 |
| - else |
671 |
| - { |
672 |
| - QMessageBox::warning( 0, tr( "Save error" ), errorMessage ); |
673 |
| - } |
| 562 | + delete dialog; |
674 | 563 | }
|
675 | 564 |
|
676 |
| - |
677 |
| - |
678 | 565 | QString QgsLegendLayer::nameFromLayer( QgsMapLayer* layer )
|
679 | 566 | {
|
680 | 567 | QString sourcename = layer->publicSource(); //todo: move this duplicated code into a new function
|
|
0 commit comments