File indexing completed on 2024-04-28 15:29:29

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