File indexing completed on 2024-05-12 07:52:05

0001 /*  -*- C++ -*-
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 Jason Harris <kstars@30doradus.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KPLOTOBJECT_H
0009 #define KPLOTOBJECT_H
0010 
0011 #include <kplotting_export.h>
0012 
0013 #include <QColor>
0014 #include <QString>
0015 
0016 #include <memory>
0017 
0018 class QBrush;
0019 class QPainter;
0020 class QPen;
0021 class QPointF;
0022 class KPlotWidget;
0023 class KPlotPoint;
0024 
0025 /**
0026  * @class KPlotObject
0027  * @short Encapsulates a data set to be plotted in a KPlotWidget.
0028  *
0029  * Think of a KPlotObject as a set of data displayed as a group in the plot.
0030  * Each KPlotObject consists of a list of KPlotPoints, a "type" controlling
0031  * how the data points are displayed (some combination of Points, Lines, or
0032  * Bars), a color, and a size. There is also a parameter which controls the
0033  * shape of the points used to display the KPlotObject.
0034  *
0035  * @note KPlotObject will take care of the points added to it, so when clearing
0036  * the points list (eg with clearPoints()) any previous reference to a KPlotPoint
0037  * already added to a KPlotObject will be invalid.
0038  *
0039  * @author Jason Harris
0040  * @version 1.1
0041  */
0042 class KPLOTTING_EXPORT KPlotObject
0043 {
0044 public:
0045     /**
0046      * The type classification of the KPlotObject.
0047      *
0048      * These are bitmask values that can be OR'd together, so that a set
0049      * of points can be represented in the plot in multiple ways.
0050      *
0051      * @note points should be added in order of increasing x-coordinate
0052      * when using Bars.
0053      */
0054     enum PlotType {
0055         UnknownType = 0,
0056         Points = 1, ///< each KPlotPoint is represented with a drawn point
0057         Lines = 2, ///< each KPlotPoint is connected with a line
0058         Bars = 4, ///< each KPlotPoint is shown as a vertical bar
0059     };
0060     Q_DECLARE_FLAGS(PlotTypes, PlotType)
0061 
0062     /**
0063      * The available shape styles for plotted points.
0064      */
0065     enum PointStyle {
0066         NoPoints = 0,
0067         Circle = 1,
0068         Letter = 2,
0069         Triangle = 3,
0070         Square = 4,
0071         Pentagon = 5,
0072         Hexagon = 6,
0073         Asterisk = 7,
0074         Star = 8,
0075         UnknownPoint,
0076     };
0077 
0078     /**
0079      * Constructor.
0080      * @param color The color for plotting this object. By default this sets
0081      * the color for Points, Lines and Bars, but there are functions to
0082      * override any of these.
0083      * @param otype the PlotType for this object (Points, Lines or Bars)
0084      * @param size the size to use for plotted points, in pixels
0085      * @param ps The PointStyle describing the shape for plotted points
0086      */
0087     explicit KPlotObject(const QColor &color = Qt::white, PlotType otype = Points, double size = 2.0, PointStyle ps = Circle);
0088 
0089     /**
0090      * Destructor.
0091      */
0092     ~KPlotObject();
0093 
0094     /**
0095      * @return the plot flags of the object
0096      */
0097     PlotTypes plotTypes() const;
0098 
0099     /**
0100      * Set whether points will be drawn for this object
0101      * @param b if true, points will be drawn
0102      */
0103     void setShowPoints(bool b);
0104 
0105     /**
0106      * Set whether lines will be drawn for this object
0107      * @param b if true, lines will be drawn
0108      */
0109     void setShowLines(bool b);
0110 
0111     /**
0112      * Set whether bars will be drawn for this object
0113      * @param b if true, bars will be drawn
0114      */
0115     void setShowBars(bool b);
0116 
0117     /**
0118      * @return the size of the plotted points in this object, in pixels
0119      */
0120     double size() const;
0121 
0122     /**
0123      * Set the size for plotted points in this object, in pixels
0124      * @param s the new size
0125      */
0126     void setSize(double s);
0127 
0128     /**
0129      * @return the style used for drawing the points in this object
0130      */
0131     PointStyle pointStyle() const;
0132 
0133     /**
0134      * Set a new style for drawing the points in this object
0135      * @param p the new style
0136      */
0137     void setPointStyle(PointStyle p);
0138 
0139     /**
0140      * @return the default pen for this Object.
0141      * If no other pens are set, this pen will be used for
0142      * points, lines, bars and labels (this pen is always used for points).
0143      */
0144     const QPen &pen() const;
0145 
0146     /**
0147      * Set the default pen for this object
0148      * @p The pen to use
0149      */
0150     void setPen(const QPen &p);
0151 
0152     /**
0153      * @return the pen to use for drawing lines for this Object.
0154      */
0155     const QPen &linePen() const;
0156 
0157     /**
0158      * Set the pen to use for drawing lines for this object
0159      * @p The pen to use
0160      */
0161     void setLinePen(const QPen &p);
0162 
0163     /**
0164      * @return the pen to use for drawing bars for this Object.
0165      */
0166     const QPen &barPen() const;
0167 
0168     /**
0169      * Set the pen to use for drawing bars for this object
0170      * @p The pen to use
0171      */
0172     void setBarPen(const QPen &p);
0173 
0174     /**
0175      * @return the pen to use for drawing labels for this Object.
0176      */
0177     const QPen &labelPen() const;
0178 
0179     /**
0180      * Set the pen to use for labels for this object
0181      * @p The pen to use
0182      */
0183     void setLabelPen(const QPen &p);
0184 
0185     /**
0186      * @return the default Brush to use for this Object.
0187      */
0188     const QBrush brush() const;
0189 
0190     /**
0191      * Set the default brush to use for this object
0192      * @b The brush to use
0193      */
0194     void setBrush(const QBrush &b);
0195 
0196     /**
0197      * @return the brush to use for filling bars for this Object.
0198      */
0199     const QBrush barBrush() const;
0200 
0201     /**
0202      * Set the brush to use for drawing bars for this object
0203      * @b The brush to use
0204      */
0205     void setBarBrush(const QBrush &b);
0206 
0207     /**
0208      * @return the list of KPlotPoints that make up this object
0209      */
0210     QList<KPlotPoint *> points() const;
0211 
0212     /**
0213      * Add a point to the object's list of points, using input data to construct a KPlotPoint.
0214      * @param p the QPointF to add.
0215      * @param label the optional text label for this point
0216      * @param barWidth the width of the bar, if this object is to be drawn with bars
0217      * @note if @param barWidth is left at its default value of 0.0, then the width will be
0218      * automatically set to the distance between this point and the one to its right.
0219      */
0220     void addPoint(const QPointF &p, const QString &label = QString(), double barWidth = 0.0);
0221 
0222     /**
0223      * Add a given KPlotPoint to the object's list of points.
0224      * @overload
0225      * @param p pointer to the KPlotPoint to add.
0226      */
0227     void addPoint(KPlotPoint *p);
0228 
0229     /**
0230      * Add a point to the object's list of points, using input data to construct a KPlotPoint.
0231      * @overload
0232      * @param x the X-coordinate of the point to add.
0233      * @param y the Y-coordinate of the point to add.
0234      * @param label the optional text label
0235      * @param barWidth the width of the bar, if this object is to be drawn with bars
0236      * @note if @param barWidth is left at its default value of 0.0, then the width will be
0237      * automatically set to the distance between this point and the one to its right.
0238      */
0239     void addPoint(double x, double y, const QString &label = QString(), double barWidth = 0.0);
0240 
0241     /**
0242      * Remove the QPointF at position index from the list of points
0243      * @param index the index of the point to be removed.
0244      */
0245     void removePoint(int index);
0246 
0247     /**
0248      * Remove and destroy the points of this object
0249      */
0250     void clearPoints();
0251 
0252     /**
0253      * Draw this KPlotObject on the given QPainter
0254      * @param p The QPainter to draw on
0255      * @param pw the KPlotWidget to draw on (this is needed
0256      * for the KPlotWidget::mapToWidget() function)
0257      */
0258     void draw(QPainter *p, KPlotWidget *pw);
0259 
0260 private:
0261     class Private;
0262     std::unique_ptr<Private> const d;
0263 
0264     Q_DISABLE_COPY(KPlotObject)
0265 };
0266 Q_DECLARE_OPERATORS_FOR_FLAGS(KPlotObject::PlotTypes)
0267 
0268 #endif