##// END OF EJS Templates
Fixed: chart not redrawing when logaxis's logBase value changed
Marek Rosa -
r2322:2741539ef9e9
parent child
Show More
@@ -1,123 +1,127
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartlogvalueaxisx_p.h"
21 #include "chartlogvalueaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
31 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
32 : HorizontalAxis(axis, item),
32 : HorizontalAxis(axis, item),
33 m_axis(axis)
33 m_axis(axis)
34 {
34 {
35 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
35 }
36 }
36
37
37 ChartLogValueAxisX::~ChartLogValueAxisX()
38 ChartLogValueAxisX::~ChartLogValueAxisX()
38 {
39 {
39 }
40 }
40
41
41 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
42 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
42 {
43 {
43 QVector<qreal> points;
44 QVector<qreal> points;
44
45
45 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
46 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
46 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
47 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
47 qreal leftEdge = logMin < logMax ? logMin : logMax;
48 qreal leftEdge = logMin < logMax ? logMin : logMax;
48 qreal ceilEdge = ceil(leftEdge);
49 qreal ceilEdge = ceil(leftEdge);
49 int tickCount = qAbs(qRound(logMax - logMin));
50 int tickCount = qAbs(qRound(logMax - logMin));
50 tickCount++;
51 tickCount++;
51
52
52 points.resize(tickCount);
53 points.resize(tickCount);
53 const QRectF &gridRect = gridGeometry();
54 const QRectF &gridRect = gridGeometry();
54 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
55 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
55 for (int i = 0; i < tickCount; ++i)
56 for (int i = 0; i < tickCount; ++i)
56 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
57 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
57
58
58 return points;
59 return points;
59 }
60 }
60
61
61 void ChartLogValueAxisX::updateGeometry()
62 void ChartLogValueAxisX::updateGeometry()
62 {
63 {
63 const QVector<qreal>& layout = ChartAxis::layout();
64 const QVector<qreal>& layout = ChartAxis::layout();
64 if (layout.isEmpty())
65 if (layout.isEmpty())
65 return;
66 return;
66 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 HorizontalAxis::updateGeometry();
68 HorizontalAxis::updateGeometry();
68 }
69 }
69
70
70 //void ChartLogValueAxisX::handleAxisUpdated()
71 void ChartLogValueAxisX::handleBaseChanged(qreal base)
71 //{
72 {
72 // ChartAxis::handleAxisUpdated();
73 Q_UNUSED(base);
73 //}
74 if(presenter()) presenter()->layout()->invalidate();
75 }
74
76
75 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
77 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 {
78 {
77 Q_UNUSED(constraint)
79 Q_UNUSED(constraint)
78
80
79 QFontMetrics fn(font());
81 QFontMetrics fn(font());
80 QSizeF sh;
82 QSizeF sh;
81
83
82 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
84 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
83 QStringList ticksList;
85 QStringList ticksList;
84 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
86 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
85 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
87 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
86 int tickCount = qAbs(qRound(logMax - logMin));
88 int tickCount = qAbs(qRound(logMax - logMin));
87 tickCount++;
89 tickCount++;
88
90
89 if (m_axis->max() > m_axis->min() && tickCount > 1)
91 if (m_axis->max() > m_axis->min() && tickCount > 1)
90 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
92 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
91 else
93 else
92 ticksList.append(QString(" "));
94 ticksList.append(QString(" "));
93 qreal width = 0;
95 qreal width = 0;
94 qreal height = 0;
96 qreal height = 0;
95
97
96
98
97 switch (which) {
99 switch (which) {
98 case Qt::MinimumSize:{
100 case Qt::MinimumSize:{
99 int count = qMax(ticksList.last().count(),ticksList.first().count());
101 int count = qMax(ticksList.last().count(),ticksList.first().count());
100 width = fn.averageCharWidth() * count;
102 width = fn.averageCharWidth() * count;
101 height = fn.height() + labelPadding();
103 height = fn.height() + labelPadding();
102 width = qMax(width,base.width());
104 width = qMax(width,base.width());
103 height += base.height();
105 height += base.height();
104 sh = QSizeF(width,height);
106 sh = QSizeF(width,height);
105 break;
107 break;
106 }
108 }
107 case Qt::PreferredSize: {
109 case Qt::PreferredSize: {
108 int count = qMax(ticksList.last().count(),ticksList.first().count());
110 int count = qMax(ticksList.last().count(),ticksList.first().count());
109 width=fn.averageCharWidth() * count;
111 width=fn.averageCharWidth() * count;
110 height=fn.height()+labelPadding();
112 height=fn.height()+labelPadding();
111 width=qMax(width,base.width());
113 width=qMax(width,base.width());
112 height+=base.height();
114 height+=base.height();
113 sh = QSizeF(width,height);
115 sh = QSizeF(width,height);
114 break;
116 break;
115 }
117 }
116 default:
118 default:
117 break;
119 break;
118 }
120 }
119
121
120 return sh;
122 return sh;
121 }
123 }
122
124
125 #include "moc_chartlogvalueaxisx_p.cpp"
126
123 QTCOMMERCIALCHART_END_NAMESPACE
127 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,64
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTLOGVALUEAXISX_H
30 #ifndef CHARTLOGVALUEAXISX_H
31 #define CHARTLOGVALUEAXISX_H
31 #define CHARTLOGVALUEAXISX_H
32
32
33 #include "horizontalaxis_p.h"
33 #include "horizontalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QLogValueAxis;
37 class QLogValueAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartLogValueAxisX : public HorizontalAxis
40 class ChartLogValueAxisX : public HorizontalAxis
41 {
41 {
42 Q_OBJECT
43
42 public:
44 public:
43 ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item);
45 ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item);
44 ~ChartLogValueAxisX();
46 ~ChartLogValueAxisX();
45
47
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
47
49
48 protected:
50 protected:
49 void handleAxisUpdated();
51 void handleAxisUpdated();
50 QVector<qreal> calculateLayout() const;
52 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
53 void updateGeometry();
52
54
55 private Q_SLOTS:
56 void handleBaseChanged(qreal base);
57
53 private:
58 private:
54 QLogValueAxis *m_axis;
59 QLogValueAxis *m_axis;
55 };
60 };
56
61
57 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
58
63
59 #endif /* CHARTLOGVALUEAXISX_H */
64 #endif /* CHARTLOGVALUEAXISX_H */
@@ -1,120 +1,124
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartlogvalueaxisy_p.h"
21 #include "chartlogvalueaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
31 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 : VerticalAxis(axis, item),
32 : VerticalAxis(axis, item),
33 m_axis(axis)
33 m_axis(axis)
34 {
34 {
35 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
35 }
36 }
36
37
37 ChartLogValueAxisY::~ChartLogValueAxisY()
38 ChartLogValueAxisY::~ChartLogValueAxisY()
38 {
39 {
39 }
40 }
40
41
41 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
42 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
42 {
43 {
43 QVector<qreal> points;
44 QVector<qreal> points;
44 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
45 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
45 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
46 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
46 qreal leftEdge = logMin < logMax ? logMin : logMax;
47 qreal leftEdge = logMin < logMax ? logMin : logMax;
47 qreal ceilEdge = ceil(leftEdge);
48 qreal ceilEdge = ceil(leftEdge);
48 int tickCount = qAbs(qRound(logMax - logMin));
49 int tickCount = qAbs(qRound(logMax - logMin));
49 tickCount++;
50 tickCount++;
50
51
51 points.resize(tickCount);
52 points.resize(tickCount);
52 const QRectF &gridRect = gridGeometry();
53 const QRectF &gridRect = gridGeometry();
53 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
54 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
54 for (int i = 0; i < tickCount; ++i)
55 for (int i = 0; i < tickCount; ++i)
55 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
56 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
56
57
57 return points;
58 return points;
58 }
59 }
59
60
60
61
61 void ChartLogValueAxisY::updateGeometry()
62 void ChartLogValueAxisY::updateGeometry()
62 {
63 {
63 const QVector<qreal> &layout = ChartAxis::layout();
64 const QVector<qreal> &layout = ChartAxis::layout();
64 if (layout.isEmpty())
65 if (layout.isEmpty())
65 return;
66 return;
66 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
67 VerticalAxis::updateGeometry();
68 VerticalAxis::updateGeometry();
68 }
69 }
69
70
70 //void ChartLogValueAxisY::handleAxisUpdated()
71 void ChartLogValueAxisY::handleBaseChanged(qreal base)
71 //{
72 {
72 // ChartAxis::handleAxisUpdated();
73 Q_UNUSED(base);
73 //}
74 if(presenter()) presenter()->layout()->invalidate();
75 }
74
76
75 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
77 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 {
78 {
77 Q_UNUSED(constraint)
79 Q_UNUSED(constraint)
78
80
79 QFontMetrics fn(font());
81 QFontMetrics fn(font());
80 QSizeF sh;
82 QSizeF sh;
81
83
82 QSizeF base = VerticalAxis::sizeHint(which, constraint);
84 QSizeF base = VerticalAxis::sizeHint(which, constraint);
83 QStringList ticksList;
85 QStringList ticksList;
84 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
86 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
85 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
87 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
86 int tickCount = qAbs(qRound(logMax - logMin));
88 int tickCount = qAbs(qRound(logMax - logMin));
87 tickCount++;
89 tickCount++;
88 if (m_axis->max() > m_axis->min() && tickCount > 1)
90 if (m_axis->max() > m_axis->min() && tickCount > 1)
89 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
91 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
90 else
92 else
91 ticksList.append(QString(" "));
93 ticksList.append(QString(" "));
92 qreal width = 0;
94 qreal width = 0;
93 qreal height = 0;
95 qreal height = 0;
94
96
95 switch (which) {
97 switch (which) {
96 case Qt::MinimumSize: {
98 case Qt::MinimumSize: {
97 width = fn.boundingRect("...").width() + labelPadding();
99 width = fn.boundingRect("...").width() + labelPadding();
98 width += base.width();
100 width += base.width();
99 height = fn.height();
101 height = fn.height();
100 height = qMax(height,base.height());
102 height = qMax(height,base.height());
101 sh = QSizeF(width,height);
103 sh = QSizeF(width,height);
102 break;
104 break;
103 }
105 }
104 case Qt::PreferredSize: {
106 case Qt::PreferredSize: {
105 int count = qMax(ticksList.first().count() , ticksList.last().count());
107 int count = qMax(ticksList.first().count() , ticksList.last().count());
106 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
108 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
107 width += base.width();
109 width += base.width();
108 height = fn.height() * ticksList.count();
110 height = fn.height() * ticksList.count();
109 height = qMax(height,base.height());
111 height = qMax(height,base.height());
110 sh = QSizeF(width,height);
112 sh = QSizeF(width,height);
111 break;
113 break;
112 }
114 }
113 default:
115 default:
114 break;
116 break;
115 }
117 }
116
118
117 return sh;
119 return sh;
118 }
120 }
119
121
122 #include "moc_chartlogvalueaxisy_p.cpp"
123
120 QTCOMMERCIALCHART_END_NAMESPACE
124 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,64
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTLOGVALUEAXISY_H
30 #ifndef CHARTLOGVALUEAXISY_H
31 #define CHARTLOGVALUEAXISY_H
31 #define CHARTLOGVALUEAXISY_H
32
32
33 #include "verticalaxis_p.h"
33 #include "verticalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QLogValueAxis;
37 class QLogValueAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartLogValueAxisY : public VerticalAxis
40 class ChartLogValueAxisY : public VerticalAxis
41 {
41 {
42 Q_OBJECT
43
42 public:
44 public:
43 ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item);
45 ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item);
44 ~ChartLogValueAxisY();
46 ~ChartLogValueAxisY();
45
47
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
47
49
48 protected:
50 protected:
49 void handleAxisUpdated();
51 void handleAxisUpdated();
50 QVector<qreal> calculateLayout() const;
52 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
53 void updateGeometry();
52
54
55 private Q_SLOTS:
56 void handleBaseChanged(qreal base);
57
53 private:
58 private:
54 QLogValueAxis *m_axis;
59 QLogValueAxis *m_axis;
55 };
60 };
56
61
57 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
58
63
59 #endif /* CHARTLOGVALUEAXISY_H */
64 #endif /* CHARTLOGVALUEAXISY_H */
@@ -1,229 +1,231
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "logxlogydomain_p.h"
21 #include "logxlogydomain_p.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include <qmath.h>
24 #include <qmath.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 LogXLogYDomain::LogXLogYDomain(QObject *parent)
28 LogXLogYDomain::LogXLogYDomain(QObject *parent)
29 : AbstractDomain(parent),
29 : AbstractDomain(parent),
30 m_logMinX(0),
30 m_logMinX(0),
31 m_logMaxX(1),
31 m_logMaxX(1),
32 m_logBaseX(10),
32 m_logBaseX(10),
33 m_logMinY(0),
33 m_logMinY(0),
34 m_logMaxY(1),
34 m_logMaxY(1),
35 m_logBaseY(10)
35 m_logBaseY(10)
36 {
36 {
37 }
37 }
38
38
39 LogXLogYDomain::~LogXLogYDomain()
39 LogXLogYDomain::~LogXLogYDomain()
40 {
40 {
41 }
41 }
42
42
43 void LogXLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
43 void LogXLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
44 {
44 {
45 bool axisXChanged = false;
45 bool axisXChanged = false;
46 bool axisYChanged = false;
46 bool axisYChanged = false;
47
47
48 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
48 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
49 m_minX = minX;
49 m_minX = minX;
50 m_maxX = maxX;
50 m_maxX = maxX;
51 axisXChanged = true;
51 axisXChanged = true;
52 m_logMinX = log10(m_minX) / log10(m_logBaseX);
52 m_logMinX = log10(m_minX) / log10(m_logBaseX);
53 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
53 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
54 if(!m_signalsBlocked)
54 if(!m_signalsBlocked)
55 emit rangeHorizontalChanged(m_minX, m_maxX);
55 emit rangeHorizontalChanged(m_minX, m_maxX);
56 }
56 }
57
57
58 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
58 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
59 m_minY = minY;
59 m_minY = minY;
60 m_maxY = maxY;
60 m_maxY = maxY;
61 axisYChanged = true;
61 axisYChanged = true;
62 m_logMinY = log10(m_minY) / log10(m_logBaseY);
62 m_logMinY = log10(m_minY) / log10(m_logBaseY);
63 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
63 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
64 if(!m_signalsBlocked)
64 if(!m_signalsBlocked)
65 emit rangeVerticalChanged(m_minY, m_maxY);
65 emit rangeVerticalChanged(m_minY, m_maxY);
66 }
66 }
67
67
68 if (axisXChanged || axisYChanged)
68 if (axisXChanged || axisYChanged)
69 emit updated();
69 emit updated();
70 }
70 }
71
71
72 void LogXLogYDomain::zoomIn(const QRectF &rect)
72 void LogXLogYDomain::zoomIn(const QRectF &rect)
73 {
73 {
74 qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
74 qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
75 qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
75 qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
76 qreal minX = qPow(m_logBaseX, newLogMinX);
76 qreal minX = qPow(m_logBaseX, newLogMinX);
77 qreal maxX = qPow(m_logBaseX, newLogMaxX);
77 qreal maxX = qPow(m_logBaseX, newLogMaxX);
78
78
79 qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height();
79 qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height();
80 qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height();
80 qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height();
81 qreal minY = qPow(m_logBaseY, newLogMinY);
81 qreal minY = qPow(m_logBaseY, newLogMinY);
82 qreal maxY = qPow(m_logBaseY, newLogMaxY);
82 qreal maxY = qPow(m_logBaseY, newLogMaxY);
83
83
84 setRange(minX, maxX, minY, maxY);
84 setRange(minX, maxX, minY, maxY);
85 }
85 }
86
86
87 void LogXLogYDomain::zoomOut(const QRectF &rect)
87 void LogXLogYDomain::zoomOut(const QRectF &rect)
88 {
88 {
89 qreal ratioX = m_size.width()/rect.width();
89 qreal ratioX = m_size.width()/rect.width();
90 qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX;
90 qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX;
91 qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX;
91 qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX;
92 qreal minX = qPow(m_logBaseX, newLogMinX);
92 qreal minX = qPow(m_logBaseX, newLogMinX);
93 qreal maxX = qPow(m_logBaseX, newLogMaxX);
93 qreal maxX = qPow(m_logBaseX, newLogMaxX);
94
94
95 qreal ratioY = m_size.height()/rect.height();
95 qreal ratioY = m_size.height()/rect.height();
96 qreal newLogMinY = m_logMinY - (m_logMaxY - m_logMinY) / ratioY;
96 qreal newLogMinY = m_logMinY - (m_logMaxY - m_logMinY) / ratioY;
97 qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY;
97 qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY;
98 qreal minY = qPow(m_logBaseY, newLogMinY);
98 qreal minY = qPow(m_logBaseY, newLogMinY);
99 qreal maxY = qPow(m_logBaseY, newLogMaxY);
99 qreal maxY = qPow(m_logBaseY, newLogMaxY);
100
100
101 setRange(minX, maxX, minY, maxY);
101 setRange(minX, maxX, minY, maxY);
102 }
102 }
103
103
104 void LogXLogYDomain::move(qreal dx, qreal dy)
104 void LogXLogYDomain::move(qreal dx, qreal dy)
105 {
105 {
106 qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width();
106 qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width();
107 qreal minX = qPow(m_logBaseX, m_logMinX + stepX);
107 qreal minX = qPow(m_logBaseX, m_logMinX + stepX);
108 qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX);
108 qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX);
109
109
110 qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height();
110 qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height();
111 qreal minY = qPow(m_logBaseY, m_logMinY + stepY);
111 qreal minY = qPow(m_logBaseY, m_logMinY + stepY);
112 qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY);
112 qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY);
113
113
114 setRange(minX, maxX, minY, maxY);
114 setRange(minX, maxX, minY, maxY);
115 }
115 }
116
116
117 QPointF LogXLogYDomain::calculateGeometryPoint(const QPointF &point) const
117 QPointF LogXLogYDomain::calculateGeometryPoint(const QPointF &point) const
118 {
118 {
119 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
119 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
120 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
120 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
121 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
121 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
122 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
122 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
123 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - leftEdgeX * deltaX;
123 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - leftEdgeX * deltaX;
124 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - leftEdgeY * -deltaY + m_size.height();
124 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - leftEdgeY * -deltaY + m_size.height();
125 return QPointF(x, y);
125 return QPointF(x, y);
126 }
126 }
127
127
128 QVector<QPointF> LogXLogYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
128 QVector<QPointF> LogXLogYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
129 {
129 {
130 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
130 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
131 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
131 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
132 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
132 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
133 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
133 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
134
134
135 QVector<QPointF> result;
135 QVector<QPointF> result;
136 result.resize(vector.count());
136 result.resize(vector.count());
137
137
138 for (int i = 0; i < vector.count(); ++i) {
138 for (int i = 0; i < vector.count(); ++i) {
139 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - leftEdgeX * deltaX;
139 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - leftEdgeX * deltaX;
140 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - leftEdgeY * -deltaY + m_size.height();
140 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - leftEdgeY * -deltaY + m_size.height();
141 result[i].setX(x);
141 result[i].setX(x);
142 result[i].setY(y);
142 result[i].setY(y);
143 }
143 }
144 return result;
144 return result;
145 }
145 }
146
146
147 QPointF LogXLogYDomain::calculateDomainPoint(const QPointF &point) const
147 QPointF LogXLogYDomain::calculateDomainPoint(const QPointF &point) const
148 {
148 {
149 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
149 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
150 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
150 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
151 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
151 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
152 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
152 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
153 qreal x = qPow(m_logBaseX, leftEdgeX + point.x() / deltaX);
153 qreal x = qPow(m_logBaseX, leftEdgeX + point.x() / deltaX);
154 qreal y = qPow(m_logBaseY, leftEdgeY + (m_size.height() - point.y()) / deltaY);
154 qreal y = qPow(m_logBaseY, leftEdgeY + (m_size.height() - point.y()) / deltaY);
155 return QPointF(x, y);
155 return QPointF(x, y);
156 }
156 }
157
157
158 bool LogXLogYDomain::attachAxis(QAbstractAxis* axis)
158 bool LogXLogYDomain::attachAxis(QAbstractAxis* axis)
159 {
159 {
160 AbstractDomain::attachAxis(axis);
160 AbstractDomain::attachAxis(axis);
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
162
162
163 if(logAxis && logAxis->orientation()==Qt::Vertical) {
163 if(logAxis && logAxis->orientation()==Qt::Vertical) {
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
165 handleVerticalAxisBaseChanged(logAxis->base());
165 handleVerticalAxisBaseChanged(logAxis->base());
166 }
166 }
167
167
168 if(logAxis && logAxis->orientation()==Qt::Horizontal) {
168 if(logAxis && logAxis->orientation()==Qt::Horizontal) {
169 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
169 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
170 handleHorizontalAxisBaseChanged(logAxis->base());
170 handleHorizontalAxisBaseChanged(logAxis->base());
171 }
171 }
172
172
173 return true;
173 return true;
174 }
174 }
175
175
176 bool LogXLogYDomain::detachAxis(QAbstractAxis* axis)
176 bool LogXLogYDomain::detachAxis(QAbstractAxis* axis)
177 {
177 {
178 AbstractDomain::detachAxis(axis);
178 AbstractDomain::detachAxis(axis);
179 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
179 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
180
180
181 if(logAxis && logAxis->orientation()==Qt::Vertical)
181 if(logAxis && logAxis->orientation()==Qt::Vertical)
182 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
182 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
183
183
184 if(logAxis && logAxis->orientation()==Qt::Horizontal)
184 if(logAxis && logAxis->orientation()==Qt::Horizontal)
185 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
185 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
186
186
187 return true;
187 return true;
188 }
188 }
189
189
190 void LogXLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
190 void LogXLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
191 {
191 {
192 m_logBaseY = baseY;
192 m_logBaseY = baseY;
193 m_logMinY = log10(m_minY) / log10(m_logBaseY);
193 m_logMinY = log10(m_minY) / log10(m_logBaseY);
194 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
194 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
195 emit updated();
195 }
196 }
196
197
197 void LogXLogYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
198 void LogXLogYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
198 {
199 {
199 m_logBaseX = baseX;
200 m_logBaseX = baseX;
200 m_logMinX = log10(m_minX) / log10(m_logBaseX);
201 m_logMinX = log10(m_minX) / log10(m_logBaseX);
201 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
202 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
203 emit updated();
202 }
204 }
203
205
204 // operators
206 // operators
205
207
206 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
208 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
207 {
209 {
208 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
210 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
209 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
211 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
210 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
212 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
211 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
213 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
212 }
214 }
213
215
214
216
215 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
217 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
216 {
218 {
217 return !(domain1 == domain2);
219 return !(domain1 == domain2);
218 }
220 }
219
221
220
222
221 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXLogYDomain &domain)
223 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXLogYDomain &domain)
222 {
224 {
223 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
225 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
224 return dbg.maybeSpace();
226 return dbg.maybeSpace();
225 }
227 }
226
228
227 #include "moc_logxlogydomain_p.cpp"
229 #include "moc_logxlogydomain_p.cpp"
228
230
229 QTCOMMERCIALCHART_END_NAMESPACE
231 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,214 +1,215
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "logxydomain_p.h"
21 #include "logxydomain_p.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include <qmath.h>
24 #include <qmath.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 LogXYDomain::LogXYDomain(QObject *parent)
28 LogXYDomain::LogXYDomain(QObject *parent)
29 : AbstractDomain(parent),
29 : AbstractDomain(parent),
30 m_logMinX(0),
30 m_logMinX(0),
31 m_logMaxX(1),
31 m_logMaxX(1),
32 m_logBaseX(10)
32 m_logBaseX(10)
33 {
33 {
34 }
34 }
35
35
36 LogXYDomain::~LogXYDomain()
36 LogXYDomain::~LogXYDomain()
37 {
37 {
38 }
38 }
39
39
40 void LogXYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
40 void LogXYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
41 {
41 {
42 bool axisXChanged = false;
42 bool axisXChanged = false;
43 bool axisYChanged = false;
43 bool axisYChanged = false;
44
44
45 if (!qFuzzyCompare(m_minX, minX) || !qFuzzyCompare(m_maxX, maxX)) {
45 if (!qFuzzyCompare(m_minX, minX) || !qFuzzyCompare(m_maxX, maxX)) {
46 m_minX = minX;
46 m_minX = minX;
47 m_maxX = maxX;
47 m_maxX = maxX;
48 axisXChanged = true;
48 axisXChanged = true;
49 m_logMinX = log10(m_minX) / log10(m_logBaseX);
49 m_logMinX = log10(m_minX) / log10(m_logBaseX);
50 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
50 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
51 if(!m_signalsBlocked)
51 if(!m_signalsBlocked)
52 emit rangeHorizontalChanged(m_minX, m_maxX);
52 emit rangeHorizontalChanged(m_minX, m_maxX);
53 }
53 }
54
54
55 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
55 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
56 m_minY = minY;
56 m_minY = minY;
57 m_maxY = maxY;
57 m_maxY = maxY;
58 axisYChanged = true;
58 axisYChanged = true;
59 if(!m_signalsBlocked)
59 if(!m_signalsBlocked)
60 emit rangeVerticalChanged(m_minY, m_maxY);
60 emit rangeVerticalChanged(m_minY, m_maxY);
61 }
61 }
62
62
63 if (axisXChanged || axisYChanged)
63 if (axisXChanged || axisYChanged)
64 emit updated();
64 emit updated();
65 }
65 }
66
66
67 void LogXYDomain::zoomIn(const QRectF &rect)
67 void LogXYDomain::zoomIn(const QRectF &rect)
68 {
68 {
69 qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
69 qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
70 qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
70 qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX;
71 qreal minX = qPow(m_logBaseX, newLogMinX);
71 qreal minX = qPow(m_logBaseX, newLogMinX);
72 qreal maxX = qPow(m_logBaseX, newLogMaxX);
72 qreal maxX = qPow(m_logBaseX, newLogMaxX);
73
73
74 qreal dy = spanY() / m_size.height();
74 qreal dy = spanY() / m_size.height();
75 qreal minY = m_minY;
75 qreal minY = m_minY;
76 qreal maxY = m_maxY;
76 qreal maxY = m_maxY;
77
77
78 minY = maxY - dy * rect.bottom();
78 minY = maxY - dy * rect.bottom();
79 maxY = maxY - dy * rect.top();
79 maxY = maxY - dy * rect.top();
80
80
81 setRange(minX, maxX, minY, maxY);
81 setRange(minX, maxX, minY, maxY);
82 }
82 }
83
83
84 void LogXYDomain::zoomOut(const QRectF &rect)
84 void LogXYDomain::zoomOut(const QRectF &rect)
85 {
85 {
86 qreal ratioX = m_size.width()/rect.width();
86 qreal ratioX = m_size.width()/rect.width();
87 qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX;
87 qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX;
88 qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX;
88 qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX;
89 qreal minX = qPow(m_logBaseX, newLogMinX);
89 qreal minX = qPow(m_logBaseX, newLogMinX);
90 qreal maxX = qPow(m_logBaseX, newLogMaxX);
90 qreal maxX = qPow(m_logBaseX, newLogMaxX);
91
91
92 qreal dy = spanY() / rect.height();
92 qreal dy = spanY() / rect.height();
93 qreal minY = m_minY;
93 qreal minY = m_minY;
94 qreal maxY = m_maxY;
94 qreal maxY = m_maxY;
95
95
96 maxY = minY + dy * rect.bottom();
96 maxY = minY + dy * rect.bottom();
97 minY = maxY - dy * m_size.height();
97 minY = maxY - dy * m_size.height();
98
98
99 setRange(minX, maxX, minY, maxY);
99 setRange(minX, maxX, minY, maxY);
100 }
100 }
101
101
102 void LogXYDomain::move(qreal dx, qreal dy)
102 void LogXYDomain::move(qreal dx, qreal dy)
103 {
103 {
104 qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width();
104 qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width();
105 qreal minX = qPow(m_logBaseX, m_logMinX + stepX);
105 qreal minX = qPow(m_logBaseX, m_logMinX + stepX);
106 qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX);
106 qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX);
107
107
108 qreal y = spanY() / m_size.height();
108 qreal y = spanY() / m_size.height();
109 qreal minY = m_minY;
109 qreal minY = m_minY;
110 qreal maxY = m_maxY;
110 qreal maxY = m_maxY;
111
111
112 if (dy != 0) {
112 if (dy != 0) {
113 minY = minY + y * dy;
113 minY = minY + y * dy;
114 maxY = maxY + y * dy;
114 maxY = maxY + y * dy;
115 }
115 }
116 setRange(minX, maxX, minY, maxY);
116 setRange(minX, maxX, minY, maxY);
117 }
117 }
118
118
119 QPointF LogXYDomain::calculateGeometryPoint(const QPointF &point) const
119 QPointF LogXYDomain::calculateGeometryPoint(const QPointF &point) const
120 {
120 {
121 const qreal leftEdge = m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
121 const qreal leftEdge = m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
122 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
122 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
123 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
123 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
124
124
125 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - leftEdge * deltaX;
125 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - leftEdge * deltaX;
126 qreal y = (point.y() - m_minY) * -deltaY + m_size.height();
126 qreal y = (point.y() - m_minY) * -deltaY + m_size.height();
127 return QPointF(x, y);
127 return QPointF(x, y);
128 }
128 }
129
129
130 QVector<QPointF> LogXYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
130 QVector<QPointF> LogXYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
131 {
131 {
132 const qreal leftEdge = m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
132 const qreal leftEdge = m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
133 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
133 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
134 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
134 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
135
135
136 QVector<QPointF> result;
136 QVector<QPointF> result;
137 result.resize(vector.count());
137 result.resize(vector.count());
138
138
139 for (int i = 0; i < vector.count(); ++i) {
139 for (int i = 0; i < vector.count(); ++i) {
140 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - leftEdge * deltaX;
140 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - leftEdge * deltaX;
141 qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height();
141 qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height();
142 result[i].setX(x);
142 result[i].setX(x);
143 result[i].setY(y);
143 result[i].setY(y);
144 }
144 }
145 return result;
145 return result;
146 }
146 }
147
147
148 QPointF LogXYDomain::calculateDomainPoint(const QPointF &point) const
148 QPointF LogXYDomain::calculateDomainPoint(const QPointF &point) const
149 {
149 {
150 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
150 const qreal leftEdgeX= m_logMinX < m_logMaxX ? m_logMinX : m_logMaxX;
151 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
151 const qreal deltaX = m_size.width() / qAbs(m_logMaxX - m_logMinX);
152 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
152 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
153 qreal x = qPow(m_logBaseX, leftEdgeX + point.x() / deltaX);
153 qreal x = qPow(m_logBaseX, leftEdgeX + point.x() / deltaX);
154 qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY;
154 qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY;
155 return QPointF(x, y);
155 return QPointF(x, y);
156 }
156 }
157
157
158 bool LogXYDomain::attachAxis(QAbstractAxis* axis)
158 bool LogXYDomain::attachAxis(QAbstractAxis* axis)
159 {
159 {
160 AbstractDomain::attachAxis(axis);
160 AbstractDomain::attachAxis(axis);
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
162
162
163 if(logAxis && logAxis->orientation()==Qt::Horizontal) {
163 if(logAxis && logAxis->orientation()==Qt::Horizontal) {
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
165 handleHorizontalAxisBaseChanged(logAxis->base());
165 handleHorizontalAxisBaseChanged(logAxis->base());
166 }
166 }
167
167
168 return true;
168 return true;
169 }
169 }
170
170
171 bool LogXYDomain::detachAxis(QAbstractAxis* axis)
171 bool LogXYDomain::detachAxis(QAbstractAxis* axis)
172 {
172 {
173 AbstractDomain::detachAxis(axis);
173 AbstractDomain::detachAxis(axis);
174 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
174 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
175
175
176 if(logAxis && logAxis->orientation()==Qt::Horizontal)
176 if(logAxis && logAxis->orientation()==Qt::Horizontal)
177 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
177 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
178
178
179 return true;
179 return true;
180 }
180 }
181
181
182 void LogXYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
182 void LogXYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
183 {
183 {
184 m_logBaseX = baseX;
184 m_logBaseX = baseX;
185 m_logMinX = log10(m_minX) / log10(m_logBaseX);
185 m_logMinX = log10(m_minX) / log10(m_logBaseX);
186 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
186 m_logMaxX = log10(m_maxX) / log10(m_logBaseX);
187 emit updated();
187 }
188 }
188
189
189 // operators
190 // operators
190
191
191 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXYDomain &domain1, const LogXYDomain &domain2)
192 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXYDomain &domain1, const LogXYDomain &domain2)
192 {
193 {
193 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
194 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
194 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
195 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
195 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
196 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
196 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
197 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
197 }
198 }
198
199
199
200
200 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXYDomain &domain1, const LogXYDomain &domain2)
201 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXYDomain &domain1, const LogXYDomain &domain2)
201 {
202 {
202 return !(domain1 == domain2);
203 return !(domain1 == domain2);
203 }
204 }
204
205
205
206
206 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXYDomain &domain)
207 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXYDomain &domain)
207 {
208 {
208 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
209 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
209 return dbg.maybeSpace();
210 return dbg.maybeSpace();
210 }
211 }
211
212
212 #include "moc_logxydomain_p.cpp"
213 #include "moc_logxydomain_p.cpp"
213
214
214 QTCOMMERCIALCHART_END_NAMESPACE
215 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,212 +1,213
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "xlogydomain_p.h"
21 #include "xlogydomain_p.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include <qmath.h>
24 #include <qmath.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 XLogYDomain::XLogYDomain(QObject *parent)
28 XLogYDomain::XLogYDomain(QObject *parent)
29 : AbstractDomain(parent),
29 : AbstractDomain(parent),
30 m_logMinY(0),
30 m_logMinY(0),
31 m_logMaxY(1),
31 m_logMaxY(1),
32 m_logBaseY(10)
32 m_logBaseY(10)
33 {
33 {
34 }
34 }
35
35
36 XLogYDomain::~XLogYDomain()
36 XLogYDomain::~XLogYDomain()
37 {
37 {
38 }
38 }
39
39
40 void XLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
40 void XLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
41 {
41 {
42 bool axisXChanged = false;
42 bool axisXChanged = false;
43 bool axisYChanged = false;
43 bool axisYChanged = false;
44
44
45 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
45 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
46 m_minX = minX;
46 m_minX = minX;
47 m_maxX = maxX;
47 m_maxX = maxX;
48 axisXChanged = true;
48 axisXChanged = true;
49 if(!m_signalsBlocked)
49 if(!m_signalsBlocked)
50 emit rangeHorizontalChanged(m_minX, m_maxX);
50 emit rangeHorizontalChanged(m_minX, m_maxX);
51 }
51 }
52
52
53 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
53 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
54 m_minY = minY;
54 m_minY = minY;
55 m_maxY = maxY;
55 m_maxY = maxY;
56 axisYChanged = true;
56 axisYChanged = true;
57 m_logMinY = log10(m_minY) / log10(m_logBaseY);
57 m_logMinY = log10(m_minY) / log10(m_logBaseY);
58 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
58 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
59 if(!m_signalsBlocked)
59 if(!m_signalsBlocked)
60 emit rangeVerticalChanged(m_minY, m_maxY);
60 emit rangeVerticalChanged(m_minY, m_maxY);
61 }
61 }
62
62
63 if (axisXChanged || axisYChanged)
63 if (axisXChanged || axisYChanged)
64 emit updated();
64 emit updated();
65 }
65 }
66
66
67 void XLogYDomain::zoomIn(const QRectF &rect)
67 void XLogYDomain::zoomIn(const QRectF &rect)
68 {
68 {
69 qreal dx = spanX() / m_size.width();
69 qreal dx = spanX() / m_size.width();
70 qreal maxX = m_maxX;
70 qreal maxX = m_maxX;
71 qreal minX = m_minX;
71 qreal minX = m_minX;
72
72
73 maxX = minX + dx * rect.right();
73 maxX = minX + dx * rect.right();
74 minX = minX + dx * rect.left();
74 minX = minX + dx * rect.left();
75
75
76 qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height();
76 qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height();
77 qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height();
77 qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height();
78 qreal minY = qPow(m_logBaseY, newLogMinY);
78 qreal minY = qPow(m_logBaseY, newLogMinY);
79 qreal maxY = qPow(m_logBaseY, newLogMaxY);
79 qreal maxY = qPow(m_logBaseY, newLogMaxY);
80
80
81 setRange(minX, maxX, minY, maxY);
81 setRange(minX, maxX, minY, maxY);
82 }
82 }
83
83
84 void XLogYDomain::zoomOut(const QRectF &rect)
84 void XLogYDomain::zoomOut(const QRectF &rect)
85 {
85 {
86 qreal dx = spanX() / rect.width();
86 qreal dx = spanX() / rect.width();
87 qreal maxX = m_maxX;
87 qreal maxX = m_maxX;
88 qreal minX = m_minX;
88 qreal minX = m_minX;
89
89
90 minX = maxX - dx * rect.right();
90 minX = maxX - dx * rect.right();
91 maxX = minX + dx * m_size.width();
91 maxX = minX + dx * m_size.width();
92
92
93 qreal ratioY = m_size.height()/rect.height();
93 qreal ratioY = m_size.height()/rect.height();
94 qreal newLogMinY = m_logMinY - (m_logMaxY - m_logMinY) / ratioY;
94 qreal newLogMinY = m_logMinY - (m_logMaxY - m_logMinY) / ratioY;
95 qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY;
95 qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY;
96 qreal minY = qPow(m_logBaseY, newLogMinY);
96 qreal minY = qPow(m_logBaseY, newLogMinY);
97 qreal maxY = qPow(m_logBaseY, newLogMaxY);
97 qreal maxY = qPow(m_logBaseY, newLogMaxY);
98
98
99 setRange(minX, maxX, minY, maxY);
99 setRange(minX, maxX, minY, maxY);
100 }
100 }
101
101
102 void XLogYDomain::move(qreal dx, qreal dy)
102 void XLogYDomain::move(qreal dx, qreal dy)
103 {
103 {
104 qreal x = spanX() / m_size.width();
104 qreal x = spanX() / m_size.width();
105 qreal maxX = m_maxX;
105 qreal maxX = m_maxX;
106 qreal minX = m_minX;
106 qreal minX = m_minX;
107
107
108 if (dx != 0) {
108 if (dx != 0) {
109 minX = minX + x * dx;
109 minX = minX + x * dx;
110 maxX = maxX + x * dx;
110 maxX = maxX + x * dx;
111 }
111 }
112
112
113 qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height();
113 qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height();
114 qreal minY = qPow(m_logBaseY, m_logMinY + stepY);
114 qreal minY = qPow(m_logBaseY, m_logMinY + stepY);
115 qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY);
115 qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY);
116
116
117 setRange(minX, maxX, minY, maxY);
117 setRange(minX, maxX, minY, maxY);
118 }
118 }
119
119
120 QPointF XLogYDomain::calculateGeometryPoint(const QPointF &point) const
120 QPointF XLogYDomain::calculateGeometryPoint(const QPointF &point) const
121 {
121 {
122 const qreal leftEdge = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
122 const qreal leftEdge = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
123 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
123 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
124 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
124 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
125
125
126 qreal x = (point.x() - m_minX) * deltaX;
126 qreal x = (point.x() - m_minX) * deltaX;
127 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - leftEdge * -deltaY + m_size.height();
127 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - leftEdge * -deltaY + m_size.height();
128 return QPointF(x, y);
128 return QPointF(x, y);
129 }
129 }
130
130
131 QVector<QPointF> XLogYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
131 QVector<QPointF> XLogYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const
132 {
132 {
133 const qreal leftEdge = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
133 const qreal leftEdge = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
134 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
134 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
135 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
135 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
136
136
137 QVector<QPointF> result;
137 QVector<QPointF> result;
138 result.resize(vector.count());
138 result.resize(vector.count());
139
139
140 for (int i = 0; i < vector.count(); ++i) {
140 for (int i = 0; i < vector.count(); ++i) {
141 qreal x = (vector[i].x() - m_minX) * deltaX;
141 qreal x = (vector[i].x() - m_minX) * deltaX;
142 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - leftEdge * -deltaY + m_size.height();
142 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - leftEdge * -deltaY + m_size.height();
143 result[i].setX(x);
143 result[i].setX(x);
144 result[i].setY(y);
144 result[i].setY(y);
145 }
145 }
146 return result;
146 return result;
147 }
147 }
148
148
149 QPointF XLogYDomain::calculateDomainPoint(const QPointF &point) const
149 QPointF XLogYDomain::calculateDomainPoint(const QPointF &point) const
150 {
150 {
151 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
151 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
152 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
152 const qreal leftEdgeY = m_logMinY < m_logMaxY ? m_logMinY : m_logMaxY;
153 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
153 const qreal deltaY = m_size.height() / qAbs(m_logMaxY - m_logMinY);
154 qreal x = point.x() / deltaX + m_minX;
154 qreal x = point.x() / deltaX + m_minX;
155 qreal y = qPow(m_logBaseY, leftEdgeY + (m_size.height() - point.y()) / deltaY);
155 qreal y = qPow(m_logBaseY, leftEdgeY + (m_size.height() - point.y()) / deltaY);
156 return QPointF(x, y);
156 return QPointF(x, y);
157 }
157 }
158
158
159 bool XLogYDomain::attachAxis(QAbstractAxis* axis)
159 bool XLogYDomain::attachAxis(QAbstractAxis* axis)
160 {
160 {
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
161 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
162
162
163 if(logAxis && logAxis->orientation()==Qt::Vertical){
163 if(logAxis && logAxis->orientation()==Qt::Vertical){
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
164 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
165 handleVerticalAxisBaseChanged(logAxis->base());
165 handleVerticalAxisBaseChanged(logAxis->base());
166 }
166 }
167 return AbstractDomain::attachAxis(axis);
167 return AbstractDomain::attachAxis(axis);
168 }
168 }
169
169
170 bool XLogYDomain::detachAxis(QAbstractAxis* axis)
170 bool XLogYDomain::detachAxis(QAbstractAxis* axis)
171 {
171 {
172 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
172 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
173
173
174 if(logAxis && logAxis->orientation()==Qt::Vertical)
174 if(logAxis && logAxis->orientation()==Qt::Vertical)
175 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
175 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
176
176
177 return AbstractDomain::detachAxis(axis);
177 return AbstractDomain::detachAxis(axis);
178 }
178 }
179
179
180 void XLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
180 void XLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
181 {
181 {
182 m_logBaseY = baseY;
182 m_logBaseY = baseY;
183 m_logMinY = log10(m_minY) / log10(m_logBaseY);
183 m_logMinY = log10(m_minY) / log10(m_logBaseY);
184 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
184 m_logMaxY = log10(m_maxY) / log10(m_logBaseY);
185 emit updated();
185 }
186 }
186
187
187 // operators
188 // operators
188
189
189 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const XLogYDomain &domain1, const XLogYDomain &domain2)
190 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const XLogYDomain &domain1, const XLogYDomain &domain2)
190 {
191 {
191 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
192 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) &&
192 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
193 qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) &&
193 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
194 qFuzzyIsNull(domain1.m_minX - domain2.m_minX) &&
194 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
195 qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
195 }
196 }
196
197
197
198
198 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const XLogYDomain &domain1, const XLogYDomain &domain2)
199 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const XLogYDomain &domain1, const XLogYDomain &domain2)
199 {
200 {
200 return !(domain1 == domain2);
201 return !(domain1 == domain2);
201 }
202 }
202
203
203
204
204 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const XLogYDomain &domain)
205 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const XLogYDomain &domain)
205 {
206 {
206 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
207 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
207 return dbg.maybeSpace();
208 return dbg.maybeSpace();
208 }
209 }
209
210
210 #include "moc_xlogydomain_p.cpp"
211 #include "moc_xlogydomain_p.cpp"
211
212
212 QTCOMMERCIALCHART_END_NAMESPACE
213 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now