Skip to content

Commit 40443eb

Browse files
committedNov 4, 2018
[FEATURE][browser] Add "New" menu to context menu on directories
With option to create a new geopackage or shapefile in the clicked directory
1 parent 348c1dc commit 40443eb

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed
 

‎python/gui/auto_generated/qgsnewvectorlayerdialog.sip.in

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ class QgsNewVectorLayerDialog: QDialog
1717
%End
1818
public:
1919

20-
static QString runAndCreateLayer( QWidget *parent = 0, QString *enc = 0, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
20+
static QString runAndCreateLayer( QWidget *parent = 0, QString *enc = 0, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
21+
const QString &initialPath = QString() );
2122
%Docstring
2223
Runs the dialog and creates a layer matching the dialog parameters.
2324

25+
If the ``initialPath`` argument is specified, then the dialog will default to the specified filename.
26+
2427
:return: fileName on success, empty string use aborted, QString() if creation failed
2528
%End
2629

@@ -42,9 +45,21 @@ Returns the file format for storage
4245
%Docstring
4346
Returns the file format for storage
4447
%End
48+
4549
QString filename() const;
4650
%Docstring
4751
Returns the name for the new layer
52+
53+
.. seealso:: :py:func:`setFilename`
54+
%End
55+
56+
void setFilename( const QString &filename );
57+
%Docstring
58+
Sets the initial file name to show in the dialog.
59+
60+
.. seealso:: :py:func:`filename`
61+
62+
.. versionadded:: 3.6
4863
%End
4964

5065
QgsCoordinateReferenceSystem crs() const;

‎src/app/browser/qgsinbuiltdataitemproviders.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "qgsbrowserdockwidget_p.h"
2828
#include "qgswindowmanagerinterface.h"
2929
#include "qgsrasterlayer.h"
30+
#include "qgsnewvectorlayerdialog.h"
31+
#include "qgsnewgeopackagelayerdialog.h"
3032
#include <QMenu>
3133
#include <QInputDialog>
3234
#include <QMessageBox>
@@ -47,8 +49,9 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
4749

4850
QgsSettings settings;
4951

52+
QMenu *newMenu = new QMenu( tr( "New" ), menu );
5053

51-
QAction *createFolder = new QAction( tr( "New Directory…" ), menu );
54+
QAction *createFolder = new QAction( tr( "Directory…" ), menu );
5255
connect( createFolder, &QAction::triggered, this, [ = ]
5356
{
5457
bool ok = false;
@@ -71,7 +74,34 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
7174
}
7275
}
7376
} );
74-
menu->addAction( createFolder );
77+
newMenu->addAction( createFolder );
78+
79+
QAction *createGpkg = new QAction( tr( "GeoPackage…" ), newMenu );
80+
createGpkg->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionNewGeoPackageLayer.svg" ) ) );
81+
connect( createGpkg, &QAction::triggered, this, [ = ]
82+
{
83+
QgsNewGeoPackageLayerDialog dialog( QgisApp::instance() );
84+
QDir dir( directoryItem->dirPath() );
85+
dialog.setDatabasePath( dir.filePath( QStringLiteral( "new_geopackage" ) ) );
86+
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
87+
if ( dialog.exec() )
88+
item->refresh();
89+
} );
90+
newMenu->addAction( createGpkg );
91+
92+
QAction *createShp = new QAction( tr( "ShapeFile…" ), newMenu );
93+
createShp->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionNewVectorLayer.svg" ) ) );
94+
connect( createShp, &QAction::triggered, this, [ = ]
95+
{
96+
QString enc;
97+
QDir dir( directoryItem->dirPath() );
98+
const QString newFile = QgsNewVectorLayerDialog::runAndCreateLayer( QgisApp::instance(), &enc, QgsProject::instance()->defaultCrsForNewLayers(), dir.filePath( QStringLiteral( "new_layer.shp" ) ) );
99+
if ( !newFile.isEmpty() )
100+
item->refresh();
101+
} );
102+
newMenu->addAction( createShp );
103+
104+
menu->addMenu( newMenu );
75105

76106
menu->addSeparator();
77107

@@ -97,7 +127,6 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
97127
renameFavorite( favoriteItem );
98128
} );
99129
menu->addAction( actionRename );
100-
menu->addSeparator();
101130
QAction *removeFavoriteAction = new QAction( tr( "Remove Favorite" ), menu );
102131
connect( removeFavoriteAction, &QAction::triggered, this, [ = ]
103132
{

‎src/gui/qgsnewvectorlayerdialog.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <QComboBox>
3131
#include <QLibrary>
3232
#include <QFileDialog>
33-
33+
#include <QMessageBox>
3434

3535
QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFlags fl )
3636
: QDialog( parent, fl )
@@ -104,6 +104,7 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
104104

105105
mFileName->setStorageMode( QgsFileWidget::SaveFile );
106106
mFileName->setFilter( QgsVectorFileWriter::filterForDriver( mFileFormatComboBox->currentData( Qt::UserRole ).toString() ) );
107+
mFileName->setConfirmOverwrite( false );
107108
mFileName->setDialogTitle( tr( "Save Layer As" ) );
108109
mFileName->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
109110
connect( mFileName, &QgsFileWidget::fileChanged, this, [ = ]
@@ -245,22 +246,33 @@ QString QgsNewVectorLayerDialog::filename() const
245246
return mFileName->filePath();
246247
}
247248

249+
void QgsNewVectorLayerDialog::setFilename( const QString &filename )
250+
{
251+
mFileName->setFilePath( filename );
252+
}
253+
248254
void QgsNewVectorLayerDialog::checkOk()
249255
{
250256
bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
251257
mOkButton->setEnabled( ok );
252258
}
253259

254260
// this is static
255-
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pEnc, const QgsCoordinateReferenceSystem &crs )
261+
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pEnc, const QgsCoordinateReferenceSystem &crs, const QString &initialPath )
256262
{
257263
QgsNewVectorLayerDialog geomDialog( parent );
258264
geomDialog.setCrs( crs );
265+
if ( !initialPath.isEmpty() )
266+
geomDialog.setFilename( initialPath );
259267
if ( geomDialog.exec() == QDialog::Rejected )
260268
{
261269
return QString();
262270
}
263271

272+
if ( QFile::exists( geomDialog.filename() ) && QMessageBox::warning( parent, tr( "New ShapeFile Layer" ), tr( "The layer already exists. Are you sure you want to overwrite the existing file?" ),
273+
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
274+
return QString();
275+
264276
QgsWkbTypes::Type geometrytype = geomDialog.selectedType();
265277
QString fileformat = geomDialog.selectedFileFormat();
266278
QString enc = geomDialog.selectedFileEncoding();

‎src/gui/qgsnewvectorlayerdialog.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
3636

3737
/**
3838
* Runs the dialog and creates a layer matching the dialog parameters.
39+
*
40+
* If the \a initialPath argument is specified, then the dialog will default to the specified filename.
41+
*
3942
* \returns fileName on success, empty string use aborted, QString() if creation failed
4043
*/
41-
static QString runAndCreateLayer( QWidget *parent = nullptr, QString *enc = nullptr, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
44+
static QString runAndCreateLayer( QWidget *parent = nullptr, QString *enc = nullptr, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
45+
const QString &initialPath = QString() );
4246

4347
QgsNewVectorLayerDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
4448
~QgsNewVectorLayerDialog() override;
@@ -50,9 +54,23 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
5054
QString selectedFileFormat() const;
5155
//! Returns the file format for storage
5256
QString selectedFileEncoding() const;
53-
//! Returns the name for the new layer
57+
58+
/**
59+
* Returns the name for the new layer
60+
*
61+
* \see setFilename()
62+
*/
5463
QString filename() const;
5564

65+
/**
66+
* Sets the initial file name to show in the dialog.
67+
*
68+
* \see filename()
69+
*
70+
* \since QGIS 3.6
71+
*/
72+
void setFilename( const QString &filename );
73+
5674
/**
5775
* Returns the selected CRS for the new layer.
5876
* \see setCrs()

0 commit comments

Comments
 (0)
Please sign in to comment.