LCOV - code coverage report
Current view: top level - analysis/processing - qgsalgorithmsetlayerencoding.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 0 39 0.0 %
Date: 2021-04-10 08:29:14 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /***************************************************************************
       2                 :            :                          qgsalgorithmsetlayerencoding.cpp
       3                 :            :                          ------------------------------
       4                 :            :     begin                : February 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                 :            :  ***************************************************************************/
      17                 :            : 
      18                 :            : #include "qgsalgorithmsetlayerencoding.h"
      19                 :            : #include "qgsvectorlayer.h"
      20                 :            : #include "qgsvectordataprovider.h"
      21                 :            : 
      22                 :            : ///@cond PRIVATE
      23                 :            : 
      24                 :          0 : QString QgsSetLayerEncodingAlgorithm::name() const
      25                 :            : {
      26                 :          0 :   return QStringLiteral( "setlayerencoding" );
      27                 :            : }
      28                 :            : 
      29                 :          0 : QString QgsSetLayerEncodingAlgorithm::displayName() const
      30                 :            : {
      31                 :          0 :   return QObject::tr( "Set layer encoding" );
      32                 :            : }
      33                 :            : 
      34                 :          0 : QStringList QgsSetLayerEncodingAlgorithm::tags() const
      35                 :            : {
      36                 :          0 :   return QObject::tr( "change,alter,attribute,codepage" ).split( ',' );
      37                 :          0 : }
      38                 :            : 
      39                 :          0 : QString QgsSetLayerEncodingAlgorithm::group() const
      40                 :            : {
      41                 :          0 :   return QObject::tr( "Vector general" );
      42                 :            : }
      43                 :            : 
      44                 :          0 : QString QgsSetLayerEncodingAlgorithm::groupId() const
      45                 :            : {
      46                 :          0 :   return QStringLiteral( "vectorgeneral" );
      47                 :            : }
      48                 :            : 
      49                 :          0 : QString QgsSetLayerEncodingAlgorithm::shortHelpString() const
      50                 :            : {
      51                 :          0 :   return QObject::tr( "This algorithm sets the encoding used for reading a layer's attributes. No permanent changes "
      52                 :            :                       "are made to the layer, rather it affects only how the layer is read during the current session.\n\n"
      53                 :            :                       "Changing the encoding is only supported for some vector layer data sources." );
      54                 :            : }
      55                 :            : 
      56                 :          0 : QString QgsSetLayerEncodingAlgorithm::shortDescription() const
      57                 :            : {
      58                 :          0 :   return QObject::tr( "Sets the encoding used for reading a layer's attributes" );
      59                 :            : }
      60                 :            : 
      61                 :          0 : QgsSetLayerEncodingAlgorithm *QgsSetLayerEncodingAlgorithm::createInstance() const
      62                 :            : {
      63                 :          0 :   return new QgsSetLayerEncodingAlgorithm();
      64                 :            : }
      65                 :            : 
      66                 :          0 : void QgsSetLayerEncodingAlgorithm::initAlgorithm( const QVariantMap & )
      67                 :            : {
      68                 :          0 :   addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
      69                 :          0 :   addParameter( new QgsProcessingParameterString( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ) ) );
      70                 :            : 
      71                 :          0 :   addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ) ) );
      72                 :          0 : }
      73                 :            : 
      74                 :          0 : bool QgsSetLayerEncodingAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
      75                 :            : {
      76                 :          0 :   QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
      77                 :            : 
      78                 :          0 :   if ( !layer )
      79                 :          0 :     throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
      80                 :            : 
      81                 :          0 :   const QString encoding = parameterAsString( parameters, QStringLiteral( "ENCODING" ), context );
      82                 :            : 
      83                 :          0 :   mOutputId = layer->id();
      84                 :          0 :   QgsVectorDataProvider *provider = layer->dataProvider();
      85                 :            : 
      86                 :          0 :   if ( provider->capabilities() & QgsVectorDataProvider::SelectEncoding )
      87                 :            :   {
      88                 :          0 :     layer->setProviderEncoding( encoding );
      89                 :          0 :   }
      90                 :            :   else
      91                 :            :   {
      92                 :          0 :     feedback->pushInfo( QObject::tr( "Layer's data provider does not support changing the attribute encoding" ) );
      93                 :            :     // we don't return false here -- rather we allow the algorithm to gracefully handle an attempt to be flexible to different layer sources,
      94                 :            :     // otherwise we potentially break model input flexibility!
      95                 :            :   }
      96                 :            :   return true;
      97                 :          0 : }
      98                 :            : 
      99                 :          0 : QVariantMap QgsSetLayerEncodingAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
     100                 :            : {
     101                 :          0 :   QVariantMap outputs;
     102                 :          0 :   outputs.insert( QStringLiteral( "OUTPUT" ), mOutputId );
     103                 :          0 :   return outputs;
     104                 :          0 : }
     105                 :            : 
     106                 :            : ///@endcond

Generated by: LCOV version 1.14