QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmcreatedirectory.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcreatedirectory.cpp
3 ---------------------
4 begin : June 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18
19#include <QString>
20
21using namespace Qt::StringLiterals;
22
24
25QString QgsCreateDirectoryAlgorithm::name() const
26{
27 return u"createdirectory"_s;
28}
29
30Qgis::ProcessingAlgorithmFlags QgsCreateDirectoryAlgorithm::flags() const
31{
33}
34
35QString QgsCreateDirectoryAlgorithm::displayName() const
36{
37 return QObject::tr( "Create directory" );
38}
39
40QStringList QgsCreateDirectoryAlgorithm::tags() const
41{
42 return QObject::tr( "new,make,folder" ).split( ',' );
43}
44
45QString QgsCreateDirectoryAlgorithm::group() const
46{
47 return QObject::tr( "Modeler tools" );
48}
49
50QString QgsCreateDirectoryAlgorithm::groupId() const
51{
52 return u"modelertools"_s;
53}
54
55QString QgsCreateDirectoryAlgorithm::shortHelpString() const
56{
57 return QObject::tr(
58 "This algorithm creates a new directory on a file system. Directories will be created recursively, creating all "
59 "required parent directories in order to construct the full specified directory path.\n\n"
60 "No errors will be raised if the directory already exists."
61 );
62}
63
64QString QgsCreateDirectoryAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Creates a new directory on a file system." );
67}
68
69QgsCreateDirectoryAlgorithm *QgsCreateDirectoryAlgorithm::createInstance() const
70{
71 return new QgsCreateDirectoryAlgorithm();
72}
73
74void QgsCreateDirectoryAlgorithm::initAlgorithm( const QVariantMap & )
75{
76 addParameter( new QgsProcessingParameterMapLayer( u"PATH"_s, QObject::tr( "Directory path" ) ) );
77 addOutput( new QgsProcessingOutputFolder( u"OUTPUT"_s, QObject::tr( "Directory" ) ) );
78}
79
80QVariantMap QgsCreateDirectoryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
81{
82 const QString path = parameterAsString( parameters, u"PATH"_s, context );
83
84 if ( !path.isEmpty() )
85 {
86 if ( QFile::exists( path ) )
87 {
88 if ( !QFileInfo( path ).isDir() )
89 throw QgsProcessingException( QObject::tr( "A file with the name %1 already exists -- cannot create a new directory here." ).arg( path ) );
90 if ( feedback )
91 feedback->pushInfo( QObject::tr( "The directory %1 already exists." ).arg( path ) );
92 }
93 else
94 {
95 if ( !QDir().mkpath( path ) )
96 {
97 throw QgsProcessingException( QObject::tr( "Could not create directory %1. Please check that you have write permissions for the specified path." ).arg( path ) );
98 }
99
100 if ( feedback )
101 feedback->pushInfo( QObject::tr( "Created %1" ).arg( path ) );
102 }
103 }
104
105 QVariantMap results;
106 results.insert( u"OUTPUT"_s, path );
107 return results;
108}
109
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3724
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3698
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3709
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A folder output for processing algorithms.
A map layer parameter for processing algorithms.