MainPanel.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #include "MainPanel.h"
  2. #include "utils/iconutils.h"
  3. // Qt Core
  4. #include <QDebug>
  5. #include <QJsonDocument>
  6. #include <QJsonObject>
  7. #include <QJsonParseError>
  8. #include <QResizeEvent>
  9. #include <QSizePolicy>
  10. #include <QTimer>
  11. #include <QtConcurrent>
  12. // Qt Widgets
  13. #include <QComboBox>
  14. #include <QFrame>
  15. #include <QHBoxLayout>
  16. #include <QLabel>
  17. #include <QSplitter>
  18. #include <QVBoxLayout>
  19. // Third Party
  20. #include <qtpromise/qpromise.h>
  21. #include <qtpromise/qpromisefuture.h>
  22. #include <qtpromise/qpromisehelpers.h>
  23. // Project Includes
  24. #include "AVPlayer/avplayerwidget.h"
  25. #include "api/roomapi.h"
  26. #include "appevent.h"
  27. #include "network/websocketclient.h"
  28. #include "themesettingswidget.h"
  29. #include "widgets/bubbletip.h"
  30. #include "widgets/chatView/chatwindow.h"
  31. #include "widgets/framelessbase.h"
  32. #include "widgets/functionbutton.h"
  33. #include "widgets/maskoverlay.h"
  34. #include "widgets/recorderwidget.h"
  35. #include "widgets/statswidget.h"
  36. #include "widgets/userprofilewidget.h"
  37. #include <util/jsonmapper.h>
  38. MainPanel::MainPanel(QWidget *parent)
  39. : TWidget(parent)
  40. , chatView(nullptr)
  41. {
  42. setAttribute(Qt::WA_StyledBackground, true);
  43. // ========== 初始化播放控制组件 ==========
  44. initPlaybackControls();
  45. // ========== 初始化聊天相关组件 ==========
  46. initChatComponents();
  47. // ========== 初始化布局组件 ==========
  48. initLayoutComponents();
  49. // ========== 初始化功能按钮 ==========
  50. initFunctionButtons();
  51. // ========== 连接信号槽 ==========
  52. connectSignals();
  53. // ========== 初始化音频设备 ==========
  54. initAudioDeviceSelectors();
  55. }
  56. void MainPanel::initPlaybackControls()
  57. {
  58. m_debounceTimer = new QTimer(this);
  59. m_debounceTimer->setInterval(500);
  60. m_debounceTimer->setSingleShot(true);
  61. connect(m_debounceTimer, &QTimer::timeout, this, &MainPanel::handleDebouncedPlay);
  62. }
  63. void MainPanel::initChatComponents()
  64. {
  65. webSocketClient = new WebSocketClient(this);
  66. // 启用WebSocket自动重连,提升聊天室稳定性
  67. if (webSocketClient) {
  68. webSocketClient->setAutoReconnect(true);
  69. }
  70. chatView = new ChatWindow(webSocketClient);
  71. chatView->setMinimumWidth(400);
  72. // 防御:明确不随关闭销毁,避免父窗口关闭时误删
  73. chatView->setAttribute(Qt::WA_DeleteOnClose, false);
  74. // 连接聊天窗口关闭请求信号
  75. connect(chatView, &ChatWindow::windowCloseRequested, this, &MainPanel::onChatWindowCloseRequested);
  76. }
  77. void MainPanel::initLayoutComponents()
  78. {
  79. // 创建右侧面板
  80. m_rightWidget = new QWidget;
  81. QVBoxLayout *vbox = new QVBoxLayout(m_rightWidget);
  82. vbox->setContentsMargins(0, 0, 0, 0);
  83. // 创建聊天窗口容器
  84. m_chatContainer = new QWidget(m_rightWidget);
  85. QVBoxLayout *chatLayout = new QVBoxLayout(m_chatContainer);
  86. chatLayout->setContentsMargins(0, 0, 0, 0);
  87. chatLayout->addWidget(chatView);
  88. vbox->addWidget(m_chatContainer, 1);
  89. // 创建主分割器
  90. splitter = new QSplitter(Qt::Horizontal, this);
  91. playerContainer = new QWidget(this);
  92. splitter->addWidget(playerContainer);
  93. splitter->addWidget(m_rightWidget);
  94. splitter->setStretchFactor(0, 60);
  95. splitter->setStretchFactor(1, 30);
  96. this->mainLayout()->addWidget(splitter, 1);
  97. this->mainLayout()->setContentsMargins(0, 0, 0, 0);
  98. this->mainLayout()->setSpacing(0);
  99. // 为playerContainer设置初始布局
  100. QVBoxLayout *playerLayout = new QVBoxLayout(playerContainer);
  101. playerLayout->setContentsMargins(0, 0, 0, 0);
  102. }
  103. void MainPanel::initFunctionButtons()
  104. {
  105. buttonGroup = new PopoverButtonGroup(Qt::Horizontal, playerContainer);
  106. // 添加功能按钮
  107. // FunctionButton *settingsBtn = new FunctionButton(IconUtils::createSettingsIcon(), "设置", this);
  108. // // 移除 Popover 箭头,改为直接打开设置窗口
  109. // buttonGroup->addButton(settingsBtn, nullptr);
  110. // connect(settingsBtn, &QPushButton::clicked, this, &MainPanel::onSettingsButtonClicked);
  111. // 移除:搜索按钮(暂不使用)
  112. // FunctionButton *searchBtn = new FunctionButton(IconUtils::createSearchIcon(), "搜索", this);
  113. // Popover *searchPopover = new Popover(this);
  114. // buttonGroup->addButton(searchBtn, searchPopover);
  115. FunctionButton *userBtn = new FunctionButton(IconUtils::createUserIcon(), "用户", this);
  116. Popover *userPopover = new Popover(this);
  117. // 构建用户Popover内容:提供“退出”操作
  118. {
  119. QWidget *userContent = new QWidget(userPopover);
  120. QVBoxLayout *userLayout = new QVBoxLayout(userContent);
  121. userLayout->setContentsMargins(8, 8, 8, 8);
  122. userLayout->setSpacing(8);
  123. QPushButton *logoutBtn = new QPushButton(tr("退出"), userContent);
  124. logoutBtn->setMinimumWidth(120);
  125. connect(logoutBtn, &QPushButton::clicked, this, [this]() {
  126. emit logoutClicked();
  127. });
  128. userLayout->addWidget(logoutBtn);
  129. userPopover->setContentWidget(userContent);
  130. }
  131. buttonGroup->addButton(userBtn, userPopover);
  132. // 添加音频设备选择按钮
  133. FunctionButton *audioDeviceBtn = new FunctionButton(IconUtils::createAudioDeviceIcon(), "音频设备", this);
  134. Popover *audioDevicePopover = new Popover(this);
  135. // // 使用解耦合版本的音频设备选择器
  136. // m_audioDeviceSelectorDecoupled = new AudioDeviceSelectorIconDecoupled(this);
  137. // audioDevicePopover->setContentWidget(m_audioDeviceSelectorDecoupled);
  138. // 使用 RecorderAudioWidget 作为 Popover 内容(麦克风 + 扬声器)
  139. QWidget *audioContent = new QWidget(audioDevicePopover);
  140. QVBoxLayout *audioLayout = new QVBoxLayout(audioContent);
  141. audioLayout->setContentsMargins(8, 8, 8, 8);
  142. audioLayout->setSpacing(8);
  143. // 麦克风区域
  144. QLabel *micTitle = new QLabel(tr("麦克风"), audioContent);
  145. micTitle->setStyleSheet("font-weight:600;");
  146. m_micWidget = new QComboBox(audioContent);
  147. m_micWidget->setEditable(false);
  148. audioLayout->addWidget(micTitle);
  149. audioLayout->addWidget(m_micWidget);
  150. // 扬声器区域
  151. QLabel *speakerTitle = new QLabel(tr("扬声器"), audioContent);
  152. speakerTitle->setStyleSheet("font-weight:600;");
  153. m_speakerWidget = new QComboBox(audioContent);
  154. m_speakerWidget->setEditable(false);
  155. audioLayout->addWidget(speakerTitle);
  156. audioLayout->addWidget(m_speakerWidget);
  157. // 视频编码器区域
  158. QLabel *encoderTitle = new QLabel(tr("视频编码器"), audioContent);
  159. encoderTitle->setStyleSheet("font-weight:600;");
  160. m_encoderWidget = new QComboBox(audioContent);
  161. m_encoderWidget->setEditable(false);
  162. audioLayout->addWidget(encoderTitle);
  163. audioLayout->addWidget(m_encoderWidget);
  164. audioDevicePopover->setContentWidget(audioContent);
  165. buttonGroup->addButton(audioDeviceBtn, audioDevicePopover);
  166. // 新增:独立的推流按钮(无弹层)
  167. m_streamButton = new FunctionButton(IconUtils::createStreamIcon(), tr("推流"), this);
  168. buttonGroup->addButton(m_streamButton);
  169. // 新增:聊天按钮(推流:显示/隐藏;非推流:弹出/嵌入)
  170. m_chatButton = new FunctionButton(IconUtils::createChatIcon(), tr("聊天"), this);
  171. buttonGroup->addButton(m_chatButton);
  172. // 移除:执行操作按钮(暂不使用)
  173. // FunctionButton *actionButton = new FunctionButton(IconUtils::createSettingsIcon(),
  174. // "执行操作",
  175. // this);
  176. // buttonGroup->addButton(actionButton, nullptr);
  177. // 将buttonGroup添加到playerContainer的布局中
  178. QVBoxLayout *playerLayout = qobject_cast<QVBoxLayout*>(playerContainer->layout());
  179. if (!playerLayout) {
  180. playerLayout = new QVBoxLayout(playerContainer);
  181. playerLayout->setContentsMargins(0, 0, 0, 0);
  182. }
  183. playerLayout->addStretch(1); // 添加弹性空间,将buttonGroup推到底部
  184. playerLayout->addWidget(buttonGroup, 0); // 添加buttonGroup到底部,不拉伸
  185. buttonGroup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 使用固定大小策略
  186. }
  187. void MainPanel::connectSignals()
  188. {
  189. // 连接功能按钮信号
  190. connect(m_streamButton, &QPushButton::clicked, this, &MainPanel::onStreamButtonClicked);
  191. connect(m_chatButton, &QPushButton::clicked, this, &MainPanel::onChatButtonClicked);
  192. // 连接WebSocket相关信号
  193. // 已移除与 UserProfileWidget 相关的在线状态更新
  194. // connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats); // 暂时移除统计在主面板的更新
  195. connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
  196. // 这里可以处理 liveStatus 相关逻辑
  197. QJsonParseError err;
  198. QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
  199. if (err.error != QJsonParseError::NoError || !doc.isObject()) {
  200. qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
  201. return;
  202. }
  203. QJsonObject obj = doc.object();
  204. int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
  205. if (liveStatus == 1) {
  206. qDebug() << "[MainPanel] liveStatus: 直播中" << chatView;
  207. if (chatView) {
  208. const QString id = webSocketClient->roomId();
  209. // 使用防抖机制处理频繁的请求
  210. m_pendingRoomId = id;
  211. m_debounceTimer->start(); // 重新开始计时,如果在500ms内再次收到请求,会重置定时器
  212. }
  213. }
  214. });
  215. // 初始化音频设备列表
  216. initAudioDeviceSelectors();
  217. }
  218. MainPanel::~MainPanel()
  219. {
  220. if (m_avPlayerStandalone) {
  221. m_avPlayerStandalone->deleteLater();
  222. m_avPlayerStandalone = nullptr;
  223. }
  224. if (m_recorderStandalone) {
  225. m_recorderStandalone->deleteLater();
  226. m_recorderStandalone = nullptr;
  227. }
  228. }
  229. void MainPanel::setRole(const QStringList &roleList)
  230. {
  231. bool isRec = roleList.contains("role.admin") || roleList.contains("role.recorder") || roleList.contains("录制");
  232. if (isRec) {
  233. auto rec = new RecorderWidget(this);
  234. setPlayerWidget(rec);
  235. } else {
  236. auto av = new AVPlayerWidget(this);
  237. setPlayerWidget(av);
  238. }
  239. if (m_streamButton) {
  240. m_streamButton->setVisible(isRec);
  241. m_streamButton->setText(tr("推流"));
  242. }
  243. }
  244. void MainPanel::setPushRoomId(const QString &id)
  245. {
  246. if (!webSocketClient) return;
  247. // 初始化聊天室连接
  248. webSocketClient->connectToRoom(id);
  249. if (chatView) {
  250. chatView->initWebsocket(id);
  251. }
  252. // 若当前是播放器,使用防抖播放
  253. m_pendingRoomId = id;
  254. m_debounceTimer->start();
  255. }
  256. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  257. {
  258. if (!newPlayer) return;
  259. if (!playerContainer) return;
  260. if (playerWidget) {
  261. playerWidget->deleteLater();
  262. playerWidget = nullptr;
  263. }
  264. playerWidget = newPlayer;
  265. // 将新播放器添加到容器布局
  266. if (auto layout = qobject_cast<QVBoxLayout*>(playerContainer->layout())) {
  267. layout->insertWidget(0, playerWidget, 1);
  268. } else {
  269. auto layout2 = new QVBoxLayout(playerContainer);
  270. layout2->setContentsMargins(0, 0, 0, 0);
  271. layout2->setSpacing(0);
  272. layout2->addWidget(playerWidget, 1);
  273. }
  274. // 更新推流按钮可见性
  275. if (m_streamButton) {
  276. const bool isRec = qobject_cast<RecorderWidget*>(playerWidget) != nullptr;
  277. m_streamButton->setVisible(isRec);
  278. if (!isRec) {
  279. m_isStreaming = false;
  280. m_streamButton->setText(tr("推流"));
  281. }
  282. }
  283. // 连接 RecorderWidget 的推流信号,保持 UI 状态同步
  284. if (auto rec = qobject_cast<RecorderWidget*>(playerWidget)) {
  285. connect(rec, &RecorderWidget::streamingStarted, this, [this, rec]() {
  286. m_isStreaming = true;
  287. if (m_streamButton) m_streamButton->setText(tr("停止推流"));
  288. // 进入极简模式
  289. if (QWidget *tlw = window()) {
  290. tlw->setUpdatesEnabled(false);
  291. }
  292. rec->hidePreview();
  293. if (chatView) chatView->hide();
  294. if (m_rightWidget) m_rightWidget->hide();
  295. if (splitter) splitter->hide();
  296. // 使用自定义标题的紧凑浮窗承载工具栏
  297. if (!m_compactFrame) {
  298. m_compactFrame = new TMainWindow();
  299. m_compactFrame->setWindowTitle(tr("共享控制"));
  300. m_compactFrame->setWindowFlag(Qt::Tool, true);
  301. // m_compactFrame->setAttribute(Qt::WA_DeleteOnClose);
  302. if (buttonGroup) {
  303. if (auto layout = qobject_cast<QVBoxLayout*>(playerContainer->layout())) {
  304. layout->removeWidget(buttonGroup);
  305. }
  306. auto container = new QWidget(m_compactFrame);
  307. auto lay = new QHBoxLayout(container);
  308. lay->setContentsMargins(12, 8, 12, 8);
  309. lay->setSpacing(10);
  310. buttonGroup->setParent(container);
  311. lay->addWidget(buttonGroup);
  312. m_compactFrame->setCentralWidget(container);
  313. // 使用 sizeHint 直接设置初始尺寸,避免反复 adjustSize 带来的重算
  314. const QSize hint = buttonGroup->sizeHint();
  315. int w = qMax(360, hint.width() + 24);
  316. int h = qMax(80, hint.height() + 16);
  317. int titleH = 0;
  318. if (auto mw = m_compactFrame->menuWidget()) {
  319. mw->adjustSize();
  320. titleH = mw->sizeHint().height();
  321. }
  322. m_compactFrame->setMinimumSize(w, h + titleH);
  323. m_compactFrame->resize(w, h + titleH);
  324. }
  325. }
  326. m_compactFrame->show();
  327. m_compactFrame->raise();
  328. m_compactMode = true;
  329. // 主窗口保持原几何但隐藏
  330. if (QWidget *tlw = window()) {
  331. m_savedWindowGeometry = tlw->geometry();
  332. tlw->hide();
  333. }
  334. });
  335. connect(rec, &RecorderWidget::streamingStopped, this, [this, rec]() {
  336. m_isStreaming = false;
  337. if (m_streamButton) m_streamButton->setText(tr("推流"));
  338. // 退出极简模式
  339. if (QWidget *tlw = window()) {
  340. tlw->setUpdatesEnabled(false);
  341. }
  342. rec->showPreview();
  343. if (splitter) splitter->show();
  344. if (m_rightWidget) m_rightWidget->show();
  345. // 销毁紧凑浮窗并将 buttonGroup 归还
  346. if (m_compactFrame) {
  347. if (buttonGroup) {
  348. buttonGroup->hide();
  349. buttonGroup->setParent(playerContainer);
  350. if (auto layout = qobject_cast<QVBoxLayout*>(playerContainer->layout())) {
  351. layout->addWidget(buttonGroup, 0);
  352. }
  353. buttonGroup->show();
  354. }
  355. m_compactFrame->close();
  356. m_compactFrame->deleteLater();
  357. m_compactFrame = nullptr;
  358. }
  359. m_compactMode = false;
  360. // 恢复主窗口显示
  361. if (QWidget *tlw = window()) {
  362. tlw->setGeometry(m_savedWindowGeometry);
  363. tlw->show();
  364. tlw->raise();
  365. tlw->setUpdatesEnabled(true);
  366. }
  367. showChatEmbedded();
  368. if (m_chatButton) m_chatButton->setText(tr("聊天"));
  369. applyModeLayout(); // 恢复布局为非推流状态
  370. });
  371. }
  372. // 根据当前模式(录制/播放)应用一次自适应布局
  373. applyModeLayout();
  374. }
  375. void MainPanel::handleDebouncedPlay()
  376. {
  377. // 在定时器触发时执行播放逻辑
  378. const QString id = m_pendingRoomId;
  379. if (id.isEmpty()) return;
  380. qDebug() << "[MainPanel] Debounced startPlay for room" << id;
  381. AVPlayerWidget *av = qobject_cast<AVPlayerWidget*>(playerWidget);
  382. if (av) {
  383. av->stopPlay();
  384. av->setPlayRoomId(id);
  385. av->startPlay();
  386. return;
  387. }
  388. RecorderWidget *rec = qobject_cast<RecorderWidget*>(playerWidget);
  389. if (rec) {
  390. // rec->startLive();
  391. // - RecorderWidget::Settings s = rec->m_settings; // 需要通过公共接口设置,避免直接访问成员
  392. }
  393. }
  394. void MainPanel::initAudioDeviceSelectors()
  395. {
  396. // TODO: 枚举音频设备、填充 m_micWidget 和 m_speakerWidget;枚举编码器填充 m_encoderWidget
  397. }
  398. void MainPanel::showRecorderStandalone()
  399. {
  400. if (m_recorderFrame) {
  401. m_recorderFrame->raise();
  402. m_recorderFrame->activateWindow();
  403. return;
  404. }
  405. m_recorderFrame = new TMainWindow();
  406. m_recorderFrame->setAttribute(Qt::WA_DeleteOnClose);
  407. m_recorderFrame->setWindowTitle(tr("录制器"));
  408. m_recorderStandalone = new RecorderWidget(m_recorderFrame);
  409. m_recorderFrame->setCentralWidget(m_recorderStandalone);
  410. m_recorderFrame->resize(960, 600);
  411. m_recorderFrame->show();
  412. connect(m_recorderFrame, &QObject::destroyed, this, [this]() {
  413. m_recorderFrame = nullptr;
  414. m_recorderStandalone = nullptr;
  415. });
  416. }
  417. void MainPanel::showPlayerStandalone()
  418. {
  419. if (m_playerFrame) {
  420. m_playerFrame->raise();
  421. m_playerFrame->activateWindow();
  422. return;
  423. }
  424. m_playerFrame = new TMainWindow();
  425. m_playerFrame->setAttribute(Qt::WA_DeleteOnClose);
  426. m_playerFrame->setWindowTitle(tr("播放器"));
  427. m_avPlayerStandalone = new AVPlayerWidget(m_playerFrame);
  428. m_playerFrame->setCentralWidget(m_avPlayerStandalone);
  429. m_playerFrame->resize(960, 600);
  430. m_playerFrame->show();
  431. connect(m_playerFrame, &QObject::destroyed, this, [this]() {
  432. m_playerFrame = nullptr;
  433. m_avPlayerStandalone = nullptr;
  434. });
  435. }
  436. void MainPanel::showChatStandalone()
  437. {
  438. if (!chatView) return;
  439. if (m_isStreaming) return; // 推流时保持仅浮窗
  440. // - if (m_chatFrame) {
  441. // - m_chatFrame->raise();
  442. // - m_chatFrame->activateWindow();
  443. // - return;
  444. // - }
  445. if (m_chatFrame) {
  446. // 确保 chatView 已正确作为中央部件挂载
  447. if (m_chatFrame->centralWidget() != chatView && chatView) {
  448. chatView->hide();
  449. if (chatView->parent() != m_chatFrame) {
  450. chatView->setParent(m_chatFrame);
  451. }
  452. m_chatFrame->setCentralWidget(chatView);
  453. chatView->show();
  454. }
  455. if (!m_chatFrame->isVisible())
  456. m_chatFrame->show();
  457. m_chatFrame->raise();
  458. m_chatFrame->activateWindow();
  459. return;
  460. }
  461. // 从嵌入容器移除
  462. if (m_chatContainer) {
  463. if (auto l = qobject_cast<QVBoxLayout*>(m_chatContainer->layout())) {
  464. l->removeWidget(chatView);
  465. }
  466. }
  467. m_chatFrame = new TMainWindow();
  468. m_chatFrame->setAttribute(Qt::WA_DeleteOnClose);
  469. m_chatFrame->setWindowTitle(tr("聊天"));
  470. m_chatFrame->installEventFilter(this);
  471. chatView->setParent(m_chatFrame);
  472. m_chatFrame->setCentralWidget(chatView);
  473. m_chatFrame->resize(380, 540);
  474. m_chatFrame->show();
  475. // - connect(m_chatFrame, &QObject::destroyed, this, [this]() {
  476. // - m_chatFrame = nullptr;
  477. // - onChatWindowCloseRequested();
  478. // - });
  479. connect(m_chatFrame, &QObject::destroyed, this, [this]() { m_chatFrame = nullptr; });
  480. }
  481. void MainPanel::showChatEmbedded()
  482. {
  483. if (!chatView || !m_chatContainer) return;
  484. // 如存在独立窗口包装,先将 chatView 移回容器再关闭窗口,避免被父窗口销毁
  485. if (m_chatFrame) {
  486. // 断开临时回调,避免 destroyed 中的副作用
  487. disconnect(m_chatFrame, nullptr, this, nullptr);
  488. if (chatView->parent() == m_chatFrame) {
  489. // 使用 takeCentralWidget 而非 setCentralWidget(nullptr) 避免误删中央部件
  490. if (m_chatFrame->centralWidget()) {
  491. QWidget *w = m_chatFrame->takeCentralWidget();
  492. Q_UNUSED(w);
  493. }
  494. }
  495. chatView->hide();
  496. chatView->setParent(m_chatContainer);
  497. chatView->setWindowTitle(QString());
  498. if (auto l = qobject_cast<QVBoxLayout *>(m_chatContainer->layout())) {
  499. if (l->indexOf(chatView) < 0)
  500. l->addWidget(chatView);
  501. } else {
  502. auto l2 = new QVBoxLayout(m_chatContainer);
  503. l2->setContentsMargins(0, 0, 0, 0);
  504. l2->addWidget(chatView);
  505. }
  506. chatView->show();
  507. // 现在安全地关闭独立窗口
  508. m_chatFrame->close();
  509. m_chatFrame = nullptr;
  510. } else {
  511. // 无独立窗口,仅确保嵌入
  512. chatView->hide();
  513. chatView->setParent(m_chatContainer);
  514. chatView->setWindowTitle(QString());
  515. if (auto l = qobject_cast<QVBoxLayout *>(m_chatContainer->layout())) {
  516. if (l->indexOf(chatView) < 0)
  517. l->addWidget(chatView);
  518. } else {
  519. auto l2 = new QVBoxLayout(m_chatContainer);
  520. l2->setContentsMargins(0, 0, 0, 0);
  521. l2->addWidget(chatView);
  522. }
  523. chatView->show();
  524. }
  525. }
  526. void MainPanel::onRecordButtonClicked()
  527. {
  528. // TODO: 录制控制逻辑
  529. }
  530. void MainPanel::onStreamButtonClicked()
  531. {
  532. RecorderWidget *rec = qobject_cast<RecorderWidget*>(playerWidget);
  533. if (!rec) {
  534. qDebug() << "[MainPanel] 当前不处于录制器模式,无法推流";
  535. return;
  536. }
  537. if (!m_isStreaming) {
  538. rec->startStreaming(); // 成功与否由信号驱动 UI
  539. } else {
  540. rec->stopStreaming();
  541. }
  542. }
  543. void MainPanel::onChatWindowCloseRequested()
  544. {
  545. // 如果处在推流状态,认为是“隐藏聊天”而不是关闭程序
  546. if (m_isStreaming) {
  547. // 显式隐藏独立聊天窗口,统一行为到 eventFilter 的隐藏逻辑
  548. if (m_chatFrame && m_chatFrame->isVisible()) {
  549. m_chatFrame->hide();
  550. }
  551. if (m_chatButton) m_chatButton->setText(tr("显示聊天"));
  552. return; // ChatWindow 本身不要销毁
  553. }
  554. // 非推流:回归嵌入显示
  555. showChatEmbedded();
  556. if (m_chatButton) m_chatButton->setText(tr("聊天"));
  557. applyModeLayout();
  558. }
  559. void MainPanel::onChatButtonClicked()
  560. {
  561. if (!chatView) return;
  562. if (m_isStreaming) {
  563. // 推流时:只在独立聊天窗口上 显示/隐藏 切换,避免频繁 reparent
  564. if (!m_chatFrame) {
  565. // 如果当前在嵌入容器里,先从布局移除
  566. if (m_chatContainer) {
  567. if (auto l = qobject_cast<QVBoxLayout*>(m_chatContainer->layout())) {
  568. l->removeWidget(chatView);
  569. }
  570. }
  571. m_chatFrame = new TMainWindow();
  572. m_chatFrame->setAttribute(Qt::WA_DeleteOnClose);
  573. m_chatFrame->setWindowTitle(tr("聊天"));
  574. m_chatFrame->installEventFilter(this);
  575. chatView->setParent(m_chatFrame);
  576. m_chatFrame->setCentralWidget(chatView);
  577. chatView->show();
  578. m_chatFrame->resize(380, 540);
  579. m_chatFrame->show();
  580. m_chatFrame->raise();
  581. m_chatFrame->activateWindow();
  582. connect(m_chatFrame, &QObject::destroyed, this, [this]() { m_chatFrame = nullptr; });
  583. if (m_chatButton) m_chatButton->setText(tr("隐藏聊天"));
  584. return; // 结束推流分支处理
  585. } else {
  586. // 已有独立窗口:切换显示/隐藏
  587. if (chatView->parent() != m_chatFrame) {
  588. chatView->setParent(m_chatFrame);
  589. if (m_chatFrame->centralWidget() != chatView)
  590. m_chatFrame->setCentralWidget(chatView);
  591. }
  592. chatView->show();
  593. if (m_chatFrame->isVisible()) {
  594. m_chatFrame->hide();
  595. if (m_chatButton) m_chatButton->setText(tr("显示聊天"));
  596. } else {
  597. m_chatFrame->show();
  598. m_chatFrame->raise();
  599. m_chatFrame->activateWindow();
  600. if (m_chatButton) m_chatButton->setText(tr("隐藏聊天"));
  601. }
  602. return; // 结束推流分支处理
  603. }
  604. }
  605. // 非推流:在弹出与嵌入间切换
  606. if (m_chatFrame) {
  607. // 已经是弹出状态,切回嵌入
  608. showChatEmbedded();
  609. if (m_chatButton)
  610. m_chatButton->setText(tr("聊天"));
  611. applyModeLayout();
  612. } else {
  613. // 目前为嵌入状态,弹出为独立窗口
  614. showChatStandalone();
  615. if (m_chatButton)
  616. m_chatButton->setText(tr("嵌入聊天"));
  617. applyModeLayout();
  618. }
  619. }
  620. // ===== 浮动工具栏 =====
  621. void MainPanel::showFloatingToolbar()
  622. {
  623. // 改为使用 m_compactFrame 承载,无需再在主窗口中悬浮
  624. if (!m_compactFrame)
  625. return;
  626. m_compactFrame->show();
  627. m_compactFrame->raise();
  628. }
  629. void MainPanel::hideFloatingToolbar()
  630. {
  631. if (m_compactFrame)
  632. m_compactFrame->hide();
  633. }
  634. // 新增:根据模式与窗口大小自适应布局
  635. void MainPanel::applyModeLayout()
  636. {
  637. // 若控件未初始化,直接返回
  638. if (!splitter || !playerContainer) return;
  639. // 推流期间,主窗口处于隐藏/极简状态,这里只保证分割区域隐藏
  640. if (m_isStreaming) {
  641. if (m_rightWidget) m_rightWidget->hide();
  642. splitter->hide();
  643. return;
  644. }
  645. const bool isRecorder = qobject_cast<RecorderWidget*>(playerWidget) != nullptr;
  646. const bool isPlayer = qobject_cast<AVPlayerWidget*>(playerWidget) != nullptr;
  647. // 保证分割窗口可见,用于布局
  648. splitter->show();
  649. const int panelW = width() > 0 ? width() : 1200;
  650. if (isPlayer) {
  651. // 播放模式:根据聊天是否嵌入来决定右侧面板显示
  652. const bool embeddedChat = (chatView && m_chatContainer && chatView->parent() == m_chatContainer);
  653. if (embeddedChat) {
  654. if (m_rightWidget) m_rightWidget->show();
  655. int chatW = panelW / 3;
  656. chatW = qBound(300, chatW, 420);
  657. const int leftW = qMax(0, panelW - chatW);
  658. QList<int> sizes; sizes << leftW << chatW;
  659. splitter->setSizes(sizes);
  660. } else {
  661. if (m_rightWidget) m_rightWidget->hide();
  662. QList<int> sizes; sizes << panelW << 0;
  663. splitter->setSizes(sizes);
  664. }
  665. return;
  666. }
  667. // 录制模式(非推流):展示右侧面板,根据聊天当前状态决定布局
  668. if (m_rightWidget) m_rightWidget->show();
  669. // 检查聊天当前是否为嵌入状态
  670. const bool embeddedChat = (chatView && m_chatContainer && chatView->parent() == m_chatContainer);
  671. if (embeddedChat) {
  672. // 聊天为嵌入状态,设置分割布局
  673. int chatW = panelW / 3;
  674. chatW = qBound(300, chatW, 420);
  675. const int leftW = qMax(0, panelW - chatW);
  676. QList<int> sizes; sizes << leftW << chatW;
  677. splitter->setSizes(sizes);
  678. } else {
  679. // 聊天为独立窗口状态,隐藏右侧面板
  680. if (m_rightWidget) m_rightWidget->hide();
  681. QList<int> sizes; sizes << panelW << 0;
  682. splitter->setSizes(sizes);
  683. }
  684. }
  685. void MainPanel::resizeEvent(QResizeEvent* event)
  686. {
  687. QWidget::resizeEvent(event);
  688. // 随窗口大小调整左右比例
  689. applyModeLayout();
  690. }
  691. bool MainPanel::eventFilter(QObject *watched, QEvent *event)
  692. {
  693. // 拦截聊天独立窗口的关闭事件
  694. if (watched == m_chatFrame && event->type() == QEvent::Close) {
  695. // 推流期间:关闭操作改为隐藏窗口,避免销毁与频繁 reparent 导致异常
  696. if (m_isStreaming) {
  697. event->ignore();
  698. if (m_chatFrame) {
  699. m_chatFrame->hide();
  700. }
  701. if (m_chatButton)
  702. m_chatButton->setText(tr("显示聊天"));
  703. return true; // 事件已处理,不再继续关闭
  704. }
  705. // 非推流:允许关闭,但先把 chatView 放回嵌入容器,避免被父窗口销毁
  706. if (chatView && m_chatContainer && chatView->parent() == m_chatFrame) {
  707. // 取走中央部件,避免 setCentralWidget(nullptr) 触发潜在删除
  708. if (m_chatFrame->centralWidget()) {
  709. QWidget *w = m_chatFrame->takeCentralWidget();
  710. Q_UNUSED(w);
  711. }
  712. chatView->hide();
  713. chatView->setParent(m_chatContainer);
  714. chatView->setWindowTitle(QString());
  715. if (auto l = qobject_cast<QVBoxLayout *>(m_chatContainer->layout())) {
  716. if (l->indexOf(chatView) < 0)
  717. l->addWidget(chatView);
  718. } else {
  719. auto l2 = new QVBoxLayout(m_chatContainer);
  720. l2->setContentsMargins(0, 0, 0, 0);
  721. l2->addWidget(chatView);
  722. }
  723. chatView->show();
  724. if (m_chatButton)
  725. m_chatButton->setText(tr("聊天"));
  726. applyModeLayout();
  727. }
  728. return false; // 继续关闭流程
  729. }
  730. return QWidget::eventFilter(watched, event);
  731. }