Line data Source code
1 : #include "lane.h"
2 :
3 8 : Lane::Lane(QWidget *parent) : QWidget(parent), lane(1)
4 : {
5 8 : if (parent)
6 : {
7 8 : setMinimumSize(parent->width() * 0.5, parent->height() * 0.5);
8 : }
9 8 : QString path = QCoreApplication::applicationDirPath();
10 8 : QString digital_path = QDir(path).filePath("../fonts_icon/sports.png");
11 8 : digital_path = QDir::cleanPath(digital_path);
12 8 : pixmap = QPixmap(digital_path);
13 8 : pixmap = pixmap.scaled(this->width() * 0.4 , height() * 0.5, Qt::KeepAspectRatio, Qt::SmoothTransformation);
14 8 : animationTimer = new QTimer(this);
15 8 : connect(animationTimer, &QTimer::timeout, this, [this]() {
16 42 : m_leftDashOffset = int(m_leftDashOffset + 1) % 30;
17 42 : m_rightDashOffset = int(m_rightDashOffset + 1) % 30;
18 42 : update();
19 42 : });
20 8 : animationTimer->start(80);
21 8 : setters();
22 8 : set_lane(82);
23 8 : }
24 :
25 8 : void Lane::setters()
26 : {
27 8 : leftOpacityAnimation = new QPropertyAnimation(this, "leftOpacity");
28 8 : rightOpacityAnimation = new QPropertyAnimation(this, "rightOpacity");
29 :
30 8 : laneGradient = QLinearGradient(0, height() * 0.9, 0, 0);
31 8 : laneGradient.setColorAt(0.0, QColor(20, 20, 20, 20));
32 8 : laneGradient.setColorAt(0.4, QColor(150, 150, 150, 150));
33 8 : laneGradient.setColorAt(0.5, QColor(150, 150, 150, 150));
34 8 : laneGradient.setColorAt(1.0, QColor(20, 20, 20, 1));
35 :
36 8 : redGradient = QLinearGradient(0, height() * 0.9, 0, 0);
37 8 : redGradient.setColorAt(0.0, QColor(50, 5, 0, 50));
38 8 : redGradient.setColorAt(0.4, QColor(200, 30, 0, 150));
39 8 : redGradient.setColorAt(0.6, QColor(200, 30, 0, 150));
40 8 : redGradient.setColorAt(1.0, QColor(50, 5, 0, 50));
41 :
42 8 : leftGrayPen = QPen(QBrush(laneGradient), 5, Qt::DashLine);
43 8 : leftRedPen = QPen(QBrush(redGradient), 5, Qt::DashLine);
44 8 : rightGrayPen = QPen(QBrush(laneGradient), 5, Qt::DashLine);
45 8 : rightRedPen = QPen(QBrush(redGradient), 5, Qt::DashLine);
46 8 : }
47 :
48 1 : void Lane::showNoLanePopup()
49 : {
50 1 : QString path = QCoreApplication::applicationDirPath();
51 1 : QString digital_path = QDir(path).filePath("../fonts_icon/alert.png");
52 1 : digital_path = QDir::cleanPath(digital_path);
53 1 : if (!popup)
54 : {
55 1 : popup = new QWidget(this);
56 1 : popup->setAttribute(Qt::WA_DeleteOnClose);
57 1 : popup->setStyleSheet("background-color: transparent;");
58 1 : QHBoxLayout *layout = new QHBoxLayout(popup);
59 1 : QLabel *iconLabel = new QLabel(popup);
60 1 : QPixmap warningIcon(digital_path);
61 1 : iconLabel->setPixmap(warningIcon.scaled(30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation));
62 :
63 1 : QLabel *textLabel = new QLabel(" Take manual control", popup);
64 1 : textLabel->setStyleSheet("color: gray; font-size: 20px;");
65 1 : layout->addWidget(iconLabel, 0, Qt::AlignTop | Qt::AlignCenter);
66 1 : layout->addWidget(textLabel, 0, Qt::AlignCenter);
67 1 : popup->setLayout(layout);
68 1 : popup->adjustSize();
69 1 : popup->move((width() - popup->width()) / 2, height() - popup->height());
70 :
71 1 : opacityEffect = new QGraphicsOpacityEffect(popup);
72 1 : opacityEffect->setOpacity(0.0);
73 1 : popup->setGraphicsEffect(opacityEffect);
74 1 : popup->show();
75 :
76 1 : QPropertyAnimation *fadeIn = new QPropertyAnimation(opacityEffect, "opacity");
77 1 : fadeIn->setDuration(300);
78 1 : fadeIn->setStartValue(0.0);
79 1 : fadeIn->setEndValue(1.0);
80 1 : fadeIn->setEasingCurve(QEasingCurve::InOutSine);
81 1 : fadeIn->start(QAbstractAnimation::DeleteWhenStopped);
82 1 : }
83 1 : }
84 :
85 179 : void Lane::paintEvent(QPaintEvent *event)
86 : {
87 179 : QPainter painter(this);
88 179 : painter.setRenderHint(QPainter::Antialiasing);
89 :
90 179 : QRect rect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, pixmap.size(), this->rect());
91 179 : painter.setRenderHint(QPainter::SmoothPixmapTransform);
92 179 : painter.drawPixmap(rect, pixmap);
93 :
94 179 : int laneBottomLeft = width() * 0.2;
95 179 : int laneBottomRight = width() * 0.8;
96 179 : int laneTopLeft = width() * 0.4;
97 179 : int laneTopRight = width() * 0.6;
98 :
99 179 : leftGrayPen.setDashOffset(m_leftDashOffset);
100 179 : painter.setPen(leftGrayPen);
101 179 : painter.setOpacity(1.0 - m_leftOpacity);
102 179 : painter.drawLine(QPoint(laneBottomLeft, height()), QPoint(laneTopLeft, height() * 0.1));
103 :
104 179 : leftRedPen.setDashOffset(m_leftDashOffset);
105 179 : painter.setPen(leftRedPen);
106 179 : painter.setOpacity(m_leftOpacity);
107 179 : painter.drawLine(QPoint(laneBottomLeft, height()), QPoint(laneTopLeft, height() * 0.1));
108 :
109 179 : rightGrayPen.setDashOffset(m_rightDashOffset);
110 179 : painter.setPen(rightGrayPen);
111 179 : painter.setOpacity(1.0 - m_rightOpacity);
112 179 : painter.drawLine(QPoint(laneBottomRight, height()), QPoint(laneTopRight, height() * 0.1));
113 :
114 179 : rightRedPen.setDashOffset(m_rightDashOffset);
115 179 : painter.setPen(rightRedPen);
116 179 : painter.setOpacity(m_rightOpacity);
117 179 : painter.drawLine(QPoint(laneBottomRight, height()), QPoint(laneTopRight, height() * 0.1));
118 179 : }
119 :
120 15 : void Lane::set_lane(int i)
121 : {
122 15 : if (lane && lane == i)
123 1 : return;
124 14 : lane = i;
125 14 : leftOpacityAnimation->stop();
126 14 : rightOpacityAnimation->stop();
127 14 : qreal targetLeftOpacity = 0.0;
128 14 : qreal targetRightOpacity = 0.0;
129 14 : if (i == 76)
130 1 : targetLeftOpacity = 1.0;
131 13 : else if (i == 82)
132 9 : targetRightOpacity = 1.0;
133 :
134 14 : if (m_leftOpacity != targetLeftOpacity)
135 : {
136 12 : leftOpacityAnimation->setDuration(400);
137 12 : leftOpacityAnimation->setStartValue(m_leftOpacity);
138 12 : leftOpacityAnimation->setEndValue(targetLeftOpacity);
139 12 : leftOpacityAnimation->setEasingCurve(QEasingCurve::InOutSine);
140 12 : leftOpacityAnimation->start();
141 : }
142 14 : if (m_rightOpacity != targetRightOpacity)
143 : {
144 4 : rightOpacityAnimation->setDuration(400);
145 4 : rightOpacityAnimation->setStartValue(m_rightOpacity);
146 4 : rightOpacityAnimation->setEndValue(targetRightOpacity);
147 4 : rightOpacityAnimation->setEasingCurve(QEasingCurve::InOutSine);
148 4 : rightOpacityAnimation->start();
149 : }
150 14 : if (lane == 0)
151 1 : showNoLanePopup();
152 13 : else if (popup)
153 : {
154 1 : QPropertyAnimation *fadeOut = new QPropertyAnimation(opacityEffect, "opacity");
155 1 : fadeOut->setDuration(400);
156 1 : fadeOut->setStartValue(opacityEffect->opacity());
157 1 : fadeOut->setEndValue(0.0);
158 1 : fadeOut->setEasingCurve(QEasingCurve::InOutSine);
159 1 : connect(fadeOut, &QPropertyAnimation::finished, this, [this]() {
160 1 : if (popup)
161 : {
162 1 : popup->close();
163 1 : popup->deleteLater();
164 1 : popup = nullptr;
165 : }
166 1 : });
167 1 : fadeOut->start(QAbstractAnimation::DeleteWhenStopped);
168 : }
169 14 : update();
170 : }
171 :
172 38 : qreal Lane::leftOpacity() const
173 : {
174 38 : return m_leftOpacity;
175 : }
176 :
177 153 : void Lane::setLeftOpacity(qreal opacity)
178 : {
179 153 : m_leftOpacity = opacity;
180 153 : update();
181 153 : emit leftOpacityChanged();
182 153 : }
183 :
184 22 : qreal Lane::rightOpacity() const
185 : {
186 22 : return m_rightOpacity;
187 : }
188 :
189 1 : qreal Lane::leftDashOffset() const
190 : {
191 1 : return m_leftDashOffset;
192 : }
193 :
194 1 : qreal Lane::rightDashOffset() const
195 : {
196 1 : return m_rightDashOffset;
197 : }
198 :
199 108 : void Lane::setRightOpacity(qreal opacity)
200 : {
201 108 : m_rightOpacity = opacity;
202 108 : update();
203 108 : emit rightOpacityChanged();
204 108 : }
205 :
206 0 : QGraphicsOpacityEffect *Lane::get_opacityEffect()
207 : {
208 0 : return opacityEffect;
209 : }
210 :
211 2 : int Lane::get_lane()
212 : {
213 2 : return lane;
214 : }
215 :
216 1 : QWidget *Lane::get_popup()
217 : {
218 1 : return popup;
219 : }
220 :
221 16 : Lane::~Lane()
222 : {
223 8 : if (leftOpacityAnimation) {
224 8 : leftOpacityAnimation->stop();
225 8 : delete leftOpacityAnimation;
226 8 : leftOpacityAnimation = nullptr;
227 : }
228 8 : if (rightOpacityAnimation) {
229 8 : rightOpacityAnimation->stop();
230 8 : delete rightOpacityAnimation;
231 8 : rightOpacityAnimation = nullptr;
232 : }
233 8 : if (animationTimer)
234 : {
235 8 : animationTimer->stop();
236 : }
237 8 : if (popup)
238 : {
239 0 : popup->close();
240 0 : popup->deleteLater();
241 0 : popup = nullptr;
242 : }
243 8 : std::cout << "Remove Lane" << std::endl;
244 16 : }
|