MainPanel.cpp 30 KB

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