mainwindowa.cpp 48 KB

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