/***************************************************************************
                         qgslegendstyle.h
                         -------------------
    begin                : March 2013
    copyright            : (C) 2013 by Radim Blazek
    email                : radim.blazek@gmail.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 QGSLEGENDSTYLE_H
#define QGSLEGENDSTYLE_H

#include <QFont>
#include <QMap>
#include <QString>
#include <QDomElement>
#include <QDomDocument>

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgstextformat.h"
#include "qgsreadwritecontext.h"

/**
 * \ingroup core
 * \brief Contains detailed styling information relating to how a layout legend should be rendered.
 */
class CORE_EXPORT QgsLegendStyle
{
  public:

    // TODO QGIS 4.0 - use Qt enum instead

    //! Margin sides
    enum Side
    {
      Top = 0, //!< Top side
      Bottom = 1, //!< Bottom side
      Left = 2, //!< Left side
      Right = 3, //!< Right side
    };

    QgsLegendStyle();

    /**
     * Returns the font used for rendering this legend component.
     * \see setFont()
     * \deprecated QGIS 3.40. Use textFormat() instead.
     */
    Q_DECL_DEPRECATED QFont font() const SIP_DEPRECATED { return mTextFormat.font(); }

    /**
     * Sets the \a font used for rendering this legend component.
     * \see font()
     * \deprecated QGIS 3.40. Use setTextFormat() instead.
     */
    Q_DECL_DEPRECATED void setFont( const QFont &font ) SIP_DEPRECATED;

    /**
     * Returns the text format used for rendering this legend component.
     * \see setTextFormat()
     * \since QGIS 3.30
     */
    QgsTextFormat &textFormat() { return mTextFormat; }

    /**
     * Returns the text format used for rendering this legend component.
     * \see setTextFormat()
     * \since QGIS 3.30
     */
    QgsTextFormat textFormat() const SIP_SKIP { return mTextFormat; }

    /**
     * Sets the text \a format used for rendering this legend component.
     * \see textFormat()
     * \since QGIS 3.30
     */
    void setTextFormat( const QgsTextFormat &format ) { mTextFormat = format; }

    /**
     * Returns the margin (in mm) for the specified \a side of the component.
     *
     * \note Not all legend components respect all margin side settings!
     *
     * \see setMargin()
     */
    double margin( Side side ) const { return mMarginMap.value( side ); }

    /**
     * Sets the \a margin (in mm) for the specified \a side of the component.
     *
     * \note Not all legend components respect all margin side settings!
     *
     * \see margin()
     */
    void setMargin( Side side, double margin ) { mMarginMap[side] = margin; }

    /**
     * Sets all margin sides to the same \a margin size (in mm).
     * \see margin()
     */
    void setMargin( double margin );

    /**
     * Returns the alignment for the legend component.
     *
     * \see setAlignment()
     * \since QGIS 3.10
     */
    Qt::Alignment alignment() const { return mAlignment; }

    /**
     * Sets the alignment for the legend component.
     *
     * \see alignment()
     * \since QGIS 3.10
     */
    void setAlignment( Qt::Alignment alignment ) { mAlignment = alignment; }

    /**
    * Returns the indent (in mm) of a group or subgroup.
    *
    * \see indent()
    * \since QGIS 3.22
    */
    double indent() const { return mIndent; }

    /**
     * Sets the indent (in mm) of a group or subgroup.
     *
     * \see indent()
     * \since QGIS 3.22
     */
    void setIndent( double indent ) { mIndent = indent; }

    /**
     * Writes the component's style definition to an XML element.
     * \see readXml()
     */
    void writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() ) const;

    /**
     * Reads the component's style definition from an XML element.
     * \see writeXml()
     */
    void readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() );

    /**
     * Updates any data-defined properties in the style, using the specified
     * render \a context.
     *
     * \since QGIS 3.42
     */
    void updateDataDefinedProperties( QgsRenderContext &context );

    /**
     * Returns the name for a style component as a string.
     *
     * This is a non-localised version, for internal use.
     *
     * \see styleFromName()
     * \see styleLabel()
     */
    static QString styleName( Qgis::LegendComponent s );

    /**
     * Returns the style from name string.
     * \see styleName()
     */
    static Qgis::LegendComponent styleFromName( const QString &styleName );

    /**
     * Returns a translated string representing a style component, for use in UI.
     * \see styleName()
     */
    static QString styleLabel( Qgis::LegendComponent s );

  private:
    QgsTextFormat mTextFormat;
    QMap<Side, double> mMarginMap;
    Qt::Alignment mAlignment = Qt::AlignLeft;
    double mIndent = 0;
};

#endif
