mainwindowa.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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/1.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::video_seek_inc(double incr) // incr seconds
  840. {
  841. auto pState = m_playerController->state();
  842. if (!pState)
  843. return;
  844. auto pos = get_master_clock(pState);
  845. if (isnan(pos))
  846. pos = (double) pState->seek_pos / AV_TIME_BASE;
  847. qDebug("!seek_by_bytes pos=%lf", pos);
  848. pos += incr;
  849. m_playerController->videoSeek(pos, incr);
  850. }
  851. void MainWindowA::play_seek()
  852. {
  853. if (auto pPlayControl = get_play_control()) {
  854. auto maxValue = pPlayControl->get_progress_slider_max();
  855. auto total_time = pPlayControl->get_total_time();
  856. auto value = pPlayControl->get_progress_slider_value();
  857. double seek_time = 0;
  858. if (maxValue > 0)
  859. seek_time = value * total_time * 1.0 / maxValue;
  860. qDebug() << "val:" << value << ",maxVal:" << maxValue << ",total time" << total_time
  861. << ",seek time:" << seek_time;
  862. m_playerController->videoSeek(seek_time);
  863. }
  864. update_paly_control_status();
  865. }
  866. void MainWindowA::play_mute(bool mute)
  867. {
  868. auto pState = m_playerController->state();
  869. if (pState)
  870. toggle_mute(pState, mute);
  871. }
  872. void MainWindowA::set_volume(int volume)
  873. {
  874. auto pPlayControl = get_play_control();
  875. if (!pPlayControl)
  876. return;
  877. auto max_val = pPlayControl->get_volum_slider_max();
  878. auto vol = volume * 1.0 / max_val;
  879. m_playerController->setDeviceVolume(vol);
  880. volume_settings(true, vol);
  881. }
  882. void MainWindowA::set_play_speed()
  883. {
  884. auto pPlayControl = get_play_control();
  885. if (!pPlayControl)
  886. return;
  887. auto speed = pPlayControl->get_speed();
  888. // qDebug("set_play_spped, speed control changed, speed:%d", speed);
  889. if (m_playerController) {
  890. if (auto pState = m_playerController->state()) {
  891. #if USE_AVFILTER_AUDIO
  892. set_audio_playspeed(pState, speed);
  893. #endif
  894. }
  895. }
  896. }
  897. void MainWindowA::play_speed_adjust(bool up)
  898. {
  899. if (auto pPlayControl = get_play_control())
  900. pPlayControl->speed_adjust(up);
  901. set_play_speed();
  902. }
  903. void MainWindowA::hide_statusbar(bool bHide)
  904. {
  905. onRequestHideStatusBar(bHide);
  906. }
  907. void MainWindowA::hide_menubar(bool bHide)
  908. {
  909. menuBar()->setVisible(!bHide);
  910. // qDebug("is full screen:%d, menu is visible:%d", isFullScreen(), bVisible);
  911. if (isFullScreen()) {
  912. centralWidget()->resize(centralWidget()->size());
  913. }
  914. update_play_control();
  915. }
  916. void MainWindowA::on_actionAbout_triggered() {}
  917. void MainWindowA::play_failed(const QString& file)
  918. {
  919. show_msg_dlg(QString("File play failed, file: %1").arg(toNativePath(file)));
  920. }
  921. void MainWindowA::play_start_seek()
  922. {
  923. play_seek();
  924. m_playerController->pausePlay();
  925. }
  926. void MainWindowA::play_control_key(Qt::Key key)
  927. {
  928. if (!m_playerController)
  929. return;
  930. auto pState = m_playerController->state();
  931. if (!pState)
  932. return;
  933. switch (key) {
  934. case Qt::Key_Space: // pause/continue
  935. m_playerController->pausePlay();
  936. break;
  937. case Qt::Key_M:
  938. toggle_mute(pState, !pState->muted);
  939. update_paly_control_muted();
  940. break;
  941. case Qt::Key_Up: // volume
  942. set_volume_updown(true);
  943. break;
  944. case Qt::Key_Down: // volume
  945. set_volume_updown(false);
  946. break;
  947. case Qt::Key_Left:
  948. m_playerController->playSeekPre();
  949. break;
  950. case Qt::Key_Right:
  951. m_playerController->playSeekNext();
  952. break;
  953. case Qt::Key_Comma:
  954. play_speed_adjust(false);
  955. break;
  956. case Qt::Key_Period:
  957. play_speed_adjust(true);
  958. break;
  959. default:
  960. qDebug("key:(%d) pressed, not handled!\n", key);
  961. break;
  962. }
  963. }
  964. void MainWindowA::frameReady(AVFrame* frame)
  965. {
  966. auto widget = get_video_widget();
  967. widget->Render(frame);
  968. }
  969. void MainWindowA::subtitle_ready(const QString& text)
  970. {
  971. set_subtitle(text);
  972. }
  973. void MainWindowA::set_subtitle(const QString& str)
  974. {
  975. m_subtitle = str;
  976. qDebug() << "subtitle received:" << m_subtitle;
  977. }
  978. void MainWindowA::clear_subtitle_str()
  979. {
  980. set_subtitle("");
  981. }
  982. void MainWindowA::displayStatusMessage(const QString& message)
  983. {
  984. int timeout = 0; // 5000: 5 second timeout
  985. if (auto bar = statusBar())
  986. bar->showMessage(message, timeout);
  987. }
  988. void MainWindowA::print_decodeContext(const AVCodecContext* pDecodeCtx, bool bVideo) const
  989. {
  990. if (!pDecodeCtx)
  991. return;
  992. if (bVideo) {
  993. qInfo("video codec_name: %s", pDecodeCtx->codec->name);
  994. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  995. pDecodeCtx->codec_type,
  996. pDecodeCtx->codec_id,
  997. pDecodeCtx->codec_tag);
  998. qInfo("width: %d, height: %d, codec_tag: %d", pDecodeCtx->width, pDecodeCtx->height);
  999. } else {
  1000. qInfo("audio codec_name: %s", pDecodeCtx->codec->name);
  1001. qInfo("codec_type: %d, codec_id: %d, codec_tag: %d",
  1002. pDecodeCtx->codec_type,
  1003. pDecodeCtx->codec_id,
  1004. pDecodeCtx->codec_tag);
  1005. qInfo("sample_rate: %d, channels: %d, sample_fmt: %d",
  1006. pDecodeCtx->sample_rate,
  1007. pDecodeCtx->ch_layout.nb_channels,
  1008. pDecodeCtx->sample_fmt);
  1009. qInfo("frame_size: %d, frame_number: %d, block_align: %d",
  1010. pDecodeCtx->frame_size,
  1011. pDecodeCtx->frame_num,
  1012. pDecodeCtx->block_align);
  1013. }
  1014. }
  1015. void MainWindowA::save_settings()
  1016. {
  1017. auto res = m_actionHide_Play_Ctronl->isChecked();
  1018. m_settings.set_general("hidePlayContrl", int(res));
  1019. res = m_actionFullscreen->isChecked();
  1020. m_settings.set_general("fullScreen", int(res));
  1021. res = m_actionHardware_decode->isChecked();
  1022. m_settings.set_general("openDXVA2", int(res));
  1023. res = m_actionLoop_Play->isChecked();
  1024. m_settings.set_general("loopPlay", int(res));
  1025. m_settings.set_general("style", get_selected_style());
  1026. m_settings.set_info("software", "Video player");
  1027. //m_settings.set_info("version", PLAYER_VERSION);
  1028. m_settings.set_info("author", "Steven Huang");
  1029. }
  1030. void MainWindowA::read_settings()
  1031. {
  1032. int value;
  1033. auto values = m_settings.get_general("hidePlayContrl");
  1034. if (values.isValid()) {
  1035. value = values.toInt();
  1036. m_actionHide_Play_Ctronl->setChecked(!!value);
  1037. hide_play_control(value);
  1038. }
  1039. values = m_settings.get_general("fullScreen");
  1040. if (values.isValid()) {
  1041. value = values.toInt();
  1042. m_actionFullscreen->setChecked(!!value);
  1043. show_fullscreen(value);
  1044. }
  1045. values = m_settings.get_general("openDXVA2");
  1046. if (values.isValid()) {
  1047. value = values.toInt();
  1048. m_actionHardware_decode->setChecked(!!value);
  1049. }
  1050. values = m_settings.get_general("loopPlay");
  1051. if (values.isValid()) {
  1052. value = values.toInt();
  1053. m_actionLoop_Play->setChecked(!!value);
  1054. }
  1055. }
  1056. float MainWindowA::volume_settings(bool set, float vol)
  1057. {
  1058. if (set) {
  1059. m_settings.set_general("volume", QString::number(float(vol), 'f', 1));
  1060. } else {
  1061. auto value = 0.2f; // default sound volume
  1062. auto values = m_settings.get_general("volume");
  1063. if (values.isValid())
  1064. value = values.toFloat();
  1065. return value;
  1066. }
  1067. return 0;
  1068. }
  1069. QString MainWindowA::get_selected_style() const
  1070. {
  1071. auto pMenu = m_menuStyle;
  1072. for (auto action : pMenu->actions()) {
  1073. if (!(action->isSeparator() || action->menu())) {
  1074. qDebug("action: %s", qUtf8Printable(action->text()));
  1075. if (action->isChecked())
  1076. return action->data().toString();
  1077. }
  1078. }
  1079. return QString("");
  1080. }
  1081. void MainWindowA::set_style_action(const QString& style)
  1082. {
  1083. auto pMenu = m_menuStyle;
  1084. for (auto action : pMenu->actions()) {
  1085. if (!(action->isSeparator() || action->menu())) {
  1086. if (action->data().toString() == style)
  1087. action->setChecked(true);
  1088. }
  1089. }
  1090. }
  1091. void MainWindowA::audio_data(const AudioData& data)
  1092. {
  1093. if (m_audio_effect_wnd)
  1094. m_audio_effect_wnd->paint_data(data);
  1095. }
  1096. void MainWindowA::update_menus()
  1097. {
  1098. qDebug() << "update_menus: " << m_playerController->isPlaying();
  1099. enable_menus(m_playerController->isPlaying());
  1100. enable_v_menus(m_playerController->playingHasVideo());
  1101. enable_a_menus(m_playerController->playingHasAudio());
  1102. }
  1103. void MainWindowA::enable_menus(bool enable)
  1104. {
  1105. m_actionStop->setEnabled(enable);
  1106. m_actionMedia_Info->setEnabled(enable);
  1107. }
  1108. void MainWindowA::enable_v_menus(bool enable)
  1109. {
  1110. m_actionAspect_Ratio->setEnabled(enable);
  1111. m_actionOriginalSize->setEnabled(enable);
  1112. m_actionHardware_decode->setEnabled(enable);
  1113. for (auto& pAction : m_menuCV->actions()) {
  1114. if (pAction)
  1115. pAction->setEnabled(enable);
  1116. }
  1117. }
  1118. void MainWindowA::enable_a_menus(bool enable)
  1119. {
  1120. m_menuAudioVisualize->setEnabled(enable);
  1121. }
  1122. bool MainWindowA::get_avisual_format(BarHelper::VisualFormat& fmt) const
  1123. {
  1124. if (m_actionLine->isChecked()) {
  1125. fmt.gType = BarHelper::e_GtLine;
  1126. } else if (m_actionBar->isChecked()) {
  1127. fmt.gType = BarHelper::e_GtBar;
  1128. } else if (m_actionPie->isChecked()) {
  1129. fmt.gType = BarHelper::e_GtPie;
  1130. }
  1131. if (m_actionSampling->isChecked()) {
  1132. fmt.vType = BarHelper::e_VtSampleing;
  1133. } else if (m_actionFrequency->isChecked()) {
  1134. fmt.vType = BarHelper::e_VtFrequency;
  1135. }
  1136. return true;
  1137. }
  1138. void MainWindowA::create_playlist_wnd()
  1139. {
  1140. m_playListWnd = std::make_unique<PlayListWnd>(this);
  1141. connect(m_playListWnd.get(),
  1142. &PlayListWnd::play_file,
  1143. m_playerController,
  1144. &PlayerController::startToPlay);
  1145. // connect(m_playListWnd.get(), &PlayListWnd::save_playlist_signal, this, &MainWindowA::save_playlist);
  1146. connect(m_playListWnd.get(), &PlayListWnd::hiden, this, &MainWindowA::playlist_hiden);
  1147. connect(m_playListWnd.get(),
  1148. &PlayListWnd::playlist_file_saved,
  1149. this,
  1150. &MainWindowA::playlist_file_saved);
  1151. }
  1152. void MainWindowA::on_actionPlayList_triggered()
  1153. {
  1154. show_playlist(m_actionPlayList->isChecked());
  1155. }
  1156. void MainWindowA::show_playlist(bool show)
  1157. {
  1158. if (!m_playListWnd)
  1159. return;
  1160. if (show) {
  1161. m_playListWnd->show();
  1162. m_playListWnd->set_cur_palyingfile();
  1163. } else {
  1164. m_playListWnd->hide();
  1165. }
  1166. }
  1167. void MainWindowA::playlist_hiden()
  1168. {
  1169. m_actionPlayList->setChecked(false);
  1170. }
  1171. void MainWindowA::add_to_playlist(const QString& file)
  1172. {
  1173. if (m_playListWnd)
  1174. m_playListWnd->add_file(file);
  1175. }
  1176. QString MainWindowA::get_playingfile() const
  1177. {
  1178. if (m_playerController->isPlaying())
  1179. return m_videoFile;
  1180. return QString("");
  1181. }
  1182. void MainWindowA::on_actionOpenNetworkUrl_triggered()
  1183. {
  1184. NetworkUrlDlg dialog(this);
  1185. if (dialog.exec() == QDialog::Accepted) {
  1186. if (auto url = dialog.get_url(); !url.isEmpty()) {
  1187. m_playerController->startToPlay(url);
  1188. } else {
  1189. show_msg_dlg("Please input a valid youtube url. ");
  1190. }
  1191. }
  1192. }
  1193. void MainWindowA::hide_cursor(bool bHide)
  1194. {
  1195. // if (bHide) {
  1196. // QApplication::setOverrideCursor(Qt::BlankCursor);
  1197. // } else {
  1198. // QGuiApplication::restoreOverrideCursor();
  1199. // }
  1200. }
  1201. bool MainWindowA::cursor_in_window(QWidget* pWnd)
  1202. {
  1203. if (!pWnd)
  1204. return false;
  1205. auto rt = pWnd->rect();
  1206. return rt.contains(pWnd->mapFromGlobal(QCursor::pos()));
  1207. }
  1208. void MainWindowA::create_savedPlaylists_menu()
  1209. {
  1210. for (int i = 0; i < MaxPlaylist; ++i) {
  1211. m_savedPlaylists[i] = std::make_unique<QAction>(this);
  1212. m_savedPlaylists[i]->setVisible(false);
  1213. connect(m_savedPlaylists[i].get(), SIGNAL(triggered()), this, SLOT(open_playlist()));
  1214. }
  1215. m_PlaylistsClear = std::make_unique<QAction>(this);
  1216. m_PlaylistsClear->setText(tr("清除"));
  1217. connect(m_PlaylistsClear.get(), SIGNAL(triggered()), this, SLOT(clear_savedPlaylists()));
  1218. auto pMenu = m_menuSavedPlaylist;
  1219. pMenu->clear();
  1220. for (int i = 0; i < MaxPlaylist; ++i)
  1221. pMenu->addAction(m_savedPlaylists[i].get());
  1222. pMenu->addSeparator();
  1223. pMenu->addAction(m_PlaylistsClear.get());
  1224. update_savedPlaylists_actions();
  1225. }
  1226. void MainWindowA::remove_playlist_file(const QString& fileName)
  1227. {
  1228. auto files = m_settings.get_savedplaylists().toStringList();
  1229. files.removeAll(fileName);
  1230. m_settings.set_savedplaylists(files);
  1231. update_savedPlaylists_actions();
  1232. }
  1233. void MainWindowA::update_savedPlaylists_actions()
  1234. {
  1235. auto files = m_settings.get_savedplaylists().toStringList();
  1236. int num = qMin(files.size(), (int) MaxPlaylist);
  1237. m_menuSavedPlaylist->setEnabled(num > 0);
  1238. for (int i = 0; i < num; ++i) {
  1239. QString text = tr("%1 %2").arg(i + 1).arg(stripped_name(files[i]));
  1240. m_savedPlaylists[i]->setText(
  1241. QApplication::translate("MainWindowA", text.toStdString().c_str(), nullptr));
  1242. m_savedPlaylists[i]->setData(files[i]);
  1243. m_savedPlaylists[i]->setVisible(true);
  1244. }
  1245. for (int j = num; j < MaxPlaylist; ++j)
  1246. m_savedPlaylists[j]->setVisible(false);
  1247. }
  1248. void MainWindowA::clear_savedPlaylists()
  1249. {
  1250. auto files = m_settings.get_savedplaylists().toStringList();
  1251. for (const auto& i : files) {
  1252. QFile file(i);
  1253. file.remove();
  1254. }
  1255. files.clear();
  1256. m_settings.set_savedplaylists(files);
  1257. update_savedPlaylists_actions();
  1258. }
  1259. void MainWindowA::open_playlist()
  1260. {
  1261. if (!m_playListWnd)
  1262. return;
  1263. auto action = qobject_cast<QAction*>(sender());
  1264. if (!action)
  1265. return;
  1266. auto file = action->data().toString();
  1267. QStringList files;
  1268. if (read_playlist(file, files)) {
  1269. m_playListWnd->update_files(files);
  1270. show_playlist();
  1271. m_actionPlayList->setChecked(true);
  1272. } else {
  1273. remove_playlist_file(file);
  1274. }
  1275. }
  1276. void MainWindowA::playlist_file_saved(const QString& file)
  1277. {
  1278. auto files = m_settings.get_savedplaylists().toStringList();
  1279. files.removeAll(file);
  1280. files.prepend(file);
  1281. if (files.size() > MaxPlaylist) {
  1282. show_msg_dlg(QString("You can only save %1 playlist files!").arg(MaxPlaylist));
  1283. }
  1284. while (files.size() > MaxPlaylist)
  1285. files.removeLast();
  1286. m_settings.set_savedplaylists(files);
  1287. update_savedPlaylists_actions();
  1288. }
  1289. bool MainWindowA::read_playlist(const QString& playlist_file, QStringList& files) const
  1290. {
  1291. QFile file(playlist_file);
  1292. if (file.open(QIODevice::ReadOnly)) {
  1293. QTextStream stream(&file);
  1294. stream.setCodec("UTF-8");
  1295. files = stream.readAll().split(PLAYLIST_SEPERATE_CHAR);
  1296. files.removeAll(QString(""));
  1297. file.close();
  1298. return true;
  1299. }
  1300. return false;
  1301. }
  1302. // ========== CV菜单Action统一处理槽 ==========
  1303. void MainWindowA::onCvActionToggled()
  1304. {
  1305. if (!m_video_widget)
  1306. return;
  1307. // 灰度
  1308. m_video_widget->setGray(m_actionGrayscale && m_actionGrayscale->isChecked());
  1309. // 二值化
  1310. m_video_widget->setThreshold(m_actionThreshold && m_actionThreshold->isChecked(), 0.5f);
  1311. // 模糊
  1312. m_video_widget->setBlur(m_actionBlur && m_actionBlur->isChecked(), 1.0f);
  1313. // 反色
  1314. m_video_widget->setReverse(m_actionReverse && m_actionReverse->isChecked());
  1315. // 色彩减少
  1316. m_video_widget->setColorReduce(m_actionColorReduce && m_actionColorReduce->isChecked(), 8);
  1317. // 伽马
  1318. m_video_widget->setGamma(m_actionGamma && m_actionGamma->isChecked(), 1.2f);
  1319. // 对比度/亮度
  1320. m_video_widget->setContrastBright(m_actionContrastBright && m_actionContrastBright->isChecked(),
  1321. 1.2f,
  1322. 0.1f);
  1323. // 镜像
  1324. m_video_widget->setMirror(m_actionMirro && m_actionMirro->isChecked());
  1325. // 其它特效可继续扩展
  1326. }
  1327. // 新增槽函数实现
  1328. void MainWindowA::onShowMessage(const QString& message,
  1329. const QString& windowTitle,
  1330. const QString& styleSheet)
  1331. {
  1332. QMessageBox msgBox;
  1333. msgBox.setText(message);
  1334. msgBox.setWindowTitle(windowTitle);
  1335. msgBox.setStyleSheet(styleSheet);
  1336. msgBox.show();
  1337. msgBox.move(frameGeometry().center() - msgBox.rect().center());
  1338. msgBox.setWindowFlags(msgBox.windowFlags() | Qt::Dialog);
  1339. msgBox.setModal(true);
  1340. msgBox.exec();
  1341. }
  1342. void MainWindowA::onRequestFullscreen(bool bFullscreen)
  1343. {
  1344. if (bFullscreen) {
  1345. showFullScreen();
  1346. } else {
  1347. showNormal();
  1348. update_video_label();
  1349. }
  1350. hide_cursor(bFullscreen);
  1351. }
  1352. void MainWindowA::onRequestHideStatusBar(bool bHide)
  1353. {
  1354. statusBar()->setVisible(!bHide);
  1355. auto sz_status = statusBar()->size();
  1356. if (isFullScreen()) {
  1357. centralWidget()->resize(centralWidget()->size());
  1358. } else {
  1359. auto sz = size();
  1360. if (statusBar()->isVisible()) {
  1361. sz += QSize(0, sz_status.height());
  1362. } else {
  1363. sz -= QSize(0, sz_status.height());
  1364. }
  1365. resize(sz);
  1366. }
  1367. }