EllipsisPushButton.cpp
Go to the documentation of this file.
2
3EllipsisPushButton::EllipsisPushButton(QWidget* parent) : QPushButton(parent)
4{
5}
6
7EllipsisPushButton::EllipsisPushButton(const QString& text, QWidget* parent) :
8 QPushButton(text, parent)
9{
10}
11
12void
14{
15 if (timeoutSec <= 0.0)
16 return;
17
18 this->timeoutSec = timeoutSec;
19 elapsedSec = 0.0;
20 progressActive = true;
21
22 setToolTip(QString("Timeout in %1 sec").arg(timeoutSec, 0, 'f', 1));
23
24 if (!progressTimer.isActive())
25 {
26 connect(&progressTimer,
27 &QTimer::timeout,
28 this,
29 [this]()
30 {
31 elapsedSec += 0.05;
32 double remaining = std::max(0.0, this->timeoutSec - elapsedSec);
33 setToolTip(QString("Timeout in %1 sec").arg(remaining, 0, 'f', 1));
34 update();
35 });
36 }
37
38 progressTimer.start(50); // 20 FPS
39}
40
41void
43{
44 if (!progressActive)
45 return;
46
47 elapsedSec = timeoutSec;
48 progressActive = false;
49 progressTimer.stop();
50 setToolTip(QString("Execute"));
51
52 if (resetColorTimer.isActive())
53 resetColorTimer.stop();
54 QColor color = failed ? QColor(Qt::red) : QColor(Qt::green);
55 this->originalStyle = styleSheet();
56 setStyleSheet(QString("background-color: %1").arg(color.name()));
57
58 resetColorTimer.setSingleShot(true);
59 connect(&resetColorTimer, &QTimer::timeout, this, [this]() { setStyleSheet(originalStyle); });
60 resetColorTimer.start(1000);
61
62 update();
63}
64
65void
67{
68
69 if (resetColorTimer.isActive())
70 {
71 resetColorTimer.stop();
72 setStyleSheet(originalStyle);
73 }
74
75 QPushButton::mousePressEvent(event);
76}
77
78void
80{
81 QString fullText = text();
82 QFontMetrics metrics(font());
83 int textWidth = width() - 6;
84 QString elided = metrics.elidedText(fullText, Qt::ElideRight, textWidth);
85
86 setText(elided);
87
88 QPushButton::paintEvent(event);
89
90 setText(fullText);
91
92 // ==== Progress-Bar ====
93 if (!progressActive || timeoutSec <= 0.0)
94 return;
95
96 QPainter p(this);
97 p.setRenderHint(QPainter::Antialiasing);
98
99 double percent = std::min(1.0, elapsedSec / timeoutSec);
100
101 int barHeight = height() * 0.25;
102 int barWidth = static_cast<int>(width() * percent);
103
104 QRect barRect(0, height() - barHeight, barWidth, barHeight);
105
106 QLinearGradient grad(barRect.topLeft(), barRect.topRight());
107 grad.setColorAt(0.0, QColor(180, 40, 40));
108 grad.setColorAt(1.0, QColor(255, 80, 80));
109
110 p.fillRect(barRect, grad);
111}
void mousePressEvent(QMouseEvent *event) override
void startTimeout(double timeoutSec)
EllipsisPushButton(QWidget *parent=nullptr)
void finishProgress(bool failed)
void paintEvent(QPaintEvent *event) override