mainwindowa.cpp 49 KB

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