/*
 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
 *
 * This file is part of the KD Chart library.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef KCHARTCHART_H
#define KCHARTCHART_H

#include <QWidget>

#include "kchart_export.h"
#include "KChartGlobal.h"

/*
Simplified(*) overview of object ownership in a chart:

                Chart is-a QWidget
                 |
          n CoordinatePlanes is-a AbstractArea is-a AbstractLayoutItem is-a QLayoutItem
                 |
            n Diagrams is-a QAbstractItemView is-a QWidget
              /   |   \
  AbstractGrid    |   Axes (can be shared between diagrams) is-a AbstractArea is-a... QLayoutItem
 (no base class)  |
                Legends is-a AbstractAreaWidget is-a QWidget

(*) less important classes, including base classes, removed.


Layout rules:

In principle, every size or existence change in one of the objects listed above must be propagated
to all other objects. This could change their size.
There are also settings changes that invalidate the size of other components, where the size changes
are detected and propagated.


Painting call tree (simplified):

Chart::paint() (from users) / paintEvent() (from framework)
ChartPrivate::paintAll()-----------------------------------------------\
CoordinatePlane::paintAll() (from AbstractArea)--------\               Axis::paintAll()-\
CoordinatePlane::paint() (from AbstractLayoutItem)     Grid::drawGrid()                 Axis::paint()
Diagram::paint( PaintContext* paintContext )

Note that grids are painted from the coordinate plane, not from the diagram as ownership would suggest.

*/

namespace KChart {

    class BackgroundAttributes;
    class FrameAttributes;
    class AbstractDiagram;
    class AbstractCoordinatePlane;
    class HeaderFooter;
    class Legend;

    typedef QList<AbstractCoordinatePlane*> CoordinatePlaneList;
    typedef QList<HeaderFooter*> HeaderFooterList;
    typedef QList<Legend*> LegendList;


    /**
     * @class Chart KChartChart.h KChartChart
     * @brief A chart with one or more diagrams.
     *
     * The Chart class represents a drawing consisting of one or more diagrams
     * and various optional elements such as legends, axes, text boxes, headers
     * or footers. It takes ownership of all these elements when they are assigned
     * to it. Each diagram is associated with a coordinate plane, of which the chart
     * can have more than one. The coordinate planes (and thus the associated diagrams)
     * can be laid out in various ways.
     *
     * The Chart class makes heavy use of the Qt Interview framework for model/view
     * programming, and thus requires data to be presented to it in a QAbstractItemModel
     * compatible way. For many simple charts, especially if the visualized data is
     * static, KChart::Widget provides an abstracted interface, that hides the complexity
     * of Interview to a large extent.
     */
    class KCHART_EXPORT Chart : public QWidget
    {
        Q_OBJECT
         // KD Chart 3.0: leading is inter-line distance of text. this here is MARGIN or SPACING.
        Q_PROPERTY( int globalLeadingTop READ globalLeadingTop WRITE setGlobalLeadingTop )
        Q_PROPERTY( int globalLeadingBottom READ globalLeadingBottom WRITE setGlobalLeadingBottom )
        Q_PROPERTY( int globalLeadingLeft READ globalLeadingLeft WRITE setGlobalLeadingLeft )
        Q_PROPERTY( int globalLeadingRight READ globalLeadingRight WRITE setGlobalLeadingRight )
        Q_PROPERTY( bool useNewLayoutSystem READ useNewLayoutSystem WRITE setUseNewLayoutSystem )

        KCHART_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET( Chart )

    public:
        explicit Chart ( QWidget* parent = nullptr );
        ~Chart() override;

        /**
         * @brief useNewLayoutSystem
         * Be very careful activating the new layout system,
         * its still experimental and works only if the user knows
         * what he is doing. The reason is that the system does not prevent
         * the user from creating sharing graphs that are not layoutable in a
         * plane and still needs assistance from the user.
         */
        bool useNewLayoutSystem() const;
        void setUseNewLayoutSystem( bool value );

        /**
          \brief Specify the frame attributes to be used, by default is it a thin black line.

          To hide the frame line, you could do something like this:
          \verbatim
          KChart::FrameAttributes frameAttrs( my_chart->frameAttributes() );
          frameAttrs.setVisible( false );
          my_chart->setFrameAttributes( frameAttrs );
          \endverbatim

          \sa setBackgroundAttributes
          */
        void setFrameAttributes( const FrameAttributes &a );
        FrameAttributes frameAttributes() const;

        /**
          \brief Specify the background attributes to be used, by default there is no background.

          To set a light blue background, you could do something like this:
          \verbatim
          KChart::BackgroundAttributes backgroundAttrs( my_chart->backgroundAttributes() );
          backgroundAttrs.setVisible( true );
          backgroundAttrs.setBrush( QColor(0xd0,0xd0,0xff) );
          my_chart->setBackgroundAttributes( backgroundAttrs );
          \endverbatim

          \sa setFrameAttributes
          */
        void setBackgroundAttributes( const BackgroundAttributes &a );
        BackgroundAttributes backgroundAttributes() const;

        /**
         * Each chart must have at least one coordinate plane.
         * Initially a default CartesianCoordinatePlane is created.
         * Use replaceCoordinatePlane() to replace it with a different
         * one, such as a PolarCoordinatePlane.
         * @return The first coordinate plane of the chart.
         */
        AbstractCoordinatePlane* coordinatePlane();

        /**
         * The list of coordinate planes.
         * @return The list of coordinate planes.
         */
        CoordinatePlaneList coordinatePlanes();

        /**
         * Adds a coordinate plane to the chart. The chart takes ownership.
         * @param plane The coordinate plane to add.
         *
         * \sa replaceCoordinatePlane, takeCoordinatePlane
         */
        void addCoordinatePlane( AbstractCoordinatePlane* plane );

        /**
         * Inserts a coordinate plane to the chart at index @p index.
		 * The chart takes ownership.
		 *
		 * @param index The index where to add the plane
         * @param plane The coordinate plane to add.
         *
         * \sa replaceCoordinatePlane, takeCoordinatePlane
         */
		void insertCoordinatePlane( int index, AbstractCoordinatePlane* plane );

        /**
         * Replaces the old coordinate plane, or appends the
         * plane, it there is none yet.
         *
         * @param plane The coordinate plane to be used instead of the old plane.
         * This parameter must not be zero, or the method will do nothing.
         *
         * @param oldPlane The coordinate plane to be removed by the new plane. This
         * plane will be deleted automatically. If the parameter is omitted,
         * the very first coordinate plane will be replaced. In case, there was no
         * plane yet, the new plane will just be added.
         *
         * \note If you want to re-use the old coordinate plane, call takeCoordinatePlane and
         * addCoordinatePlane, instead of using replaceCoordinatePlane.
         *
         * \sa addCoordinatePlane, takeCoordinatePlane
         */
        void replaceCoordinatePlane( AbstractCoordinatePlane* plane,
                                     AbstractCoordinatePlane* oldPlane = nullptr );

        /**
         * Removes the coordinate plane from the chart, without deleting it.
         *
         * The chart no longer owns the plane, so it is
         * the caller's responsibility to delete the plane.
         *
         * \sa addCoordinatePlane, takeCoordinatePlane
         */
        void takeCoordinatePlane( AbstractCoordinatePlane* plane );

        /**
         * Set the coordinate plane layout that should be used as model for
         * the internal used layout. The layout needs to be an instance of
         * QHBoxLayout or QVBoxLayout.
         */
        void setCoordinatePlaneLayout( QLayout * layout );
        QLayout* coordinatePlaneLayout();

        /**
         * The first header or footer of the chart. By default there is none.
         * @return The first header or footer of the chart or 0 if there was none
         * added to the chart.
         */
        HeaderFooter* headerFooter();

        /**
         * The list of headers and footers associated with the chart.
         * @return The list of headers and footers associated with the chart.
         */
        HeaderFooterList headerFooters();

        /**
         * Adds a header or a footer to the chart. The chart takes ownership.
         * @param headerFooter The header (or footer, resp.) to add.
         *
         * \sa replaceHeaderFooter, takeHeaderFooter
         */
        void addHeaderFooter( HeaderFooter* headerFooter );

        /**
         * Replaces the old header (or footer, resp.), or appends the
         * new header or footer, it there is none yet.
         *
         * @param headerFooter The header or footer to be used instead of the old one.
         * This parameter must not be zero, or the method will do nothing.
         *
         * @param oldHeaderFooter The header or footer to be removed by the new one. This
         * header or footer will be deleted automatically. If the parameter is omitted,
         * the very first header or footer will be replaced. In case, there was no
         * header and no footer yet, the new header or footer will just be added.
         *
         * \note If you want to re-use the old header or footer, call takeHeaderFooter and
         * addHeaderFooter, instead of using replaceHeaderFooter.
         *
         * \sa addHeaderFooter, takeHeaderFooter
         */
        void replaceHeaderFooter ( HeaderFooter* headerFooter,
                                   HeaderFooter* oldHeaderFooter = nullptr );

        /**
         * Removes the header (or footer, resp.) from the chart, without deleting it.
         *
         * The chart no longer owns the header or footer, so it is
         * the caller's responsibility to delete the header or footer.
         *
         * \sa addHeaderFooter, replaceHeaderFooter
         */
        void takeHeaderFooter( HeaderFooter* headerFooter );

        /**
         * The first legend of the chart or 0 if there was none added to the chart.
         * @return The first legend of the chart or 0 if none exists.
         */
        Legend* legend();

        /**
         * The list of all legends associated with the chart.
         * @return The list of all legends associated with the chart.
         */
        LegendList legends();

        /**
         * Add the given legend to the chart. The chart takes ownership.
         * @param legend The legend to add.
         *
         * \sa replaceLegend, takeLegend
         */
        void addLegend( Legend* legend );

        /**
         * Replaces the old legend, or appends the
         * new legend, it there is none yet.
         *
         * @param legend The legend to be used instead of the old one.
         * This parameter must not be zero, or the method will do nothing.
         *
         * @param oldLegend The legend to be removed by the new one. This
         * legend will be deleted automatically. If the parameter is omitted,
         * the very first legend will be replaced. In case, there was no
         * legend yet, the new legend will just be added.
         *
         * If you want to re-use the old legend, call takeLegend and
         * addLegend, instead of using replaceLegend.
         *
         * \note Whenever addLegend is called the font sizes used by the
         * Legend are set to relative and they get coupled to the Chart's size,
         * with their relative values being 20 for the item texts and 24 to the
         * title text. So if you want to use custom font sizes for the Legend
         * make sure to set them after calling addLegend.
         *
         * \sa addLegend, takeLegend
         */
        void replaceLegend ( Legend* legend, Legend* oldLegend = nullptr );

        /**
         * Removes the legend from the chart, without deleting it.
         *
         * The chart no longer owns the legend, so it is
         * the caller's responsibility to delete the legend.
         *
         * \sa addLegend, takeLegend
         */
        void takeLegend( Legend* legend );

        /**
         * Set the padding between the margin of the widget and the area that
         * the contents are drawn into.
         * @param left The padding on the left side.
         * @param top The padding at the top.
         * @param right The padding on the left hand side.
         * @param bottom The padding on the bottom.
         *
         * \note Using previous versions of KD Chart you might have called
         * setGlobalLeading() to make room for long Abscissa labels (or for an
         * overlapping top label of an Ordinate axis, resp.) that would not fit
         * into the normal axis area. This is \em no \em longer \em needed
         * because KD Chart now is using hidden auto-spacer items reserving
         * as much free space as is needed for axes with overlapping content
         * at the respective sides.
         *
         * \sa setGlobalLeadingTop, setGlobalLeadingBottom, setGlobalLeadingLeft, setGlobalLeadingRight
         * \sa globalLeadingTop, globalLeadingBottom, globalLeadingLeft, globalLeadingRight
         */
        void setGlobalLeading( int left, int top, int right, int bottom );

        /**
         * Set the padding between the start of the widget and the start
         * of the area that is used for drawing on the left.
         * @param leading The padding value.
         *
         * \sa setGlobalLeading
         */
        void setGlobalLeadingLeft( int leading );

        /**
         * The padding between the start of the widget and the start
         * of the area that is used for drawing on the left.
         * @return The padding between the start of the widget and the start
         * of the area that is used for drawing on the left.
         *
         * \sa setGlobalLeading
         */
        int globalLeadingLeft() const;

        /**
         * Set the padding between the start of the widget and the start
         * of the area that is used for drawing at the top.
         * @param leading The padding value.
         *
         * \sa setGlobalLeading
         */
        void setGlobalLeadingTop( int leading );

        /**
         * The padding between the start of the widget and the start
         * of the area that is used for drawing at the top.
         * @return The padding between the start of the widget and the start
         * of the area that is used for drawing at the top.
         *
         * \sa setGlobalLeading
         */
        int globalLeadingTop() const;

        /**
         * Set the padding between the start of the widget and the start
         * of the area that is used for drawing on the right.
         * @param leading The padding value.
         *
         * \sa setGlobalLeading
         */
        void setGlobalLeadingRight( int leading );

        /**
         * The padding between the start of the widget and the start
         * of the area that is used for drawing on the right.
         * @return The padding between the start of the widget and the start
         * of the area that is used for drawing on the right.
         *
         * \sa setGlobalLeading
         */
        int globalLeadingRight() const;

        /**
         * Set the padding between the start of the widget and the start
         * of the area that is used for drawing on the bottom.
         * @param leading The padding value.
         *
         * \sa setGlobalLeading
         */
        void setGlobalLeadingBottom( int leading );

        /**
         * The padding between the start of the widget and the start
         * of the area that is used for drawing at the bottom.
         * @return The padding between the start of the widget and the start
         * of the area that is used for drawing at the bottom.
         *
         * \sa setGlobalLeading
         */
        int globalLeadingBottom() const;

        /**
          * Paints all the contents of the chart. Use this method to make KChart
          * draw into your QPainter.
          *
          * \note Any global leading settings will be used by the paint method too,
          * so make sure to set them to zero, if you want the drawing to have the exact
          * size of the target rectangle.
          *
          * \param painter The painter to be drawn into.
          * \param rect The rectangle to be filled by the Chart's drawing.
          *
          * \sa setGlobalLeading
          */
        void paint( QPainter* painter, const QRect& rect );

        void reLayoutFloatingLegends();

    Q_SIGNALS:
        /** Emitted upon change of a property of the Chart or any of its components. */
        void propertiesChanged();
        void finishedDrawing();

    protected:
        /**
          * Adjusts the internal layout when the chart is resized.
          */
        /* reimp */ void resizeEvent ( QResizeEvent * event ) override;

        /**
          * @brief Draws the background and frame, then calls paint().
          *
          * In most cases there is no need to override this method in a derived
          * class, but if you do, do not forget to call paint().
          * @sa paint
          */
        /* reimp */ void paintEvent( QPaintEvent* event ) override;

        /** reimp */
        void mousePressEvent( QMouseEvent* event ) override;
        /** reimp */
        void mouseDoubleClickEvent( QMouseEvent* event ) override;
        /** reimp */
        void mouseMoveEvent( QMouseEvent* event ) override;
        /** reimp */
        void mouseReleaseEvent( QMouseEvent* event ) override;
        /** reimp */
        bool event( QEvent* event ) override;

    private:
        // TODO move this to the private class
        void addLegendInternal( Legend *legend, bool setMeasures );
    };

// Here we have a few docu block to be included into the API documentation:
/**
     * \dir src
     * \brief Implementation directory of KChart.
     *
     * This directory contains the header files and the source files of both,
     * the private and the public classes.
     *
     * \note Only classes that have an include wrapper in the \c $KCHARTDIR/include
     * directory are part of the supported API.
     * All other classes are to be considered as implementation details, they
     * could be changed in future versions of KChart without notice.
     *
     * In other words: No class that is not mentioned in the \c $KCHARTDIR/include
     * directory may be directly used by your application.
     *
     * The recommended way to include classes of the KChart API is including
     * them by class name, so instead of including KChartChart.h you would say:
     *
    \verbatim
#include <KChartChart>
    \endverbatim
     *
     * When following this there is no reason to include the \c $KCHARTDIR/src
     * directory, it is sufficient to include \c $KCHARTDIR/include
 */
}
/**
     * @class QAbstractItemView "(do not include)"
     * @brief Class only listed here to document inheritance of some KChart classes.
     *
     * Please consult the respective Qt documentation for details:
     * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
     * @class QAbstractProxyModel "(do not include)"
     * @brief Class only listed here to document inheritance of some KChart classes.
     *
 * Please consult the respective Qt documentation for details:
     * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
     * @class QFrame "(do not include)"
     * @brief Class only listed here to document inheritance of some KChart classes.
     *
 * Please consult the respective Qt documentation for details:
     * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
     * @class QObject "(do not include)"
     * @brief Class only listed here to document inheritance of some KChart classes.
     *
 * Please consult the respective Qt documentation for details:
     * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
     * @class QSortFilterProxyModel "(do not include)"
     * @brief Class only listed here to document inheritance of some KChart classes.
     *
 * Please consult the respective Qt documentation for details:
     * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
 * @class QWidget "(do not include)"
 * @brief Class only listed here to document inheritance of some KChart classes.
 *
 * Please consult the respective Qt documentation for details:
 * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
 * @class QTextDocument "(do not include)"
 * @brief Class only listed here to document inheritance of some KChart classes.
 *
 * Please consult the respective Qt documentation for details:
 * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
 * @class QLayoutItem "(do not include)"
 * @brief Class only listed here to document inheritance of some KChart classes.
 *
 * Please consult the respective Qt documentation for details:
 * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */
/**
 * @class QGraphicsPolygonItem "(do not include)"
 * @brief Class only listed here to document inheritance of some KChart classes.
 *
 * Please consult the respective Qt documentation for details:
 * <A HREF="http://doc.trolltech.com/">http://doc.trolltech.com/</A>
 */


#endif
