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