-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinearscale.cpp
More file actions
111 lines (95 loc) · 2.51 KB
/
linearscale.cpp
File metadata and controls
111 lines (95 loc) · 2.51 KB
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
#include "linearscale.hpp"
#include "scalemap.hpp"
#include <QtDeclarative/QDeclarativeContext>
LinearScale::LinearScale(QQuickItem* parent)
: QQuickItem(parent)
, m_scaleMap(0)
, m_orientation(Qt::Horizontal)
, m_delegate(0)
{
m_timer.setSingleShot(true);
m_timer.setInterval(100);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateTicks()));
}
ScaleMap* LinearScale::scaleMap() const
{
return m_scaleMap;
}
void LinearScale::setScaleMap(ScaleMap* scaleMap)
{
if (m_scaleMap != scaleMap)
{
m_scaleMap = scaleMap;
emit scaleMapChanged(scaleMap);
}
}
Qt::Orientation LinearScale::orientation() const
{
return m_orientation;
}
void LinearScale::setOrientation(Qt::Orientation orientation)
{
if (m_orientation != orientation)
{
m_orientation = orientation;
emit orientationChanged(orientation);
}
}
QDeclarativeComponent* LinearScale::delegate() const
{
return m_delegate;
}
void LinearScale::setDelegate(QDeclarativeComponent *delegate)
{
if (m_delegate != delegate)
{
m_delegate = delegate;
emit delegateChanged(delegate);
}
}
void LinearScale::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
QQuickItem::geometryChanged(newGeometry, oldGeometry);
if (m_scaleMap)
{
if (m_orientation == Qt::Horizontal)
m_scaleMap->setPixelLength(newGeometry.width());
else
m_scaleMap->setPixelLength(newGeometry.height());
}
if (!m_timer.isActive())
m_timer.start();
}
void LinearScale::updateTicks()
{
qDeleteAll(m_ticks);
m_ticks.clear();
if (!m_delegate || !m_scaleMap)
return;
QList<double> ticks;
ticks << 1 << 10 << 100 << 1000 << 10000;
foreach (double tick, ticks)
{
double pixelTick = m_scaleMap->mapToPixel(tick);
if (m_orientation == Qt::Vertical)
pixelTick = height() - pixelTick;
QDeclarativeContext* context = new QDeclarativeContext(qmlContext(this));
context->setContextProperty("tickValue", tick);
context->setContextProperty("tickPosition", pixelTick);
QObject* o = m_delegate->create(context);
context->setParent(o);
QQuickItem* item = qobject_cast<QQuickItem*>(o);
if (item)
{
item->setParent(this);
item->setParentItem(this);
m_ticks << item;
}
else
{
delete o;
}
}
update();
}