MainPanel.cpp 30 KB

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