/***************************************************************************
                         qgsalgorithmpromotetomultipart.cpp
                         ---------------------
    begin                : April 2017
    copyright            : (C) 2017 by Nyall Dawson
    email                : nyall dot dawson 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 "qgsalgorithmpromotetomultipart.h"
#include "qgsvectorlayer.h"

///@cond PRIVATE

QString QgsPromoteToMultipartAlgorithm::name() const
{
  return QStringLiteral( "promotetomulti" );
}

QString QgsPromoteToMultipartAlgorithm::displayName() const
{
  return QObject::tr( "Promote to multipart" );
}

QStringList QgsPromoteToMultipartAlgorithm::tags() const
{
  return QObject::tr( "multi,single,multiple,convert,force,parts" ).split( ',' );
}

QString QgsPromoteToMultipartAlgorithm::group() const
{
  return QObject::tr( "Vector geometry" );
}

QString QgsPromoteToMultipartAlgorithm::groupId() const
{
  return QStringLiteral( "vectorgeometry" );
}

QString QgsPromoteToMultipartAlgorithm::outputName() const
{
  return QObject::tr( "Multiparts" );
}

QString QgsPromoteToMultipartAlgorithm::shortHelpString() const
{
  return QObject::tr( "This algorithm takes a vector layer with singlepart geometries and generates a new one in which all geometries are "
                      "multipart. Input features which are already multipart features will remain unchanged." )
         + QStringLiteral( "\n\n" ) + QObject::tr( "This algorithm can be used to force geometries to multipart types in order to be compatible with data providers "
                                                   "with strict singlepart/multipart compatibility checks." )
         + QStringLiteral( "\n\n" ) + QObject::tr( "See the 'Collect geometries' or 'Aggregate' algorithms for alternative options." );
}

QString QgsPromoteToMultipartAlgorithm::shortDescription() const
{
  return QObject::tr( "Generates a vector layer in which all geometries are multipart." );
}

QgsPromoteToMultipartAlgorithm *QgsPromoteToMultipartAlgorithm::createInstance() const
{
  return new QgsPromoteToMultipartAlgorithm();
}

bool QgsPromoteToMultipartAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
{
  const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
  if ( !layer )
    return false;

  if ( !QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( layer ) )
    return false;
  return QgsWkbTypes::isMultiType( layer->wkbType() );
}

Qgis::WkbType QgsPromoteToMultipartAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
{
  return QgsWkbTypes::multiType( inputWkbType );
}

Qgis::ProcessingFeatureSourceFlags QgsPromoteToMultipartAlgorithm::sourceFlags() const
{
  return Qgis::ProcessingFeatureSourceFlag::SkipGeometryValidityChecks;
}

QgsFeatureList QgsPromoteToMultipartAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
{
  QgsFeature f = feature;
  if ( f.hasGeometry() && !f.geometry().isMultipart() )
  {
    QgsGeometry g = f.geometry();
    g.convertToMultiType();
    f.setGeometry( g );
  }
  return QgsFeatureList() << f;
}

///@endcond
