summaryrefslogtreecommitdiffstats
path: root/src/axis/chartaxis_p.h
blob: acfc127a24806bc4ba52d4194f436b6683dd84b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/

//  W A R N I N G
//  -------------
//
// This file is not part of the QtCommercial Chart API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

#ifndef CHARTAXIS_H
#define CHARTAXIS_H

#include "qchartglobal.h"
#include "chartelement_p.h"
#include "axisanimation_p.h"
#include <QGraphicsItem>
#include <QGraphicsLayoutItem>
#include <QFont>

QTCOMMERCIALCHART_BEGIN_NAMESPACE

class QAbstractAxis;
class ChartPresenter;

class ChartAxis : public ChartElement, public QGraphicsLayoutItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsLayoutItem)
public:
    enum AxisType{ X_AXIS,Y_AXIS };

    ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
    ~ChartAxis();

    virtual AxisType axisType() const = 0;

    void setArrowOpacity(qreal opacity);
    qreal arrowOpacity() const;
    void setArrowVisibility(bool visible);

    void setGridOpacity(qreal opacity);
    qreal gridOpacity() const;
    void setGridVisibility(bool visible);

    void setLabelsOpacity(qreal opacity);
    qreal labelsOpacity() const;
    void setLabelsVisibility(bool visible);

    void setShadesOpacity(qreal opacity);
    qreal shadesOpacity() const;
    void setShadesVisibility(bool visible);

    void setLabelsAngle(int angle);
    int labelsAngle()const { return m_labelsAngle; }

    void setShadesBrush(const QBrush &brush);
    void setShadesPen(const QPen &pen);

    void setArrowPen(const QPen &pen);
    void setGridPen(const QPen &pen);

    void setLabelsPen(const QPen &pen);
    void setLabelsBrush(const QBrush &brush);
    void setLabelsFont(const QFont &font);

    void setTitlePen(const QPen &pen);
    void setTitleBrush(const QBrush &brush);
    void setTitleFont(const QFont &font);
    void setTitleText(const QString& title);


    void setLayout(QVector<qreal> &layout);
    QVector<qreal> layout() const { return m_layoutVector; }

    void setAnimation(AxisAnimation* animation);
    ChartAnimation* animation() const { return m_animation; };

    Qt::Orientation orientation() const;

    bool isVisible();
    void hide();

    void setGeometry(const QRectF &size);
    QRectF geometry() const { return m_rect; }

    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;

protected:
    virtual void updateGeometry() = 0;
    virtual QVector<qreal> calculateLayout() const = 0;
    QStringList createNumberLabels(qreal min, qreal max,int ticks) const;


public Q_SLOTS:
    virtual void handleAxisUpdated();
    virtual void handleDomainUpdated();


private:
    inline bool isEmpty();
    void createItems(int count);
    void deleteItems(int count);
    void updateLayout(QVector<qreal> &layout);
    void axisSelected();

protected:
    QAbstractAxis* m_chartAxis;
    int m_labelsAngle;
    //TODO: to be removed
    QRectF m_rect;
    QScopedPointer<QGraphicsItemGroup> m_grid;
    QScopedPointer<QGraphicsItemGroup> m_shades;
    QScopedPointer<QGraphicsItemGroup> m_labels;
    QScopedPointer<QGraphicsItemGroup> m_arrow;
    QGraphicsSimpleTextItem* m_title;
    QVector<qreal> m_layoutVector;
    qreal m_min;
    qreal m_max;
    AxisAnimation *m_animation;
    QFont m_font;
    QString m_titleText;

    friend class AxisAnimation;
    friend class AxisItem;

};

class AxisItem: public QGraphicsLineItem
{

public:
    explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}

protected:
   void mousePressEvent(QGraphicsSceneMouseEvent *event)
   {
       Q_UNUSED(event)
       m_axis->axisSelected();
   }

   QRectF boundingRect() const
   {
      return shape().boundingRect();
   }

   QPainterPath shape() const
   {
       QPainterPath path = QGraphicsLineItem::shape();
       QRectF rect = path.boundingRect();
       path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
       return path;
   }

private:
   ChartAxis* m_axis;

};

QTCOMMERCIALCHART_END_NAMESPACE

#endif /* CHARTAXI_H */