File indexing completed on 2024-05-12 15:54:20

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