-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxaxis_ticks.cpp
136 lines (98 loc) · 3.82 KB
/
xaxis_ticks.cpp
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
/*
Copyright 2022 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <appl/xaxis_ticks.hpp>
#include <QtGui/QPainter>
#include <QtGui/QFontMetrics>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QMouseEvent>
#include <QtCharts/QChart>
#include <iostream>
XaxisTicks::XaxisTicks(QChart *chart):
QGraphicsItem(chart),
m_chart(chart)
{
for (int n = 0; n < max_number_of_labels; n++){
m_labels.push_back(std::make_unique<QGraphicsSimpleTextItem>(m_chart));
m_labels.back()->setVisible(false);
}
}
QRectF XaxisTicks::boundingRect() const
{
QRectF rect = m_chart->rect();
return rect;
}
void XaxisTicks::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect1 = m_chart->rect();
QRectF rect2 = m_chart->plotArea();
auto bot_chart = rect1.y() + rect1.height();
auto bot_plot = rect2.y() + rect2.height();
auto lbl_ypos = bot_plot + (bot_chart - bot_plot)*0.05;
for (int n = 0; n < max_number_of_labels; n++)
m_labels[n]->setVisible(false);
if (m_xaxis_ticks.size() > 0) {
for (int n = 0; n < m_xaxis_ticks.size(); n++) {
QPainterPath path3;
qreal xpos = rect2.x() + rect2.width()*static_cast<qreal>(std::get<1>(m_xaxis_ticks[n]));
path3.moveTo(xpos, rect2.y());
path3.lineTo(xpos, rect2.y() + rect2.height());
painter->setPen(QPen(Qt::gray, 0.5, Qt::SolidLine));
painter->drawPath(path3);
QString lbl = QString::fromStdString(std::get<0>(m_xaxis_ticks[n]));
m_labels[n]->setText(lbl);
QFont font1("serifed style"); // default on Linux
font1.setPointSize(8);
//font1.setFamily("Arial"); // default on Windows
//font1.setFamily("serifed style"); // default on Linux
int left_shift;
if (lbl.length() == 4)
left_shift = 10;
else if (lbl.length() == 8)
left_shift = 20;
else if (lbl.length() == 10)
left_shift = 25;
else if (lbl.length() == 11)
left_shift = 28;
else if (lbl.length() == 17)
left_shift = 43;
else if (lbl.length() == 20)
left_shift = 50;
else if (lbl.length() == 24)
left_shift = 60;
else
left_shift = 0;
m_labels[n]->setFont(font1);
m_labels[n]->setPos(xpos - left_shift, lbl_ypos );
m_labels[n]->setVisible(true);
}
}
}
void XaxisTicks::prepare_update()
{
prepareGeometryChange();
}
void XaxisTicks::set_xaxis_ticks(const std::vector<std::tuple<std::string, double>>& xaxis_ticks)
{
if (xaxis_ticks.size() > max_number_of_labels)
throw std::runtime_error("number of xaxis ticks " + std::to_string(xaxis_ticks.size()) + " are larger than maximum " +
std::to_string(max_number_of_labels) );
m_xaxis_ticks = xaxis_ticks;
}
void XaxisTicks::set_visible(bool value)
{
this->setVisible(value);
if (!value)
for (int n = 0; n < max_number_of_labels; n++)
m_labels[n]->setVisible(false);
}