Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkOpenCVVideoControls.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <QmitkVideoBackground.h>
19 #include <QmitkRenderWindow.h>
20 #include <mitkOpenCVVideoSource.h>
21 
23 
24 class QmitkOpenCVVideoControlsPrivate
25 {
26 public:
27 
28  QmitkOpenCVVideoControlsPrivate(QmitkOpenCVVideoControls* q, const std::string& id)
29  : q(q)
30  , m_Id(id)
31  {}
32 
37 
39 
43  std::string m_Id;
44  void ToPropertyList();
45  void FromPropertyList();
46 };
47 
49  , QmitkRenderWindow* _RenderWindow
50  , QWidget * parent, Qt::WindowFlags f)
51  : QWidget(parent, f)
52  , m_VideoBackground(nullptr)
53  , m_RenderWindow(nullptr)
54  , m_VideoSource(nullptr)
55  , m_Controls(new Ui::QmitkOpenCVVideoControls)
56  , m_SliderCurrentlyMoved(false)
57  , d(new QmitkOpenCVVideoControlsPrivate(this, "QmitkOpenCVVideoControls"))
58 {
59  m_Controls->setupUi(this);
60  m_Controls->FileChooser->SetFileMustExist(true);
61  m_Controls->FileChooser->SetSelectDir(false);
62 
63  this->SetRenderWindow(_RenderWindow);
64  this->SetVideoBackground(_VideoBackground);
65  d->FromPropertyList();
66 
67  mitk::IPersistenceService* persistenceService = d->GetPersistenceService();
68 
69  if (persistenceService != nullptr)
70  {
71  persistenceService->AddPropertyListReplacedObserver(this);
72  }
73  else
74  {
75  MITK_WARN << "No Persistence Service available in constructor";
76  }
77 }
78 
80 {
82  {
83  this->Stop(); // emulate stop
84  }
85 
86  mitk::IPersistenceService* persistenceService = d->GetPersistenceService();
87  if (persistenceService != nullptr)
88  {
89  persistenceService->RemovePropertyListReplacedObserver(this);
90  }
91  else
92  {
93  MITK_WARN << "No Persistence Service available in destructor";
94  }
95 
96  d->ToPropertyList();
97 }
98 
100 {
101  m_Controls->GrabbingDevicePanel->setEnabled(true);
102  m_Controls->VideoFilePanel->setEnabled(false);
103 }
104 
106 {
107  m_Controls->GrabbingDevicePanel->setEnabled(false);
108  m_Controls->VideoFilePanel->setEnabled(true);
109  m_Controls->FileChooser->setEnabled(true);
110 }
111 
112 //void QmitkOpenCVVideoControls::on_VideoProgressSlider_sliderMoved( int value )
113 //{
114 // MITK_DEBUG << "progress bar slider clicked";
115 // double progressRatio = static_cast<double>(value)/static_cast<double>(m_Controls->VideoProgressSlider->maximum());
116 // MITK_DEBUG << "progressRatio" << progressRatio;
117 // m_VideoSource->SetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO, progressRatio);
118 //}
119 
121 {
122  m_SliderCurrentlyMoved = true;
123  // temporary pause the video while sliding
126  MITK_DEBUG << "freezing video with old pos ratio: " << m_VideoSource->GetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO);
127 }
128 
130 {
131  double progressRatio = static_cast<double>(m_Controls->VideoProgressSlider->value())
132  / static_cast<double>(m_Controls->VideoProgressSlider->maximum());
133  m_VideoSource->SetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO, progressRatio);
134 
135  MITK_DEBUG << "resuming video with new pos ratio: " << progressRatio;
136  // resume the video ( if it was not paused by the user)
137  if (m_VideoSource->GetCapturePaused() && m_Controls->PlayButton->isChecked())
139  m_SliderCurrentlyMoved = false;
140 }
141 
143 {
144  MITK_INFO << "repeat video clicked";
145  m_VideoSource->SetRepeatVideo(checked);
146 }
147 
149 {
150  MITK_INFO << "play button clicked";
151  if (checked)
152  {
153  this->Play();
154  }
155  else
156  {
157  // show pause button
158  this->SwitchPlayButton(true);
160  }
161 }
162 
164 {
165  this->Stop();
166 }
167 
169 {
171  {
172  this->SwitchPlayButton(false);
174  }
175  else
176  {
177  if (m_Controls->UseGrabbingDeviceButton->isChecked())
178  {
179  m_VideoSource->SetVideoCameraInput(m_Controls->GrabbingDeviceNumber->text().toInt(), false);
180  m_Controls->VideoFileControls->setEnabled(false);
181  }
182  else
183  {
184  m_VideoSource->SetVideoFileInput(m_Controls->FileChooser->GetFile().c_str(), m_Controls->RepeatVideoButton->isChecked(), false);
185  m_VideoSource->SetRepeatVideo(m_Controls->RepeatVideoButton->isChecked());
186  m_Controls->VideoProgressSlider->setValue(0);
187  }
188 
190 
192  {
193  MITK_ERROR << "Video could not be initialized!";
194  m_Controls->PlayButton->setChecked(false);
195  }
196  else
197  {
198  int hertz = m_Controls->UpdateRate->text().toInt();
199  int updateTime = itk::Math::Round<int, double>(1000.0 / hertz);
200 
201  // resets the whole background
202  m_VideoBackground->SetTimerDelay(updateTime);
204  this->connect(m_VideoBackground, SIGNAL(NewFrameAvailable(mitk::VideoSource*))
205  , this, SLOT(NewFrameAvailable(mitk::VideoSource*)));
208 
210  this->m_Controls->StopButton->setEnabled(true);
211  // show video file controls
212  if (m_Controls->UseVideoFileButton->isChecked())
213  {
214  m_Controls->VideoFileControls->setEnabled(true);
215  m_Controls->RepeatVideoButton->setEnabled(true);
216  m_Controls->VideoProgressSlider->setEnabled(true);
217  }
218  // show pause button
219  this->SwitchPlayButton(false);
220  // disable other controls
221  m_Controls->GrabbingDevicePanel->setEnabled(false);
222  m_Controls->VideoFilePanel->setEnabled(false);
223  m_Controls->UseGrabbingDeviceButton->setEnabled(false);
224  m_Controls->UseVideoFileButton->setEnabled(false);
225  m_Controls->UpdateRatePanel->setEnabled(false);
226  }
227  }
228 }
229 
231 {
232  // disable video file controls, stop button and show play button again
233  m_Controls->UseGrabbingDeviceButton->setEnabled(true);
234  m_Controls->UseVideoFileButton->setEnabled(true);
235  if (m_Controls->UseGrabbingDeviceButton->isChecked())
237  else
239 
240  m_Controls->UpdateRatePanel->setEnabled(true);
241  m_Controls->VideoFileControls->setEnabled(false);
242  this->m_Controls->StopButton->setEnabled(false);
243  this->SwitchPlayButton(true);
244 
245  if (m_VideoBackground)
246  {
248 
249  if (m_RenderWindow)
251 
252  this->disconnect(m_VideoBackground, SIGNAL(NewFrameAvailable(mitk::VideoSource*))
253  , this, SLOT(NewFrameAvailable(mitk::VideoSource*)));
254  }
255  if (m_VideoSource != 0)
257 }
258 
260 {
261  this->Stop();
262 }
263 
265 {
266  if (paused)
267  {
268  m_Controls->PlayButton->setText("Play");
269  m_Controls->PlayButton->setIcon(QIcon(":/OpenCVVideoSupportUI/media-playback-start.png"));
270  m_Controls->PlayButton->setChecked(false);
271  }
272  else
273  {
274  m_Controls->PlayButton->setText("Pause");
275  m_Controls->PlayButton->setIcon(QIcon(":/OpenCVVideoSupportUI/media-playback-pause.png"));
276  m_Controls->PlayButton->setChecked(true);
277  }
278 }
279 
281 {
284  m_Controls->VideoProgressSlider->setValue(itk::Math::Round<int, double>(m_VideoSource->GetVideoCaptureProperty(CV_CAP_PROP_POS_AVI_RATIO)
285  *m_Controls->VideoProgressSlider->maximum()));
286 }
287 
289 {
290  if (m_Controls->RepeatVideoButton->isChecked())
291  {
292  this->Reset();
293  this->Play();
294  }
295  else
296  {
297  this->Stop();
298  }
299 }
300 
302 {
303  if (m_RenderWindow == _RenderWindow)
304  return;
305 
306  // In Reset() m_MultiWidget is used, set it to 0 now for avoiding errors
307  if (_RenderWindow == 0)
308  m_RenderWindow = 0;
309  this->Reset();
310 
311  m_RenderWindow = _RenderWindow;
312 
313  if (m_RenderWindow == 0)
314  {
315  this->setEnabled(false);
316  }
317  else
318  {
319  this->setEnabled(true);
320  }
321 }
322 
324 {
325  return m_RenderWindow;
326 }
327 
329 {
330  if (m_VideoBackground == _VideoBackground)
331  return;
332 
333  if (m_VideoBackground != nullptr)
334  this->disconnect(m_VideoBackground, SIGNAL(destroyed(QObject*))
335  , this, SLOT(QObjectDestroyed(QObject*)));
336 
337  this->Reset();
338 
339  m_VideoBackground = _VideoBackground;
340 
341  if (m_VideoBackground == nullptr)
342  {
343  m_VideoSource = 0;
344  MITK_WARN << "m_MultiWidget is 0";
345  this->setEnabled(false);
346  }
347  else
348  {
349  this->setEnabled(true);
351  // preset form entries
352  if (m_VideoSource != nullptr)
353  {
354  if (!m_VideoSource->GetVideoFileName().empty())
355  {
356  m_Controls->FileChooser->SetFile(m_VideoSource->GetVideoFileName());
358  }
359  else if (m_VideoSource->GetGrabbingDeviceNumber() >= 0)
360  m_Controls->GrabbingDeviceNumber->setValue(m_VideoSource->GetGrabbingDeviceNumber());
361 
362  m_Controls->UpdateRate->setValue(m_VideoBackground->GetTimerDelay());
363 
364  this->connect(m_VideoBackground, SIGNAL(destroyed(QObject*))
365  , this, SLOT(QObjectDestroyed(QObject*)));
366  }
367  else
368  {
369  MITK_WARN << "m_VideoSource is 0";
370  this->setEnabled(false);
371  }
372  }
373 }
374 
376 {
377  return m_VideoBackground;
378 }
379 
380 void QmitkOpenCVVideoControls::QObjectDestroyed(QObject * obj /*= 0 */)
381 {
382  if (m_VideoBackground == obj)
383  {
384  m_VideoSource = nullptr;
385  this->SetVideoBackground(nullptr);
386  }
387 }
388 
389 void QmitkOpenCVVideoControlsPrivate::ToPropertyList()
390 {
391  mitk::IPersistenceService* persistenceService = this->GetPersistenceService();
392 
393  if (persistenceService != nullptr)
394  {
395  mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(m_Id);
396  propList->Set("deviceType", q->m_Controls->UseGrabbingDeviceButton->isChecked() ? 0 : 1);
397  propList->Set("grabbingDeviceNumber", q->m_Controls->GrabbingDeviceNumber->value());
398  propList->Set("updateRate", q->m_Controls->UpdateRate->value());
399  propList->Set("repeatVideo", q->m_Controls->RepeatVideoButton->isChecked());
400  }
401  else
402  {
403  MITK_WARN << "Persistence Service not available.";
404  }
405 }
406 
407 void QmitkOpenCVVideoControlsPrivate::FromPropertyList()
408 {
409  mitk::IPersistenceService* persistenceService = this->GetPersistenceService();
410 
411  if (persistenceService != nullptr)
412  {
413  mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(m_Id);
414 
415  bool repeatVideo = false;
416  propList->Get("repeatVideo", repeatVideo);
417  q->m_Controls->RepeatVideoButton->setChecked(repeatVideo);
418 
419  int updateRate = 25;
420  propList->Get("updateRate", updateRate);
421  q->m_Controls->UpdateRate->setValue(updateRate);
422 
423  int grabbingDeviceNumber = 0;
424  propList->Get("grabbingDeviceNumber", grabbingDeviceNumber);
425  q->m_Controls->GrabbingDeviceNumber->setValue(grabbingDeviceNumber);
426 
427  int deviceType = 0;
428  propList->Get("deviceType", deviceType);
429  if (deviceType == 0)
430  {
431  q->m_Controls->UseGrabbingDeviceButton->setChecked(true);
432  }
433  else
434  {
435  q->m_Controls->UseVideoFileButton->setChecked(true);
436  }
437  }
438  else
439  {
440  MITK_WARN << "Persistence Service not available.";
441  }
442 }
443 
444 void QmitkOpenCVVideoControls::AfterPropertyListReplaced(const std::string& id, mitk::PropertyList* /*propertyList*/)
445 {
446  if (id == d->m_Id)
447  d->FromPropertyList();
448 }
virtual int SetVideoCaptureProperty(int property_id, double value)
virtual void StopCapturing() override
virtual void AddPropertyListReplacedObserver(PropertyListReplacedObserver *observer)=0
void RemoveRenderWindow(vtkRenderWindow *renderWindow)
removes a renderwindow = disables video background there
void on_UseGrabbingDeviceButton_clicked(bool checked=false)
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual std::string GetVideoFileName() const
void Disable()
disables visualization of the video.
void NewFrameAvailable(mitk::VideoSource *videoSource)
virtual double GetVideoCaptureProperty(int property_id)
void on_StopButton_clicked(bool checked=false)
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
virtual bool IsCapturingEnabled() const
void AddRenderWindow(vtkRenderWindow *renderWindow)
add a RenderWindow in which the video is displayed. -> must be initialized before enabling the backgr...
void EndOfVideoSourceReached(mitk::VideoSource *videoSource)
Key-value list holding instances of BaseProperty.
virtual vtkRenderWindow * GetVtkRenderWindow() override
QmitkVideoBackground * GetVideoBackground() const
Ui::QmitkOpenCVVideoControls * m_Controls
void NewOpenCVFrameAvailable(const IplImage *)
virtual void StartCapturing() override
QmitkOpenCVVideoControls(QmitkVideoBackground *_VideoBackground, QmitkRenderWindow *_RenderWindow, QWidget *parent=0, Qt::WindowFlags f=0)
virtual void SetRepeatVideo(bool _arg)
Offers widgets to play/pause/stop a video on a certain render window with the use of an !initialized!...
#define MITK_WARN
Definition: mitkLogMacros.h:23
virtual void SetVideoFileInput(const char *filename, bool repeatVideo, bool useCVCAMLib=false)
QmitkRenderWindow * GetRenderWindow() const
virtual void SetVideoCameraInput(int cameraindex, bool useCVCAMLib=false)
virtual void PauseCapturing() override
void SetVideoBackground(QmitkVideoBackground *_VideoBackground)
MITK implementation of the QVTKWidget.
void on_PlayButton_clicked(bool checked=false)
mitk::VideoSource * GetVideoSource()
virtual const IplImage * GetCurrentFrame()
#define PERSISTENCE_GET_SERVICE_METHOD_MACRO
virtual mitk::PropertyList::Pointer GetPropertyList(std::string &id, bool *existed=0)=0
void on_UseVideoFileButton_clicked(bool checked=false)
virtual short GetGrabbingDeviceNumber() const
void SetTimerDelay(int ms)
sets the update rate of the video in milli seconds, by default 25.
void AfterPropertyListReplaced(const std::string &id, mitk::PropertyList *propertyList) override
mitk::IPropertyPersistence * GetPersistenceService()
void SetRenderWindow(QmitkRenderWindow *_RenderWindow)
virtual void RemovePropertyListReplacedObserver(PropertyListReplacedObserver *observer)=0
virtual bool GetCapturePaused() const
void on_RepeatVideoButton_clicked(bool checked=false)
mitk::OpenCVVideoSource * m_VideoSource
QmitkVideoBackground * m_VideoBackground