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

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 KCHARTRELATIVEPOSITION_H
0021 #define KCHARTRELATIVEPOSITION_H
0022 
0023 #include <QDebug>
0024 #include <QMetaType>
0025 #include <Qt>
0026 #include <QPointF>
0027 #include <QSizeF>
0028 #include "KChartGlobal.h"
0029 
0030 namespace KChart {
0031 
0032     class Position;
0033     class PositionPoints;
0034     class Measure;
0035 
0036 /**
0037   \class RelativePosition KChartRelativePosition.h
0038   \brief Defines relative position information: reference area, position
0039   in this area (reference position), horizontal / vertical padding, and rotation.
0040 
0041   See detailed description of \a KChart::Position for an illustration of the
0042   different possible reference positions.
0043 
0044   Using RelativePosition you can specify the relative parts
0045   of some position information, and you can specify the absolute parts:
0046   the reference area, and the position in this area.
0047 
0048   \note To get an absolute position, you have three options:
0049   \li either you declare both, the relative and the absolute parts,
0050       using setReferenceArea for the latter,
0051   \li or you specify a set of points, using setReferencePoints,
0052   \li or you don't use either, leaving it to KChart to find a suitable reference area.
0053   */
0054 class KCHART_EXPORT RelativePosition
0055 {
0056 public:
0057     RelativePosition();
0058     RelativePosition( const RelativePosition& );
0059 
0060     RelativePosition & operator=( const RelativePosition & other );
0061 
0062     ~RelativePosition();
0063 
0064     /**
0065      * \brief Set the reference area to be used to find the anchor point.
0066      *
0067      * The reference area's type can be either a QWidget subclass or a KChart::AbstractArea subclass.
0068      *
0069      * \note Usage of reference area and reference points is mutually exclusive:
0070      * Only one can be used, so any previously set reference points are cleared
0071      * when you call setReferenceArea.
0072      *
0073      * Also note: In a few cases KChart will ignore your area (or points, resp.) settings!
0074      * Relative positioning of data value texts is an example: For these
0075      * the reference area is always taken to be the data area.
0076      *
0077      * \sa setReferencePosition, setAlignment, setHorizontalPadding, setVerticalPadding
0078      */
0079     void setReferenceArea( QObject* area );
0080     QObject* referenceArea() const;
0081 
0082     /**
0083      * \brief Set a set of points from which the anchor point will be selected.
0084      *
0085      * \note Usage of reference area and reference points is mutually exclusive:
0086      * Only one can be used, so any previously set reference area is cleared
0087      * when you call setReferencePoints.
0088      *
0089      * Also note: In a few cases KChart will ignore your points (or area, resp.) settings!
0090      * Relative positioning of data value texts is an example: For these
0091      * the reference area is always taken to be the data area.
0092      *
0093      * \sa setReferenceArea, setReferencePosition, setAlignment, setHorizontalPadding, setVerticalPadding
0094      */
0095     void setReferencePoints( const PositionPoints& points );
0096     const PositionPoints referencePoints() const;
0097 
0098     /**
0099      * \brief Set the position of the anchor point.
0100      *
0101      * The anchor point of a RelativePosition may be one of the pre-defined
0102      * points of it's reference area - for details see KChart::Position.
0103      *
0104      * See detailed description of \a KChart::Position for an illustration of the
0105      * different possible reference positions.
0106      *
0107      *  \sa resetReferencePosition, setReferenceArea, setAlignment, setHorizontalPadding, setVerticalPadding, KChart::Position
0108      */
0109     void setReferencePosition( Position position );
0110 
0111     /**
0112      * \brief Resets the position of the anchor point to the built-in default.
0113      *
0114      * If the anchor point of a RelativePosition is reset (or never changed from the
0115      * default setting) KChart will choose an appropriate Position at run-time.
0116      *
0117      * e.g. BarDiagrams will use Position::North / Position::South for positive / negative values.
0118      *
0119      *  \sa setReferencePosition, setReferenceArea, setAlignment, setHorizontalPadding, setVerticalPadding, KChart::Position
0120      */
0121     void resetReferencePosition();
0122     Position referencePosition() const;
0123 
0124     /**
0125      * Set the alignment of the content placed by this RelativePosition.
0126      *
0127      * Padding is applied first to obtain the final reference point
0128      * for the content's alignment
0129      *
0130      * \note To print centered content, besides calling setAlignment( Qt::AlignCenter )
0131      * you might also want to set zero padding to have your text centered more precisely.
0132      *
0133      * \sa setReferencePosition, setReferenceArea, setHorizontalPadding, setVerticalPadding
0134      */
0135     void setAlignment( Qt::Alignment flags );
0136     Qt::Alignment alignment() const;
0137 
0138     /**
0139      * Set the width of the horizontal padding between the anchor point and the content
0140      * placed by this RelativePosition.
0141      *
0142      * \note When printing data value texts this Measure is used to find the alignment
0143      * point for this text, then alignment() is use to determine how to align the text
0144      * relative to that point.
0145      * The font height is used as reference size for both horizontal and vertical padding
0146      * if the respective padding's Measure is using automatic reference area detection.
0147      *
0148      * \sa setVerticalPadding, setReferencePosition, setReferenceArea
0149      */
0150     void setHorizontalPadding( const Measure& padding );
0151     Measure horizontalPadding() const;
0152 
0153     /**
0154      * Set the height of the vertical padding between the anchor point and the content
0155      * placed by this RelativePosition.
0156      *
0157      * \note When printing data value texts this Measure is used to find the alignment
0158      * point for this text, then alignment() is use to determine how to align the text
0159      * relative to that point.
0160      * The font height is used as reference size for both horizontal and vertical padding
0161      * if the respective padding's Measure is using automatic reference area detection.
0162      *
0163      * \sa setHorizontalPadding, setReferencePosition, setReferenceArea
0164      */
0165     void setVerticalPadding( const Measure& padding );
0166     Measure verticalPadding() const;
0167 
0168     void setRotation( qreal rot );
0169     qreal rotation() const;
0170 
0171     /**
0172      * \brief Return the reference point, according to the reference area/position, and ignoring padding.
0173      *
0174      * This method is called at drawing time.
0175      * The returned point is used to test if the label of a data value is to be printed: a label
0176      * is printed only if its reference point is inside or touching the coordinate plane.
0177      *
0178      * If polarDegrees is set, the degree information will be returned that was stored for the
0179      * respective point. This is used by the PieDiagram class to determine how vertical/horizontal
0180      * padding settings should affect the position of the data value texts' reference points.
0181      * \sa calculatedPoint, setReferenceArea, setReferencePosition, setHorizontalPadding, setVerticalPadding
0182      */
0183     const QPointF referencePoint(qreal* polarDegrees = nullptr) const;
0184 
0185     /**
0186      * \brief Calculate a point, accordin to the reference area/position and the padding.
0187      *
0188      * This method is called at drawing time: The returned point is used as anchor point.
0189      * Note that it is the task of the calling code to place the content, taking the alignment
0190      * property into account. This class does not know the size of the content so it
0191      * cannot place it.
0192      *
0193      * \sa referencePoint, setReferenceArea, setReferencePosition, setHorizontalPadding, setVerticalPadding
0194      */
0195     const QPointF calculatedPoint( const QSizeF& autoSize ) const;
0196 
0197     bool operator==( const RelativePosition& ) const;
0198     bool operator!=( const RelativePosition & other ) const;
0199 
0200 private:
0201     KCHART_DECLARE_PRIVATE_BASE_VALUE( RelativePosition )
0202 };
0203 
0204 inline bool RelativePosition::operator!=( const RelativePosition & other ) const { return !operator==( other ); }
0205 }
0206 
0207 #if !defined(QT_NO_DEBUG_STREAM)
0208 KCHART_EXPORT QDebug operator<<(QDebug, const KChart::RelativePosition& );
0209 #endif /* QT_NO_DEBUG_STREAM */
0210 
0211 KCHART_DECLARE_SWAP_SPECIALISATION( KChart::RelativePosition )
0212 
0213 QT_BEGIN_NAMESPACE
0214 Q_DECLARE_TYPEINFO( KChart::RelativePosition, Q_MOVABLE_TYPE );
0215 QT_END_NAMESPACE
0216 
0217 Q_DECLARE_METATYPE( KChart::RelativePosition )
0218 
0219 #endif // KCHARTRELATIVEPOSITION_H