MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
QmitkOpenCVVideoControls.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14#include <QmitkVideoBackground.h>
15#include <QmitkRenderWindow.h>
17
18#include <mitkIPersistenceService.h>
19
20#include <opencv2/videoio/legacy/constants_c.h>
21
23{
24public:
25
27 : q(q)
28 , m_Id(id)
29 {}
30
34 PERSISTENCE_GET_SERVICE_METHOD_MACRO
35
37
41 std::string m_Id;
42 void ToPropertyList();
43 void FromPropertyList();
44};
45
46QmitkOpenCVVideoControls::QmitkOpenCVVideoControls(QmitkVideoBackground* _VideoBackground
47 , QmitkRenderWindow* _RenderWindow
48 , QWidget * parent, Qt::WindowFlags f)
49 : QWidget(parent, f)
50 , m_VideoBackground(nullptr)
51 , m_RenderWindow(nullptr)
52 , m_VideoSource(nullptr)
53 , m_Controls(new Ui::QmitkOpenCVVideoControls)
54 , m_SliderCurrentlyMoved(false)
55 , d(new QmitkOpenCVVideoControlsPrivate(this, "QmitkOpenCVVideoControls"))
56{
57 m_Controls->setupUi(this);
58 m_Controls->FileChooser->SetFileMustExist(true);
59 m_Controls->FileChooser->SetSelectDir(false);
60
61 this->SetRenderWindow(_RenderWindow);
62 this->SetVideoBackground(_VideoBackground);
63 d->FromPropertyList();
64
65 mitk::IPersistenceService* persistenceService = d->GetPersistenceService();
66
67 if (persistenceService != nullptr)
68 {
69 persistenceService->AddPropertyListReplacedObserver(this);
70 }
71 else
72 {
73 MITK_WARN << "No Persistence Service available in constructor";
74 }
75}
76
78{
79 if (m_VideoSource != nullptr && m_VideoSource->IsCapturingEnabled())
80 {
81 this->Stop(); // emulate stop
82 }
83
84 mitk::IPersistenceService* persistenceService = d->GetPersistenceService();
85 if (persistenceService != nullptr)
86 {
87 persistenceService->RemovePropertyListReplacedObserver(this);
88 }
89 else
90 {
91 MITK_WARN << "No Persistence Service available in destructor";
92 }
93
94 d->ToPropertyList();
95}
96
102
104{
105 m_Controls->GrabbingDevicePanel->setEnabled(true);
106 m_Controls->VideoFilePanel->setEnabled(false);
107}
108
110{
111 m_Controls->GrabbingDevicePanel->setEnabled(false);
112 m_Controls->VideoFilePanel->setEnabled(true);
113 m_Controls->FileChooser->setEnabled(true);
114}
115
117{
119 // temporary pause the video while sliding
120 if (!m_VideoSource->GetCapturePaused())
122}
123
125{
126 double progressRatio = static_cast<double>(m_Controls->VideoProgressSlider->value())
127 / static_cast<double>(m_Controls->VideoProgressSlider->maximum());
128 m_VideoSource->SetVideoCaptureProperty(CV_CAP_PROP_POS_FRAMES, progressRatio*m_VideoSource->GetVideoCaptureProperty(CV_CAP_PROP_FRAME_COUNT));
129
130 // resume the video ( if it was not paused by the user)
131 if (m_VideoSource->GetCapturePaused() && m_Controls->PlayButton->isChecked())
134}
135
137{
138 MITK_INFO << "repeat video clicked";
139 m_VideoSource->SetRepeatVideo(checked);
140}
141
143{
144 MITK_INFO << "play button clicked";
145 if (checked)
146 {
147 this->Play();
148 }
149 else
150 {
151 // show pause button
152 this->IsPlaying(true);
154 }
155}
156
158{
159 this->Stop();
160}
161
163{
164 if (m_VideoSource->GetCapturePaused())
165 {
166 this->IsPlaying(false);
168 }
169 else
170 {
171 if (m_Controls->UseGrabbingDeviceButton->isChecked())
172 {
173 m_VideoSource->SetVideoCameraInput(m_Controls->GrabbingDeviceNumber->text().toInt(), false);
174 m_Controls->VideoFileControls->setEnabled(false);
175 }
176 else
177 {
178 m_VideoSource->SetVideoFileInput(m_Controls->FileChooser->GetFile().c_str(), m_Controls->RepeatVideoButton->isChecked(), false);
179 m_VideoSource->SetRepeatVideo(m_Controls->RepeatVideoButton->isChecked());
180 m_Controls->VideoProgressSlider->setValue(0);
181 }
182
184
185 if (!m_VideoSource->IsCapturingEnabled())
186 {
187 MITK_ERROR << "Video could not be initialized!";
188 m_Controls->PlayButton->setChecked(false);
189 }
190 else
191 {
192 int hertz = m_Controls->UpdateRate->text().toInt();
193 int updateTime = itk::Math::Round<int, double>(1000.0 / hertz);
194
195 // resets the whole background
196 m_VideoBackground->SetTimerDelay(updateTime);
197 m_VideoBackground->AddRenderWindow(m_RenderWindow->GetVtkRenderWindow());
198 this->connect(m_VideoBackground, SIGNAL(NewFrameAvailable(mitk::VideoSource*))
199 , this, SLOT(NewFrameAvailable(mitk::VideoSource*)));
200 this->connect(m_VideoBackground, SIGNAL(EndOfVideoSourceReached(mitk::VideoSource*))
201 , this, SLOT(EndOfVideoSourceReached(mitk::VideoSource*)));
202
203 m_VideoBackground->Enable();
204 this->m_Controls->StopButton->setEnabled(true);
205 // show video file controls
206 if (m_Controls->UseVideoFileButton->isChecked())
207 {
208 m_Controls->VideoFileControls->setEnabled(true);
209 m_Controls->RepeatVideoButton->setEnabled(true);
210 m_Controls->VideoProgressSlider->setEnabled(true);
211 }
212 // show pause button
213 this->IsPlaying(false);
214 // disable other controls
215 m_Controls->GrabbingDevicePanel->setEnabled(false);
216 m_Controls->VideoFilePanel->setEnabled(false);
217 m_Controls->UseGrabbingDeviceButton->setEnabled(false);
218 m_Controls->UseVideoFileButton->setEnabled(false);
219 m_Controls->UpdateRatePanel->setEnabled(false);
220 }
221 }
222}
223
225{
226 // disable video file controls, stop button and show play button again
227 m_Controls->UseGrabbingDeviceButton->setEnabled(true);
228 m_Controls->UseVideoFileButton->setEnabled(true);
229 if (m_Controls->UseGrabbingDeviceButton->isChecked())
231 else
233
234 m_Controls->UpdateRatePanel->setEnabled(true);
235 m_Controls->VideoProgressSlider->setValue(0);
236 m_Controls->VideoFileControls->setEnabled(false);
237 this->m_Controls->StopButton->setEnabled(false);
238 this->IsPlaying(true);
239
241 {
242 m_VideoBackground->Disable();
243
244 if (m_RenderWindow)
245 m_VideoBackground->RemoveRenderWindow(m_RenderWindow->GetVtkRenderWindow());
246
247 this->disconnect(m_VideoBackground, SIGNAL(NewFrameAvailable(mitk::VideoSource*))
248 , this, SLOT(NewFrameAvailable(mitk::VideoSource*)));
249 }
250 if (m_VideoSource != nullptr)
252}
253
255{
256 this->Stop();
257}
258
260{
261 if (paused)
262 {
263 m_Controls->PlayButton->setText("Play");
264 m_Controls->PlayButton->setIcon(QIcon(":/OpenCVVideoSupportUI/media-playback-start.png"));
265 m_Controls->PlayButton->setChecked(false);
266 }
267 else
268 {
269 m_Controls->PlayButton->setText("Pause");
270 m_Controls->PlayButton->setIcon(QIcon(":/OpenCVVideoSupportUI/media-playback-pause.png"));
271 m_Controls->PlayButton->setChecked(true);
272 }
273}
274
275void QmitkOpenCVVideoControls::NewFrameAvailable(mitk::VideoSource* /*videoSource*/)
276{
279 {
280 m_Controls->VideoProgressSlider->setValue(itk::Math::Round<int, double>(m_VideoSource->GetVideoCaptureProperty(CV_CAP_PROP_POS_FRAMES)
281 *(1 / m_VideoSource->GetVideoCaptureProperty(CV_CAP_PROP_FRAME_COUNT)
282 *m_Controls->VideoProgressSlider->maximum())));
283 }
284}
285
286void QmitkOpenCVVideoControls::EndOfVideoSourceReached(mitk::VideoSource* /*videoSource*/)
287{
288 if (m_Controls->RepeatVideoButton->isChecked())
289 {
290 this->Reset();
291 this->Play();
292 }
293 else
294 {
295 this->Stop();
296 }
297}
298
299void QmitkOpenCVVideoControls::SetRenderWindow(QmitkRenderWindow* _RenderWindow)
300{
301 if (m_RenderWindow == _RenderWindow)
302 return;
303
304 // In Reset() m_MultiWidget is used, set it to 0 now for avoiding errors
305 if (_RenderWindow == nullptr)
306 m_RenderWindow = nullptr;
307 this->Reset();
308
309 m_RenderWindow = _RenderWindow;
310
311 if (m_RenderWindow == nullptr)
312 {
313 this->setEnabled(false);
314 }
315 else
316 {
317 this->setEnabled(true);
318 }
319}
320
322{
323 return m_RenderWindow;
324}
325
326void QmitkOpenCVVideoControls::SetVideoBackground(QmitkVideoBackground* _VideoBackground)
327{
328 if (m_VideoBackground == _VideoBackground)
329 return;
330
331 if (m_VideoBackground != nullptr)
332 this->disconnect(m_VideoBackground, SIGNAL(destroyed(QObject*))
333 , this, SLOT(QObjectDestroyed(QObject*)));
334
335 this->Reset();
336
337 m_VideoBackground = _VideoBackground;
338
339 if (m_VideoBackground == nullptr)
340 {
341 m_VideoSource = nullptr;
342 MITK_WARN << "m_MultiWidget is 0";
343 this->setEnabled(false);
344 }
345 else
346 {
347 this->setEnabled(true);
348 m_VideoSource = dynamic_cast<mitk::OpenCVVideoSource*>(m_VideoBackground->GetVideoSource());
349 // preset form entries
350 if (m_VideoSource != nullptr)
351 {
352 if (!m_VideoSource->GetVideoFileName().empty())
353 {
354 m_Controls->FileChooser->SetFile(m_VideoSource->GetVideoFileName());
356 }
357 else if (m_VideoSource->GetGrabbingDeviceNumber() >= 0)
358 m_Controls->GrabbingDeviceNumber->setValue(m_VideoSource->GetGrabbingDeviceNumber());
359
360 m_Controls->UpdateRate->setValue(m_VideoBackground->GetTimerDelay());
361
362 this->connect(m_VideoBackground, SIGNAL(destroyed(QObject*))
363 , this, SLOT(QObjectDestroyed(QObject*)));
364 }
365 else
366 {
367 MITK_WARN << "m_VideoSource is 0";
368 this->setEnabled(false);
369 }
370 }
371}
372
374{
375 return m_VideoBackground;
376}
377
379{
380 if (m_VideoBackground == obj)
381 {
382 m_VideoSource = nullptr;
383 this->SetVideoBackground(nullptr);
384 }
385}
386
388{
389 mitk::IPersistenceService* persistenceService = this->GetPersistenceService();
390
391 if (persistenceService != nullptr)
392 {
393 mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(m_Id);
394 propList->Set("deviceType", q->m_Controls->UseGrabbingDeviceButton->isChecked() ? 0 : 1);
395 propList->Set("grabbingDeviceNumber", q->m_Controls->GrabbingDeviceNumber->value());
396 propList->Set("updateRate", q->m_Controls->UpdateRate->value());
397 propList->Set("repeatVideo", q->m_Controls->RepeatVideoButton->isChecked());
398 }
399 else
400 {
401 MITK_WARN << "Persistence Service not available.";
402 }
403}
404
406{
407 mitk::IPersistenceService* persistenceService = this->GetPersistenceService();
408
409 if (persistenceService != nullptr)
410 {
411 mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(m_Id);
412
413 bool repeatVideo = false;
414 propList->Get("repeatVideo", repeatVideo);
415 q->m_Controls->RepeatVideoButton->setChecked(repeatVideo);
416
417 int updateRate = 25;
418 propList->Get("updateRate", updateRate);
419 q->m_Controls->UpdateRate->setValue(updateRate);
420
421 int grabbingDeviceNumber = 0;
422 propList->Get("grabbingDeviceNumber", grabbingDeviceNumber);
423 q->m_Controls->GrabbingDeviceNumber->setValue(grabbingDeviceNumber);
424
425 int deviceType = 0;
426 propList->Get("deviceType", deviceType);
427 if (deviceType == 0)
428 {
429 q->m_Controls->UseGrabbingDeviceButton->setChecked(true);
430 }
431 else
432 {
433 q->m_Controls->UseVideoFileButton->setChecked(true);
434 }
435 }
436 else
437 {
438 MITK_WARN << "Persistence Service not available.";
439 }
440}
441
442void QmitkOpenCVVideoControls::AfterPropertyListReplaced(const std::string& id, mitk::PropertyList* /*propertyList*/)
443{
444 if (id == d->m_Id)
445 d->FromPropertyList();
446}
QmitkOpenCVVideoControlsPrivate(QmitkOpenCVVideoControls *q, const std::string &id)
PERSISTENCE_GET_SERVICE_METHOD_MACRO QmitkOpenCVVideoControls * q
Offers widgets to play/pause/stop a video on a certain render window with the use of an !...
void AfterPropertyListReplaced(const std::string &id, mitk::PropertyList *propertyList) override
QmitkVideoBackground * m_VideoBackground
void on_RepeatVideoButton_clicked(bool checked=false)
void SetVideoBackground(QmitkVideoBackground *_VideoBackground)
void on_UseGrabbingDeviceButton_clicked(bool checked=false)
void QObjectDestroyed(QObject *obj=nullptr)
void on_StopButton_clicked(bool checked=false)
void on_PlayButton_clicked(bool checked=false)
void NewOpenCVFrameAvailable(const IplImage *)
mitk::OpenCVVideoSource * m_VideoSource
void on_UseVideoFileButton_clicked(bool checked=false)
Ui::QmitkOpenCVVideoControls * m_Controls
QmitkVideoBackground * GetVideoBackground() const
void SetRenderWindow(QmitkRenderWindow *_RenderWindow)
QmitkRenderWindow * GetRenderWindow() const
void EndOfVideoSourceReached(mitk::VideoSource *videoSource)
void NewFrameAvailable(mitk::VideoSource *videoSource)
QmitkOpenCVVideoControls(QmitkVideoBackground *_VideoBackground, QmitkRenderWindow *_RenderWindow, QWidget *parent=nullptr, Qt::WindowFlags f={})
void on_VideoProgressSlider_valueChanged(int value)
virtual void SetVideoCameraInput(int cameraindex, bool useCVCAMLib=false)
virtual const IplImage * GetCurrentFrame()
virtual int SetVideoCaptureProperty(int property_id, double value)
virtual double GetVideoCaptureProperty(int property_id)
virtual void SetVideoFileInput(const char *filename, bool repeatVideo, bool useCVCAMLib=false)