play_control_window.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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, &PlayerController::playSeek);
  101. connect(slider_speed, &QSlider::valueChanged, this, &PlayControlWnd::speed_changed);
  102. connect(slider_speed, &QSlider::valueChanged, playerController, [this, playerController]() {
  103. auto speed = get_speed();
  104. playerController->setPlaySpeed(speed);
  105. });
  106. clear_all();
  107. }
  108. PlayControlWnd::~PlayControlWnd()
  109. {
  110. }
  111. void PlayControlWnd::set_focus_policy()
  112. {
  113. /*
  114. * Disable widgets in this window to accept foucs,
  115. * so all key event will be transfered to the
  116. * parent widget. The mainwindow will handle these keyevent
  117. * to control the playing.
  118. */
  119. // setFocusPolicy(Qt::NoFocus);
  120. progress_slider->setFocusPolicy(Qt::NoFocus);
  121. slider_speed->setFocusPolicy(Qt::NoFocus);
  122. slider_vol->setFocusPolicy(Qt::NoFocus);
  123. btn_pre->setFocusPolicy(Qt::NoFocus);
  124. btn_play->setFocusPolicy(Qt::NoFocus);
  125. btn_next->setFocusPolicy(Qt::NoFocus);
  126. btn_stop->setFocusPolicy(Qt::NoFocus);
  127. check_mute->setFocusPolicy(Qt::NoFocus);
  128. }
  129. void PlayControlWnd::volume_muted(int mute)
  130. {
  131. bool enable = !mute;
  132. label_vol->setEnabled(enable);
  133. slider_vol->setEnabled(enable);
  134. check_mute->setChecked(!enable);
  135. }
  136. void PlayControlWnd::speed_changed(int value)
  137. {
  138. int max = slider_speed->maximum();
  139. value %= (max + 1);
  140. double speed = (value - 1) * PLAY_SPEED_STEP + PLAY_SPEED_START;
  141. QString str = QString::number(speed, 'f', 2) + "x";
  142. // qDebug() << speed << max << "str=" << str;
  143. label_speed->setText(str);
  144. }
  145. double PlayControlWnd::get_speed() const
  146. {
  147. int value = slider_speed->value();
  148. return (value - 1) * PLAY_SPEED_STEP + PLAY_SPEED_START;
  149. }
  150. void PlayControlWnd::speed_adjust(bool up)
  151. {
  152. int value = slider_speed->value();
  153. int max = slider_speed->maximum();
  154. if (up)
  155. {
  156. value += 1;
  157. }
  158. else
  159. {
  160. value -= 1;
  161. }
  162. value = value > max ? max : value;
  163. value = value < 0 ? 0 : value;
  164. slider_speed->setValue(value);
  165. }
  166. void PlayControlWnd::init_slider_speed()
  167. {
  168. int maxSpeed = (PLAY_SPEED_STOP + PLAY_SPEED_STEP - PLAY_SPEED_START) / PLAY_SPEED_STEP; // 8
  169. slider_speed->setMaximum(maxSpeed);
  170. slider_speed->setValue((1 + PLAY_SPEED_STEP - PLAY_SPEED_START) / PLAY_SPEED_STEP); // set 1x speed
  171. }
  172. void PlayControlWnd::set_volume_slider(float volume)
  173. {
  174. enable_slider_vol(true);
  175. auto max = slider_vol->maximum();
  176. slider_vol->setValue(int(volume * max));
  177. }
  178. inline double PlayControlWnd::get_time_secs(int64_t hours, int64_t mins, int64_t secs)
  179. {
  180. return hours * 60 * 60 + mins * 60 + secs;
  181. }
  182. inline QSlider* PlayControlWnd::get_progress_slider() const
  183. {
  184. return progress_slider;
  185. }
  186. inline QSlider* PlayControlWnd::get_volume_slider() const
  187. {
  188. return slider_vol;
  189. }
  190. inline QSlider* PlayControlWnd::get_speed_slider() const
  191. {
  192. return slider_speed;
  193. }
  194. int PlayControlWnd::get_volum_slider_max()
  195. {
  196. return get_volume_slider()->maximum();
  197. }
  198. int PlayControlWnd::get_progress_slider_max()
  199. {
  200. return get_progress_slider()->maximum();
  201. }
  202. int PlayControlWnd::get_progress_slider_value()
  203. {
  204. return get_progress_slider()->value();
  205. }
  206. void PlayControlWnd::enable_progressbar(bool enable)
  207. {
  208. get_progress_slider()->setEnabled(enable);
  209. }
  210. void PlayControlWnd::enable_slider_vol(bool enable)
  211. {
  212. slider_vol->setEnabled(enable);
  213. }
  214. void PlayControlWnd::enable_slider_speed(bool enable)
  215. {
  216. slider_speed->setEnabled(enable);
  217. }
  218. void PlayControlWnd::get_play_time_params(int64_t total_secs, int64_t& hours, int64_t& mins, int64_t& secs)
  219. {
  220. #if 1
  221. mins = total_secs / 60;
  222. secs = total_secs % 60;
  223. hours = mins / 60;
  224. mins %= 60;
  225. #else
  226. hours = int(total_secs / 3600);
  227. mins = (total_secs - hours * 3600) / 60;
  228. secs = (total_secs - hours * 3600 - mins * 60);
  229. #endif
  230. }
  231. void PlayControlWnd::update_play_time(int64_t hours, int64_t mins, int64_t secs)
  232. {
  233. auto time_str = get_play_time(hours, mins, secs);
  234. label_curTime->setText(time_str);
  235. int percent = 0;
  236. auto total = get_total_time();
  237. auto cur = get_time_secs(hours, mins, secs);
  238. cur = cur > total ? total : cur;
  239. if (total > 0)
  240. {
  241. percent = cur * get_progress_slider()->maximum() / total;
  242. }
  243. get_progress_slider()->setValue(percent);
  244. }
  245. void PlayControlWnd::update_play_time(int64_t total_secs)
  246. {
  247. int64_t hours = 0, mins = 0, secs = 0;
  248. double total = get_total_time();
  249. total_secs = total_secs < 0 ? 0 : total_secs;
  250. total_secs = total_secs > total ? total : total_secs;
  251. get_play_time_params(total_secs, hours, mins, secs);
  252. // qDebug() << "total:" << total_secs << "h:" << hours << "m:" << mins <<
  253. // "ses:" << secs;
  254. update_play_time(hours, mins, secs);
  255. }
  256. double PlayControlWnd::get_total_time() const
  257. {
  258. return get_time_secs(m_hours, m_mins, m_secs);
  259. }
  260. void PlayControlWnd::set_total_time(int64_t hours, int64_t mins, int64_t secs)
  261. {
  262. enable_progressbar();
  263. enable_play_buttons();
  264. enable_slider_vol();
  265. enable_slider_speed();
  266. check_mute->setEnabled(true);
  267. init_slider_speed();
  268. m_hours = hours;
  269. m_mins = mins;
  270. m_secs = secs;
  271. set_progress_bar(get_total_time());
  272. label_totalTime->setText(get_play_time(hours, mins, secs));
  273. }
  274. void PlayControlWnd::set_progress_bar(double total_secs)
  275. {
  276. get_progress_slider()->setMaximum(total_secs);
  277. }
  278. QString PlayControlWnd::get_play_time(int64_t hours, int64_t mins, int64_t secs)
  279. {
  280. QString str;
  281. if (hours == 0)
  282. {
  283. str = QString("%1:%2")
  284. .arg(QString::number(mins).rightJustified(2, '0'))
  285. .arg(QString::number(secs).rightJustified(2, '0'));
  286. }
  287. else
  288. {
  289. str = QString("%1:%2:%3")
  290. .arg(QString::number(hours).rightJustified(2, '0'))
  291. .arg(QString::number(mins).rightJustified(2, '0'))
  292. .arg(QString::number(secs).rightJustified(2, '0'));
  293. }
  294. return str;
  295. }
  296. void PlayControlWnd::clear_time()
  297. {
  298. m_hours = 0;
  299. m_mins = 0;
  300. m_secs = 0;
  301. get_progress_slider()->setValue(0);
  302. label_totalTime->setText("--:--");
  303. label_curTime->setText("--:--");
  304. }
  305. void PlayControlWnd::clear_all()
  306. {
  307. clear_time();
  308. enable_progressbar(false);
  309. update_btn_play();
  310. enable_slider_speed(false);
  311. enable_slider_vol(false);
  312. enable_play_buttons(false);
  313. check_mute->setEnabled(false);
  314. init_slider_speed();
  315. }
  316. void PlayControlWnd::update_btn_play(bool bPause)
  317. {
  318. if (bPause)
  319. {
  320. btn_play->setText("Play");
  321. }
  322. else
  323. {
  324. btn_play->setText("Pause");
  325. }
  326. }
  327. void PlayControlWnd::enable_play_buttons(bool enable)
  328. {
  329. btn_next->setEnabled(enable);
  330. btn_pre->setEnabled(enable);
  331. btn_play->setEnabled(enable);
  332. btn_stop->setEnabled(enable);
  333. }
  334. void PlayControlWnd::keyPressEvent(QKeyEvent* event)
  335. {
  336. if (auto pParent = (MainWindowA*) parent()) {
  337. QApplication::sendEvent(pParent, event);
  338. event->ignore();
  339. } else {
  340. QWidget::keyPressEvent(event);
  341. }
  342. }