play_control_window.cpp 11 KB

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