summaryrefslogtreecommitdiffstats
path: root/src/axis/chartaxis_p.h
blob: bb6c6615acff08f745567761461e1123fe2a973a (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
/****************************************************************************
**
** 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$
**
****************************************************************************/

#ifndef AXIS_H
#define AXIS_H

#include "qchartglobal.h"
#include "chart_p.h"
#include <QGraphicsItem>

QTCOMMERCIALCHART_BEGIN_NAMESPACE

class QAxis;
class ChartPresenter;

class ChartAxis : public Chart
{
    Q_OBJECT
public:
    enum AxisType{X_AXIS,Y_AXIS};

    ChartAxis(QAxis *axis, ChartPresenter *presenter, AxisType type = X_AXIS);
    ~ChartAxis();

    AxisType axisType() const { return m_type; }

    void setAxisOpacity(qreal opacity);
    qreal axisOpacity() const;

    void setGridOpacity(qreal opacity);
    qreal gridOpacity() const;

    void setLabelsOpacity(qreal opacity);
    qreal labelsOpacity() const;

    void setShadesOpacity(qreal opacity);
    qreal shadesOpacity() const;

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

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

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

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

    inline QRectF geometry() const { return m_rect; }
    inline QVector<qreal> layout() { return m_layoutVector; }

public Q_SLOTS:
    void handleAxisUpdated();
    void handleAxisCategoriesUpdated();
    void handleRangeChanged(qreal min , qreal max,int tickCount);
    void handleGeometryChanged(const QRectF &size);


private:
    inline bool isEmpty();
    void createItems(int count);
    void deleteItems(int count);

    QVector<qreal> calculateLayout() const;
    void updateLayout(QVector<qreal> &layout);
    void setLayout(QVector<qreal> &layout);

    bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
    void axisSelected();

private:
    QAxis* m_chartAxis;
    AxisType m_type;
    QRectF m_rect;
    int m_labelsAngle;
    QScopedPointer<QGraphicsItemGroup> m_grid;
    QScopedPointer<QGraphicsItemGroup> m_shades;
    QScopedPointer<QGraphicsItemGroup> m_labels;
    QScopedPointer<QGraphicsItemGroup> m_axis;
    QVector<qreal> m_layoutVector;
    qreal m_min;
    qreal m_max;
    int m_ticksCount;

    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 /* AXISITEM_H_ */