pqPlayBackEventsDialog.cxx 16.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*=========================================================================

   Program: ParaView
   Module:    pqPlayBackEventsDialog.cxx

   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
10
   under the terms of the ParaView license version 1.2.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

33
#include "pqPlayBackEventsDialog.h"
34
#include "pqCommentEventPlayer.h"
35 36 37 38 39 40 41 42 43
#include "pqEventDispatcher.h"
#include "pqEventPlayer.h"
#include "pqTestUtility.h"

#include "ui_pqPlayBackEventsDialog.h"

#include <QCheckBox>
#include <QFile>
#include <QFileDialog>
44
#include <QMessageBox>
45
#include <QMoveEvent>
46 47 48
#include <QProgressBar>
#include <QPushButton>
#include <QStringListModel>
49
#include <QTableWidget>
50 51 52 53 54 55 56 57 58 59 60
#include <QTextStream>
#include <QTimer>

#include <QDebug>

//////////////////////////////////////////////////////////////////////////////////
// pqImplementation

struct pqPlayBackEventsDialog::pqImplementation
{
public:
61 62
  pqImplementation(
    pqEventPlayer& player, pqEventDispatcher& dispatcher, pqTestUtility* testUtility);
63 64 65 66
  ~pqImplementation();
  void init(pqPlayBackEventsDialog* dialog);
  void setProgressBarsValue(int value);
  void setProgressBarValue(int row, int value);
67
  QString setMaxLenght(const QString& name, int max);
68 69 70

  Ui::pqPlayBackEventsDialog Ui;

71 72 73
  pqEventPlayer& Player;
  pqEventDispatcher& Dispatcher;
  pqTestUtility* TestUtility;
74

75 76 77 78 79 80
  int CurrentLine; // Add counter to the Dispatcher
  int MaxLines;
  int CurrentFile;
  QStringList Filenames;
  QStringList CurrentEvent;
  QRect OldRect;
81 82 83
};

// ----------------------------------------------------------------------------
84 85
pqPlayBackEventsDialog::pqImplementation::pqImplementation(
  pqEventPlayer& player, pqEventDispatcher& dispatcher, pqTestUtility* testUtility)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  : Player(player)
  , Dispatcher(dispatcher)
  , TestUtility(testUtility)
{
  this->CurrentLine = 0;
  this->MaxLines = 0;
  this->CurrentFile = 0;
  this->Filenames = QStringList();
  this->CurrentEvent = QStringList();
}

// ----------------------------------------------------------------------------
pqPlayBackEventsDialog::pqImplementation::~pqImplementation()
{
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::pqImplementation::init(pqPlayBackEventsDialog* dialog)
{
  this->Ui.setupUi(dialog);

107
  this->Ui.loadFileButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon));
108 109 110 111 112 113 114 115

  this->Ui.playerErrorTextLabel->setVisible(false);
  this->Ui.playerErrorIconLabel->setVisible(false);
  this->Ui.infoErrorTextLabel->setVisible(false);
  this->Ui.infoErrorIconLabel->setVisible(false);
  this->Ui.logBrowser->setVisible(false);

  pqWidgetEventPlayer* widgetPlayer =
116 117
    this->Player.getWidgetEventPlayer(QString("pqCommentEventPlayer"));
  pqCommentEventPlayer* commentPlayer = qobject_cast<pqCommentEventPlayer*>(widgetPlayer);
118
  if (commentPlayer)
119 120 121 122
  {
    QObject::connect(
      commentPlayer, SIGNAL(comment(QString)), this->Ui.logBrowser, SLOT(append(QString)));
  }
123 124 125

  dialog->setMaximumHeight(dialog->minimumSizeHint().height());

126 127
  QObject::connect(&this->Player, SIGNAL(eventAboutToBePlayed(QString, QString, QString)), dialog,
    SLOT(onEventAboutToBePlayed(QString, QString, QString)));
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  QObject::connect(
    this->Ui.timeStepSpinBox, SIGNAL(valueChanged(int)), &this->Dispatcher, SLOT(setTimeStep(int)));

  QObject::connect(this->Ui.loadFileButton, SIGNAL(clicked()), dialog, SLOT(loadFiles()));
  QObject::connect(this->Ui.plusButton, SIGNAL(clicked()), dialog, SLOT(insertFiles()));
  QObject::connect(this->Ui.minusButton, SIGNAL(clicked()), dialog, SLOT(removeFiles()));

  QObject::connect(
    this->Ui.playPauseButton, SIGNAL(toggled(bool)), dialog, SLOT(onPlayOrPause(bool)));
  QObject::connect(
    this->Ui.playPauseButton, SIGNAL(toggled(bool)), &this->Dispatcher, SLOT(run(bool)));
  QObject::connect(this->Ui.stopButton, SIGNAL(clicked()), this->TestUtility, SLOT(stopTests()));
  QObject::connect(this->Ui.stepButton, SIGNAL(clicked()), dialog, SLOT(onOneStep()));

  QObject::connect(
    this->TestUtility, SIGNAL(playbackStarted(QString)), dialog, SLOT(onStarted(QString)));

  QObject::connect(this->TestUtility, SIGNAL(playbackStarted()), dialog, SLOT(onStarted()));
  QObject::connect(this->TestUtility, SIGNAL(playbackStopped()), dialog, SLOT(onStopped()));
  QObject::connect(&this->Dispatcher, SIGNAL(paused()), dialog, SLOT(updateUi()));
  QObject::connect(&this->Dispatcher, SIGNAL(restarted()), dialog, SLOT(updateUi()));

  QObject::connect(
    &this->Player, SIGNAL(errorMessage(QString)), this->Ui.logBrowser, SLOT(append(QString)));
153 154 155 156 157
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::pqImplementation::setProgressBarsValue(int value)
{
158 159
  for (int i = 0; i < this->Ui.tableWidget->rowCount(); ++i)
  {
160
    this->setProgressBarValue(i, value);
161
  }
162 163 164
}

// ----------------------------------------------------------------------------
165
void pqPlayBackEventsDialog::pqImplementation::setProgressBarValue(int row, int value)
166 167 168 169 170 171
{
  QWidget* widget = this->Ui.tableWidget->cellWidget(row, 2);
  QProgressBar* progressBar = qobject_cast<QProgressBar*>(widget);
  progressBar->setValue(value);
}

172
// ----------------------------------------------------------------------------
173
QString pqPlayBackEventsDialog::pqImplementation::setMaxLenght(const QString& name, int max)
174
{
175 176 177 178
  if (name.length() > max)
  {
    return name.left(max / 2) + "..." + name.right(max / 2);
  }
179 180 181
  return name;
}

182 183 184 185
///////////////////////////////////////////////////////////////////////////////////
// pqPlayBackEventsDialog

// ----------------------------------------------------------------------------
186 187
pqPlayBackEventsDialog::pqPlayBackEventsDialog(
  pqEventPlayer& Player, pqEventDispatcher& Dispatcher, pqTestUtility* TestUtility, QWidget* Parent)
188 189 190 191
  : QDialog(Parent)
  , Implementation(new pqImplementation(Player, Dispatcher, TestUtility))
{
  this->Implementation->init(this);
192 193
  this->setAttribute(Qt::WA_DeleteOnClose);
  this->loadFiles();
194 195 196 197 198 199 200 201 202
}

// ----------------------------------------------------------------------------
pqPlayBackEventsDialog::~pqPlayBackEventsDialog()
{
  delete Implementation;
}

// ----------------------------------------------------------------------------
203
void pqPlayBackEventsDialog::done(int value)
204 205 206 207 208 209
{
  this->Implementation->TestUtility->stopTests();
  QDialog::done(value);
}

// ----------------------------------------------------------------------------
210
QStringList pqPlayBackEventsDialog::selectedFileNames() const
211 212
{
  QStringList list;
213 214 215 216
  for (int i = 0; i < this->Implementation->Ui.tableWidget->rowCount(); ++i)
  {
    QCheckBox* box =
      qobject_cast<QCheckBox*>(this->Implementation->Ui.tableWidget->cellWidget(i, 0));
217
    if (box->isChecked())
218
    {
219 220
      list << this->Implementation->Filenames[i];
    }
221
  }
222 223 224 225
  return list;
}

// ----------------------------------------------------------------------------
226 227
void pqPlayBackEventsDialog::onEventAboutToBePlayed(
  const QString& Object, const QString& Command, const QString& Arguments)
228 229 230 231 232 233 234 235 236
{
  ++this->Implementation->CurrentLine;
  QStringList newEvent;
  newEvent << Object << Command << Arguments;
  this->Implementation->CurrentEvent = newEvent;
  this->updateUi();
}

// ----------------------------------------------------------------------------
237
void pqPlayBackEventsDialog::loadFiles()
238
{
239
  QFileDialog* dialog = new QFileDialog(this, "Macro File Name", QString(), "XML Files (*.xml)");
240 241
  dialog->setFileMode(QFileDialog::ExistingFiles);
  if (dialog->exec())
242
  {
243 244
    this->Implementation->Filenames = dialog->selectedFiles();
    this->Implementation->Ui.tableWidget->setRowCount(0);
245
    this->loadFiles(this->Implementation->Filenames);
246
  }
247 248
  delete dialog;
}
249

250 251 252
// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::insertFiles()
{
253
  QFileDialog* dialog = new QFileDialog(this, "Macro File Name", QString(), "XML Files (*.xml)");
254 255
  dialog->setFileMode(QFileDialog::ExistingFiles);
  if (dialog->exec())
256
  {
257 258
    this->Implementation->Filenames << dialog->selectedFiles();
    this->loadFiles(dialog->selectedFiles());
259
  }
260
  delete dialog;
261 262 263
}

// ----------------------------------------------------------------------------
264
void pqPlayBackEventsDialog::removeFiles()
265
{
266
  if (QMessageBox::Ok == QMessageBox::warning(this, QString("Remove files"),
267 268 269 270 271
                           QString("Are you sure you want to \n"
                                   "remove all checked files ?\n"),
                           QMessageBox::Ok, QMessageBox::Cancel))
  {
    foreach (QString file, this->selectedFileNames())
272
    {
273 274 275
      int index = this->Implementation->Filenames.indexOf(file);
      this->Implementation->Ui.tableWidget->removeRow(index);
      this->Implementation->Filenames.removeAt(index);
276
    }
277 278
    this->updateUi();
  }
279 280 281 282 283
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::loadFiles(const QStringList& filenames)
{
284 285
  for (int i = 0; i < filenames.count(); i++)
  {
286
    this->addFile(filenames[i]);
287
  }
288 289 290 291 292 293 294 295 296
  this->Implementation->Ui.tableWidget->resizeColumnToContents(0);
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::addFile(const QString& filename)
{
  QFileInfo info(filename);
  int newIndex = this->Implementation->Ui.tableWidget->rowCount();
  this->Implementation->Ui.tableWidget->insertRow(newIndex);
297
  this->Implementation->Ui.tableWidget->setItem(newIndex, 1, new QTableWidgetItem(info.fileName()));
298
  this->Implementation->Ui.tableWidget->setCellWidget(
299
    newIndex, 2, new QProgressBar(this->Implementation->Ui.tableWidget));
300 301 302
  this->Implementation->setProgressBarValue(newIndex, 0);
  QCheckBox* check = new QCheckBox(this->Implementation->Ui.tableWidget);
  check->setChecked(true);
303
  QObject::connect(check, SIGNAL(toggled(bool)), this, SLOT(updateUi()));
304 305
  this->Implementation->Ui.tableWidget->setCellWidget(newIndex, 0, check);
  this->updateUi();
306 307 308 309 310
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onPlayOrPause(bool playOrPause)
{
311 312
  if (this->Implementation->TestUtility->playingTest() || !playOrPause)
  {
313
    return;
314
  }
315 316 317 318 319 320 321 322 323 324

  QStringList newList = this->selectedFileNames();
  this->Implementation->TestUtility->playTests(newList);
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onOneStep()
{
  this->Implementation->Ui.playPauseButton->setChecked(false);
  if (!this->Implementation->TestUtility->playingTest())
325
  {
326 327 328 329
    this->Implementation->Dispatcher.run(false);
    this->Implementation->Dispatcher.oneStep();
    QStringList newList = this->selectedFileNames();
    this->Implementation->TestUtility->playTests(newList);
330
  }
331
  else
332
  {
333
    this->Implementation->Dispatcher.oneStep();
334
  }
335 336 337 338 339
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onStarted(const QString& filename)
{
340 341 342
  this->Implementation->CurrentFile = this->Implementation->Filenames.indexOf(filename);
  this->Implementation->Ui.tableWidget->setCurrentCell(this->Implementation->CurrentFile, 1,
    QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);
343
  this->Implementation->Ui.logBrowser->clear();
344 345 346 347 348 349 350

  this->Implementation->MaxLines = 0;
  this->Implementation->CurrentLine = 0;

  QFile file(filename);
  QFileInfo infoFile(file);
  file.open(QIODevice::ReadOnly);
351
  this->Implementation->Ui.logBrowser->append(QString("Start file : %1").arg(infoFile.fileName()));
352
  QTextStream stream(&file);
353
  stream.setCodec("UTF-8");
354
  this->Implementation->Ui.currentFileLabel->setText(infoFile.fileName());
355 356
  while (!stream.atEnd())
  {
357
    QString line = stream.readLine();
358 359
    if (line.trimmed().startsWith("<event"))
    {
360 361
      ++this->Implementation->MaxLines;
    }
362
  }
363 364
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378
// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onStarted()
{
  this->Implementation->setProgressBarsValue(0);
  this->updateUi();
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onStopped()
{
  this->Implementation->Ui.playPauseButton->setChecked(false);
  this->updateUi();
}

379 380 381
// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::updateUi()
{
382 383
  // Update Moda/Modeless
  this->onModal(this->Implementation->TestUtility->playingTest() &&
384
    !(this->Implementation->TestUtility->playingTest() &&
385 386
                  this->Implementation->Dispatcher.isPaused()));

387
  // Update player buttons
388
  this->Implementation->Ui.playPauseButton->setChecked(
389 390
    this->Implementation->TestUtility->playingTest() &&
    !this->Implementation->Dispatcher.isPaused());
391
  this->Implementation->Ui.playPauseButton->setEnabled(
392
    !this->Implementation->Filenames.isEmpty() && this->selectedFileNames().count() > 0);
393
  this->Implementation->Ui.stepButton->setEnabled(
394 395
    !this->Implementation->Filenames.isEmpty() && this->selectedFileNames().count() > 0);
  this->Implementation->Ui.stopButton->setEnabled(this->Implementation->TestUtility->playingTest());
396

397
  // loadFile, plus and minus buttons
398
  this->Implementation->Ui.loadFileButton->setEnabled(
399
    !this->Implementation->TestUtility->playingTest());
400
  this->Implementation->Ui.plusButton->setEnabled(
401
    !this->Implementation->TestUtility->playingTest());
402
  this->Implementation->Ui.minusButton->setEnabled(
403
    !this->Implementation->TestUtility->playingTest());
404 405

  // Time step
406
  this->Implementation->Ui.timeStepSpinBox->setEnabled(!this->Implementation->Filenames.isEmpty());
407

408 409
  // Error feedback
  this->Implementation->Ui.playerErrorTextLabel->setVisible(
410
    !this->Implementation->Dispatcher.status());
411
  this->Implementation->Ui.playerErrorIconLabel->setVisible(
412
    !this->Implementation->Dispatcher.status());
413
  this->Implementation->Ui.infoErrorTextLabel->setVisible(
414
    !this->Implementation->Dispatcher.status());
415
  this->Implementation->Ui.infoErrorIconLabel->setVisible(
416
    !this->Implementation->Dispatcher.status());
417

418 419 420
  QString command = tr("Command : ");
  QString argument = tr("Argument(s) : ");
  QString object = tr("Object : ");
421 422 423 424 425 426
  if (this->Implementation->TestUtility->playingTest() &&
    !this->Implementation->CurrentEvent.isEmpty())
  {
    command += this->Implementation->setMaxLenght(this->Implementation->CurrentEvent[1], 40);
    argument += this->Implementation->setMaxLenght(this->Implementation->CurrentEvent[2], 40);
    object += this->Implementation->setMaxLenght(this->Implementation->CurrentEvent[0], 40);
427
    this->Implementation->setProgressBarValue(this->Implementation->CurrentFile,
428 429 430 431
      static_cast<int>((static_cast<double>(this->Implementation->CurrentLine) /
                         static_cast<double>(this->Implementation->MaxLines - 1)) *
                                                100));
  }
432
  else
433 434 435
  {
    this->Implementation->Ui.currentFileLabel->setText(QString("No Test is playing ..."));
  }
436

437 438 439
  this->Implementation->Ui.commandLabel->setText(command);
  this->Implementation->Ui.argumentsLabel->setText(argument);
  this->Implementation->Ui.objectLabel->setText(object);
440 441

  this->update();
442
}
443 444 445 446 447 448

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::onModal(bool value)
{
  // From modal to modeless we don't need to hide() show() the dialog
  if (value)
449
  {
450 451
    this->setAttribute(Qt::WA_WState_Visible, false);
    this->setAttribute(Qt::WA_WState_Hidden, true);
452
  }
453 454
  this->setModal(value);
  if (value)
455
  {
456 457 458
    this->Implementation->OldRect = this->frameGeometry();
    this->setVisible(true);
    this->Implementation->OldRect = QRect();
459
  }
460 461 462 463 464 465
  this->raise();
}

// ----------------------------------------------------------------------------
void pqPlayBackEventsDialog::moveEvent(QMoveEvent* event)
{
466 467
  if (this->Implementation->OldRect.isValid())
  {
468 469 470
    QPoint oldPos = this->Implementation->OldRect.topLeft();
    this->Implementation->OldRect = QRect();
    this->move(oldPos);
471
  }
472
  else
473
  {
474
    this->Superclass::moveEvent(event);
475
  }
476
}