File indexing completed on 2024-05-12 04:20:37

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef __KCHARTWIDGET_H__
0010 #define __KCHARTWIDGET_H__
0011 
0012 #include "KChartGlobal.h"
0013 
0014 #include <QWidget>
0015 
0016 #include "KChartEnums.h"
0017 #include "KChartHeaderFooter.h"
0018 
0019 QT_BEGIN_NAMESPACE
0020 template <typename T> class QList;
0021 QT_END_NAMESPACE
0022 
0023 namespace KChart {
0024 
0025     // some forward declarations
0026     class AbstractDiagram;
0027     class Chart;
0028     class AbstractCoordinatePlane;
0029     class TableModel;
0030     class BarDiagram;
0031     class LineDiagram;
0032     class Plotter;
0033     class PieDiagram;
0034     class RingDiagram;
0035     class PolarDiagram;
0036     class Legend;
0037     class Position;
0038 
0039     /**
0040     * \class Widget KChartWidget.h
0041     * \brief The KChart widget for usage without Interwiev.
0042     *
0043     * If you want to use KChart with Interview, use KChart::Chart instead.
0044     */
0045    class KCHART_EXPORT Widget : public QWidget
0046     {
0047         Q_OBJECT
0048 
0049         Q_DISABLE_COPY( Widget )
0050         KCHART_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET( Widget )
0051 
0052     public:
0053         /**
0054          * Standard Qt-style Constructor
0055          *
0056          * Creates a new widget with all data initialized empty.
0057          *
0058          * \param parent the widget parent; passed on to QWidget
0059          */
0060        explicit Widget( QWidget* parent = nullptr );
0061 
0062         /** Destructor. */
0063        ~Widget() override;
0064         /** Sets the data in the given column using a QVector of qreal for the Y values. */
0065         void setDataset( int column, const QVector< qreal > & data, const QString& title = QString() );
0066         /** Sets the data in the given column using a QVector of QPairs
0067          *  of qreal for the (X, Y) values. */
0068         void setDataset( int column, const QVector< QPair< qreal, qreal > > &  data, const QString& title = QString() );
0069         /** Sets the Y value data for a given cell. */
0070         void setDataCell( int row, int column, qreal data );
0071         /** Sets the data for a given column using an (X, Y) QPair of qreals. */
0072         void setDataCell( int row, int column, QPair< qreal, qreal > data );
0073         /** Resets all data. */
0074         void resetData();
0075 
0076     public Q_SLOTS:
0077         /** Sets all global leadings (borders). */
0078         void setGlobalLeading( int left, int top, int right, int bottom );
0079         /** Sets the left leading (border). */
0080        void setGlobalLeadingLeft( int leading );
0081         /** Sets the top leading (border). */
0082        void setGlobalLeadingTop( int leading );
0083         /** Sets the right leading (border). */
0084        void setGlobalLeadingRight( int leading );
0085         /** Sets the bottom leading (border). */
0086        void setGlobalLeadingBottom( int leading );
0087 
0088     public:
0089         /** Returns the left leading (border). */
0090         int globalLeadingLeft() const;
0091         /** Returns the top leading (border). */
0092         int globalLeadingTop() const;
0093         /** Returns the right leading (border). */
0094         int globalLeadingRight() const;
0095         /** Returns the bottom leading (border). */
0096         int globalLeadingBottom() const;
0097 
0098         /** Returns the first of all headers. */
0099         HeaderFooter* firstHeaderFooter();
0100         /** Returns a list with all headers. */
0101         QList<HeaderFooter*> allHeadersFooters();
0102 
0103         /** Adds a new header/footer with the given text to the position. */
0104         void addHeaderFooter( const QString& text,
0105                               HeaderFooter::HeaderFooterType type,
0106                               Position position );
0107 
0108         /**
0109           * Adds the existing header / footer object \a header.
0110           * \sa replaceHeaderFooter, takeHeaderFooter
0111         */
0112         void addHeaderFooter( HeaderFooter* header );
0113 
0114         /**
0115          * Replaces the old header (or footer, resp.), or appends the
0116          * new header or footer, it there is none yet.
0117          *
0118          * @param header The header or footer to be used instead of the old one.
0119          * This parameter must not be zero, or the method will do nothing.
0120          *
0121          * @param oldHeader The header or footer to be removed by the new one. This
0122          * header or footer will be deleted automatically. If the parameter is omitted,
0123          * the very first header or footer will be replaced. In case, there was no
0124          * header and no footer yet, the new header or footer will just be added.
0125          *
0126          * \note If you want to re-use the old header or footer, call takeHeaderFooter and
0127          * addHeaderFooter, instead of using replaceHeaderFooter.
0128          *
0129          * \sa addHeaderFooter, takeHeaderFooter
0130          */
0131         void replaceHeaderFooter( HeaderFooter* header,
0132                                   HeaderFooter* oldHeader = nullptr );
0133 
0134         /** Remove the header (or footer, resp.) from the widget,
0135          * without deleting it.
0136          * The chart no longer owns the header or footer, so it is
0137          * the caller's responsibility to delete the header or footer.
0138          *
0139          * \sa addHeaderFooter, replaceHeaderFooter
0140          */
0141         void takeHeaderFooter( HeaderFooter* header );
0142 
0143         /** Returns the first of all legends. */
0144         Legend* legend();
0145         /** Returns a list with all legends. */
0146         QList<Legend*> allLegends();
0147 
0148         /** Adds an empty legend on the given position. */
0149         void addLegend( Position position );
0150         /** Adds a new, already existing, legend. */
0151         void addLegend (Legend* legend );
0152 
0153         void replaceLegend( Legend* legend, Legend* oldLegend = nullptr );
0154         void takeLegend( Legend* legend );
0155 
0156 
0157         /** Returns a pointer to the current diagram. */
0158         AbstractDiagram* diagram();
0159 
0160         /** If the current diagram is a BarDiagram, it is returned; otherwise 0 is returned.
0161           * This function provides type-safe casting.
0162           */
0163         BarDiagram* barDiagram();
0164         /** If the current diagram is a LineDiagram, it is returned; otherwise 0 is returned.
0165          * This function provides type-safe casting.
0166          */
0167         LineDiagram* lineDiagram();
0168         /** If the current diagram is a LineDiagram, it is returned; otherwise 0 is returned.
0169          * This function provides type-safe casting.
0170          *
0171          * \note Do not use lineDiagram for multi-dimensional diagrams, but use plotter instead
0172          *
0173          * \sa plotter
0174          */
0175         Plotter* plotter();
0176         /** If the current diagram is a Plotter, it is returned; otherwise 0 is returned.
0177           * This function provides type-safe casting.
0178           */
0179         PieDiagram* pieDiagram();
0180         /** If the current diagram is a RingDiagram, it is returned; otherwise 0 is returned.
0181           * This function provides type-safe casting.
0182           */
0183         RingDiagram* ringDiagram();
0184         /** If the current diagram is a PolarDiagram, it is returned; otherwise 0 is returned.
0185           * This function provides type-safe casting.
0186           */
0187         PolarDiagram* polarDiagram();
0188 
0189         /** Returns a pointer to the current coordinate plane. */
0190         AbstractCoordinatePlane* coordinatePlane();
0191 
0192 
0193         enum ChartType { NoType, Bar, Line, Plot, Pie, Ring, Polar };
0194 
0195         /** Returns the type of the chart. */
0196         ChartType type() const;
0197 
0198         /** Sub type values, matching the values defines for the respective Diagram classes. */
0199         enum SubType { Normal, Stacked, Percent, Rows };
0200 
0201         /** Returns the sub-type of the chart. */
0202         SubType subType() const;
0203 
0204     public Q_SLOTS:
0205         /** Sets the type of the chart. */
0206         void setType( KChart::Widget::ChartType chartType, KChart::Widget::SubType subType=Normal );
0207         /** \brief Sets the type of the chart without changing the main type.
0208           *
0209           * Make sure to use a sub-type that matches the main type,
0210           * so e.g. setting sub-type Rows makes sense for Bar charts only,
0211           * and it will be ignored for all other chart types.
0212           *
0213           * \sa KChart::BarDiagram::BarType, KChart::LineDiagram::LineType
0214           * \sa KChart::PieDiagram::PieType, KChart::RingDiagram::RingType
0215           * \sa KChart::PolarDiagram::PolarType
0216           */
0217         void setSubType( KChart::Widget::SubType subType );
0218 
0219     private:
0220         /** Justifies the model, so that the given rows and columns fit into it. */
0221         void justifyModelSize( int rows, int columns );
0222         /** Checks whether the given width matches with the one used until now. */
0223        bool checkDatasetWidth( int width );
0224     };
0225 }
0226 
0227 #endif // KChartWidget_H