QGIS API Documentation 3.99.0-Master (357b655ed83)
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( "This algorithm creates a new directory on a file system. Directories will be created recursively, creating all "
58 "required parent directories in order to construct the full specified directory path.\n\n"
59 "No errors will be raised if the directory already exists." );
60}
61
62QString QgsCreateDirectoryAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Creates a new directory on a file system." );
65}
66
67QgsCreateDirectoryAlgorithm *QgsCreateDirectoryAlgorithm::createInstance() const
68{
69 return new QgsCreateDirectoryAlgorithm();
70}
71
72void QgsCreateDirectoryAlgorithm::initAlgorithm( const QVariantMap & )
73{
74 addParameter( new QgsProcessingParameterMapLayer( u"PATH"_s, QObject::tr( "Directory path" ) ) );
75 addOutput( new QgsProcessingOutputFolder( u"OUTPUT"_s, QObject::tr( "Directory" ) ) );
76}
77
78QVariantMap QgsCreateDirectoryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
79{
80 const QString path = parameterAsString( parameters, u"PATH"_s, context );
81
82 if ( !path.isEmpty() )
83 {
84 if ( QFile::exists( path ) )
85 {
86 if ( !QFileInfo( path ).isDir() )
87 throw QgsProcessingException( QObject::tr( "A file with the name %1 already exists -- cannot create a new directory here." ).arg( path ) );
88 if ( feedback )
89 feedback->pushInfo( QObject::tr( "The directory %1 already exists." ).arg( path ) );
90 }
91 else
92 {
93 if ( !QDir().mkpath( path ) )
94 {
95 throw QgsProcessingException( QObject::tr( "Could not create directory %1. Please check that you have write permissions for the specified path." ).arg( path ) );
96 }
97
98 if ( feedback )
99 feedback->pushInfo( QObject::tr( "Created %1" ).arg( path ) );
100 }
101 }
102
103 QVariantMap results;
104 results.insert( u"OUTPUT"_s, path );
105 return results;
106}
107
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3680
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3654
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition qgis.h:3665
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.