Repository navigation
Expand file tree
/
Copy pathconnectionstatusoverlay.cpp
More file actions
339 lines (303 loc) · 9.44 KB
/
Copy pathconnectionstatusoverlay.cpp
File metadata and controls
339 lines (303 loc) · 9.44 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "connectionstatusoverlay.h"
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QPlainTextEdit>
#include <QProgressBar>
#include <QPushButton>
#include <QStyle>
#include <QVBoxLayout>
namespace {
const char *const kOverlayStyle = R"(
QFrame#connectionStatusCard {
background-color: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 14px;
}
QLabel#connectionStatusIcon {
background-color: transparent;
border: none;
}
QLabel#connectionStatusTitle {
color: #0f172a;
font-size: 19px;
font-weight: bold;
background-color: transparent;
border: none;
}
QLabel#connectionStatusTarget {
color: #64748b;
font-size: 13px;
background-color: transparent;
border: none;
}
QLabel#connectionStatusReason {
color: #1e293b;
font-size: 14px;
background-color: transparent;
border: none;
}
QLabel#connectionStatusHint {
color: #64748b;
font-size: 13px;
background-color: transparent;
border: none;
}
QPlainTextEdit#connectionStatusDetail {
background-color: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
color: #475569;
font-family: "DejaVu Sans Mono", "Consolas", "Menlo", monospace;
font-size: 12px;
padding: 6px;
}
QProgressBar#connectionStatusProgress {
background-color: #e2e8f0;
border: none;
border-radius: 2px;
}
QProgressBar#connectionStatusProgress::chunk {
background-color: #2563eb;
border-radius: 2px;
}
QPushButton#connectionStatusPrimary {
background-color: #2563eb;
color: #ffffff;
border: none;
border-radius: 6px;
padding: 8px 22px;
font-size: 14px;
font-weight: bold;
}
QPushButton#connectionStatusPrimary:hover {
background-color: #1d4ed8;
}
QPushButton#connectionStatusPrimary:pressed {
background-color: #1e40af;
}
QPushButton#connectionStatusSecondary {
background-color: #ffffff;
color: #334155;
border: 1px solid #cbd5e1;
border-radius: 6px;
padding: 8px 22px;
font-size: 14px;
}
QPushButton#connectionStatusSecondary:hover {
background-color: #f1f5f9;
}
QPushButton#connectionStatusSecondary:pressed {
background-color: #e2e8f0;
}
)";
} // namespace
ConnectionStatusOverlay::ConnectionStatusOverlay(QWidget *parent)
: QWidget(parent) {
setObjectName(QStringLiteral("connectionStatusOverlay"));
setFocusPolicy(Qt::StrongFocus);
buildUi();
setStyleSheet(QString::fromLatin1(kOverlayStyle));
hide();
}
void ConnectionStatusOverlay::buildUi() {
QVBoxLayout *outer = new QVBoxLayout(this);
outer->setContentsMargins(24, 24, 24, 24);
outer->addStretch(1);
QHBoxLayout *row = new QHBoxLayout;
row->setContentsMargins(0, 0, 0, 0);
row->addStretch(1);
card_ = new QFrame(this);
card_->setObjectName(QStringLiteral("connectionStatusCard"));
card_->setMinimumWidth(360);
card_->setMaximumWidth(560);
row->addWidget(card_, 0, Qt::AlignCenter);
row->addStretch(1);
outer->addLayout(row);
outer->addStretch(1);
QVBoxLayout *content = new QVBoxLayout(card_);
content->setContentsMargins(30, 26, 30, 24);
content->setSpacing(8);
icon_ = new QLabel(card_);
icon_->setObjectName(QStringLiteral("connectionStatusIcon"));
icon_->setAlignment(Qt::AlignCenter);
icon_->setFixedHeight(48);
content->addWidget(icon_);
title_ = new QLabel(card_);
title_->setObjectName(QStringLiteral("connectionStatusTitle"));
title_->setAlignment(Qt::AlignCenter);
title_->setWordWrap(true);
content->addWidget(title_);
target_ = new QLabel(card_);
target_->setObjectName(QStringLiteral("connectionStatusTarget"));
target_->setAlignment(Qt::AlignCenter);
target_->setTextInteractionFlags(Qt::TextSelectableByMouse);
content->addWidget(target_);
content->addSpacing(2);
reason_ = new QLabel(card_);
reason_->setObjectName(QStringLiteral("connectionStatusReason"));
reason_->setAlignment(Qt::AlignCenter);
reason_->setWordWrap(true);
content->addWidget(reason_);
hint_ = new QLabel(card_);
hint_->setObjectName(QStringLiteral("connectionStatusHint"));
hint_->setAlignment(Qt::AlignCenter);
hint_->setWordWrap(true);
content->addWidget(hint_);
detail_ = new QPlainTextEdit(card_);
detail_->setObjectName(QStringLiteral("connectionStatusDetail"));
detail_->setReadOnly(true);
detail_->setMinimumHeight(58);
detail_->setMaximumHeight(92);
detail_->setLineWrapMode(QPlainTextEdit::WidgetWidth);
detail_->setFocusPolicy(Qt::ClickFocus);
content->addWidget(detail_);
progress_ = new QProgressBar(card_);
progress_->setObjectName(QStringLiteral("connectionStatusProgress"));
progress_->setRange(0, 0);
progress_->setTextVisible(false);
progress_->setFixedHeight(4);
content->addWidget(progress_);
content->addSpacing(8);
QHBoxLayout *buttons = new QHBoxLayout;
buttons->setSpacing(12);
buttons->addStretch(1);
primary_ = new QPushButton(card_);
primary_->setObjectName(QStringLiteral("connectionStatusPrimary"));
primary_->setCursor(Qt::PointingHandCursor);
primary_->setMinimumHeight(36);
primary_->setMinimumWidth(112);
secondary_ = new QPushButton(card_);
secondary_->setObjectName(QStringLiteral("connectionStatusSecondary"));
secondary_->setCursor(Qt::PointingHandCursor);
secondary_->setMinimumHeight(36);
secondary_->setMinimumWidth(112);
buttons->addWidget(primary_);
buttons->addWidget(secondary_);
buttons->addStretch(1);
content->addLayout(buttons);
connect(primary_, &QPushButton::clicked, this, [this]() {
if (state_ == State::Connecting) {
emit closeRequested();
} else {
emit retryRequested();
}
});
connect(secondary_, &QPushButton::clicked, this,
[this]() { emit closeRequested(); });
}
void ConnectionStatusOverlay::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.fillRect(rect(), QColor(9, 14, 24, 170));
}
void ConnectionStatusOverlay::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
updateCardWidth();
}
void ConnectionStatusOverlay::updateCardWidth() {
if (!card_) {
return;
}
const int available = width() - 48;
card_->setFixedWidth(qBound(340, available, 560));
}
void ConnectionStatusOverlay::present() {
updateCardWidth();
show();
raise();
if (state_ == State::Failed) {
primary_->setFocus(Qt::OtherFocusReason);
}
}
void ConnectionStatusOverlay::applyState(State state) {
state_ = state;
detail_->hide();
hint_->hide();
target_->show();
progress_->hide();
secondary_->show();
switch (state_) {
case State::Connecting:
icon_->hide();
progress_->show();
primary_->setText(QStringLiteral("取消连接"));
secondary_->hide();
break;
case State::Failed:
icon_->setPixmap(style()
->standardIcon(QStyle::SP_MessageBoxWarning)
.pixmap(44, 44));
icon_->show();
primary_->setText(QStringLiteral("重试"));
secondary_->setText(closeButtonText_.isEmpty()
? QStringLiteral("关闭标签页")
: closeButtonText_);
break;
case State::Disconnected:
icon_->setPixmap(style()
->standardIcon(QStyle::SP_MessageBoxInformation)
.pixmap(44, 44));
icon_->show();
primary_->setText(QStringLiteral("重新连接"));
secondary_->setText(closeButtonText_.isEmpty()
? QStringLiteral("关闭标签页")
: closeButtonText_);
break;
}
}
void ConnectionStatusOverlay::showConnecting(const QString &target,
const QString &hint) {
applyState(State::Connecting);
title_->setText(QStringLiteral("正在连接"));
target_->setText(target);
reason_->setText(hint.isEmpty() ? QStringLiteral("正在建立连接,请稍候…")
: hint);
hint_->clear();
setDetail(QString());
present();
}
void ConnectionStatusOverlay::showFailure(const QString &target,
const QString &reason,
const QString &hint,
const QString &detail) {
applyState(State::Failed);
title_->setText(QStringLiteral("无法连接到服务器"));
target_->setText(target);
reason_->setText(reason.isEmpty() ? QStringLiteral("连接失败")
: reason);
hint_->setText(hint);
hint_->setVisible(!hint.isEmpty());
setDetail(detail);
present();
}
void ConnectionStatusOverlay::showDisconnected(const QString &target,
const QString &reason) {
applyState(State::Disconnected);
title_->setText(QStringLiteral("连接已断开"));
target_->setText(target);
reason_->setText(reason.isEmpty()
? QStringLiteral("远程主机已结束当前 SSH 会话。")
: reason);
hint_->setText(QStringLiteral("会话中的命令可能已经终止。"));
hint_->show();
setDetail(QString());
present();
}
void ConnectionStatusOverlay::setDetail(const QString &detail) {
const QString trimmed = detail.trimmed();
detail_->setPlainText(trimmed);
detail_->setVisible(!trimmed.isEmpty());
}
void ConnectionStatusOverlay::hideOverlay() {
hide();
detail_->clear();
}
void ConnectionStatusOverlay::setCloseButtonText(const QString &text) {
closeButtonText_ = text;
if (secondary_ && state_ != State::Connecting) {
secondary_->setText(closeButtonText_.isEmpty()
? QStringLiteral("关闭标签页")
: closeButtonText_);
}
}