niftkIGIDataSourcePlaybackWidget.cxx 17.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=============================================================================

  NifTK: A software platform for medical image computing.

  Copyright (c) University College London (UCL). All rights reserved.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  See LICENSE.txt in the top level directory for details.

=============================================================================*/

#include "niftkIGIDataSourcePlaybackWidget.h"
16
#include "niftkIGIDataSourcePlaybackControlsWidget.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <niftkIGIInitialisationDialog.h>
#include <niftkIGIConfigurationDialog.h>
#include <QMessageBox>
#include <QTableWidgetItem>
#include <QVector>
#include <QDateTime>
#include <QTextStream>
#include <QList>
#include <QPainter>

namespace niftk
{

//-----------------------------------------------------------------------------
31
IGIDataSourcePlaybackWidget::IGIDataSourcePlaybackWidget(mitk::DataStorage::Pointer dataStorage,
32
    IGIDataSourceManager* manager,
33
    bool verticalLayout,
34
    QWidget *parent)
35
: m_FixedRecordTime(0,0,0,0)
36 37
, m_RecordTime(0,0,0,0)
, m_MSecFixedRecordTime(0)
38
, m_VerticalLayout (verticalLayout)
39 40 41 42
{
  m_Manager = manager;
  Ui_IGIDataSourcePlaybackWidget::setupUi(parent);

43 44 45 46 47 48 49 50 51 52 53 54
  if ( m_VerticalLayout )
  {
    m_PlaybackControlsWidget = new IGIDataSourcePlaybackControlsWidget ( m_ToolManagerPlaybackGroupBox_Vertical );
    m_ToolManagerPlaybackGroupBox_Horizontal->setVisible (false);
  }
  else
  {
    m_PlaybackControlsWidget = new IGIDataSourcePlaybackControlsWidget ( m_ToolManagerPlaybackGroupBox_Horizontal );
    m_ToolManagerPlaybackGroupBox_Vertical->setVisible (false);
  }


55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  m_PlayPushButton->setIcon(QIcon(":/niftkIGIDataSourcesManagerResources/play.png"));
  m_RecordPushButton->setIcon(QIcon(":/niftkIGIDataSourcesManagerResources/record.png"));
  m_StopPushButton->setIcon(QIcon(":/niftkIGIDataSourcesManagerResources/stop.png"));

  m_PlayPushButton->setEnabled(true);
  m_RecordPushButton->setEnabled(true);
  m_StopPushButton->setEnabled(false);

  m_DirectoryChooser->setFilters(ctkPathLineEdit::Dirs);
  m_DirectoryChooser->setOptions(ctkPathLineEdit::ShowDirsOnly);
  m_DirectoryChooser->setEnabled(true);

  bool ok = QObject::connect(m_RecordPushButton, SIGNAL(clicked()),
                        this, SLOT(OnRecordStart()) );
  assert(ok);
  ok = QObject::connect(m_StopPushButton, SIGNAL(clicked()),
                        this, SLOT(OnStop()) );
  assert(ok);
  ok = QObject::connect(m_PlayPushButton, SIGNAL(clicked()),
                        this, SLOT(OnPlayStart()));
  assert(ok);
  ok = QObject::connect(m_Manager, SIGNAL(PlaybackTimerAdvanced(int)),
                        this, SLOT(OnPlaybackTimeAdvanced(int)));
  assert(ok);
  ok = QObject::connect(m_Manager, SIGNAL(TimerUpdated(QString, QString)),
                        this, SLOT(OnTimerUpdated(QString, QString)));
  assert(ok);
  ok = QObject::connect(m_GrabScreenCheckbox, SIGNAL(clicked(bool)),
                        this, SLOT(OnGrabScreen(bool)));
  assert(ok);
85 86 87 88 89

  m_FixedRecordTimer = new QTimer(this);
  ok = QObject::connect( m_FixedRecordTimer, SIGNAL( timeout() ),
                         this, SLOT( OnStop() ) );
  assert(ok);
90

91 92
  ok = QObject::connect(m_Manager, SIGNAL(UpdateFinishedRendering()),
                        this, SLOT(OnUpdateRecordTimeDisplay()));
93
  assert(ok);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  //signals from the playback controls widget
  ok = QObject::connect(m_PlaybackControlsWidget->m_TimeStampEdit, SIGNAL(editingFinished()),
                        this, SLOT(OnPlaybackTimestampEditFinished()));
  assert(ok);
  ok = QObject::connect(m_PlaybackControlsWidget->m_PlayingPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnPlayingPushButtonClicked(bool)));
  assert(ok);
  ok = QObject::connect(m_PlaybackControlsWidget->m_EndPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnEndPushButtonClicked(bool)));
  assert(ok);
  ok = QObject::connect(m_PlaybackControlsWidget->m_PlaybackSlider, SIGNAL(sliderReleased()),
                        this, SLOT(OnSliderReleased()));
  assert(ok);
  ok = QObject::connect(m_PlaybackControlsWidget->m_StartPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnStartPushButtonClicked(bool)));
  assert(ok);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}


//-----------------------------------------------------------------------------
IGIDataSourcePlaybackWidget::~IGIDataSourcePlaybackWidget()
{
  if (m_Manager->IsPlayingBack())
  {
    m_Manager->StopPlayback();
  }

  bool ok = QObject::disconnect(m_RecordPushButton, SIGNAL(clicked()),
                           this, SLOT(OnRecordStart()) );
  assert(ok);
  ok = QObject::disconnect(m_StopPushButton, SIGNAL(clicked()),
                           this, SLOT(OnStop()) );
  assert(ok);
  ok = QObject::disconnect(m_PlayPushButton, SIGNAL(clicked()),
                           this, SLOT(OnPlayStart()));
  assert(ok);
  ok = QObject::disconnect(m_Manager, SIGNAL(PlaybackTimerAdvanced(int)),
                           this, SLOT(OnPlaybackTimeAdvanced(int)));
  assert(ok);
  ok = QObject::disconnect(m_Manager, SIGNAL(TimerUpdated(QString, QString)),
                           this, SLOT(OnTimerUpdated(QString, QString)));
  assert(ok);
  ok = QObject::disconnect(m_GrabScreenCheckbox, SIGNAL(clicked(bool)),
                        this, SLOT(OnGrabScreen(bool)));
  assert(ok);
140 141 142 143 144 145

  m_FixedRecordTimer->stop();
  ok = QObject::disconnect(m_FixedRecordTimer, SIGNAL(timeout()),
                           this, SLOT(OnStop()) );
  assert(ok);

146 147
  ok = QObject::disconnect(m_Manager, SIGNAL(UpdateFinishedRendering()),
                           this, SLOT(OnUpdateRecordTimeDisplay()));
148
  assert(ok);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
 //signals from the playback controls widget
  ok = QObject::disconnect(m_PlaybackControlsWidget->m_TimeStampEdit, SIGNAL(editingFinished()),
                        this, SLOT(OnPlaybackTimestampEditFinished()));
  assert(ok);
  ok = QObject::disconnect(m_PlaybackControlsWidget->m_PlayingPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnPlayingPushButtonClicked(bool)));
  assert(ok);
  ok = QObject::disconnect(m_PlaybackControlsWidget->m_EndPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnEndPushButtonClicked(bool)));
  assert(ok);
  ok = QObject::disconnect(m_PlaybackControlsWidget->m_PlaybackSlider, SIGNAL(sliderReleased()),
                        this, SLOT(OnSliderReleased()));
  assert(ok);
  ok = QObject::disconnect(m_PlaybackControlsWidget->m_StartPushButton, SIGNAL(clicked(bool)),
                        this, SLOT(OnStartPushButtonClicked(bool)));
  assert(ok);
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  // m_Manager belongs to the calling widget
}

//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::StartRecording()
{
  this->OnRecordStart();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::StopRecording()
{
  this->OnStop();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::PauseUpdate()
{
  m_Manager->StopUpdateTimer();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::RestartUpdate()
{
  m_Manager->StartUpdateTimer();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnPlayStart()
{
  if (m_PlayPushButton->isChecked())
  {
    QString playbackpath = m_DirectoryChooser->currentPath();

    // playback button should only be enabled if there's a path in m_DirectoryChooser.
    if (playbackpath.isEmpty())
    {
      m_PlayPushButton->setChecked(false);
    }
    else
    {
      try
      {
213 214
        IGIDataSourceI::IGITimeType overallStartTime = std::numeric_limits<IGIDataSourceI::IGITimeType>::max();
        IGIDataSourceI::IGITimeType overallEndTime   = std::numeric_limits<IGIDataSourceI::IGITimeType>::min();
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        int sliderMaximum = 0;
        int sliderSingleStep = 0;
        int sliderPageStep = 0;
        int sliderValue = 0;

        m_Manager->StartPlayback(playbackpath,
                                 playbackpath + QDir::separator() + "descriptor.cfg",
                                 overallStartTime,
                                 overallEndTime,
                                 sliderMaximum,
                                 sliderSingleStep,
                                 sliderPageStep,
                                 sliderValue
                                 );

230 231 232 233
        m_PlaybackControlsWidget->m_PlaybackSlider->setMinimum(sliderValue);
        m_PlaybackControlsWidget->m_PlaybackSlider->setMaximum(sliderMaximum);
        m_PlaybackControlsWidget->m_PlaybackSlider->setSingleStep(sliderSingleStep);
        m_PlaybackControlsWidget->m_PlaybackSlider->setPageStep(sliderPageStep);
234

235 236 237 238 239 240 241 242
        if ( m_VerticalLayout )
        {
          m_ToolManagerPlaybackGroupBox_Vertical->setCollapsed(false);
        }
        else
        {
          m_ToolManagerPlaybackGroupBox_Horizontal->setCollapsed(false);
        }
243 244 245 246 247 248 249

        // Can stop playback with stop button (in addition to unchecking the playbutton)
        m_StopPushButton->setEnabled(true);

        // For now, cannot start recording directly from playback mode.
        // could be possible: leave this enabled and simply stop all playback when user clicks on record.
        m_RecordPushButton->setEnabled(false);
250
        m_FixedRecordTimeInterval->setEnabled(false);
251 252 253
        m_PlaybackControlsWidget->m_TimeStampEdit->setReadOnly(false);
        m_PlaybackControlsWidget->m_PlaybackSlider->setEnabled(true);
        m_PlaybackControlsWidget->m_PlaybackSlider->setValue(sliderValue);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

        // Stop the user editing the path.
        // In order to start editing the path, you must stop playing back.
        m_DirectoryChooser->setEnabled(false);
        m_PlayPushButton->setEnabled(false);
      }
      catch (const mitk::Exception& e)
      {
        MITK_ERROR << "Caught exception while trying to initialise data playback: " << e.GetDescription();

        try
        {
          // try stopping playback if we had it started already on some sources.
          m_Manager->StopPlayback();
        }
        catch (mitk::Exception& e)
        {
          // Swallow, as we have a messge box anyhow.
          MITK_ERROR << "Caught exception while trying to stop data source playback during an exception handler."
                     << std::endl << e.GetDescription();
        }

        QMessageBox msgbox;
        msgbox.setText("Data playback initialisation failed.");
        msgbox.setInformativeText(e.GetDescription());
        msgbox.setIcon(QMessageBox::Critical);
        msgbox.exec();

        // Switch off playback. hopefully, user will fix the error
        // and can then try to click on playback again.
        m_PlayPushButton->setChecked(false);
      }
    }
  }
  else
  {
    m_Manager->StopPlayback();
    m_StopPushButton->setEnabled(false);
    m_RecordPushButton->setEnabled(true);
293
    m_FixedRecordTimeInterval->setEnabled(true);
294 295 296
    m_PlaybackControlsWidget->m_TimeStampEdit->setReadOnly(true);
    m_PlaybackControlsWidget->m_PlaybackSlider->setEnabled(false);
    m_PlaybackControlsWidget->m_PlaybackSlider->setValue(0);
297 298 299 300
  }
}


301 302 303 304 305 306 307
//-----------------------------------------------------------------------------
bool IGIDataSourcePlaybackWidget::IsRecording() const
{
  return m_Manager->IsRecording();
}


308 309 310 311 312 313 314 315 316 317
//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnRecordStart()
{
  if (!m_RecordPushButton->isEnabled())
  {
    // shortcut in case we are already recording.
    return;
  }

  m_RecordPushButton->setEnabled(false);
318
  m_FixedRecordTimeInterval->setEnabled(false);
319 320 321 322 323 324 325 326 327
  m_StopPushButton->setEnabled(true);
  assert(!m_PlayPushButton->isChecked());
  m_PlayPushButton->setEnabled(false);

  QString directoryName = m_Manager->GetDirectoryName();
  QDir directory(directoryName);
  m_DirectoryChooser->setCurrentPath(directory.absolutePath());
  m_DirectoryChooser->setEnabled(false);

328
  // If a fixed recording time has been set then initiate the timer
329
  m_FixedRecordTime = m_FixedRecordTimeInterval->time();
330 331 332 333 334 335 336

  // If a fixed recording time has been set then start a timer
  if ( m_FixedRecordTime.hour()   ||
       m_FixedRecordTime.minute() ||
       m_FixedRecordTime.second() ||
       m_FixedRecordTime.msec() )
  {
337
    m_MSecFixedRecordTime =
338 339 340 341 342
      m_FixedRecordTime.msec() +
      1000*( m_FixedRecordTime.second() +
      60*( m_FixedRecordTime.minute() +
      60*m_FixedRecordTime.hour() ) );

343
    m_FixedRecordTimer->start( m_MSecFixedRecordTime );
344
    MITK_INFO << "Starting countdown timer:" << m_MSecFixedRecordTime << " (ms).";
345 346 347
  }
  else
  {
348
    m_MSecFixedRecordTime = 0;
349
  }
350
  m_RecordTime.start();
351

352 353
  try
  {
354 355 356
    m_Manager->StartRecording(directory.absolutePath());
  }
  catch (mitk::Exception& e)
357 358 359 360 361 362 363 364 365 366
  {
    QMessageBox msgbox;
    msgbox.setText("Error creating descriptor file.");
    msgbox.setInformativeText("Cannot create descriptor file due to: " + QString::fromStdString(e.GetDescription()));
    msgbox.setIcon(QMessageBox::Warning);
    msgbox.exec();
  }
}


367 368 369
//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnUpdateRecordTimeDisplay()
{
370
  if (m_StopPushButton->isEnabled() && !m_Manager->IsPlayingBack())
371
  {
372
    QTime t( 0, 0, 0, 0 );
373

374 375 376 377 378 379 380 381 382 383 384
    if ( m_FixedRecordTimer->isActive() )
    {
      t = t.addMSecs( m_MSecFixedRecordTime - m_RecordTime.elapsed() );
    }
    else
    {
      t = t.addMSecs( m_RecordTime.elapsed() );
    }

    m_FixedRecordTimeInterval->setTime( t );
  }
385 386
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnStop()
{
  if (m_PlayPushButton->isChecked())
  {
    m_Manager->SetIsPlayingBackAutomatically(false);
    m_Manager->StopPlayback();
    m_PlayPushButton->setChecked(false);
  }
  else
  {
    m_Manager->StopRecording();
    m_RecordPushButton->setChecked(false);
  }
  m_RecordPushButton->setEnabled(true);
402
  m_FixedRecordTimeInterval->setEnabled(true);
403 404 405
  m_StopPushButton->setEnabled(false);
  m_PlayPushButton->setEnabled(true);
  m_DirectoryChooser->setEnabled(true);
406 407 408

  m_FixedRecordTimer->stop();
  m_FixedRecordTimeInterval->setTime(m_FixedRecordTime);
409
  m_FixedRecordTimeInterval->setEnabled(true);
410 411 412 413 414
}

//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnPlaybackTimestampEditFinished()
{
415
  int result = m_Manager->ComputePlaybackTimeSliderValue(m_PlaybackControlsWidget->m_TimeStampEdit->text());
416 417
  if (result != -1)
  {
418
    m_PlaybackControlsWidget->m_PlaybackSlider->setValue(result);
419 420 421 422 423 424 425 426 427
    this->OnSliderReleased();
  }
}

//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnPlaybackTimeAdvanced(int newSliderValue)
{
  if (newSliderValue != -1)
  {
428 429 430
    m_PlaybackControlsWidget->m_PlaybackSlider->blockSignals(true);
    m_PlaybackControlsWidget->m_PlaybackSlider->setValue(newSliderValue);
    m_PlaybackControlsWidget->m_PlaybackSlider->blockSignals(false);
431 432 433 434 435 436 437 438
  }
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnTimerUpdated(QString rawString, QString humanReadableString)
{
  // Only update text if user is not editing
439
  if (!m_PlaybackControlsWidget->m_TimeStampEdit->hasFocus())
440
  {
441
    m_PlaybackControlsWidget->m_TimeStampEdit->blockSignals(true);
442 443
    // Avoid flickering the text field. it makes copy-n-paste impossible
    // during playback mode because it resets the selection every few milliseconds.
444
    if (m_PlaybackControlsWidget->m_TimeStampEdit->text() != humanReadableString)
445
    {
446
      m_PlaybackControlsWidget->m_TimeStampEdit->setText(humanReadableString);
447
    }
448
    if (m_PlaybackControlsWidget->m_TimeStampEdit->toolTip() != rawString)
449
    {
450
      m_PlaybackControlsWidget->m_TimeStampEdit->setToolTip(rawString);
451
    }
452
    m_PlaybackControlsWidget->m_TimeStampEdit->blockSignals(false);
453 454 455 456 457 458 459 460 461 462 463 464 465
  }
}

//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnPlayingPushButtonClicked(bool isChecked)
{
  m_Manager->SetIsPlayingBackAutomatically(isChecked);
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnEndPushButtonClicked(bool /*isChecked*/)
{
466
  m_PlaybackControlsWidget->m_PlaybackSlider->setValue(m_PlaybackControlsWidget->m_PlaybackSlider->maximum());
467 468 469 470 471 472 473
  this->OnSliderReleased();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnStartPushButtonClicked(bool /*isChecked*/)
{
474
  m_PlaybackControlsWidget->m_PlaybackSlider->setValue(m_PlaybackControlsWidget->m_PlaybackSlider->minimum());
475 476 477 478 479 480 481
  this->OnSliderReleased();
}


//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnSliderReleased()
{
Matt Clarkson's avatar
Matt Clarkson committed
482 483
  IGIDataSourceI::IGITimeType time =
    m_Manager->ComputeTimeFromSlider(m_PlaybackControlsWidget->m_PlaybackSlider->value());
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  m_Manager->SetPlaybackTime(time);
}

//-----------------------------------------------------------------------------
void IGIDataSourcePlaybackWidget::OnGrabScreen(bool isChecked)
{
  QString directoryName = this->m_DirectoryChooser->currentPath();
  if (directoryName.length() == 0)
  {
    directoryName = m_Manager->GetDirectoryName();
    this->m_DirectoryChooser->setCurrentPath(directoryName);
  }
  m_Manager->SetIsGrabbingScreen(directoryName, isChecked);
}

} // end namespace