play_control_window.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // ***********************************************************/
  2. // play_control_window.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // video play control panel.
  7. // ***********************************************************/
  8. #include "play_control_window.h"
  9. #include "mainwindowa.h"
  10. #define PLAY_SPEED_STEP 0.25
  11. #define PLAY_SPEED_START 0.5
  12. #define PLAY_SPEED_STOP 2 // 4 speed multiple from start to stop in step
  13. #include "clickable_slider.h"
  14. PlayControlWnd::PlayControlWnd(PlayerController* playerController, QWidget* parent)
  15. : QWidget(parent)
  16. {
  17. // 1. 创建控件
  18. btn_pre = new QPushButton("<<", this);
  19. btn_play = new QPushButton("Play", this);
  20. btn_next = new QPushButton(">>", this);
  21. btn_stop = new QPushButton("Stop", this);
  22. slider_speed = new QSlider(Qt::Horizontal, this);
  23. label_speed = new QLabel("1.0x", this);
  24. check_mute = new QCheckBox("Mute", this);
  25. slider_vol = new QSlider(Qt::Horizontal, this);
  26. label_vol = new QLabel("0", this);
  27. progress_slider = new ClickableSlider(this);
  28. label_curTime = new QLabel("00:00", this);
  29. label_totalTime = new QLabel("00:00", this);
  30. label_divider = new QLabel("/", this);
  31. // 2. 设置控件属性
  32. slider_speed->setMinimum(1);
  33. slider_speed->setMaximum(8);
  34. slider_speed->setValue(4);
  35. slider_speed->setTickPosition(QSlider::TicksAbove);
  36. slider_speed->setTickInterval(1);
  37. slider_speed->setFixedHeight(30);
  38. label_speed->setFixedWidth(28);
  39. label_speed->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  40. slider_vol->setMinimum(0);
  41. slider_vol->setMaximum(100);
  42. slider_vol->setSingleStep(5);
  43. slider_vol->setPageStep(5);
  44. slider_vol->setFixedHeight(22);
  45. label_vol->setFixedWidth(20);
  46. label_vol->setAlignment(Qt::AlignCenter);
  47. progress_slider->setMinimum(0);
  48. progress_slider->setMaximum(100);
  49. progress_slider->setSingleStep(1);
  50. progress_slider->setPageStep(5);
  51. progress_slider->setTracking(true);
  52. progress_slider->setFixedHeight(18);
  53. label_curTime->setFixedWidth(48);
  54. label_totalTime->setFixedWidth(48);
  55. label_divider->setText("/");
  56. label_divider->setFixedWidth(10);
  57. // 3. 布局
  58. hLayout_progress = new QHBoxLayout;
  59. hLayout_progress->setSpacing(2);
  60. hLayout_progress->addWidget(progress_slider, 1);
  61. hLayout_progress->addWidget(label_curTime);
  62. hLayout_progress->addWidget(label_divider);
  63. hLayout_progress->addWidget(label_totalTime);
  64. hLayout_controls = new QHBoxLayout;
  65. hLayout_controls->setSpacing(1);
  66. hLayout_controls->addWidget(btn_pre);
  67. hLayout_controls->addWidget(btn_play);
  68. hLayout_controls->addWidget(btn_next);
  69. hLayout_controls->addWidget(btn_stop);
  70. hLayout_controls->addSpacing(10);
  71. hLayout_controls->addWidget(slider_speed);
  72. hLayout_controls->addWidget(label_speed);
  73. hLayout_controls->addSpacing(10);
  74. hLayout_controls->addWidget(check_mute);
  75. hLayout_controls->addWidget(slider_vol);
  76. hLayout_controls->addWidget(label_vol);
  77. mainLayout = new QVBoxLayout(this);
  78. mainLayout->setContentsMargins(0, 0, 0, 0);
  79. mainLayout->setSpacing(2);
  80. mainLayout->addLayout(hLayout_progress);
  81. mainLayout->addLayout(hLayout_controls);
  82. setLayout(mainLayout);
  83. // 4. 信号槽
  84. connect(slider_vol, SIGNAL(valueChanged(int)), label_vol, SLOT(setNum(int)));
  85. connect(check_mute, &QCheckBox::stateChanged, this, &PlayControlWnd::volume_muted);
  86. connect(check_mute, &QCheckBox::stateChanged, playerController, &PlayerController::playMute);
  87. connect(btn_stop, &QPushButton::clicked, playerController, &PlayerController::stopPlay);
  88. connect(btn_play, &QPushButton::clicked, playerController, &PlayerController::pausePlay);
  89. connect(slider_vol,
  90. &QSlider::valueChanged,
  91. playerController,
  92. [this, playerController](int value) {
  93. auto maxValue = get_volum_slider_max();
  94. playerController->setVolume(value, maxValue);
  95. });
  96. connect(btn_pre, &QPushButton::pressed, playerController, &PlayerController::playSeekPre);
  97. connect(btn_next, &QPushButton::pressed, playerController, &PlayerController::playSeekNext);
  98. connect(progress_slider, &QSlider::sliderReleased, playerController, &PlayerController::playStartSeek);
  99. connect(progress_slider, &QSlider::sliderPressed, playerController, &PlayerController::pausePlay);
  100. connect(progress_slider, &ClickableSlider::onClick, playerController, [this, playerController]() {
  101. int maxValue = get_progress_slider_max();
  102. double total_time = get_total_time();
  103. int value = get_progress_slider_value();
  104. double seek_time = 0;
  105. if (maxValue > 0)
  106. seek_time = value * total_time * 1.0 / maxValue;
  107. qDebug() << "val:" << value << ",maxVal:" << maxValue << ",total time" << total_time
  108. << ",seek time:" << seek_time;
  109. // playerController->videoSeek(seek_time);
  110. playerController->videoSeekEx(value, maxValue);
  111. });
  112. connect(slider_speed, &QSlider::valueChanged, this, &PlayControlWnd::speed_changed);
  113. connect(slider_speed, &QSlider::valueChanged, playerController, [this, playerController]() {
  114. auto speed = get_speed();
  115. playerController->setPlaySpeed(speed);
  116. });
  117. clear_all();
  118. }
  119. PlayControlWnd::~PlayControlWnd()
  120. {
  121. }
  122. void PlayControlWnd::set_focus_policy()
  123. {
  124. /*
  125. * Disable widgets in this window to accept foucs,
  126. * so all key event will be transfered to the
  127. * parent widget. The mainwindow will handle these keyevent
  128. * to control the playing.
  129. */
  130. // setFocusPolicy(Qt::NoFocus);
  131. progress_slider->setFocusPolicy(Qt::NoFocus);
  132. slider_speed->setFocusPolicy(Qt::NoFocus);
  133. slider_vol->setFocusPolicy(Qt::NoFocus);
  134. btn_pre->setFocusPolicy(Qt::NoFocus);
  135. btn_play->setFocusPolicy(Qt::NoFocus);
  136. btn_next->setFocusPolicy(Qt::NoFocus);
  137. btn_stop->setFocusPolicy(Qt::NoFocus);
  138. check_mute->setFocusPolicy(Qt::NoFocus);
  139. }
  140. void PlayControlWnd::volume_muted(int mute)
  141. {
  142. bool enable = !mute;
  143. label_vol->setEnabled(enable);
  144. slider_vol->setEnabled(enable);
  145. check_mute->setChecked(!enable);
  146. }
  147. void PlayControlWnd::speed_changed(int value)
  148. {
  149. int max = slider_speed->maximum();
  150. value %= (max + 1);
  151. double speed = (value - 1) * PLAY_SPEED_STEP + PLAY_SPEED_START;
  152. QString str = QString::number(speed, 'f', 2) + "x";
  153. // qDebug() << speed << max << "str=" << str;
  154. label_speed->setText(str);
  155. }
  156. double PlayControlWnd::get_speed() const
  157. {
  158. int value = slider_speed->value();
  159. return (value - 1) * PLAY_SPEED_STEP + PLAY_SPEED_START;
  160. }
  161. void PlayControlWnd::speed_adjust(bool up)
  162. {
  163. int value = slider_speed->value();
  164. int max = slider_speed->maximum();
  165. if (up)
  166. {
  167. value += 1;
  168. }
  169. else
  170. {
  171. value -= 1;
  172. }
  173. value = value > max ? max : value;
  174. value = value < 0 ? 0 : value;
  175. slider_speed->setValue(value);
  176. }
  177. void PlayControlWnd::init_slider_speed()
  178. {
  179. int maxSpeed = (PLAY_SPEED_STOP + PLAY_SPEED_STEP - PLAY_SPEED_START) / PLAY_SPEED_STEP; // 8
  180. slider_speed->setMaximum(maxSpeed);
  181. slider_speed->setValue((1 + PLAY_SPEED_STEP - PLAY_SPEED_START) / PLAY_SPEED_STEP); // set 1x speed
  182. }
  183. void PlayControlWnd::set_volume_slider(float volume)
  184. {
  185. enable_slider_vol(true);
  186. auto max = slider_vol->maximum();
  187. slider_vol->setValue(int(volume * max));
  188. }
  189. inline double PlayControlWnd::get_time_secs(int64_t hours, int64_t mins, int64_t secs)
  190. {
  191. return hours * 60 * 60 + mins * 60 + secs;
  192. }
  193. inline QSlider* PlayControlWnd::get_progress_slider() const
  194. {
  195. return progress_slider;
  196. }
  197. inline QSlider* PlayControlWnd::get_volume_slider() const
  198. {
  199. return slider_vol;
  200. }
  201. inline QSlider* PlayControlWnd::get_speed_slider() const
  202. {
  203. return slider_speed;
  204. }
  205. int PlayControlWnd::get_volum_slider_max()
  206. {
  207. return get_volume_slider()->maximum();
  208. }
  209. int PlayControlWnd::get_progress_slider_max()
  210. {
  211. return get_progress_slider()->maximum();
  212. }
  213. int PlayControlWnd::get_progress_slider_value()
  214. {
  215. return get_progress_slider()->value();
  216. }
  217. void PlayControlWnd::enable_progressbar(bool enable)
  218. {
  219. get_progress_slider()->setEnabled(enable);
  220. }
  221. void PlayControlWnd::enable_slider_vol(bool enable)
  222. {
  223. slider_vol->setEnabled(enable);
  224. }
  225. void PlayControlWnd::enable_slider_speed(bool enable)
  226. {
  227. slider_speed->setEnabled(enable);
  228. }
  229. void PlayControlWnd::get_play_time_params(int64_t total_secs, int64_t& hours, int64_t& mins, int64_t& secs)
  230. {
  231. #if 1
  232. mins = total_secs / 60;
  233. secs = total_secs % 60;
  234. hours = mins / 60;
  235. mins %= 60;
  236. #else
  237. hours = int(total_secs / 3600);
  238. mins = (total_secs - hours * 3600) / 60;
  239. secs = (total_secs - hours * 3600 - mins * 60);
  240. #endif
  241. }
  242. void PlayControlWnd::update_play_time(int64_t hours, int64_t mins, int64_t secs)
  243. {
  244. auto time_str = get_play_time(hours, mins, secs);
  245. label_curTime->setText(time_str);
  246. int percent = 0;
  247. auto total = get_total_time();
  248. auto cur = get_time_secs(hours, mins, secs);
  249. cur = cur > total ? total : cur;
  250. if (total > 0)
  251. {
  252. percent = cur * get_progress_slider()->maximum() / total;
  253. }
  254. get_progress_slider()->setValue(percent);
  255. }
  256. void PlayControlWnd::update_play_time(int64_t total_secs)
  257. {
  258. int64_t hours = 0, mins = 0, secs = 0;
  259. double total = get_total_time();
  260. total_secs = total_secs < 0 ? 0 : total_secs;
  261. total_secs = total_secs > total ? total : total_secs;
  262. get_play_time_params(total_secs, hours, mins, secs);
  263. // qDebug() << "total:" << total_secs << "h:" << hours << "m:" << mins <<
  264. // "ses:" << secs;
  265. update_play_time(hours, mins, secs);
  266. }
  267. double PlayControlWnd::get_total_time() const
  268. {
  269. return get_time_secs(m_hours, m_mins, m_secs);
  270. }
  271. void PlayControlWnd::set_total_time(int64_t hours, int64_t mins, int64_t secs)
  272. {
  273. enable_progressbar();
  274. enable_play_buttons();
  275. enable_slider_vol();
  276. enable_slider_speed();
  277. check_mute->setEnabled(true);
  278. init_slider_speed();
  279. m_hours = hours;
  280. m_mins = mins;
  281. m_secs = secs;
  282. set_progress_bar(get_total_time());
  283. label_totalTime->setText(get_play_time(hours, mins, secs));
  284. }
  285. void PlayControlWnd::set_progress_bar(double total_secs)
  286. {
  287. get_progress_slider()->setMaximum(total_secs);
  288. }
  289. QString PlayControlWnd::get_play_time(int64_t hours, int64_t mins, int64_t secs)
  290. {
  291. QString str;
  292. if (hours == 0)
  293. {
  294. str = QString("%1:%2")
  295. .arg(QString::number(mins).rightJustified(2, '0'))
  296. .arg(QString::number(secs).rightJustified(2, '0'));
  297. }
  298. else
  299. {
  300. str = QString("%1:%2:%3")
  301. .arg(QString::number(hours).rightJustified(2, '0'))
  302. .arg(QString::number(mins).rightJustified(2, '0'))
  303. .arg(QString::number(secs).rightJustified(2, '0'));
  304. }
  305. return str;
  306. }
  307. void PlayControlWnd::clear_time()
  308. {
  309. m_hours = 0;
  310. m_mins = 0;
  311. m_secs = 0;
  312. get_progress_slider()->setValue(0);
  313. label_totalTime->setText("--:--");
  314. label_curTime->setText("--:--");
  315. }
  316. void PlayControlWnd::clear_all()
  317. {
  318. clear_time();
  319. enable_progressbar(false);
  320. update_btn_play();
  321. enable_slider_speed(false);
  322. enable_slider_vol(false);
  323. enable_play_buttons(false);
  324. check_mute->setEnabled(false);
  325. init_slider_speed();
  326. }
  327. void PlayControlWnd::update_btn_play(bool bPause)
  328. {
  329. if (bPause)
  330. {
  331. btn_play->setText("Play");
  332. }
  333. else
  334. {
  335. btn_play->setText("Pause");
  336. }
  337. }
  338. void PlayControlWnd::enable_play_buttons(bool enable)
  339. {
  340. btn_next->setEnabled(enable);
  341. btn_pre->setEnabled(enable);
  342. btn_play->setEnabled(enable);
  343. btn_stop->setEnabled(enable);
  344. }
  345. void PlayControlWnd::keyPressEvent(QKeyEvent* event)
  346. {
  347. if (auto pParent = (MainWindowA*) parent()) {
  348. QApplication::sendEvent(pParent, event);
  349. event->ignore();
  350. } else {
  351. QWidget::keyPressEvent(event);
  352. }
  353. }