mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-11-04 00:04:25 -05:00 
			
		
		
		
	[processing][needs-docs] replace set raster style and set vector style
Python algorithms with generics set layer style C++ algorithm
This commit is contained in:
		
							parent
							
								
									b85fe90955
								
							
						
					
					
						commit
						c974125fd9
					
				@ -48,7 +48,7 @@ class SetRasterStyle(QgisAlgorithm):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
 | 
			
		||||
    def flags(self):
 | 
			
		||||
        return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
 | 
			
		||||
        return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagHideFromToolbox
 | 
			
		||||
 | 
			
		||||
    def initAlgorithm(self, config=None):
 | 
			
		||||
        self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,7 @@ class SetVectorStyle(QgisAlgorithm):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
 | 
			
		||||
    def flags(self):
 | 
			
		||||
        return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
 | 
			
		||||
        return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagHideFromToolbox
 | 
			
		||||
 | 
			
		||||
    def initAlgorithm(self, config=None):
 | 
			
		||||
        self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
 | 
			
		||||
 | 
			
		||||
@ -24,6 +24,7 @@ SET(QGIS_ANALYSIS_SRCS
 | 
			
		||||
  processing/qgsalgorithmaddincrementalfield.cpp
 | 
			
		||||
  processing/qgsalgorithmaddtablefield.cpp
 | 
			
		||||
  processing/qgsalgorithmaddxyfields.cpp
 | 
			
		||||
  processing/qgsalgorithmapplylayerstyle.cpp
 | 
			
		||||
  processing/qgsalgorithmarraytranslatedfeatures.cpp
 | 
			
		||||
  processing/qgsalgorithmaspect.cpp
 | 
			
		||||
  processing/qgsalgorithmassignprojection.cpp
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										90
									
								
								src/analysis/processing/qgsalgorithmapplylayerstyle.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								src/analysis/processing/qgsalgorithmapplylayerstyle.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,90 @@
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
                         qgsalgorithmapplylayerstyle.cpp
 | 
			
		||||
                         ---------------------
 | 
			
		||||
    begin                : December 2019
 | 
			
		||||
    copyright            : (C) 2017 by Alexander Bruy
 | 
			
		||||
    email                : alexander dot bruy at gmail dot com
 | 
			
		||||
 ***************************************************************************/
 | 
			
		||||
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
 *                                                                         *
 | 
			
		||||
 *   This program is free software; you can redistribute it and/or modify  *
 | 
			
		||||
 *   it under the terms of the GNU General Public License as published by  *
 | 
			
		||||
 *   the Free Software Foundation; either version 2 of the License, or     *
 | 
			
		||||
 *   (at your option) any later version.                                   *
 | 
			
		||||
 *                                                                         *
 | 
			
		||||
 ***************************************************************************/
 | 
			
		||||
 | 
			
		||||
#include "qgsalgorithmapplylayerstyle.h"
 | 
			
		||||
 | 
			
		||||
///@cond PRIVATE
 | 
			
		||||
 | 
			
		||||
QString QgsApplyLayerStyleAlgorithm::name() const
 | 
			
		||||
{
 | 
			
		||||
  return QStringLiteral( "setlayerstyle" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString QgsApplyLayerStyleAlgorithm::displayName() const
 | 
			
		||||
{
 | 
			
		||||
  return QObject::tr( "Set layer style" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QStringList QgsApplyLayerStyleAlgorithm::tags() const
 | 
			
		||||
{
 | 
			
		||||
  return QObject::tr( "change,layer,style,qml" ).split( ',' );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString QgsApplyLayerStyleAlgorithm::group() const
 | 
			
		||||
{
 | 
			
		||||
  return QObject::tr( "Cartography" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString QgsApplyLayerStyleAlgorithm::groupId() const
 | 
			
		||||
{
 | 
			
		||||
  return QStringLiteral( "cartography" );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
 | 
			
		||||
{
 | 
			
		||||
  return QObject::tr( "This algorithm renames a layer." );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QgsProcessingAlgorithm::Flags QgsApplyLayerStyleAlgorithm::flags() const
 | 
			
		||||
{
 | 
			
		||||
  return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
 | 
			
		||||
{
 | 
			
		||||
  return new QgsApplyLayerStyleAlgorithm();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
 | 
			
		||||
{
 | 
			
		||||
  addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
 | 
			
		||||
  addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style file" ),  QgsProcessingParameterFile::File, QStringLiteral( "qml" ) ) );
 | 
			
		||||
  addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Styled" ) ) );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
 | 
			
		||||
{
 | 
			
		||||
  QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
 | 
			
		||||
  QString style = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );
 | 
			
		||||
 | 
			
		||||
  if ( !layer )
 | 
			
		||||
    throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
 | 
			
		||||
 | 
			
		||||
  bool ok = false;
 | 
			
		||||
  QString msg = layer->loadNamedStyle( style, ok );
 | 
			
		||||
  if ( !ok )
 | 
			
		||||
  {
 | 
			
		||||
    throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
 | 
			
		||||
  }
 | 
			
		||||
  layer->triggerRepaint();
 | 
			
		||||
 | 
			
		||||
  QVariantMap results;
 | 
			
		||||
  results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
 | 
			
		||||
  return results;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
///@endcond
 | 
			
		||||
							
								
								
									
										54
									
								
								src/analysis/processing/qgsalgorithmapplylayerstyle.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/analysis/processing/qgsalgorithmapplylayerstyle.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
                         qgsalgorithmapplylayerstyle.h
 | 
			
		||||
                         ---------------------
 | 
			
		||||
    begin                : December 2019
 | 
			
		||||
    copyright            : (C) 2019 by Alexander Bruy
 | 
			
		||||
    email                : alexander dot bruy at gmail dot com
 | 
			
		||||
 ***************************************************************************/
 | 
			
		||||
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
 *                                                                         *
 | 
			
		||||
 *   This program is free software; you can redistribute it and/or modify  *
 | 
			
		||||
 *   it under the terms of the GNU General Public License as published by  *
 | 
			
		||||
 *   the Free Software Foundation; either version 2 of the License, or     *
 | 
			
		||||
 *   (at your option) any later version.                                   *
 | 
			
		||||
 *                                                                         *
 | 
			
		||||
 ***************************************************************************/
 | 
			
		||||
 | 
			
		||||
#ifndef QGSALGORITHMAPPLYLAYERSTYLE_H
 | 
			
		||||
#define QGSALGORITHMAPPLYLAYERSTYLE_H
 | 
			
		||||
 | 
			
		||||
#define SIP_NO_FILE
 | 
			
		||||
 | 
			
		||||
#include "qgis_sip.h"
 | 
			
		||||
#include "qgsprocessingalgorithm.h"
 | 
			
		||||
 | 
			
		||||
///@cond PRIVATE
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Native apply layer style algorithm.
 | 
			
		||||
 */
 | 
			
		||||
class QgsApplyLayerStyleAlgorithm : public QgsProcessingAlgorithm
 | 
			
		||||
{
 | 
			
		||||
  public:
 | 
			
		||||
    QgsApplyLayerStyleAlgorithm() = default;
 | 
			
		||||
    Flags flags() const override;
 | 
			
		||||
    QString name() const override;
 | 
			
		||||
    QString displayName() const override;
 | 
			
		||||
    QStringList tags() const override;
 | 
			
		||||
    QString group() const override;
 | 
			
		||||
    QString groupId() const override;
 | 
			
		||||
    QString shortHelpString() const override;
 | 
			
		||||
    void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
 | 
			
		||||
    QgsApplyLayerStyleAlgorithm *createInstance() const override SIP_FACTORY;
 | 
			
		||||
 | 
			
		||||
  protected:
 | 
			
		||||
 | 
			
		||||
    QVariantMap processAlgorithm( const QVariantMap ¶meters,
 | 
			
		||||
                                  QgsProcessingContext &context, QgsProcessingFeedback * ) override;
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
///@endcond PRIVATE
 | 
			
		||||
 | 
			
		||||
#endif // QGSALGORITHMAPPLYLAYERSTYLE_H
 | 
			
		||||
@ -19,6 +19,7 @@
 | 
			
		||||
#include "qgsalgorithmaddincrementalfield.h"
 | 
			
		||||
#include "qgsalgorithmaddtablefield.h"
 | 
			
		||||
#include "qgsalgorithmaddxyfields.h"
 | 
			
		||||
#include "qgsalgorithmapplylayerstyle.h"
 | 
			
		||||
#include "qgsalgorithmarraytranslatedfeatures.h"
 | 
			
		||||
#include "qgsalgorithmaspect.h"
 | 
			
		||||
#include "qgsalgorithmassignprojection.h"
 | 
			
		||||
@ -192,6 +193,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
 | 
			
		||||
  addAlgorithm( new QgsAddTableFieldAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsAddXYFieldsAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsAddUniqueValueIndexAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsApplyLayerStyleAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsArrayTranslatedFeaturesAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsAspectAlgorithm() );
 | 
			
		||||
  addAlgorithm( new QgsAssignProjectionAlgorithm() );
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user