-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqcursorview.cpp
More file actions
48 lines (40 loc) · 1.51 KB
/
qcursorview.cpp
File metadata and controls
48 lines (40 loc) · 1.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
#include "qcursorview.h"
QCursorView::QCursorView(QWidget* parent = 0) : QtCharts::QChartView(parent) {}
void QCursorView::tooltip(QPointF point, bool state) {
if(m_tooltip == nullptr)
m_tooltip = new Callout(chart());
if(state) {
QLineSeries* hovered = (QLineSeries*)sender();
QVector<QPointF> points = hovered->pointsVector();
int nearestIndex = 0;
double nearestDist = 1e9;
for(int i=0; i<points.size(); i++) {
double dist = sqrt(pow(points[i].x()-point.x(),2)+pow(points[i].y()-point.y(),2));
if(dist < nearestDist) {
nearestDist = dist;
nearestIndex = i;
}
}
int y = points[nearestIndex].y();
QString nb;
if(rps=="Hexa")
nb = QString("h").append(QString::number(y,16));
else if(rps=="Binaire")
nb = QString("b").append(QString::number(y,2));
else // "Décimal"
nb = QString("d").append(QString::number(y,10));
m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(nb));
m_tooltip->setAnchor(point);
m_tooltip->setZValue(11);
m_tooltip->updateGeometry();
m_tooltip->show();
} else
m_tooltip->hide();
}
void QCursorView::addSeries(QtCharts::QLineSeries *series) {
connect(series, SIGNAL(hovered(QPointF,bool)), this, SLOT(tooltip(QPointF,bool)));
chart()->addSeries(series);
}
void QCursorView::changeNumRepresentation(QString rps) {
this->rps = rps;
}