-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkStreamUI.cpp
More file actions
310 lines (264 loc) · 7.94 KB
/
NetworkStreamUI.cpp
File metadata and controls
310 lines (264 loc) · 7.94 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
#include "NetworkStreamUI.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QSpacerItem>
#include <QApplication>
#include <QStyle>
#include <QKeyEvent>
#include <QFocusEvent>
#include <QShowEvent>
#include <QMouseEvent>
#include <QDebug>
NetworkStreamUI::NetworkStreamUI(QWidget *parent)
: QDialog(parent)
, m_urlEdit(new QLineEdit(this))
, m_autoCloseTimer(new QTimer(this))
{
setWindowTitle("网络流");
setFixedSize(500, 50);
setModal(false); // 改为非模态,允许点击外部区域
// 设置为子窗口,跟随父窗口层级,不独立置顶
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
// 确保跟随父窗口
if (parent) {
setParent(parent);
}
setupUI();
setupConnections();
validateInput();
// 设置自动关闭定时器
m_autoCloseTimer->setSingleShot(true);
connect(m_autoCloseTimer, &QTimer::timeout, this, &NetworkStreamUI::onAutoCloseTimeout);
// 安装全局事件过滤器来检测外部点击
qApp->installEventFilter(this);
}
NetworkStreamUI::~NetworkStreamUI()
{
// 移除全局事件过滤器
qApp->removeEventFilter(this);
}
void NetworkStreamUI::setupUI()
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0, 0, 0, 0);
// URL输入框
m_urlEdit->setPlaceholderText("输入流媒体地址");
m_urlEdit->setStyleSheet(
"QLineEdit { "
"border: 1px solid #4a5568; "
"border-radius: 6px; "
"padding: 12px 16px; "
"font-size: 14px; "
"font-family: 'Segoe UI', sans-serif; "
"background: #2d3748; "
"color: #e2e8f0; "
"selection-background-color: #4299e1; "
"} "
"QLineEdit:focus { "
"border: 1px solid #4299e1; "
"background: #2d3748; "
"outline: none; "
"} "
"QLineEdit:hover { "
"border: 1px solid #718096; "
"}"
);
mainLayout->addWidget(m_urlEdit);
// 设置输入框为焦点
m_urlEdit->setFocus();
// 整体窗口样式
setStyleSheet(
"QDialog { "
"background: #2d3748; "
"border: 1px solid #4a5568; "
"border-radius: 6px; "
"}"
);
}
void NetworkStreamUI::setupConnections()
{
// 回车键触发连接
connect(m_urlEdit, &QLineEdit::returnPressed, this, &NetworkStreamUI::onConnectClicked);
connect(m_urlEdit, &QLineEdit::textChanged, this, &NetworkStreamUI::validateInput);
}
void NetworkStreamUI::setUrl(const QString &url)
{
m_urlEdit->setText(url);
validateInput();
}
QString NetworkStreamUI::getUrl() const
{
return m_urlEdit->text().trimmed();
}
NetworkStreamUI::StreamSettings NetworkStreamUI::getSettings() const
{
StreamSettings settings;
settings.url = getUrl();
settings.timeout = 30;
settings.bufferSize = 10;
settings.autoReconnect = true;
settings.maxRetries = 5;
return settings;
}
void NetworkStreamUI::setStatus(const QString &status)
{
// 状态显示功能保留但不显示UI
}
void NetworkStreamUI::setProgress(int value)
{
// 进度显示功能保留但不显示UI
}
void NetworkStreamUI::setConnecting(bool connecting)
{
m_urlEdit->setEnabled(!connecting);
if (connecting) {
m_urlEdit->setPlaceholderText("正在连接...");
} else {
m_urlEdit->setPlaceholderText("输入流媒体地址");
}
}
void NetworkStreamUI::onConnectClicked()
{
if (validateInput()) {
// 停止自动关闭定时器
stopAutoCloseTimer();
// 立即关闭对话框
accept();
// 发送连接请求信号
emit connectRequested(getSettings());
}
}
bool NetworkStreamUI::validateInput()
{
QString url = m_urlEdit->text().trimmed();
bool valid = !url.isEmpty() && (
url.startsWith("rtmp://") ||
url.startsWith("rtsp://") ||
url.startsWith("http://") ||
url.startsWith("https://") ||
url.startsWith("udp://") ||
url.startsWith("tcp://") ||
url.contains("://")
);
if (!valid && !url.isEmpty()) {
m_urlEdit->setStyleSheet(
"QLineEdit { "
"border: 1px solid #f56565; "
"border-radius: 6px; "
"padding: 12px 16px; "
"font-size: 14px; "
"font-family: 'Segoe UI', sans-serif; "
"background: #2d3748; "
"color: #e2e8f0; "
"selection-background-color: #4299e1; "
"} "
"QLineEdit:focus { "
"border: 1px solid #f56565; "
"background: #2d3748; "
"outline: none; "
"} "
"QLineEdit:hover { "
"border: 1px solid #f56565; "
"}"
);
} else {
m_urlEdit->setStyleSheet(
"QLineEdit { "
"border: 1px solid #4a5568; "
"border-radius: 6px; "
"padding: 12px 16px; "
"font-size: 14px; "
"font-family: 'Segoe UI', sans-serif; "
"background: #2d3748; "
"color: #e2e8f0; "
"selection-background-color: #4299e1; "
"} "
"QLineEdit:focus { "
"border: 1px solid #4299e1; "
"background: #2d3748; "
"outline: none; "
"} "
"QLineEdit:hover { "
"border: 1px solid #718096; "
"}"
);
}
return valid;
}
void NetworkStreamUI::startAutoCloseTimer(int timeoutMs)
{
m_autoCloseTimer->start(timeoutMs);
// Auto-close timer started
}
void NetworkStreamUI::stopAutoCloseTimer()
{
if (m_autoCloseTimer->isActive()) {
m_autoCloseTimer->stop();
// Auto-close timer stopped
}
}
void NetworkStreamUI::onAutoCloseTimeout()
{
qDebug() << "Auto-close timeout - closing dialog";
reject(); // 自动关闭,使用reject表示超时
}
void NetworkStreamUI::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
qDebug() << "ESC pressed - closing dialog";
stopAutoCloseTimer();
reject();
return;
}
// 对于其他按键,重置自动关闭定时器
if (m_autoCloseTimer->isActive()) {
startAutoCloseTimer(); // 重新开始计时
}
QDialog::keyPressEvent(event);
}
void NetworkStreamUI::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event)
// 使用Qt::Popup窗口类型时,失去焦点会自动关闭
// 这里不需要额外处理,让Qt自动处理
QDialog::focusOutEvent(event);
}
void NetworkStreamUI::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
// 居中显示在父窗口中
if (parentWidget()) {
QRect parentRect = parentWidget()->geometry();
QPoint center = parentRect.center();
move(center.x() - width() / 2, center.y() - height() / 2);
}
// 对话框显示时启动自动关闭定时器
startAutoCloseTimer();
// 确保输入框获得焦点
m_urlEdit->setFocus();
m_urlEdit->selectAll(); // 选中所有文本,方便覆盖输入
}
bool NetworkStreamUI::eventFilter(QObject *obj, QEvent *event)
{
// 只在对话框可见时处理事件
if (!isVisible()) {
return QDialog::eventFilter(obj, event);
}
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
// 检查点击是否在对话框外部
QPoint globalPos = mouseEvent->globalPosition().toPoint();
QRect dialogRect = QRect(mapToGlobal(QPoint(0, 0)), size());
if (!dialogRect.contains(globalPos)) {
qDebug() << "Mouse click outside dialog - closing";
stopAutoCloseTimer();
reject();
return true; // 事件已处理
}
}
return QDialog::eventFilter(obj, event);
}