mainwindowa.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. auto widget = get_video_widget();
  955. widget->Render(frame);
  956. }
  957. void MainWindowA::subtitle_ready(const QString& text)
  958. {
  959. set_subtitle(text);
  960. }
  961. void MainWindowA::set_subtitle(const QString& str)
  962. {
  963. m_subtitle = str;
  964. qDebug() << "subtitle received:" << m_subtitle;
  965. }
  966. void MainWindowA::clear_subtitle_str()
  967. {
  968. set_subtitle("");
  969. }
  970. void MainWindowA::displayStatusMessage(const QString& message)
  971. {
  972. int timeout = 0; // 5000: 5 second timeout
  973. if (auto bar = statusBar())
  974. bar->showMessage(message, timeout);
  975. }
  976. void MainWindowA::print_decodeContext(const AVCodecContext* pDecodeCtx, bool bVideo) const
  977. {
  978. if (!pDecodeCtx)
  979. return;
  980. if (bVideo) {
  981. qInfo("video codec_name: %s", pDecodeCtx->codec->name);
  982. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  983. pDecodeCtx->codec_type,
  984. pDecodeCtx->codec_id,
  985. pDecodeCtx->codec_tag);
  986. qInfo("width: %d, height: %d, codec_tag: %d", pDecodeCtx->width, pDecodeCtx->height);
  987. } else {
  988. qInfo("audio codec_name: %s", pDecodeCtx->codec->name);
  989. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  990. pDecodeCtx->codec_type,
  991. pDecodeCtx->codec_id,
  992. pDecodeCtx->codec_tag);
  993. qInfo("sample_rate: %d, channels: %d, sample_fmt: %d",
  994. pDecodeCtx->sample_rate,
  995. pDecodeCtx->ch_layout.nb_channels,
  996. pDecodeCtx->sample_fmt);
  997. qInfo("frame_size: %d, frame_number: %d, block_align: %d",
  998. pDecodeCtx->frame_size,
  999. pDecodeCtx->frame_num,
  1000. pDecodeCtx->block_align);
  1001. }
  1002. }
  1003. void MainWindowA::save_settings()
  1004. {
  1005. auto res = m_actionHide_Play_Ctronl->isChecked();
  1006. m_settings.set_general("hidePlayContrl", int(res));
  1007. res = m_actionFullscreen->isChecked();
  1008. m_settings.set_general("fullScreen", int(res));
  1009. res = m_actionHardware_decode->isChecked();
  1010. m_settings.set_general("openDXVA2", int(res));
  1011. res = m_actionLoop_Play->isChecked();
  1012. m_settings.set_general("loopPlay", int(res));
  1013. m_settings.set_general("style", get_selected_style());
  1014. m_settings.set_info("software", "Video player");
  1015. //m_settings.set_info("version", PLAYER_VERSION);
  1016. m_settings.set_info("author", "Steven Huang");
  1017. }
  1018. void MainWindowA::read_settings()
  1019. {
  1020. int value;
  1021. auto values = m_settings.get_general("hidePlayContrl");
  1022. if (values.isValid()) {
  1023. value = values.toInt();
  1024. m_actionHide_Play_Ctronl->setChecked(!!value);
  1025. hide_play_control(value);
  1026. }
  1027. values = m_settings.get_general("fullScreen");
  1028. if (values.isValid()) {
  1029. value = values.toInt();
  1030. m_actionFullscreen->setChecked(!!value);
  1031. show_fullscreen(value);
  1032. }
  1033. values = m_settings.get_general("openDXVA2");
  1034. if (values.isValid()) {
  1035. value = values.toInt();
  1036. m_actionHardware_decode->setChecked(!!value);
  1037. }
  1038. values = m_settings.get_general("loopPlay");
  1039. if (values.isValid()) {
  1040. value = values.toInt();
  1041. m_actionLoop_Play->setChecked(!!value);
  1042. }
  1043. }
  1044. float MainWindowA::volume_settings(bool set, float vol)
  1045. {
  1046. if (set) {
  1047. m_settings.set_general("volume", QString::number(float(vol), 'f', 1));
  1048. } else {
  1049. auto value = 0.2f; // default sound volume
  1050. auto values = m_settings.get_general("volume");
  1051. if (values.isValid())
  1052. value = values.toFloat();
  1053. return value;
  1054. }
  1055. return 0;
  1056. }
  1057. QString MainWindowA::get_selected_style() const
  1058. {
  1059. auto pMenu = m_menuStyle;
  1060. for (auto action : pMenu->actions()) {
  1061. if (!(action->isSeparator() || action->menu())) {
  1062. qDebug("action: %s", qUtf8Printable(action->text()));
  1063. if (action->isChecked())
  1064. return action->data().toString();
  1065. }
  1066. }
  1067. return QString("");
  1068. }
  1069. void MainWindowA::set_style_action(const QString& style)
  1070. {
  1071. auto pMenu = m_menuStyle;
  1072. for (auto action : pMenu->actions()) {
  1073. if (!(action->isSeparator() || action->menu())) {
  1074. if (action->data().toString() == style)
  1075. action->setChecked(true);
  1076. }
  1077. }
  1078. }
  1079. void MainWindowA::audio_data(const AudioData& data)
  1080. {
  1081. if (m_audio_effect_wnd)
  1082. m_audio_effect_wnd->paint_data(data);
  1083. }
  1084. void MainWindowA::update_menus()
  1085. {
  1086. qDebug() << "update_menus: " << m_playerController->isPlaying();
  1087. enable_menus(m_playerController->isPlaying());
  1088. enable_v_menus(m_playerController->playingHasVideo());
  1089. enable_a_menus(m_playerController->playingHasAudio());
  1090. }
  1091. void MainWindowA::enable_menus(bool enable)
  1092. {
  1093. m_actionStop->setEnabled(enable);
  1094. m_actionMedia_Info->setEnabled(enable);
  1095. }
  1096. void MainWindowA::enable_v_menus(bool enable)
  1097. {
  1098. m_actionAspect_Ratio->setEnabled(enable);
  1099. m_actionOriginalSize->setEnabled(enable);
  1100. m_actionHardware_decode->setEnabled(enable);
  1101. for (auto& pAction : m_menuCV->actions()) {
  1102. if (pAction)
  1103. pAction->setEnabled(enable);
  1104. }
  1105. }
  1106. void MainWindowA::enable_a_menus(bool enable)
  1107. {
  1108. m_menuAudioVisualize->setEnabled(enable);
  1109. }
  1110. bool MainWindowA::get_avisual_format(BarHelper::VisualFormat& fmt) const
  1111. {
  1112. if (m_actionLine->isChecked()) {
  1113. fmt.gType = BarHelper::e_GtLine;
  1114. } else if (m_actionBar->isChecked()) {
  1115. fmt.gType = BarHelper::e_GtBar;
  1116. } else if (m_actionPie->isChecked()) {
  1117. fmt.gType = BarHelper::e_GtPie;
  1118. }
  1119. if (m_actionSampling->isChecked()) {
  1120. fmt.vType = BarHelper::e_VtSampleing;
  1121. } else if (m_actionFrequency->isChecked()) {
  1122. fmt.vType = BarHelper::e_VtFrequency;
  1123. }
  1124. return true;
  1125. }
  1126. void MainWindowA::create_playlist_wnd()
  1127. {
  1128. m_playListWnd = std::make_unique<PlayListWnd>(this);
  1129. connect(m_playListWnd.get(),
  1130. &PlayListWnd::play_file,
  1131. m_playerController,
  1132. &PlayerController::startToPlay);
  1133. // connect(m_playListWnd.get(), &PlayListWnd::save_playlist_signal, this, &MainWindowA::save_playlist);
  1134. connect(m_playListWnd.get(), &PlayListWnd::hiden, this, &MainWindowA::playlist_hiden);
  1135. connect(m_playListWnd.get(),
  1136. &PlayListWnd::playlist_file_saved,
  1137. this,
  1138. &MainWindowA::playlist_file_saved);
  1139. }
  1140. void MainWindowA::on_actionPlayList_triggered()
  1141. {
  1142. show_playlist(m_actionPlayList->isChecked());
  1143. }
  1144. void MainWindowA::show_playlist(bool show)
  1145. {
  1146. if (!m_playListWnd)
  1147. return;
  1148. if (show) {
  1149. m_playListWnd->show();
  1150. m_playListWnd->set_cur_palyingfile();
  1151. } else {
  1152. m_playListWnd->hide();
  1153. }
  1154. }
  1155. void MainWindowA::playlist_hiden()
  1156. {
  1157. m_actionPlayList->setChecked(false);
  1158. }
  1159. void MainWindowA::add_to_playlist(const QString& file)
  1160. {
  1161. if (m_playListWnd)
  1162. m_playListWnd->add_file(file);
  1163. }
  1164. QString MainWindowA::get_playingfile() const
  1165. {
  1166. if (m_playerController->isPlaying())
  1167. return m_videoFile;
  1168. return QString("");
  1169. }
  1170. void MainWindowA::on_actionOpenNetworkUrl_triggered()
  1171. {
  1172. NetworkUrlDlg dialog(this);
  1173. if (dialog.exec() == QDialog::Accepted) {
  1174. if (auto url = dialog.get_url(); !url.isEmpty()) {
  1175. m_playerController->startToPlay(url);
  1176. } else {
  1177. show_msg_dlg("Please input a valid youtube url. ");
  1178. }
  1179. }
  1180. }
  1181. void MainWindowA::hide_cursor(bool bHide)
  1182. {
  1183. // if (bHide) {
  1184. // QApplication::setOverrideCursor(Qt::BlankCursor);
  1185. // } else {
  1186. // QGuiApplication::restoreOverrideCursor();
  1187. // }
  1188. }
  1189. bool MainWindowA::cursor_in_window(QWidget* pWnd)
  1190. {
  1191. if (!pWnd)
  1192. return false;
  1193. auto rt = pWnd->rect();
  1194. return rt.contains(pWnd->mapFromGlobal(QCursor::pos()));
  1195. }
  1196. void MainWindowA::create_savedPlaylists_menu()
  1197. {
  1198. for (int i = 0; i < MaxPlaylist; ++i) {
  1199. m_savedPlaylists[i] = std::make_unique<QAction>(this);
  1200. m_savedPlaylists[i]->setVisible(false);
  1201. connect(m_savedPlaylists[i].get(), SIGNAL(triggered()), this, SLOT(open_playlist()));
  1202. }
  1203. m_PlaylistsClear = std::make_unique<QAction>(this);
  1204. m_PlaylistsClear->setText(tr("清除"));
  1205. connect(m_PlaylistsClear.get(), SIGNAL(triggered()), this, SLOT(clear_savedPlaylists()));
  1206. auto pMenu = m_menuSavedPlaylist;
  1207. pMenu->clear();
  1208. for (int i = 0; i < MaxPlaylist; ++i)
  1209. pMenu->addAction(m_savedPlaylists[i].get());
  1210. pMenu->addSeparator();
  1211. pMenu->addAction(m_PlaylistsClear.get());
  1212. update_savedPlaylists_actions();
  1213. }
  1214. void MainWindowA::remove_playlist_file(const QString& fileName)
  1215. {
  1216. auto files = m_settings.get_savedplaylists().toStringList();
  1217. files.removeAll(fileName);
  1218. m_settings.set_savedplaylists(files);
  1219. update_savedPlaylists_actions();
  1220. }
  1221. void MainWindowA::update_savedPlaylists_actions()
  1222. {
  1223. auto files = m_settings.get_savedplaylists().toStringList();
  1224. int num = qMin(files.size(), (int) MaxPlaylist);
  1225. m_menuSavedPlaylist->setEnabled(num > 0);
  1226. for (int i = 0; i < num; ++i) {
  1227. QString text = tr("%1 %2").arg(i + 1).arg(stripped_name(files[i]));
  1228. m_savedPlaylists[i]->setText(
  1229. QApplication::translate("MainWindowA", text.toStdString().c_str(), nullptr));
  1230. m_savedPlaylists[i]->setData(files[i]);
  1231. m_savedPlaylists[i]->setVisible(true);
  1232. }
  1233. for (int j = num; j < MaxPlaylist; ++j)
  1234. m_savedPlaylists[j]->setVisible(false);
  1235. }
  1236. void MainWindowA::clear_savedPlaylists()
  1237. {
  1238. auto files = m_settings.get_savedplaylists().toStringList();
  1239. for (const auto& i : files) {
  1240. QFile file(i);
  1241. file.remove();
  1242. }
  1243. files.clear();
  1244. m_settings.set_savedplaylists(files);
  1245. update_savedPlaylists_actions();
  1246. }
  1247. void MainWindowA::open_playlist()
  1248. {
  1249. if (!m_playListWnd)
  1250. return;
  1251. auto action = qobject_cast<QAction*>(sender());
  1252. if (!action)
  1253. return;
  1254. auto file = action->data().toString();
  1255. QStringList files;
  1256. if (read_playlist(file, files)) {
  1257. m_playListWnd->update_files(files);
  1258. show_playlist();
  1259. m_actionPlayList->setChecked(true);
  1260. } else {
  1261. remove_playlist_file(file);
  1262. }
  1263. }
  1264. void MainWindowA::playlist_file_saved(const QString& file)
  1265. {
  1266. auto files = m_settings.get_savedplaylists().toStringList();
  1267. files.removeAll(file);
  1268. files.prepend(file);
  1269. if (files.size() > MaxPlaylist) {
  1270. show_msg_dlg(QString("You can only save %1 playlist files!").arg(MaxPlaylist));
  1271. }
  1272. while (files.size() > MaxPlaylist)
  1273. files.removeLast();
  1274. m_settings.set_savedplaylists(files);
  1275. update_savedPlaylists_actions();
  1276. }
  1277. bool MainWindowA::read_playlist(const QString& playlist_file, QStringList& files) const
  1278. {
  1279. QFile file(playlist_file);
  1280. if (file.open(QIODevice::ReadOnly)) {
  1281. QTextStream stream(&file);
  1282. stream.setCodec("UTF-8");
  1283. files = stream.readAll().split(PLAYLIST_SEPERATE_CHAR);
  1284. files.removeAll(QString(""));
  1285. file.close();
  1286. return true;
  1287. }
  1288. return false;
  1289. }
  1290. // ========== CV菜单Action统一处理槽 ==========
  1291. void MainWindowA::onCvActionToggled()
  1292. {
  1293. if (!m_video_widget)
  1294. return;
  1295. // 灰度
  1296. m_video_widget->setGray(m_actionGrayscale && m_actionGrayscale->isChecked());
  1297. // 二值化
  1298. m_video_widget->setThreshold(m_actionThreshold && m_actionThreshold->isChecked(), 0.5f);
  1299. // 模糊
  1300. m_video_widget->setBlur(m_actionBlur && m_actionBlur->isChecked(), 1.0f);
  1301. // 反色
  1302. m_video_widget->setReverse(m_actionReverse && m_actionReverse->isChecked());
  1303. // 色彩减少
  1304. m_video_widget->setColorReduce(m_actionColorReduce && m_actionColorReduce->isChecked(), 8);
  1305. // 伽马
  1306. m_video_widget->setGamma(m_actionGamma && m_actionGamma->isChecked(), 1.2f);
  1307. // 对比度/亮度
  1308. m_video_widget->setContrastBright(m_actionContrastBright && m_actionContrastBright->isChecked(),
  1309. 1.2f,
  1310. 0.1f);
  1311. // 镜像
  1312. m_video_widget->setMirror(m_actionMirro && m_actionMirro->isChecked());
  1313. // 其它特效可继续扩展
  1314. }
  1315. // 新增槽函数实现
  1316. void MainWindowA::onShowMessage(const QString& message,
  1317. const QString& windowTitle,
  1318. const QString& styleSheet)
  1319. {
  1320. QMessageBox msgBox;
  1321. msgBox.setText(message);
  1322. msgBox.setWindowTitle(windowTitle);
  1323. msgBox.setStyleSheet(styleSheet);
  1324. msgBox.show();
  1325. msgBox.move(frameGeometry().center() - msgBox.rect().center());
  1326. msgBox.setWindowFlags(msgBox.windowFlags() | Qt::Dialog);
  1327. msgBox.setModal(true);
  1328. msgBox.exec();
  1329. }
  1330. void MainWindowA::onRequestFullscreen(bool bFullscreen)
  1331. {
  1332. if (bFullscreen) {
  1333. showFullScreen();
  1334. } else {
  1335. showNormal();
  1336. update_video_label();
  1337. }
  1338. hide_cursor(bFullscreen);
  1339. }
  1340. void MainWindowA::onRequestHideStatusBar(bool bHide)
  1341. {
  1342. statusBar()->setVisible(!bHide);
  1343. auto sz_status = statusBar()->size();
  1344. if (isFullScreen()) {
  1345. centralWidget()->resize(centralWidget()->size());
  1346. } else {
  1347. auto sz = size();
  1348. if (statusBar()->isVisible()) {
  1349. sz += QSize(0, sz_status.height());
  1350. } else {
  1351. sz -= QSize(0, sz_status.height());
  1352. }
  1353. resize(sz);
  1354. }
  1355. }