QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
20 
21 QString QgsCreateDirectoryAlgorithm::name() const
22 {
23  return QStringLiteral( "createdirectory" );
24 }
25 
26 QgsProcessingAlgorithm::Flags QgsCreateDirectoryAlgorithm::flags() const
27 {
28  return FlagHideFromToolbox | FlagSkipGenericModelLogging;
29 }
30 
31 QString QgsCreateDirectoryAlgorithm::displayName() const
32 {
33  return QObject::tr( "Create directory" );
34 }
35 
36 QStringList QgsCreateDirectoryAlgorithm::tags() const
37 {
38  return QObject::tr( "new,make,folder" ).split( ',' );
39 }
40 
41 QString QgsCreateDirectoryAlgorithm::group() const
42 {
43  return QObject::tr( "Modeler tools" );
44 }
45 
46 QString QgsCreateDirectoryAlgorithm::groupId() const
47 {
48  return QStringLiteral( "modelertools" );
49 }
50 
51 QString QgsCreateDirectoryAlgorithm::shortHelpString() const
52 {
53  return QObject::tr( "This algorithm creates a new directory on a file system. Directories will be created recursively, creating all "
54  "required parent directories in order to construct the full specified directory path.\n\n"
55  "No errors will be raised if the directory already exists." );
56 }
57 
58 QString QgsCreateDirectoryAlgorithm::shortDescription() const
59 {
60  return QObject::tr( "Creates a new directory on a file system." );
61 }
62 
63 QgsCreateDirectoryAlgorithm *QgsCreateDirectoryAlgorithm::createInstance() const
64 {
65  return new QgsCreateDirectoryAlgorithm();
66 }
67 
68 void QgsCreateDirectoryAlgorithm::initAlgorithm( const QVariantMap & )
69 {
70  addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "PATH" ), QObject::tr( "Directory path" ) ) );
71  addOutput( new QgsProcessingOutputFolder( QStringLiteral( "OUTPUT" ), QObject::tr( "Directory" ) ) );
72 }
73 
74 QVariantMap QgsCreateDirectoryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75 {
76  const QString path = parameterAsString( parameters, QStringLiteral( "PATH" ), context );
77 
78  if ( !path.isEmpty() )
79  {
80  if ( QFile::exists( path ) )
81  {
82  if ( !QFileInfo( path ).isDir() )
83  throw QgsProcessingException( QObject::tr( "A file with the name %1 already exists -- cannot create a new directory here." ).arg( path ) );
84  if ( feedback )
85  feedback->pushInfo( QObject::tr( "The directory %1 already exists." ).arg( path ) );
86  }
87  else
88  {
89  if ( !QDir().mkpath( path ) )
90  {
91  throw QgsProcessingException( QObject::tr( "Could not create directory %1. Please check that you have write permissions for the specified path." ).arg( path ) );
92  }
93 
94  if ( feedback )
95  feedback->pushInfo( QObject::tr( "Created %1" ).arg( path ) );
96  }
97  }
98 
99  QVariantMap results;
100  results.insert( QStringLiteral( "OUTPUT" ), path );
101  return results;
102 }
103 
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsProcessingParameterMapLayer
A map layer parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2901
qgsalgorithmcreatedirectory.h
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingOutputFolder
A folder output for processing algorithms.
Definition: qgsprocessingoutputs.h:378
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82