Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
FrmPlayer.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QKeyEvent>
4#include <QMouseEvent>
5#include <QLoggingCategory>
6#include <QToolButton>
7#include <QTime>
8
9#include "FrmPlayer.h"
10
11static Q_LOGGING_CATEGORY(log, "FrmPlayer")
12
13CFrmPlayer::CFrmPlayer(QWidget *parent) : QWidget(parent)
14 , m_paStart(nullptr)
15 , m_paPause(nullptr)
16#if HAVE_QT6_RECORD
17 , m_paRecord(nullptr)
18 , m_paRecordPause(nullptr)
19#endif
20 , m_paMuted(nullptr)
21 , m_paVolume(nullptr)
22 , m_VideoWidget(this)
23 , m_ToolBar(this)
24 , m_pbVideo(Qt::Horizontal, this)
25 , m_pbVolume(Qt::Horizontal, this)
26 , m_bMoveVideo(false)
27 , m_pParameter(nullptr)
28 , m_pLabel(nullptr)
29{
30 bool check = false;
31
32 qDebug(log) << Q_FUNC_INFO;
33
34 m_VideoWidget.installEventFilter(this);
35
36 m_paStart = m_ToolBar.addAction(
37 QIcon::fromTheme("media-playback-start"), tr("Start"));
38 m_paStart->setCheckable(true);
39 check = connect(m_paStart, SIGNAL(toggled(bool)),
40 this, SLOT(slotStart(bool)));
41 Q_ASSERT(check);
42
43 m_paPause = m_ToolBar.addAction(
44 QIcon::fromTheme("media-playback-pause"), tr("pause"));
45 m_paPause->setCheckable(true);
46 m_paPause->setEnabled(false);
47
48 m_ToolBar.addSeparator();
49 m_ToolBar.addAction(QIcon::fromTheme("media-seek-backward"), tr("Backward"),
50 this, [&](){
51 qDebug(log) << "Backward action";
52 emit sigChangePosition(m_pbVideo.value() - 1000);
53 });
54
55 m_pbVideo.setRange(0, 0);
56 m_pbVideo.setValue(0);
57 m_pbVideo.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
58 check = connect(&m_pbVideo, &QSlider::sliderPressed,
59 this, [&](){
60 m_bMoveVideo = true;
61 });
62 Q_ASSERT(check);
63 check = connect(&m_pbVideo, &QSlider::sliderReleased,
64 this, [&](){
65 m_bMoveVideo = false;
66 emit sigChangePosition(m_pbVideo.value());
67 });
68 Q_ASSERT(check);
69 m_ToolBar.addWidget(&m_pbVideo);
70
71 m_ToolBar.addAction(QIcon::fromTheme("media-seek-forward"), tr("Forward"),
72 this, [&](){
73 qDebug(log) << "Forward action";
74 emit sigChangePosition(m_pbVideo.value() + 1000);
75 });
76
77 m_ToolBar.addSeparator();
78 m_pLabel = new QLabel(&m_ToolBar);
79 m_pLabel->setText("00:00:00 / 00:00:00");
80 m_ToolBar.addWidget(m_pLabel);
81 m_ToolBar.addSeparator();
82
83 m_paScreenShot = m_ToolBar.addAction(
84 QIcon::fromTheme("camera-photo"), tr("ScreenShot"));
85 m_paScreenShot->setEnabled(false);
86
87#if HAVE_QT6_RECORD
88 m_paRecord = m_ToolBar.addAction(
89 QIcon::fromTheme("media-record"), tr("Record"));
90 m_paRecord->setCheckable(true);
91 m_paRecord->setEnabled(false);
92 check = connect(m_paRecord, &QAction::toggled, this, [&](bool checked){
93 m_paRecordPause->setEnabled(checked);
94 if(checked)
95 m_paRecordPause->setChecked(false);
96 });
97 Q_ASSERT(check);
98 m_paRecordPause = m_ToolBar.addAction(
99 QIcon::fromTheme("media-playback-pause"), tr("Record pause"));
100 m_paRecordPause->setCheckable(true);
101 m_paRecordPause->setEnabled(false);
102#endif
103
104 m_paSettings = m_ToolBar.addAction(
105 QIcon::fromTheme("system-settings"), tr("Settings"));
106
107 m_paMuted = m_ToolBar.addAction(
108 QIcon::fromTheme("audio-volume-medium"), tr("Audio"));
109 m_paMuted->setCheckable(true);
110 check = connect(m_paMuted, SIGNAL(toggled(bool)),
111 this, SLOT(slotAudioMuted(bool)));
112 Q_ASSERT(check);
113
114 m_pbVolume.setRange(0, 100);
115 m_pbVolume.setValue(0);
116 m_pbVolume.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
117 m_paVolume = m_ToolBar.addWidget(&m_pbVolume);
118 check = connect(&m_pbVolume, SIGNAL(sliderMoved(int)),
119 this, SLOT(slotAduioVolume(int)));
120 Q_ASSERT(check);
121}
122
123CFrmPlayer::~CFrmPlayer()
124{
125 qDebug(log) << Q_FUNC_INFO;
126}
127
128QVideoSink *CFrmPlayer::videoSink()
129{
130 return m_VideoWidget.videoSink();
131}
132
133int CFrmPlayer::SetParameter(CParameterPlayer* pParameter)
134{
135 if(!pParameter || m_pParameter == pParameter)
136 return -1;
137
138 m_pParameter = pParameter;
139
140 m_paMuted->setChecked(m_pParameter->GetAudioOutputMuted());
141 m_pbVolume.setValue(m_pParameter->GetAudioOutputVolume());
142 return 0;
143}
144
145void CFrmPlayer::slotAudioMuted(bool bMuted)
146{
147 if(!m_pParameter)
148 return;
149
150 if(bMuted) {
151 m_paMuted->setIcon(QIcon::fromTheme("audio-volume-muted"));
152 m_paMuted->setText(tr("Muted"));
153 } else {
154 m_paMuted->setIcon(QIcon::fromTheme("audio-volume-medium"));
155 m_paMuted->setText(tr("Audio"));
156 }
157 m_pParameter->SetAudioOutputMuted(bMuted);
158 m_paVolume->setEnabled(!bMuted);
159}
160
161void CFrmPlayer::slotAduioVolume(int volume)
162{
163 qDebug(log) << "Volume:" << volume;
164 if(!m_pParameter)
165 return;
166 m_pParameter->SetAudioOutputVolume(volume);
167}
168
169void CFrmPlayer::slotPositionChanged(qint64 pos, qint64 duration)
170{
171 qint64 currentInfo = pos / 1000;
172 qint64 dur = duration / 1000;
173
174 QString szStr;
175 if (currentInfo || dur) {
176 QTime currentTime((currentInfo / 3600) % 60,
177 (currentInfo / 60) % 60,
178 currentInfo % 60,
179 (currentInfo * 1000) % 1000);
180 QTime totalTime((dur / 3600) % 60,
181 (dur / 60) % 60,
182 dur % 60,
183 (dur * 1000) % 1000);
184 QString format = "mm:ss";
185 if (dur > 3600)
186 format = "hh:mm:ss";
187 szStr = currentTime.toString(format)
188 + " / " + totalTime.toString(format);
189 m_pLabel->setText(szStr);
190 m_pbVideo.setRange(0, duration);
191 if(!m_bMoveVideo)
192 m_pbVideo.setValue(pos);
193 }
194}
195
196void CFrmPlayer::resizeEvent(QResizeEvent *event)
197{
198 qDebug(log) << "CFrmPlayer::resizeEvent()" << event;
199 QSize s = event->size();
200 AdjustCompone(s);
201 QWidget::resizeEvent(event);
202}
203
204int CFrmPlayer::AdjustCompone(const QSize &s)
205{
206 m_VideoWidget.move(0, 0);
207 QRect rect(0, 0, s.width(), s.height() - m_ToolBar.frameGeometry().height());
208 m_VideoWidget.setGeometry(rect);
209 int left = 0;
210 int top = s.height() - m_ToolBar.frameGeometry().height();
211 m_ToolBar.move(left, top);
212 m_ToolBar.resize(s.width(), m_ToolBar.height());
213 return 0;
214}
215
216void CFrmPlayer::slotStart(bool bStart)
217{
218 QAction* p = qobject_cast<QAction*>(sender());
219 if(!p) return;
220 if(p->isChecked()) {
221 p->setIcon(QIcon::fromTheme("media-playback-stop"));
222 p->setText(tr("Stop"));
223 m_paPause->setEnabled(true);
224 m_paPause->setChecked(false);
225 m_paScreenShot->setEnabled(true);
226#if HAVE_QT6_RECORD
227 m_paRecord->setEnabled(true);
228 m_paRecord->setChecked(false);
229 m_paRecordPause->setEnabled(true);
230 m_paRecordPause->setChecked(false);
231#endif
232 } else {
233 p->setIcon(QIcon::fromTheme("media-playback-start"));
234 p->setText(tr("Start"));
235 m_paPause->setEnabled(false);
236 m_paPause->setChecked(false);
237 m_paScreenShot->setEnabled(false);
238#if HAVE_QT6_RECORD
239 m_paRecord->setEnabled(false);
240 m_paRecord->setChecked(false);
241 m_paRecordPause->setEnabled(false);
242 m_paRecordPause->setChecked(false);
243#endif
244 }
245}
246
247bool CFrmPlayer::eventFilter(QObject *watched, QEvent *event)
248{
249 //qDebug(log) << Q_FUNC_INFO << event;
250 if(&m_VideoWidget == watched)
251 {
252 switch(event->type()){
253 case QEvent::MouseMove:
254 qDebug(log) << "Mouse move";
255 break;
256 case QEvent::MouseButtonRelease:
257 m_paPause->trigger();
258 break;
259 case QEvent::MouseButtonDblClick: {
260 m_VideoWidget.setFullScreen(!m_VideoWidget.isFullScreen());
261 if(!m_VideoWidget.isFullScreen()) {
262 QSize s = size();
263 AdjustCompone(s);
264 }
265 break;
266 }
267 case QEvent::KeyRelease:
268 {
269 QKeyEvent* k = (QKeyEvent*)(event);
270 switch(k->key())
271 {
272 case Qt::Key_Escape:
273 if(m_VideoWidget.isFullScreen()) {
274 m_VideoWidget.setFullScreen(false);
275 QSize s = size();
276 AdjustCompone(s);
277 }
278 break;
279 case Qt::Key_Enter:
280 case Qt::Key_Space:
281 m_paPause->trigger();
282 break;
283 default:
284 break;
285 }
286 break;
287 }
288 default:
289 return false;
290 }
291 return true;
292 }
293 return false;
294}