mainwindowa.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. #include "mainwindowa.h"
  2. #include "common.h"
  3. #include <QApplication>
  4. #include <QMenuBar>
  5. #include <QStatusBar>
  6. #include "ffmpeg_init.h"
  7. #include "qimage_operation.h"
  8. #include "qscreen.h"
  9. #include "start_play_thread.h"
  10. #include "AvRecorder/ui/opengl_video_widget.h"
  11. #if NDEBUG
  12. #define AUTO_HIDE_PLAYCONTROL 1 // release version
  13. #else
  14. #define AUTO_HIDE_PLAYCONTROL 0
  15. #endif
  16. MainWindowA::MainWindowA(QWidget* parent)
  17. : QMainWindow(parent)
  18. {
  19. m_playerController = new PlayerController;
  20. // 1. 菜单栏和主菜单
  21. m_menuBar = new QMenuBar(this);
  22. setMenuBar(m_menuBar);
  23. // 文件菜单
  24. m_menuFile = new QMenu(tr("文件"), this);
  25. m_menuBar->addMenu(m_menuFile);
  26. m_actionOpen = new QAction(tr("打开"), this);
  27. m_actionQuit = new QAction(tr("退出"), this);
  28. m_menuFile->addAction(m_actionOpen);
  29. m_menuFile->addSeparator();
  30. m_menuFile->addAction(m_actionQuit);
  31. connect(m_actionOpen, &QAction::triggered, this, &MainWindowA::on_actionOpen_triggered);
  32. connect(m_actionQuit, &QAction::triggered, this, &MainWindowA::on_actionQuit_triggered);
  33. // 最近文件菜单
  34. m_menuRecentFiles = new QMenu(tr("最近文件"), this);
  35. m_menuBar->addMenu(m_menuRecentFiles);
  36. // 播放列表菜单
  37. m_menuSavedPlaylist = new QMenu(tr("播放列表"), this);
  38. m_menuBar->addMenu(m_menuSavedPlaylist);
  39. // CV菜单
  40. m_menuCV = new QMenu(tr("图像处理"), this);
  41. m_menuBar->addMenu(m_menuCV);
  42. // 样式菜单
  43. m_menuStyle = new QMenu(tr("样式"), this);
  44. m_menuBar->addMenu(m_menuStyle);
  45. // 音频可视化菜单
  46. m_menuAudioVisualize = new QMenu(tr("音频可视化"), this);
  47. m_menuBar->addMenu(m_menuAudioVisualize);
  48. // 帮助菜单
  49. m_menuHelp = new QMenu(tr("帮助"), this);
  50. m_menuBar->addMenu(m_menuHelp);
  51. m_actionHelp = new QAction(tr("帮助"), this);
  52. m_actionAbout = new QAction(tr("关于"), this);
  53. m_actionAbout_QT = new QAction(tr("关于Qt"), this);
  54. m_menuHelp->addAction(m_actionHelp);
  55. m_menuHelp->addAction(m_actionAbout);
  56. m_menuHelp->addAction(m_actionAbout_QT);
  57. connect(m_actionHelp, &QAction::triggered, this, &MainWindowA::on_actionHelp_triggered);
  58. connect(m_actionAbout, &QAction::triggered, this, &MainWindowA::on_actionAbout_triggered);
  59. connect(m_actionAbout_QT, &QAction::triggered, qApp, &QApplication::aboutQt);
  60. // 2. 主控件和状态栏
  61. m_centralWidget = new QWidget(this);
  62. setCentralWidget(m_centralWidget);
  63. m_statusBar = new QStatusBar(this);
  64. setStatusBar(m_statusBar);
  65. // 3. 其他Action初始化
  66. m_actionStop = new QAction(tr("停止"), this);
  67. m_actionFullscreen = new QAction(tr("全屏"), this);
  68. m_actionFullscreen->setCheckable(true);
  69. m_actionHide_Play_Ctronl = new QAction(tr("隐藏控制栏"), this);
  70. m_actionHide_Play_Ctronl->setCheckable(true);
  71. m_actionAspect_Ratio = new QAction(tr("保持比例"), this);
  72. m_actionLoop_Play = new QAction(tr("循环播放"), this);
  73. m_actionLoop_Play->setCheckable(true);
  74. m_actionMedia_Info = new QAction(tr("媒体信息"), this);
  75. m_actionKeyboard_Usage = new QAction(tr("快捷键说明"), this);
  76. m_actionPlayList = new QAction(tr("播放列表窗口"), this);
  77. m_actionPlayList->setCheckable(true);
  78. m_actionOpenNetworkUrl = new QAction(tr("打开网络地址"), this);
  79. m_actionOriginalSize = new QAction(tr("原始大小"), this);
  80. m_actionHardware_decode = new QAction(tr("硬件解码"), this);
  81. m_actionHardware_decode->setCheckable(true);
  82. // 4. CV菜单Action
  83. m_actionGrayscale = new QAction(tr("灰度"), this);
  84. m_actionGrayscale->setCheckable(true);
  85. m_actionMirro = new QAction(tr("镜像"), this);
  86. m_actionMirro->setCheckable(true);
  87. m_actionTransform = new QAction(tr("变换"), this);
  88. m_actionTransform->setCheckable(true);
  89. m_menuCV->addAction(m_actionGrayscale);
  90. m_menuCV->addAction(m_actionMirro);
  91. m_menuCV->addAction(m_actionTransform);
  92. m_menuCV->addSeparator();
  93. m_actionRotate = new QAction(tr("旋转"), this);
  94. m_actionRotate->setCheckable(true);
  95. m_actionRepeat = new QAction(tr("重复"), this);
  96. m_actionRepeat->setCheckable(true);
  97. m_actionEqualizeHist = new QAction(tr("直方图均衡"), this);
  98. m_actionEqualizeHist->setCheckable(true);
  99. m_actionThreshold = new QAction(tr("二值化"), this);
  100. m_actionThreshold->setCheckable(true);
  101. m_actionThreshold_Adaptive = new QAction(tr("自适应阈值"), this);
  102. m_actionThreshold_Adaptive->setCheckable(true);
  103. m_actionReverse = new QAction(tr("反色"), this);
  104. m_actionReverse->setCheckable(true);
  105. m_actionColorReduce = new QAction(tr("色彩减少"), this);
  106. m_actionColorReduce->setCheckable(true);
  107. m_actionGamma = new QAction(tr("伽马"), this);
  108. m_actionGamma->setCheckable(true);
  109. m_actionContrastBright = new QAction(tr("对比度/亮度"), this);
  110. m_actionContrastBright->setCheckable(true);
  111. m_actionBlur = new QAction(tr("模糊"), this);
  112. m_actionBlur->setCheckable(true);
  113. m_actionCanny = new QAction(tr("Canny"), this);
  114. m_actionCanny->setCheckable(true);
  115. m_actionSobel = new QAction(tr("Sobel"), this);
  116. m_actionSobel->setCheckable(true);
  117. m_actionLaplacian = new QAction(tr("Laplacian"), this);
  118. m_actionLaplacian->setCheckable(true);
  119. m_actionScharr = new QAction(tr("Scharr"), this);
  120. m_actionScharr->setCheckable(true);
  121. m_actionPrewitt = new QAction(tr("Prewitt"), this);
  122. m_actionPrewitt->setCheckable(true);
  123. m_actionRemoveCV = new QAction(tr("移除选择"), this);
  124. m_actionRemoveCV->setCheckable(true);
  125. m_actionTest_CV = new QAction(tr("测试CV"), this);
  126. m_actionTest_CV->setCheckable(true);
  127. m_menuCV->addAction(m_actionRotate);
  128. m_menuCV->addAction(m_actionRepeat);
  129. m_menuCV->addAction(m_actionEqualizeHist);
  130. m_menuCV->addAction(m_actionThreshold);
  131. m_menuCV->addAction(m_actionThreshold_Adaptive);
  132. m_menuCV->addAction(m_actionReverse);
  133. m_menuCV->addAction(m_actionColorReduce);
  134. m_menuCV->addAction(m_actionGamma);
  135. m_menuCV->addAction(m_actionContrastBright);
  136. m_menuCV->addAction(m_actionBlur);
  137. m_menuCV->addAction(m_actionCanny);
  138. m_menuCV->addAction(m_actionSobel);
  139. m_menuCV->addAction(m_actionLaplacian);
  140. m_menuCV->addAction(m_actionScharr);
  141. m_menuCV->addAction(m_actionPrewitt);
  142. m_menuCV->addAction(m_actionRemoveCV);
  143. m_menuCV->addSeparator();
  144. m_menuCV->addAction(m_actionTest_CV);
  145. // 批量 connect CV 菜单下所有 QAction
  146. for (QAction* action : m_menuCV->actions()) {
  147. if (action && !action->isSeparator()) {
  148. connect(action, &QAction::toggled, this, &MainWindowA::onCvActionToggled);
  149. }
  150. }
  151. // 5. 音频可视化菜单Action
  152. m_actionLine = new QAction(tr("线形"), this);
  153. m_actionLine->setCheckable(true);
  154. m_actionBar = new QAction(tr("柱形"), this);
  155. m_actionBar->setCheckable(true);
  156. m_actionPie = new QAction(tr("饼形"), this);
  157. m_actionPie->setCheckable(true);
  158. m_actionSampling = new QAction(tr("采样"), this);
  159. m_actionSampling->setCheckable(true);
  160. m_actionFrequency = new QAction(tr("频率"), this);
  161. m_actionFrequency->setCheckable(true);
  162. m_menuAudioVisualize->addAction(m_actionLine);
  163. m_menuAudioVisualize->addAction(m_actionBar);
  164. m_menuAudioVisualize->addAction(m_actionPie);
  165. m_menuAudioVisualize->addSeparator();
  166. m_menuAudioVisualize->addAction(m_actionSampling);
  167. m_menuAudioVisualize->addAction(m_actionFrequency);
  168. // 6. 分组
  169. m_CvActsGroup = new QActionGroup(this);
  170. m_CvActsGroup->addAction(m_actionRotate);
  171. m_CvActsGroup->addAction(m_actionRepeat);
  172. m_CvActsGroup->addAction(m_actionEqualizeHist);
  173. m_CvActsGroup->addAction(m_actionThreshold);
  174. m_CvActsGroup->addAction(m_actionThreshold_Adaptive);
  175. m_CvActsGroup->addAction(m_actionReverse);
  176. m_CvActsGroup->addAction(m_actionColorReduce);
  177. m_CvActsGroup->addAction(m_actionGamma);
  178. m_CvActsGroup->addAction(m_actionContrastBright);
  179. m_CvActsGroup->addAction(m_actionCanny);
  180. m_CvActsGroup->addAction(m_actionBlur);
  181. m_CvActsGroup->addAction(m_actionSobel);
  182. m_CvActsGroup->addAction(m_actionLaplacian);
  183. m_CvActsGroup->addAction(m_actionScharr);
  184. m_CvActsGroup->addAction(m_actionPrewitt);
  185. m_CvActsGroup->addAction(m_actionRemoveCV);
  186. m_AVisualGrapicTypeActsGroup = new QActionGroup(this);
  187. m_AVisualGrapicTypeActsGroup->addAction(m_actionLine);
  188. m_AVisualGrapicTypeActsGroup->addAction(m_actionBar);
  189. m_AVisualGrapicTypeActsGroup->addAction(m_actionPie);
  190. m_AVisualTypeActsGroup = new QActionGroup(this);
  191. m_AVisualTypeActsGroup->addAction(m_actionSampling);
  192. m_AVisualTypeActsGroup->addAction(m_actionFrequency);
  193. // 7. 信号槽
  194. connect(m_actionSampling, &QAction::triggered, this, &MainWindowA::popup_audio_effect);
  195. connect(m_actionFrequency, &QAction::triggered, this, &MainWindowA::popup_audio_effect);
  196. connect(m_actionLine, &QAction::triggered, this, &MainWindowA::popup_audio_effect);
  197. connect(m_actionBar, &QAction::triggered, this, &MainWindowA::popup_audio_effect);
  198. connect(m_actionPie, &QAction::triggered, this, &MainWindowA::popup_audio_effect);
  199. // ... 其余Action信号槽如有也可补全 ...
  200. // 8. 继续原有初始化流程
  201. // setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint
  202. // | Qt::WindowCloseButtonHint);
  203. // 新增:主布局,视频+控制条
  204. auto mainLayout = new QVBoxLayout(m_centralWidget);
  205. mainLayout->setContentsMargins(0, 0, 0, 0);
  206. mainLayout->setSpacing(0);
  207. create_video_label();
  208. create_play_control();
  209. create_audio_effect();
  210. if (m_video_widget)
  211. mainLayout->addWidget(m_video_widget.get(), 1);
  212. if (m_play_control_wnd)
  213. mainLayout->addWidget(m_play_control_wnd.get(), 0);
  214. mainLayout->addWidget(m_audio_effect_wnd.get(), 0);
  215. m_centralWidget->setLayout(mainLayout);
  216. setWindowTitle(tr("Video Player"));
  217. qApp->installEventFilter(this);
  218. setAcceptDrops(true);
  219. #if AUTO_HIDE_PLAYCONTROL
  220. setMouseTracking(true);
  221. m_timer.setInterval(3 * 1000);
  222. m_timer.setSingleShot(false);
  223. connect(&m_timer, &QTimer::timeout, this, &MainWindowA::check_hide_play_control);
  224. m_timer.start();
  225. #endif
  226. read_settings();
  227. update_menus();
  228. #if !NDEBUG
  229. print_screen();
  230. #endif
  231. resize(600, 600);
  232. connect(m_playerController,
  233. &PlayerController::startToPlaySignal,
  234. this,
  235. &MainWindowA::update_menus);
  236. connect(m_playerController, &PlayerController::frameReady, this, &MainWindowA::frameReady);
  237. connect(m_playerController, &PlayerController::audioData, this, &MainWindowA::audio_data);
  238. connect(m_playerController,
  239. &PlayerController::setPlayControlWnd,
  240. this,
  241. &MainWindowA::set_paly_control_wnd);
  242. connect(m_playerController,
  243. &PlayerController::updatePlayControlVolume,
  244. this,
  245. &MainWindowA::update_paly_control_volume);
  246. connect(m_playerController,
  247. &PlayerController::updatePlayControlStatus,
  248. this,
  249. &MainWindowA::update_paly_control_status);
  250. // 新增:连接UI相关信号
  251. connect(m_playerController, &PlayerController::showMessage, this, &MainWindowA::onShowMessage);
  252. connect(m_playerController,
  253. &PlayerController::requestFullscreen,
  254. this,
  255. &MainWindowA::onRequestFullscreen);
  256. connect(m_playerController,
  257. &PlayerController::requestHideStatusBar,
  258. this,
  259. &MainWindowA::onRequestHideStatusBar);
  260. connect(m_playerController,
  261. &PlayerController::updatePlayTime,
  262. this,
  263. &MainWindowA::update_play_time);
  264. //connect(m_playerController, &PlayerController::playSeek, this, &MainWindowA::play_seek);
  265. hide_play_control(false);
  266. m_playerController->startToPlay(
  267. "rtmp://106.55.186.74:1935/stream/V1/01982b9b-f82c-7632-8283-8e60695b10d0");
  268. }
  269. MainWindowA::~MainWindowA()
  270. {
  271. m_playerController->stopPlay();
  272. save_settings();
  273. }
  274. QScreen* MainWindowA::screen() const
  275. {
  276. return QApplication::primaryScreen();
  277. }
  278. QRect MainWindowA::screen_rect() const
  279. {
  280. //auto pScreen = screen();
  281. //auto scale = pScreen->devicePixelRatio();
  282. //return QRect(0, 0, rt.width() * scale, rt.height() * scale);
  283. return screen()->geometry();
  284. }
  285. qreal MainWindowA::screen_scale() const
  286. {
  287. return screen()->devicePixelRatio();
  288. }
  289. QSize MainWindowA::display_video_size(AVCodecContext* pVideo) const
  290. {
  291. auto scale = screen_scale(); //screen display scale
  292. if (pVideo && scale != 0)
  293. return QSize(pVideo->width / scale, pVideo->height / scale);
  294. return QSize(0, 0);
  295. }
  296. void MainWindowA::create_video_label()
  297. {
  298. m_video_widget = std::make_unique<OpenGLVideoWidget>(centralWidget());
  299. // 不再单独设置布局,统一在 centralWidget 的主布局中管理
  300. }
  301. void MainWindowA::create_audio_effect()
  302. {
  303. m_audio_effect_wnd = std::make_unique<AudioEffectGL>(centralWidget());
  304. m_audio_effect_wnd->setObjectName(QString::fromUtf8("audio_effect"));
  305. m_audio_effect_wnd->hide();
  306. // connect(m_audio_effect_wnd.get(), &AudioEffectGL::hiden, this, &MainWindowA::start_send_data);
  307. connect(m_audio_effect_wnd.get(),
  308. &AudioEffectGL::hiden,
  309. m_playerController,
  310. &PlayerController::startSendData);
  311. }
  312. void MainWindowA::show_audio_effect(bool bShow)
  313. {
  314. if (!m_audio_effect_wnd)
  315. return;
  316. auto pt = frameGeometry().center() - m_audio_effect_wnd->rect().center();
  317. m_audio_effect_wnd->move(pt);
  318. m_audio_effect_wnd->paint_clear();
  319. if (bShow) {
  320. m_audio_effect_wnd->show();
  321. } else {
  322. m_audio_effect_wnd->hide();
  323. }
  324. }
  325. void MainWindowA::create_recentfiles_menu()
  326. {
  327. for (int i = 0; i < MaxRecentFiles; ++i) {
  328. m_recentFileActs[i] = std::make_unique<QAction>(this);
  329. m_recentFileActs[i]->setVisible(false);
  330. connect(m_recentFileActs[i].get(), SIGNAL(triggered()), this, SLOT(open_recentFile()));
  331. }
  332. m_recentClear = std::make_unique<QAction>(this);
  333. m_recentClear->setText(tr("清除"));
  334. connect(m_recentClear.get(), SIGNAL(triggered()), this, SLOT(clear_recentfiles()));
  335. auto pMenu = m_menuRecentFiles;
  336. pMenu->clear();
  337. for (int i = 0; i < MaxRecentFiles; ++i)
  338. pMenu->addAction(m_recentFileActs[i].get());
  339. pMenu->addSeparator();
  340. pMenu->addAction(m_recentClear.get());
  341. update_recentfile_actions();
  342. }
  343. void MainWindowA::set_current_file(const QString& fileName)
  344. {
  345. setWindowFilePath(fileName);
  346. auto files = m_settings.get_recentfiles().toStringList();
  347. files.removeAll(fileName);
  348. files.prepend(fileName);
  349. while (files.size() > MaxRecentFiles)
  350. files.removeLast();
  351. m_settings.set_recentfiles(files);
  352. update_recentfile_actions();
  353. }
  354. void MainWindowA::clear_recentfiles()
  355. {
  356. auto files = m_settings.get_recentfiles().toStringList();
  357. files.clear();
  358. m_settings.set_recentfiles(files);
  359. update_recentfile_actions();
  360. }
  361. void MainWindowA::remove_recentfiles(const QString& fileName)
  362. {
  363. auto files = m_settings.get_recentfiles().toStringList();
  364. files.removeAll(fileName);
  365. m_settings.set_recentfiles(files);
  366. update_recentfile_actions();
  367. }
  368. void MainWindowA::update_recentfile_actions()
  369. {
  370. auto files = m_settings.get_recentfiles().toStringList();
  371. int numRecentFiles = qMin(files.size(), (int) MaxRecentFiles);
  372. m_menuRecentFiles->setEnabled(numRecentFiles > 0);
  373. for (int i = 0; i < numRecentFiles; ++i) {
  374. QString text = tr("%1 %2").arg(i + 1).arg(stripped_name(files[i]));
  375. m_recentFileActs[i]->setText(
  376. QApplication::translate("MainWindowA", text.toStdString().c_str(), nullptr));
  377. m_recentFileActs[i]->setData(files[i]);
  378. m_recentFileActs[i]->setVisible(true);
  379. }
  380. for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
  381. m_recentFileActs[j]->setVisible(false);
  382. }
  383. QString MainWindowA::stripped_name(const QString& fullFileName) const
  384. {
  385. return QFileInfo(fullFileName).fileName();
  386. }
  387. void MainWindowA::open_recentFile()
  388. {
  389. if (auto action = qobject_cast<QAction*>(sender()))
  390. m_playerController->startToPlay(action->data().toString());
  391. }
  392. void MainWindowA::about_media_info()
  393. {
  394. // if (!m_pVideoState)
  395. // return;
  396. // auto pState = m_playerController->state();
  397. // if (!pState)
  398. // return;
  399. // if (auto ic = pState->ic) {
  400. // auto str = dump_format(ic, 0, pState->filename);
  401. // show_msg_dlg(str, "Media information", "QLabel{min-width: 760px;}");
  402. // }
  403. }
  404. void MainWindowA::create_play_control()
  405. {
  406. m_play_control_wnd = std::make_unique<PlayControlWnd>(m_playerController, this);
  407. m_play_control_wnd->setObjectName(QString::fromUtf8("play_control"));
  408. m_play_control_wnd->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
  409. // 不再 setGeometry
  410. }
  411. void MainWindowA::update_video_label()
  412. {
  413. // 移除手动 resize,让布局自动管理
  414. // auto sizeCenter = centralWidget()->size();
  415. // if (auto pLabel = get_video_widget())
  416. // pLabel->resize(sizeCenter.width(), sizeCenter.height());
  417. }
  418. void MainWindowA::show_msg_dlg(const QString& message,
  419. const QString& windowTitle,
  420. const QString& styleSheet)
  421. {
  422. onShowMessage(message, windowTitle, styleSheet);
  423. }
  424. void MainWindowA::update_play_control()
  425. {
  426. // if (auto pPlayControl = get_play_control()) {
  427. // auto sizeCenter = centralWidget()->size();
  428. // pPlayControl->resize(sizeCenter.width(), pPlayControl->size().height());
  429. // auto frameGeoRt = frameGeometry();
  430. // auto geoRt = geometry();
  431. // // QPoint pt = ui->statusbar->pos();
  432. // int borderH = frameGeoRt.height() - (geoRt.y() - frameGeoRt.y()) - geoRt.height();
  433. // //int borderH = frameGeoRt.height() - geoRt.height();
  434. // // int borderw = frameGeoRt.width() - geoRt.width();
  435. // // int borderSelf = pPlayControl->frameGeometry().height() - (pPlayControl->geometry().y() - pPlayControl->frameGeometry().y()) - pPlayControl->geometry().height();
  436. // //auto pt = geoRt.bottomLeft() - QPoint(0, szPlayControl.height() + borderH);
  437. // auto pt = geoRt.bottomLeft() - QPoint(0, pPlayControl->size().height() - 1);
  438. // pPlayControl->move(pt);
  439. // }
  440. }
  441. void MainWindowA::print_size() const
  442. {
  443. auto rt = geometry();
  444. qDebug("geometry rt:(x:%d, y:%d, w:%d, h:%d)", rt.x(), rt.y(), rt.width(), rt.height());
  445. rt = frameGeometry();
  446. qDebug("frameGeometry rt:(x:%d, y:%d, w:%d, h:%d)", rt.x(), rt.y(), rt.width(), rt.height());
  447. auto size = this->size();
  448. qDebug("window size:(%d,%d)", size.width(), size.height());
  449. // 这里不再有ui->centralwidget,直接用m_centralWidget
  450. size = m_centralWidget->size();
  451. qDebug("centralwidget size:(%d,%d)", size.width(), size.height());
  452. // 菜单栏
  453. size = m_menuBar->size();
  454. qDebug("menubar size:(%d,%d)", size.width(), size.height());
  455. // 状态栏
  456. size = m_statusBar->size();
  457. qDebug("statusbar size:(%d,%d)", size.width(), size.height());
  458. // 菜单栏位置
  459. auto pt = m_menuBar->pos();
  460. qDebug("menuBar pt (x:%d, y:%d)", pt.x(), pt.y());
  461. }
  462. void MainWindowA::print_screen() const
  463. {
  464. auto screen = QApplication::primaryScreen();
  465. auto rt = screen->availableGeometry();
  466. qDebug("availableGeometry rt (x:%d, y:%d, width:%d, height:%d)",
  467. rt.x(),
  468. rt.y(),
  469. rt.width(),
  470. rt.height());
  471. auto sz = screen->availableSize();
  472. qDebug("availableSize sz (width:%d, height:%d)", sz.width(), sz.height());
  473. sz = screen->size();
  474. qDebug("size sz (width:%d, height:%d)", sz.width(), sz.height());
  475. rt = screen->virtualGeometry();
  476. qDebug("virtualGeometry rt (x:%d, y:%d, width:%d, height:%d)",
  477. rt.x(),
  478. rt.y(),
  479. rt.width(),
  480. rt.height());
  481. sz = screen->virtualSize();
  482. qDebug("virtualSize sz (width:%d, height:%d)", sz.width(), sz.height());
  483. rt = screen->availableVirtualGeometry();
  484. qDebug("availableVirtualGeometry rt (x:%d, y:%d, width:%d, height:%d)",
  485. rt.x(),
  486. rt.y(),
  487. rt.width(),
  488. rt.height());
  489. sz = screen->availableVirtualSize();
  490. qDebug("availableVirtualSize sz (width:%d, height:%d)", sz.width(), sz.height());
  491. rt = screen->geometry();
  492. qDebug("geometry rt (x:%d, y:%d, width:%d, height:%d)", rt.x(), rt.y(), rt.width(), rt.height());
  493. auto depth = screen->depth();
  494. qDebug() << "depth :" << depth;
  495. auto ratio = screen->devicePixelRatio();
  496. qDebug() << "devicePixelRatio :" << ratio;
  497. auto dot_per_inch = screen->logicalDotsPerInch();
  498. qDebug() << "logicalDotsPerInch :" << dot_per_inch;
  499. auto x = screen->logicalDotsPerInchX();
  500. qDebug() << "logicalDotsPerInchX :" << x;
  501. auto y = screen->logicalDotsPerInchY();
  502. qDebug() << "logicalDotsPerInchY :" << y;
  503. auto str = screen->manufacturer();
  504. qDebug() << "manufacturer :" << str;
  505. str = screen->model();
  506. qDebug() << "model :" << str;
  507. str = screen->name();
  508. qDebug() << "name :" << str;
  509. str = screen->serialNumber();
  510. qDebug() << "serialNumber :" << str;
  511. auto o = screen->nativeOrientation();
  512. qDebug() << "nativeOrientation :" << o;
  513. o = screen->orientation();
  514. qDebug() << "orientation :" << o;
  515. o = screen->primaryOrientation();
  516. qDebug() << "primaryOrientation :" << o;
  517. auto ph_d = screen->physicalDotsPerInch();
  518. qDebug() << "physicalDotsPerInch :" << ph_d;
  519. ph_d = screen->physicalDotsPerInchX();
  520. qDebug() << "physicalDotsPerInchX :" << ph_d;
  521. ph_d = screen->physicalDotsPerInchY();
  522. qDebug() << "physicalDotsPerInchY :" << ph_d;
  523. auto sz_f = screen->physicalSize();
  524. qDebug() << "physicalSize :" << sz_f;
  525. auto fr = screen->refreshRate();
  526. qDebug() << "refreshRate :" << fr;
  527. }
  528. void MainWindowA::resizeEvent(QResizeEvent* event)
  529. {
  530. update_video_label();
  531. update_play_control();
  532. QMainWindow::resizeEvent(event);
  533. }
  534. void MainWindowA::moveEvent(QMoveEvent* event)
  535. {
  536. update_play_control();
  537. QMainWindow::moveEvent(event);
  538. }
  539. void MainWindowA::keyPressEvent(QKeyEvent* event)
  540. {
  541. qDebug() << "Mainwindow key event, event:" << event->text() << "key:" << event->key()
  542. << "key_str:" << QKeySequence(event->key()).toString();
  543. switch (event->key()) {
  544. case Qt::Key_Space: // pause/continue
  545. case Qt::Key_Up: // volume up
  546. case Qt::Key_Down: // volume down
  547. case Qt::Key_Left: // play back
  548. case Qt::Key_Right: // play forward
  549. case Qt::Key_M: // mute
  550. case Qt::Key_Comma: // speed down
  551. case Qt::Key_Period: // speed up
  552. play_control_key((Qt::Key) event->key());
  553. break;
  554. case Qt::Key_A: // aspect ratio
  555. on_actionAspect_Ratio_triggered();
  556. break;
  557. case Qt::Key_O: // keep orginal size
  558. on_actionOriginalSize_triggered();
  559. break;
  560. case Qt::Key_L: // show play list wnd
  561. {
  562. show_playlist();
  563. m_actionPlayList->setChecked(true);
  564. } break;
  565. case Qt::Key_F: // full screen
  566. {
  567. bool bFullscreen = label_fullscreen();
  568. show_fullscreen(!bFullscreen);
  569. m_actionFullscreen->setChecked(!bFullscreen);
  570. } break;
  571. case Qt::Key_Escape: {
  572. show_fullscreen(false);
  573. m_actionFullscreen->setChecked(false);
  574. } break;
  575. case Qt::Key_H:
  576. on_actionKeyboard_Usage_triggered();
  577. break;
  578. default:
  579. qDebug("Not handled key event, key:%s(%d) pressed!\n",
  580. qUtf8Printable(event->text()),
  581. event->key());
  582. QWidget::keyPressEvent(event);
  583. break;
  584. }
  585. }
  586. bool MainWindowA::eventFilter(QObject* obj, QEvent* event)
  587. {
  588. if (event->type() == QEvent::MouseMove) {
  589. auto mouseEvent = static_cast<QMouseEvent*>(event);
  590. // displayStatusMessage(QString("Mouse move
  591. // (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
  592. check_hide_menubar(mouseEvent->pos());
  593. #if AUTO_HIDE_PLAYCONTROL
  594. if (!(m_actionHide_Play_Ctronl->isChecked() || label_fullscreen())) {
  595. if (cursor_in_window(get_play_control())) {
  596. m_timer.stop();
  597. auto_hide_play_control(false);
  598. } else {
  599. m_timer.start();
  600. }
  601. }
  602. hide_cursor(false);
  603. setCursor(Qt::ArrowCursor);
  604. #endif
  605. }
  606. return QMainWindow::eventFilter(obj, event);
  607. }
  608. void MainWindowA::dropEvent(QDropEvent* event)
  609. {
  610. auto mimeData = event->mimeData();
  611. if (!mimeData->hasUrls())
  612. return;
  613. if (auto urlList = mimeData->urls(); urlList.size() > 0)
  614. m_playerController->startToPlay(urlList.at(0).toLocalFile());
  615. }
  616. void MainWindowA::dragEnterEvent(QDragEnterEvent* event)
  617. {
  618. if (auto mimeData = event->mimeData(); mimeData->hasUrls())
  619. event->acceptProposedAction();
  620. event->accept();
  621. }
  622. void MainWindowA::check_hide_menubar(const QPoint& pt)
  623. {
  624. if (isFullScreen())
  625. hide_menubar(pt.y() > menuBar()->geometry().height());
  626. }
  627. void MainWindowA::check_hide_play_control()
  628. {
  629. if (!m_playerController->isPlaying())
  630. return;
  631. if (!isFullScreen() && cursor_in_window(get_play_control())) {
  632. qDebug() << "Cursor is in PlayControl window, don't hide it.";
  633. return;
  634. }
  635. auto_hide_play_control();
  636. hide_cursor();
  637. }
  638. void MainWindowA::auto_hide_play_control(bool bHide)
  639. {
  640. if (!get_play_control())
  641. return;
  642. if (!m_playerController->state())
  643. return;
  644. if (m_actionHide_Play_Ctronl->isChecked())
  645. return;
  646. hide_play_control(bHide);
  647. }
  648. void MainWindowA::on_actionOpen_triggered()
  649. {
  650. const QStringList filters(
  651. {"Videos (*.mp4 *.avi *.mkv)", "Audios (*.mp3 *.wav *.wma)", "Any files (*)"});
  652. QFileDialog dialog(this);
  653. dialog.setFileMode(QFileDialog::ExistingFile);
  654. // dialog.setNameFilter(tr("Videos (*.mp4 *.avi *.mp3)"));
  655. dialog.setNameFilters(filters);
  656. dialog.setViewMode(QFileDialog::List);
  657. if (dialog.exec()) {
  658. m_playerController->startToPlay(dialog.selectedFiles()[0]);
  659. }
  660. }
  661. void MainWindowA::on_actionAspect_Ratio_triggered() {}
  662. void MainWindowA::on_actionQuit_triggered()
  663. {
  664. QMainWindow::close();
  665. }
  666. void MainWindowA::on_actionHelp_triggered() {}
  667. void MainWindowA::on_actionStop_triggered()
  668. {
  669. m_playerController->stopPlay();
  670. }
  671. void MainWindowA::on_actionHide_Play_Ctronl_triggered()
  672. {
  673. hide_play_control(m_actionHide_Play_Ctronl->isChecked());
  674. }
  675. void MainWindowA::on_actionFullscreen_triggered()
  676. {
  677. show_fullscreen(m_actionFullscreen->isChecked());
  678. }
  679. void MainWindowA::on_actionLoop_Play_triggered()
  680. {
  681. if (auto pState = m_playerController->state())
  682. pState->loop = int(m_actionLoop_Play->isChecked());
  683. }
  684. void MainWindowA::on_actionMedia_Info_triggered()
  685. {
  686. if (m_playerController->isPlaying())
  687. about_media_info();
  688. }
  689. void MainWindowA::on_actionKeyboard_Usage_triggered()
  690. {
  691. QString str;
  692. QString indent = " ";
  693. str += "A" + indent + "Video aspect ratio\n";
  694. str += "F" + indent + "Fulllscreen/Unfullscreen\n";
  695. str += "H" + indent + "Show help\n";
  696. str += "L" + indent + "Show playlist\n";
  697. str += "M" + indent + "Mute/Unmute\n";
  698. str += "O" + indent + "Keep video original size\n";
  699. str += "Space" + indent + "Pause/Play\n";
  700. str += "Up" + indent + "Volume up\n";
  701. str += "Down" + indent + "Volume down\n";
  702. str += "Left" + indent + "Play back\n";
  703. str += "Right" + indent + "Play forward\n";
  704. str += "<" + indent + "Speed down\n";
  705. str += ">" + indent + "Speed up\n";
  706. show_msg_dlg(str, "Keyboard Control");
  707. }
  708. void MainWindowA::set_audio_effect_format(const BarHelper::VisualFormat& fmt)
  709. {
  710. if (m_audio_effect_wnd)
  711. m_audio_effect_wnd->set_draw_fmt(fmt);
  712. }
  713. void MainWindowA::popup_audio_effect()
  714. {
  715. if (m_playerController->isPlaying()) {
  716. BarHelper::VisualFormat fmt;
  717. get_avisual_format(fmt);
  718. set_audio_effect_format(fmt);
  719. show_audio_effect();
  720. m_playerController->startSendData();
  721. }
  722. }
  723. void MainWindowA::center_window(QRect screen_rec)
  724. {
  725. auto x = (screen_rec.width() - width()) / 2;
  726. auto y = (screen_rec.height() - height()) / 2;
  727. move(x, y);
  728. show();
  729. }
  730. void MainWindowA::show_fullscreen(bool bFullscreen)
  731. {
  732. onRequestFullscreen(bFullscreen);
  733. }
  734. bool MainWindowA::label_fullscreen()
  735. {
  736. if (auto pLabel = get_video_widget())
  737. return pLabel->isFullScreen();
  738. return false;
  739. }
  740. void MainWindowA::on_actionOriginalSize_triggered()
  741. {
  742. // if (!m_pVideoState)
  743. // return;
  744. // auto pVideoCtx = m_pVideoState->get_contex(AVMEDIA_TYPE_VIDEO);
  745. // auto pLabel = get_video_widget();
  746. // if (!pVideoCtx || !pLabel)
  747. // return;
  748. // auto sizeLabel = pLabel->size();
  749. // auto sz = size();
  750. // auto video_sz = display_video_size(pVideoCtx);
  751. // int new_width = video_sz.width();
  752. // int new_height = video_sz.height();
  753. // if (new_width < minimumWidth()) {
  754. // new_height = minimumWidth() * new_height / new_width;
  755. // new_width = minimumWidth();
  756. // }
  757. // if (new_height < minimumHeight()) {
  758. // new_width = minimumHeight() * new_width / new_height;
  759. // new_height = minimumHeight();
  760. // }
  761. // int w_change = new_width - sizeLabel.width();
  762. // int h_change = new_height - sizeLabel.height();
  763. // sz += QSize(w_change, h_change);
  764. // resize_window(sz);
  765. }
  766. void MainWindowA::hide_play_control(bool bHide)
  767. {
  768. // if (auto pPlayControl = get_play_control()) {
  769. // // pPlayControl->setVisible(true);
  770. // // pPlayControl->setDisabled(false);
  771. // if (pPlayControl->isVisible() == bHide) {
  772. // pPlayControl->setVisible(!bHide);
  773. // }
  774. // }
  775. }
  776. void MainWindowA::set_paly_control_wnd(bool set)
  777. {
  778. auto pPlayControl = get_play_control();
  779. if (!pPlayControl)
  780. return;
  781. if (set) {
  782. auto pState = m_playerController->state();
  783. if (!pState)
  784. return;
  785. if (auto ic = pState->ic) {
  786. int64_t hours, mins, secs, us;
  787. get_duration_time(ic->duration, hours, mins, secs, us);
  788. pPlayControl->set_total_time(hours, mins, secs);
  789. }
  790. } else {
  791. pPlayControl->clear_all();
  792. }
  793. }
  794. void MainWindowA::set_volume_updown(bool bUp, float unit)
  795. {
  796. auto volume = m_playerController->deviceVolume();
  797. auto n_volume = volume;
  798. if (bUp) {
  799. n_volume += unit;
  800. } else {
  801. n_volume -= unit;
  802. }
  803. if (n_volume > 1.0 || n_volume < 0) {
  804. QApplication::beep();
  805. }
  806. n_volume = n_volume > 1.0 ? 1.0 : n_volume;
  807. n_volume = n_volume < 0 ? 0 : n_volume;
  808. set_volume(int(n_volume * 100));
  809. update_paly_control_volume();
  810. }
  811. void MainWindowA::update_paly_control_volume()
  812. {
  813. if (auto pPlayControl = get_play_control())
  814. pPlayControl->set_volume_slider(m_playerController->deviceVolume());
  815. }
  816. void MainWindowA::update_paly_control_muted()
  817. {
  818. if (auto pPlayControl = get_play_control()) {
  819. if (auto pState = m_playerController->state())
  820. pPlayControl->volume_muted(pState->muted);
  821. }
  822. }
  823. void MainWindowA::update_paly_control_status()
  824. {
  825. if (auto pPlayControl = get_play_control()) {
  826. if (auto pState = m_playerController->state())
  827. pPlayControl->update_btn_play(!!pState->paused);
  828. }
  829. }
  830. void MainWindowA::update_play_time()
  831. {
  832. if (auto pPlayControl = get_play_control()) {
  833. // pPlayControl->setVisible(true);
  834. // pPlayControl->resize(800, 200);
  835. // pPlayControl->show();
  836. if (auto pState = m_playerController->state())
  837. pPlayControl->update_play_time(pState->audio_clock);
  838. }
  839. }
  840. void MainWindowA::play_seek()
  841. {
  842. if (auto pPlayControl = get_play_control()) {
  843. auto maxValue = pPlayControl->get_progress_slider_max();
  844. auto total_time = pPlayControl->get_total_time();
  845. auto value = pPlayControl->get_progress_slider_value();
  846. double seek_time = 0;
  847. if (maxValue > 0)
  848. seek_time = value * total_time * 1.0 / maxValue;
  849. qDebug() << "val:" << value << ",maxVal:" << maxValue << ",total time" << total_time
  850. << ",seek time:" << seek_time;
  851. m_playerController->videoSeek(seek_time);
  852. }
  853. update_paly_control_status();
  854. }
  855. void MainWindowA::play_mute(bool mute)
  856. {
  857. auto pState = m_playerController->state();
  858. if (pState)
  859. toggle_mute(pState, mute);
  860. }
  861. void MainWindowA::set_volume(int volume)
  862. {
  863. auto pPlayControl = get_play_control();
  864. if (!pPlayControl)
  865. return;
  866. auto max_val = pPlayControl->get_volum_slider_max();
  867. auto vol = volume * 1.0 / max_val;
  868. m_playerController->setDeviceVolume(vol);
  869. volume_settings(true, vol);
  870. }
  871. void MainWindowA::set_play_speed()
  872. {
  873. auto pPlayControl = get_play_control();
  874. if (!pPlayControl)
  875. return;
  876. auto speed = pPlayControl->get_speed();
  877. qDebug("set_play_spped, speed control changed, speed:%d", speed);
  878. if (m_playerController) {
  879. if (auto pState = m_playerController->state()) {
  880. #if USE_AVFILTER_AUDIO
  881. set_audio_playspeed(pState, speed);
  882. #endif
  883. }
  884. }
  885. }
  886. void MainWindowA::play_speed_adjust(bool up)
  887. {
  888. if (auto pPlayControl = get_play_control())
  889. pPlayControl->speed_adjust(up);
  890. set_play_speed();
  891. }
  892. void MainWindowA::hide_statusbar(bool bHide)
  893. {
  894. onRequestHideStatusBar(bHide);
  895. }
  896. void MainWindowA::hide_menubar(bool bHide)
  897. {
  898. menuBar()->setVisible(!bHide);
  899. // qDebug("is full screen:%d, menu is visible:%d", isFullScreen(), bVisible);
  900. if (isFullScreen()) {
  901. centralWidget()->resize(centralWidget()->size());
  902. }
  903. update_play_control();
  904. }
  905. void MainWindowA::on_actionAbout_triggered() {}
  906. void MainWindowA::play_failed(const QString& file)
  907. {
  908. show_msg_dlg(QString("File play failed, file: %1").arg(toNativePath(file)));
  909. }
  910. void MainWindowA::play_start_seek()
  911. {
  912. play_seek();
  913. m_playerController->pausePlay();
  914. }
  915. void MainWindowA::play_control_key(Qt::Key key)
  916. {
  917. if (!m_playerController)
  918. return;
  919. auto pState = m_playerController->state();
  920. if (!pState)
  921. return;
  922. switch (key) {
  923. case Qt::Key_Space: // pause/continue
  924. m_playerController->pausePlay();
  925. break;
  926. case Qt::Key_M:
  927. toggle_mute(pState, !pState->muted);
  928. update_paly_control_muted();
  929. break;
  930. case Qt::Key_Up: // volume
  931. set_volume_updown(true);
  932. break;
  933. case Qt::Key_Down: // volume
  934. set_volume_updown(false);
  935. break;
  936. case Qt::Key_Left:
  937. m_playerController->playSeekPre();
  938. break;
  939. case Qt::Key_Right:
  940. m_playerController->playSeekNext();
  941. break;
  942. case Qt::Key_Comma:
  943. play_speed_adjust(false);
  944. break;
  945. case Qt::Key_Period:
  946. play_speed_adjust(true);
  947. break;
  948. default:
  949. qDebug("key:(%d) pressed, not handled!\n", key);
  950. break;
  951. }
  952. }
  953. void MainWindowA::frameReady(AVFrame* frame)
  954. {
  955. auto widget = get_video_widget();
  956. widget->Render(frame);
  957. }
  958. void MainWindowA::subtitle_ready(const QString& text)
  959. {
  960. set_subtitle(text);
  961. }
  962. void MainWindowA::set_subtitle(const QString& str)
  963. {
  964. m_subtitle = str;
  965. qDebug() << "subtitle received:" << m_subtitle;
  966. }
  967. void MainWindowA::clear_subtitle_str()
  968. {
  969. set_subtitle("");
  970. }
  971. void MainWindowA::displayStatusMessage(const QString& message)
  972. {
  973. int timeout = 0; // 5000: 5 second timeout
  974. if (auto bar = statusBar())
  975. bar->showMessage(message, timeout);
  976. }
  977. void MainWindowA::print_decodeContext(const AVCodecContext* pDecodeCtx, bool bVideo) const
  978. {
  979. if (!pDecodeCtx)
  980. return;
  981. if (bVideo) {
  982. qInfo("video codec_name: %s", pDecodeCtx->codec->name);
  983. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  984. pDecodeCtx->codec_type,
  985. pDecodeCtx->codec_id,
  986. pDecodeCtx->codec_tag);
  987. qInfo("width: %d, height: %d, codec_tag: %d", pDecodeCtx->width, pDecodeCtx->height);
  988. } else {
  989. qInfo("audio codec_name: %s", pDecodeCtx->codec->name);
  990. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  991. pDecodeCtx->codec_type,
  992. pDecodeCtx->codec_id,
  993. pDecodeCtx->codec_tag);
  994. qInfo("sample_rate: %d, channels: %d, sample_fmt: %d",
  995. pDecodeCtx->sample_rate,
  996. pDecodeCtx->ch_layout.nb_channels,
  997. pDecodeCtx->sample_fmt);
  998. qInfo("frame_size: %d, frame_number: %d, block_align: %d",
  999. pDecodeCtx->frame_size,
  1000. pDecodeCtx->frame_num,
  1001. pDecodeCtx->block_align);
  1002. }
  1003. }
  1004. void MainWindowA::save_settings()
  1005. {
  1006. auto res = m_actionHide_Play_Ctronl->isChecked();
  1007. m_settings.set_general("hidePlayContrl", int(res));
  1008. res = m_actionFullscreen->isChecked();
  1009. m_settings.set_general("fullScreen", int(res));
  1010. res = m_actionHardware_decode->isChecked();
  1011. m_settings.set_general("openDXVA2", int(res));
  1012. res = m_actionLoop_Play->isChecked();
  1013. m_settings.set_general("loopPlay", int(res));
  1014. m_settings.set_general("style", get_selected_style());
  1015. m_settings.set_info("software", "Video player");
  1016. //m_settings.set_info("version", PLAYER_VERSION);
  1017. m_settings.set_info("author", "Steven Huang");
  1018. }
  1019. void MainWindowA::read_settings()
  1020. {
  1021. int value;
  1022. auto values = m_settings.get_general("hidePlayContrl");
  1023. if (values.isValid()) {
  1024. value = values.toInt();
  1025. m_actionHide_Play_Ctronl->setChecked(!!value);
  1026. hide_play_control(value);
  1027. }
  1028. values = m_settings.get_general("fullScreen");
  1029. if (values.isValid()) {
  1030. value = values.toInt();
  1031. m_actionFullscreen->setChecked(!!value);
  1032. show_fullscreen(value);
  1033. }
  1034. values = m_settings.get_general("openDXVA2");
  1035. if (values.isValid()) {
  1036. value = values.toInt();
  1037. m_actionHardware_decode->setChecked(!!value);
  1038. }
  1039. values = m_settings.get_general("loopPlay");
  1040. if (values.isValid()) {
  1041. value = values.toInt();
  1042. m_actionLoop_Play->setChecked(!!value);
  1043. }
  1044. }
  1045. float MainWindowA::volume_settings(bool set, float vol)
  1046. {
  1047. if (set) {
  1048. m_settings.set_general("volume", QString::number(float(vol), 'f', 1));
  1049. } else {
  1050. auto value = 0.2f; // default sound volume
  1051. auto values = m_settings.get_general("volume");
  1052. if (values.isValid())
  1053. value = values.toFloat();
  1054. return value;
  1055. }
  1056. return 0;
  1057. }
  1058. QString MainWindowA::get_selected_style() const
  1059. {
  1060. auto pMenu = m_menuStyle;
  1061. for (auto action : pMenu->actions()) {
  1062. if (!(action->isSeparator() || action->menu())) {
  1063. qDebug("action: %s", qUtf8Printable(action->text()));
  1064. if (action->isChecked())
  1065. return action->data().toString();
  1066. }
  1067. }
  1068. return QString("");
  1069. }
  1070. void MainWindowA::set_style_action(const QString& style)
  1071. {
  1072. auto pMenu = m_menuStyle;
  1073. for (auto action : pMenu->actions()) {
  1074. if (!(action->isSeparator() || action->menu())) {
  1075. if (action->data().toString() == style)
  1076. action->setChecked(true);
  1077. }
  1078. }
  1079. }
  1080. void MainWindowA::audio_data(const AudioData& data)
  1081. {
  1082. if (m_audio_effect_wnd)
  1083. m_audio_effect_wnd->paint_data(data);
  1084. }
  1085. void MainWindowA::update_menus()
  1086. {
  1087. qDebug() << "update_menus: " << m_playerController->isPlaying();
  1088. enable_menus(m_playerController->isPlaying());
  1089. enable_v_menus(m_playerController->playingHasVideo());
  1090. enable_a_menus(m_playerController->playingHasAudio());
  1091. }
  1092. void MainWindowA::enable_menus(bool enable)
  1093. {
  1094. m_actionStop->setEnabled(enable);
  1095. m_actionMedia_Info->setEnabled(enable);
  1096. }
  1097. void MainWindowA::enable_v_menus(bool enable)
  1098. {
  1099. m_actionAspect_Ratio->setEnabled(enable);
  1100. m_actionOriginalSize->setEnabled(enable);
  1101. m_actionHardware_decode->setEnabled(enable);
  1102. for (auto& pAction : m_menuCV->actions()) {
  1103. if (pAction)
  1104. pAction->setEnabled(enable);
  1105. }
  1106. }
  1107. void MainWindowA::enable_a_menus(bool enable)
  1108. {
  1109. m_menuAudioVisualize->setEnabled(enable);
  1110. }
  1111. bool MainWindowA::get_avisual_format(BarHelper::VisualFormat& fmt) const
  1112. {
  1113. if (m_actionLine->isChecked()) {
  1114. fmt.gType = BarHelper::e_GtLine;
  1115. } else if (m_actionBar->isChecked()) {
  1116. fmt.gType = BarHelper::e_GtBar;
  1117. } else if (m_actionPie->isChecked()) {
  1118. fmt.gType = BarHelper::e_GtPie;
  1119. }
  1120. if (m_actionSampling->isChecked()) {
  1121. fmt.vType = BarHelper::e_VtSampleing;
  1122. } else if (m_actionFrequency->isChecked()) {
  1123. fmt.vType = BarHelper::e_VtFrequency;
  1124. }
  1125. return true;
  1126. }
  1127. void MainWindowA::create_playlist_wnd()
  1128. {
  1129. m_playListWnd = std::make_unique<PlayListWnd>(this);
  1130. connect(m_playListWnd.get(),
  1131. &PlayListWnd::play_file,
  1132. m_playerController,
  1133. &PlayerController::startToPlay);
  1134. // connect(m_playListWnd.get(), &PlayListWnd::save_playlist_signal, this, &MainWindowA::save_playlist);
  1135. connect(m_playListWnd.get(), &PlayListWnd::hiden, this, &MainWindowA::playlist_hiden);
  1136. connect(m_playListWnd.get(),
  1137. &PlayListWnd::playlist_file_saved,
  1138. this,
  1139. &MainWindowA::playlist_file_saved);
  1140. }
  1141. void MainWindowA::on_actionPlayList_triggered()
  1142. {
  1143. show_playlist(m_actionPlayList->isChecked());
  1144. }
  1145. void MainWindowA::show_playlist(bool show)
  1146. {
  1147. if (!m_playListWnd)
  1148. return;
  1149. if (show) {
  1150. m_playListWnd->show();
  1151. m_playListWnd->set_cur_palyingfile();
  1152. } else {
  1153. m_playListWnd->hide();
  1154. }
  1155. }
  1156. void MainWindowA::playlist_hiden()
  1157. {
  1158. m_actionPlayList->setChecked(false);
  1159. }
  1160. void MainWindowA::add_to_playlist(const QString& file)
  1161. {
  1162. if (m_playListWnd)
  1163. m_playListWnd->add_file(file);
  1164. }
  1165. QString MainWindowA::get_playingfile() const
  1166. {
  1167. if (m_playerController->isPlaying())
  1168. return m_videoFile;
  1169. return QString("");
  1170. }
  1171. void MainWindowA::on_actionOpenNetworkUrl_triggered()
  1172. {
  1173. NetworkUrlDlg dialog(this);
  1174. if (dialog.exec() == QDialog::Accepted) {
  1175. if (auto url = dialog.get_url(); !url.isEmpty()) {
  1176. m_playerController->startToPlay(url);
  1177. } else {
  1178. show_msg_dlg("Please input a valid youtube url. ");
  1179. }
  1180. }
  1181. }
  1182. void MainWindowA::hide_cursor(bool bHide)
  1183. {
  1184. // if (bHide) {
  1185. // QApplication::setOverrideCursor(Qt::BlankCursor);
  1186. // } else {
  1187. // QGuiApplication::restoreOverrideCursor();
  1188. // }
  1189. }
  1190. bool MainWindowA::cursor_in_window(QWidget* pWnd)
  1191. {
  1192. if (!pWnd)
  1193. return false;
  1194. auto rt = pWnd->rect();
  1195. return rt.contains(pWnd->mapFromGlobal(QCursor::pos()));
  1196. }
  1197. void MainWindowA::create_savedPlaylists_menu()
  1198. {
  1199. for (int i = 0; i < MaxPlaylist; ++i) {
  1200. m_savedPlaylists[i] = std::make_unique<QAction>(this);
  1201. m_savedPlaylists[i]->setVisible(false);
  1202. connect(m_savedPlaylists[i].get(), SIGNAL(triggered()), this, SLOT(open_playlist()));
  1203. }
  1204. m_PlaylistsClear = std::make_unique<QAction>(this);
  1205. m_PlaylistsClear->setText(tr("清除"));
  1206. connect(m_PlaylistsClear.get(), SIGNAL(triggered()), this, SLOT(clear_savedPlaylists()));
  1207. auto pMenu = m_menuSavedPlaylist;
  1208. pMenu->clear();
  1209. for (int i = 0; i < MaxPlaylist; ++i)
  1210. pMenu->addAction(m_savedPlaylists[i].get());
  1211. pMenu->addSeparator();
  1212. pMenu->addAction(m_PlaylistsClear.get());
  1213. update_savedPlaylists_actions();
  1214. }
  1215. void MainWindowA::remove_playlist_file(const QString& fileName)
  1216. {
  1217. auto files = m_settings.get_savedplaylists().toStringList();
  1218. files.removeAll(fileName);
  1219. m_settings.set_savedplaylists(files);
  1220. update_savedPlaylists_actions();
  1221. }
  1222. void MainWindowA::update_savedPlaylists_actions()
  1223. {
  1224. auto files = m_settings.get_savedplaylists().toStringList();
  1225. int num = qMin(files.size(), (int) MaxPlaylist);
  1226. m_menuSavedPlaylist->setEnabled(num > 0);
  1227. for (int i = 0; i < num; ++i) {
  1228. QString text = tr("%1 %2").arg(i + 1).arg(stripped_name(files[i]));
  1229. m_savedPlaylists[i]->setText(
  1230. QApplication::translate("MainWindowA", text.toStdString().c_str(), nullptr));
  1231. m_savedPlaylists[i]->setData(files[i]);
  1232. m_savedPlaylists[i]->setVisible(true);
  1233. }
  1234. for (int j = num; j < MaxPlaylist; ++j)
  1235. m_savedPlaylists[j]->setVisible(false);
  1236. }
  1237. void MainWindowA::clear_savedPlaylists()
  1238. {
  1239. auto files = m_settings.get_savedplaylists().toStringList();
  1240. for (const auto& i : files) {
  1241. QFile file(i);
  1242. file.remove();
  1243. }
  1244. files.clear();
  1245. m_settings.set_savedplaylists(files);
  1246. update_savedPlaylists_actions();
  1247. }
  1248. void MainWindowA::open_playlist()
  1249. {
  1250. if (!m_playListWnd)
  1251. return;
  1252. auto action = qobject_cast<QAction*>(sender());
  1253. if (!action)
  1254. return;
  1255. auto file = action->data().toString();
  1256. QStringList files;
  1257. if (read_playlist(file, files)) {
  1258. m_playListWnd->update_files(files);
  1259. show_playlist();
  1260. m_actionPlayList->setChecked(true);
  1261. } else {
  1262. remove_playlist_file(file);
  1263. }
  1264. }
  1265. void MainWindowA::playlist_file_saved(const QString& file)
  1266. {
  1267. auto files = m_settings.get_savedplaylists().toStringList();
  1268. files.removeAll(file);
  1269. files.prepend(file);
  1270. if (files.size() > MaxPlaylist) {
  1271. show_msg_dlg(QString("You can only save %1 playlist files!").arg(MaxPlaylist));
  1272. }
  1273. while (files.size() > MaxPlaylist)
  1274. files.removeLast();
  1275. m_settings.set_savedplaylists(files);
  1276. update_savedPlaylists_actions();
  1277. }
  1278. bool MainWindowA::read_playlist(const QString& playlist_file, QStringList& files) const
  1279. {
  1280. QFile file(playlist_file);
  1281. if (file.open(QIODevice::ReadOnly)) {
  1282. QTextStream stream(&file);
  1283. stream.setCodec("UTF-8");
  1284. files = stream.readAll().split(PLAYLIST_SEPERATE_CHAR);
  1285. files.removeAll(QString(""));
  1286. file.close();
  1287. return true;
  1288. }
  1289. return false;
  1290. }
  1291. // ========== CV菜单Action统一处理槽 ==========
  1292. void MainWindowA::onCvActionToggled()
  1293. {
  1294. if (!m_video_widget)
  1295. return;
  1296. // 灰度
  1297. m_video_widget->setGray(m_actionGrayscale && m_actionGrayscale->isChecked());
  1298. // 二值化
  1299. m_video_widget->setThreshold(m_actionThreshold && m_actionThreshold->isChecked(), 0.5f);
  1300. // 模糊
  1301. m_video_widget->setBlur(m_actionBlur && m_actionBlur->isChecked(), 1.0f);
  1302. // 反色
  1303. m_video_widget->setReverse(m_actionReverse && m_actionReverse->isChecked());
  1304. // 色彩减少
  1305. m_video_widget->setColorReduce(m_actionColorReduce && m_actionColorReduce->isChecked(), 8);
  1306. // 伽马
  1307. m_video_widget->setGamma(m_actionGamma && m_actionGamma->isChecked(), 1.2f);
  1308. // 对比度/亮度
  1309. m_video_widget->setContrastBright(m_actionContrastBright && m_actionContrastBright->isChecked(),
  1310. 1.2f,
  1311. 0.1f);
  1312. // 镜像
  1313. m_video_widget->setMirror(m_actionMirro && m_actionMirro->isChecked());
  1314. // 其它特效可继续扩展
  1315. }
  1316. // 新增槽函数实现
  1317. void MainWindowA::onShowMessage(const QString& message,
  1318. const QString& windowTitle,
  1319. const QString& styleSheet)
  1320. {
  1321. QMessageBox msgBox;
  1322. msgBox.setText(message);
  1323. msgBox.setWindowTitle(windowTitle);
  1324. msgBox.setStyleSheet(styleSheet);
  1325. msgBox.show();
  1326. msgBox.move(frameGeometry().center() - msgBox.rect().center());
  1327. msgBox.setWindowFlags(msgBox.windowFlags() | Qt::Dialog);
  1328. msgBox.setModal(true);
  1329. msgBox.exec();
  1330. }
  1331. void MainWindowA::onRequestFullscreen(bool bFullscreen)
  1332. {
  1333. if (bFullscreen) {
  1334. showFullScreen();
  1335. } else {
  1336. showNormal();
  1337. update_video_label();
  1338. }
  1339. hide_cursor(bFullscreen);
  1340. }
  1341. void MainWindowA::onRequestHideStatusBar(bool bHide)
  1342. {
  1343. statusBar()->setVisible(!bHide);
  1344. auto sz_status = statusBar()->size();
  1345. if (isFullScreen()) {
  1346. centralWidget()->resize(centralWidget()->size());
  1347. } else {
  1348. auto sz = size();
  1349. if (statusBar()->isVisible()) {
  1350. sz += QSize(0, sz_status.height());
  1351. } else {
  1352. sz -= QSize(0, sz_status.height());
  1353. }
  1354. resize(sz);
  1355. }
  1356. }