mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-11-04 00:04:25 -05:00 
			
		
		
		
	Tweak behavior of new shapefile/gpkg browser actions
This commit is contained in:
		
							parent
							
								
									d0622743ff
								
							
						
					
					
						commit
						17ced9186a
					
				@ -28,7 +28,7 @@ Encapsulates the context in which a QgsDataItem is shown within the application
 | 
			
		||||
Constructor for QgsDataItemGuiContext.
 | 
			
		||||
%End
 | 
			
		||||
 | 
			
		||||
    QgsMessageBar *messageBar();
 | 
			
		||||
    QgsMessageBar *messageBar() const;
 | 
			
		||||
%Docstring
 | 
			
		||||
Returns the associated message bar.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -67,6 +67,14 @@ Sets the ``behavior`` to use when a path to an existing geopackage file is used.
 | 
			
		||||
The default behavior is to prompt the user for an action to take.
 | 
			
		||||
 | 
			
		||||
.. versionadded:: 3.0
 | 
			
		||||
%End
 | 
			
		||||
 | 
			
		||||
    void setAddToProject( bool addToProject );
 | 
			
		||||
%Docstring
 | 
			
		||||
Sets whether a newly created layer should automatically be added to the current project.
 | 
			
		||||
Defaults to true.
 | 
			
		||||
 | 
			
		||||
.. versionadded:: 3.6
 | 
			
		||||
%End
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -22,6 +22,7 @@
 | 
			
		||||
#include "qgsgui.h"
 | 
			
		||||
#include "qgsnative.h"
 | 
			
		||||
#include "qgisapp.h"
 | 
			
		||||
#include "qgsmessagebar.h"
 | 
			
		||||
#include "qgsnewnamedialog.h"
 | 
			
		||||
#include "qgsbrowsermodel.h"
 | 
			
		||||
#include "qgsbrowserdockwidget_p.h"
 | 
			
		||||
@ -29,6 +30,7 @@
 | 
			
		||||
#include "qgsrasterlayer.h"
 | 
			
		||||
#include "qgsnewvectorlayerdialog.h"
 | 
			
		||||
#include "qgsnewgeopackagelayerdialog.h"
 | 
			
		||||
#include "qgsfileutils.h"
 | 
			
		||||
#include <QMenu>
 | 
			
		||||
#include <QInputDialog>
 | 
			
		||||
#include <QMessageBox>
 | 
			
		||||
@ -40,7 +42,7 @@ QString QgsAppDirectoryItemGuiProvider::name()
 | 
			
		||||
  return QStringLiteral( "directory_items" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
 | 
			
		||||
void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext context )
 | 
			
		||||
{
 | 
			
		||||
  if ( item->type() != QgsDataItem::Directory )
 | 
			
		||||
    return;
 | 
			
		||||
@ -84,8 +86,15 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
 | 
			
		||||
    QDir dir( directoryItem->dirPath() );
 | 
			
		||||
    dialog.setDatabasePath( dir.filePath( QStringLiteral( "new_geopackage" ) ) );
 | 
			
		||||
    dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
 | 
			
		||||
    dialog.setAddToProject( false );
 | 
			
		||||
    if ( dialog.exec() )
 | 
			
		||||
    {
 | 
			
		||||
      QString file = dialog.databasePath();
 | 
			
		||||
      file = QgsFileUtils::ensureFileNameHasExtension( file, QStringList() << QStringLiteral( "gpkg" ) );
 | 
			
		||||
      context.messageBar()->pushSuccess( tr( "New GeoPackage" ), tr( "Created <a href=\"%1\">%2</a>" ).arg(
 | 
			
		||||
                                           QUrl::fromLocalFile( file ).toString(), QDir::toNativeSeparators( file ) ) );
 | 
			
		||||
      item->refresh();
 | 
			
		||||
    }
 | 
			
		||||
  } );
 | 
			
		||||
  newMenu->addAction( createGpkg );
 | 
			
		||||
 | 
			
		||||
@ -97,7 +106,11 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
 | 
			
		||||
    QDir dir( directoryItem->dirPath() );
 | 
			
		||||
    const QString newFile = QgsNewVectorLayerDialog::runAndCreateLayer( QgisApp::instance(), &enc, QgsProject::instance()->defaultCrsForNewLayers(), dir.filePath( QStringLiteral( "new_layer.shp" ) ) );
 | 
			
		||||
    if ( !newFile.isEmpty() )
 | 
			
		||||
    {
 | 
			
		||||
      context.messageBar()->pushSuccess( tr( "New ShapeFile" ), tr( "Created <a href=\"%1\">%2</a>" ).arg(
 | 
			
		||||
                                           QUrl::fromLocalFile( newFile ).toString(), QDir::toNativeSeparators( newFile ) ) );
 | 
			
		||||
      item->refresh();
 | 
			
		||||
    }
 | 
			
		||||
  } );
 | 
			
		||||
  newMenu->addAction( createShp );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@
 | 
			
		||||
// QgsDataItemGuiContext
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
QgsMessageBar *QgsDataItemGuiContext::messageBar()
 | 
			
		||||
QgsMessageBar *QgsDataItemGuiContext::messageBar() const
 | 
			
		||||
{
 | 
			
		||||
  return mMessageBar;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -48,7 +48,7 @@ class GUI_EXPORT QgsDataItemGuiContext
 | 
			
		||||
     *
 | 
			
		||||
     * \see setMessageBar()
 | 
			
		||||
     */
 | 
			
		||||
    QgsMessageBar *messageBar();
 | 
			
		||||
    QgsMessageBar *messageBar() const;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the associated message \a bar.
 | 
			
		||||
 | 
			
		||||
@ -483,22 +483,28 @@ bool QgsNewGeoPackageLayerDialog::apply()
 | 
			
		||||
 | 
			
		||||
  QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
 | 
			
		||||
  QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
 | 
			
		||||
  QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
 | 
			
		||||
  std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
 | 
			
		||||
  if ( layer->isValid() )
 | 
			
		||||
  {
 | 
			
		||||
    if ( mAddToProject )
 | 
			
		||||
    {
 | 
			
		||||
      // register this layer with the central layers registry
 | 
			
		||||
      QList<QgsMapLayer *> myList;
 | 
			
		||||
    myList << layer;
 | 
			
		||||
      myList << layer.release();
 | 
			
		||||
      //addMapLayers returns a list of all successfully added layers
 | 
			
		||||
      //so we compare that to our original list.
 | 
			
		||||
      if ( myList == QgsProject::instance()->addMapLayers( myList ) )
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    if ( !property( "hideDialogs" ).toBool() )
 | 
			
		||||
      QMessageBox::critical( this, tr( "New GeoPackage Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
 | 
			
		||||
    delete layer;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return false;
 | 
			
		||||
@ -509,6 +515,11 @@ void QgsNewGeoPackageLayerDialog::setOverwriteBehavior( OverwriteBehavior behavi
 | 
			
		||||
  mBehavior = behavior;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void QgsNewGeoPackageLayerDialog::setAddToProject( bool addToProject )
 | 
			
		||||
{
 | 
			
		||||
  mAddToProject = addToProject;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void QgsNewGeoPackageLayerDialog::showHelp()
 | 
			
		||||
{
 | 
			
		||||
  QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );
 | 
			
		||||
 | 
			
		||||
@ -76,6 +76,14 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
 | 
			
		||||
     */
 | 
			
		||||
    void setOverwriteBehavior( OverwriteBehavior behavior );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets whether a newly created layer should automatically be added to the current project.
 | 
			
		||||
     * Defaults to true.
 | 
			
		||||
     *
 | 
			
		||||
     * \since QGIS 3.6
 | 
			
		||||
     */
 | 
			
		||||
    void setAddToProject( bool addToProject );
 | 
			
		||||
 | 
			
		||||
  private slots:
 | 
			
		||||
    void mAddAttributeButton_clicked();
 | 
			
		||||
    void mRemoveAttributeButton_clicked();
 | 
			
		||||
@ -100,6 +108,7 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
 | 
			
		||||
    bool mTableNameEdited = false;
 | 
			
		||||
    bool mLayerIdentifierEdited = false;
 | 
			
		||||
    OverwriteBehavior mBehavior = Prompt;
 | 
			
		||||
    bool mAddToProject = true;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // QGSNEWVECTORLAYERDIALOG_H
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user