1 
2 
3 #include "tasksviewer.h"
4 
5 // Tnz6 includes
6 #include "menubarcommandids.h"
7 #include "batches.h"
8 #include "floatingpanelcommand.h"
9 #include "tsystem.h"
10 
11 // TnzQt includes
12 #include "toonzqt/dvdialog.h"
13 #include "toonzqt/menubarcommand.h"
14 #include "toonzqt/gutil.h"
15 #include "toonzqt/dvscrollwidget.h"
16 
17 // TnzCore includes
18 #include "tconvert.h"
19 
20 // Qt includes
21 #include <QTreeWidget>
22 #include <QGridLayout>
23 #include <QLabel>
24 #include <QPushButton>
25 #include <QMouseEvent>
26 #include <QMenu>
27 #include <QTextEdit>
28 #include <QListWidget>
29 #include <QToolBar>
30 #include <QTimer>
31 #include <QComboBox>
32 
33 using namespace DVGui;
34 
35 //=============================================================================
36 
37 namespace {
isMovieType(std::string type)38 bool isMovieType(std::string type) {
39   return (type == "mov" || type == "avi" || type == "3gp" || type == "mp4" ||
40           type == "webm");
41 }
42 };  // namespace
43 
44 //=============================================================================
45 
getActions() const46 const std::vector<QAction *> &TasksViewer::getActions() const {
47   return m_actions;
48 }
49 
50 //----------------------------------------------------------------------
51 
add(const QString & iconName,QString text,QToolBar * toolBar,const char * slot,QString iconText)52 void TasksViewer::add(const QString &iconName, QString text, QToolBar *toolBar,
53                       const char *slot, QString iconText) {
54 #if QT_VERSION >= 0x050500
55   QAction *action = new QAction(
56       createQIcon(iconName.toLatin1().constData(), false), text, this);
57 #else
58   QAction *action = new QAction(
59       createQIcon(iconName.toAscii().constData(), false), text, this);
60 #endif
61   action->setIconText(iconText);
62   bool ret = connect(action, SIGNAL(triggered(bool)),
63                      (TaskTreeModel *)m_treeView->model(), slot);
64   assert(ret);
65   toolBar->addAction(action);
66   m_actions.push_back(action);
67 }
68 
69 //----------------------------------------------------------------------
70 
showEvent(QShowEvent *)71 void TasksViewer::showEvent(QShowEvent *) { startTimer(); }
72 
73 //----------------------------------------------------------------------
74 
hideEvent(QHideEvent *)75 void TasksViewer::hideEvent(QHideEvent *) { stopTimer(); }
76 
77 //----------------------------------------------------------------------
78 
createToolBar()79 QWidget *TasksViewer::createToolBar() {
80   // Create toolbar. It is an horizontal layout with three internal toolbar.
81   QWidget *toolBarWidget = new QWidget(this);
82   QToolBar *cmdToolbar   = new QToolBar(toolBarWidget);
83   cmdToolbar->setIconSize(QSize(20, 20));
84   cmdToolbar->clear();
85   cmdToolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86   add("play", tr("&Start"), cmdToolbar, SLOT(start(bool)), tr("Start"));
87   add("stop", tr("&Stop"), cmdToolbar, SLOT(stop(bool)), tr("Stop"));
88   cmdToolbar->addSeparator();
89   add("render_add", tr("&Add Render Task"), cmdToolbar,
90       SLOT(addRenderTask(bool)), tr("Add Render"));
91   add("cleanup_add", tr("&Add Cleanup Task"), cmdToolbar,
92       SLOT(addCleanupTask(bool)), tr("Add Cleanup"));
93 
94   QToolBar *saveToolbar = new QToolBar(toolBarWidget);
95   saveToolbar->setIconSize(QSize(20, 20));
96   saveToolbar->clear();
97   saveToolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
98   add("save", tr("&Save Task List"), saveToolbar, SLOT(save(bool)), tr("Save"));
99   add("saveas", tr("&Save Task List As"), saveToolbar, SLOT(saveas(bool)),
100       tr("Save As"));
101   add("load", tr("&Load Task List"), saveToolbar, SLOT(load(bool)), tr("Load"));
102   saveToolbar->addSeparator();
103   add("delete", tr("&Remove"), saveToolbar, SLOT(remove(bool)), tr("Remove"));
104 
105   QVBoxLayout *toolbarLayout = new QVBoxLayout(toolBarWidget);
106   toolbarLayout->setMargin(0);
107   toolbarLayout->setSpacing(0);
108   {
109     toolbarLayout->addWidget(cmdToolbar);
110     toolbarLayout->addWidget(saveToolbar);
111   }
112   toolBarWidget->setLayout(toolbarLayout);
113 
114   return toolBarWidget;
115 }
116 //=============================================================================
117 //=============================================================================
118 /*! \class TasksViewer
119                 Inherits \b QSplitter.
120 */
121 #if QT_VERSION >= 0x050500
TasksViewer(QWidget * parent,Qt::WindowFlags flags)122 TasksViewer::TasksViewer(QWidget *parent, Qt::WindowFlags flags)
123 #else
124 TasksViewer::TasksViewer(QWidget *parent, Qt::WFlags flags)
125 #endif
126     : QSplitter(parent) {
127   QFrame *box;
128 
129   BatchesController::instance()->attach(this);
130 
131   // style sheet
132   setObjectName("Tasks");
133   setFrameStyle(QFrame::StyledPanel);
134 
135   //-------begin left side (the tree + the toolbar)
136 
137   // If there is another TasksViewer instance already, share the same TasksTree
138   TaskTreeModel *treeModel = BatchesController::instance()->getTasksTree();
139   m_treeView               = new TaskTreeView(this, treeModel);
140   BatchesController::instance()->setTasksTree(
141       (TaskTreeModel *)m_treeView->model());
142 
143   m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
144 
145   box                  = new QFrame(this);
146   QVBoxLayout *vLayout = new QVBoxLayout(box);
147   box->setLayout(vLayout);
148   vLayout->setMargin(0);
149   vLayout->setSpacing(0);
150 
151   vLayout->addWidget(createToolBar());
152   vLayout->addWidget(m_treeView);
153 
154   addWidget(box);
155 
156   //------end left side (the tree + the toolbar)
157   //------begin right side (the task sheet)
158 
159   m_taskSheet = new TaskSheet(this);
160   addWidget(m_taskSheet);
161 
162   //------end right side (the task sheet)
163 
164   setStretchFactor(1, 2);
165 
166   m_timer  = new QTimer(this);
167   bool ret = connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
168   assert(ret);
169   m_timer->start(3000);
170 }
171 
172 //-----------------------------------------------------------------------------
173 
update()174 void TasksViewer::update() {
175   //	((TaskTreeModel*)m_treeView->model())->update();
176   m_taskSheet->update(m_taskSheet->getCurrentTask());
177 }
178 
~TasksViewer()179 TasksViewer::~TasksViewer() {}
180 
181 //-----------------------------------------------------------------------------
182 
onTimer()183 void TasksViewer::onTimer() {
184   if (BatchesController::instance()
185           ->getController())  // si sta utilizzando la farm
186     BatchesController::instance()->update();
187 }
188 
189 //------------------------------------------------------------------------------------------------------------------
190 
startTimer()191 void TasksViewer::startTimer() {
192   onTimer();
193   if (!m_timer->isActive()) m_timer->start(3000);
194 }
195 
196 //------------------------------------------------------------------------------------------------------------------
197 
stopTimer()198 void TasksViewer::stopTimer() {
199   if (m_timer->isActive()) m_timer->stop();
200 }
201 
202 //-----------------------------------------------------------------------------
203 namespace {
getStatusString(TaskState status)204 QString getStatusString(TaskState status) {
205   switch (status) {
206   case Suspended:
207     return TaskSheet::tr("Suspended");
208   case Waiting:
209     return TaskSheet::tr("Waiting");
210   case Running:
211     return TaskSheet::tr("Running");
212   case Completed:
213     return TaskSheet::tr("Completed");
214   case Aborted:
215     return TaskSheet::tr("Failed");
216   case TaskUnknown:
217     return TaskSheet::tr("TaskUnknown");
218   }
219   return "";
220 }
221 
containsTask(const TFarmTask::Dependencies & dependencies,TFarmTask::Id id)222 bool containsTask(const TFarmTask::Dependencies &dependencies,
223                   TFarmTask::Id id) {
224   int i;
225   for (i = 0; i < dependencies.getTaskCount(); i++)
226     if (dependencies.getTaskId(i) == id) return true;
227   return false;
228 }
229 
230 }  // namespace
231 
232 //-----------------------------------------------------------------------------
233 
234 class DependencyItem final : public QListWidgetItem {
235 public:
236   TFarmTask *m_task;
237 
DependencyItem(TFarmTask * task,QListWidget * w)238   DependencyItem(TFarmTask *task, QListWidget *w)
239       : QListWidgetItem(task->m_name, w), m_task(task) {}
240 };
241 
242 //-----------------------------------------------------------------------------
243 
onAdded(bool)244 void TaskSheet::onAdded(bool) {
245   QList<QListWidgetItem *> l;
246   if (!m_task) return;
247 
248   l = m_notAddedBox->selectedItems();
249   if (l.isEmpty()) return;
250 
251   int i;
252   for (i = 0; i < l.size(); i++) {
253     TFarmTask *t = ((DependencyItem *)l.at(i))->m_task;
254     assert(t);
255     m_task->m_dependencies->add(t->m_id);
256   }
257   BatchesController::instance()->setDirtyFlag(true);
258   update(m_task);
259 }
260 
261 //-----------------------------------------------------------------------------
262 
onRemoved(bool)263 void TaskSheet::onRemoved(bool) {
264   QList<QListWidgetItem *> l;
265   if (!m_task) return;
266 
267   l = m_addedBox->selectedItems();
268   if (l.isEmpty()) return;
269   int i;
270   for (i = 0; i < l.size(); i++) {
271     TFarmTask *t = ((DependencyItem *)l.at(i))->m_task;
272     assert(t);
273     m_task->m_dependencies->remove(t->m_id);
274   }
275   BatchesController::instance()->setDirtyFlag(true);
276   update(m_task);
277 }
278 
279 //-----------------------------------------------------------------------------
280 
onAddedItemDoubleClicked(QListWidgetItem * item)281 void TaskSheet::onAddedItemDoubleClicked(QListWidgetItem *item) {
282   if (!m_task || !item) return;
283 
284   TFarmTask *t = ((DependencyItem *)item)->m_task;
285   assert(t);
286   m_task->m_dependencies->add(t->m_id);
287 
288   BatchesController::instance()->setDirtyFlag(true);
289   update(m_task);
290 }
291 
292 //-----------------------------------------------------------------------------
293 
onRemovedItemDoubleClicked(QListWidgetItem * item)294 void TaskSheet::onRemovedItemDoubleClicked(QListWidgetItem *item) {
295   if (!m_task || !item) return;
296 
297   TFarmTask *t = ((DependencyItem *)item)->m_task;
298   assert(t);
299   m_task->m_dependencies->remove(t->m_id);
300 
301   BatchesController::instance()->setDirtyFlag(true);
302   update(m_task);
303 }
304 
305 //-----------------------------------------------------------------------------
306 
update(TFarmTask * task)307 void TaskSheet::update(TFarmTask *task) {
308   m_task = task;
309 
310   while (m_addedBox->count()) delete m_addedBox->takeItem(0);
311   while (m_notAddedBox->count()) delete m_notAddedBox->takeItem(0);
312 
313   BatchesController *bc = BatchesController::instance();
314 
315   if (!task) {
316     widget()->hide();
317     return;
318   }
319 
320   widget()->show();
321 
322   m_name->setText(task->m_name);
323   m_status->setText(getStatusString(task->m_status));
324   m_commandLine->setText(task->getCommandLine());
325   m_server->setText(task->m_server);
326   m_submittedBy->setText(task->m_user);
327   m_submittedOn->setText(task->m_callerMachineName);
328   m_priority->setText(QString::number(task->m_priority));
329   m_submitDate->setText(task->m_submissionDate.toString());
330   m_startDate->setText(task->m_startDate.toString());
331   m_complDate->setText(task->m_completionDate.toString());
332   if (task->m_completionDate > task->m_startDate)
333     m_duration->setText(
334         QString::number(task->m_startDate.secsTo(task->m_completionDate)));
335   else
336     m_duration->clear();
337   m_stepCount->setText(QString::number(task->m_stepCount));
338   if (task->m_failedSteps >= 0)
339     m_failedSteps->setText(QString::number(task->m_failedSteps));
340   else
341     m_failedSteps->clear();
342   if (task->m_successfullSteps >= 0)
343     m_succSteps->setText(QString::number(task->m_successfullSteps));
344   else
345     m_succSteps->clear();
346 
347   if (task->m_isComposerTask) {
348     m_boxCleanup->hide();
349 
350     if (dynamic_cast<TFarmTaskGroup *>(task)) {
351       m_boxComposer->show();
352 
353       m_outputPath->setText(
354           QString::fromStdWString(task->m_outputPath.getWideString()));
355       m_from->setText(QString::number(task->m_from));
356       m_to->setText(QString::number(task->m_to));
357       m_step->setText(QString::number(task->m_step));
358       m_shrink->setText(QString::number(task->m_shrink));
359       m_multimedia->setCurrentIndex(task->m_multimedia);
360       m_threadsCombo->setCurrentIndex(task->m_threadsIndex);
361       m_rasterGranularityCombo->setCurrentIndex(task->m_maxTileSizeIndex);
362 
363       if (!isMovieType(task->m_outputPath.getType())) {
364         m_chunkSize->setEnabled(true);
365         m_chunkSize->setText(QString::number(task->m_chunkSize));
366       } else {
367         m_chunkSize->setEnabled(false);
368         m_chunkSize->setText(QString::number(task->m_stepCount));
369       }
370     } else
371       m_boxComposer->hide();
372   } else {
373     m_boxComposer->hide();
374     m_boxCleanup->show();
375     m_overwrite->setCurrentIndex((int)task->m_overwrite);
376     m_visible->setChecked(task->m_onlyVisible);
377   }
378 
379   int i;
380 
381   for (i = 0; i < task->m_dependencies->getTaskCount(); i++)
382     new DependencyItem(bc->getTask(task->m_dependencies->getTaskId(i)),
383                        m_addedBox);
384 
385   for (i = 0; i < bc->getTaskCount(); i++) {
386     TFarmTaskGroup *tg;
387     TFarmTask *t = bc->getTask(i);
388     if (!(tg = dynamic_cast<TFarmTaskGroup *>(t))) continue;
389     if (tg->m_id == task->m_id) continue;
390     if (containsTask(*task->m_dependencies, tg->m_id)) continue;
391     new DependencyItem(tg, m_notAddedBox);
392   }
393 }
394 
395 //-----------------------------------------------------------------------------
396 
updateChunks(TFarmTask * task)397 void TaskSheet::updateChunks(TFarmTask *task) {
398   TFarmTaskGroup *t = dynamic_cast<TFarmTaskGroup *>(task);
399 
400   if (t) {
401     while (t->getTaskCount())
402       BatchesController::instance()->removeTask(t->getTask(0)->m_id);
403 
404     t->changeChunkSize(t->m_chunkSize);
405     int i;
406     for (i = 0; i < t->getTaskCount(); i++)
407       BatchesController::instance()->addTask(t->getTask(i)->m_id,
408                                              t->getTask(i));
409 
410     ((TaskTreeModel *)m_viewer->m_treeView->model())->setupModelData();
411     ((TaskTreeModel *)m_viewer->m_treeView->model())->update();
412   }
413 }
414 
415 //-----------------------------------------------------------------------------
416 namespace {
417 
create(QTextEdit * & ret,QGridLayout * layout,QString name,int row,int span=5)418 void inline create(QTextEdit *&ret, QGridLayout *layout, QString name, int row,
419                    int span = 5) {
420   ret = new QTextEdit();
421   ret->setReadOnly(true);
422   ret->setObjectName("TaskSheetItem");
423   QLabel *label = new QLabel(name);
424   label->setObjectName("TaskSheetItemLabel");
425   layout->addWidget(label, row, 0, 1, 2, Qt::AlignRight | Qt::AlignVCenter);
426   layout->addWidget(ret, row, 2, 1, span);
427 }
428 
429 //-----------------------------------------------------------------------------
430 
create(LineEdit * & ret,QGridLayout * layout,QString name,int row,int span=3)431 void inline create(LineEdit *&ret, QGridLayout *layout, QString name, int row,
432                    int span = 3) {
433   ret           = new LineEdit();
434   QLabel *label = new QLabel(name);
435   label->setObjectName("TaskSheetItemLabel");
436   layout->addWidget(label, row, 0, 1, 2, Qt::AlignRight | Qt::AlignVCenter);
437   layout->addWidget(ret, row, 2, 1, span);
438 }
439 
440 //-----------------------------------------------------------------------------
441 
create(QComboBox * & ret,QGridLayout * layout,QString name,int row,int span=3)442 void inline create(QComboBox *&ret, QGridLayout *layout, QString name, int row,
443                    int span = 3) {
444   ret           = new QComboBox();
445   QLabel *label = new QLabel(name);
446   label->setObjectName("TaskSheetItemLabel");
447   layout->addWidget(label, row, 0);
448   layout->addWidget(ret, row, 2, 1, span);
449 }
450 
451 //-----------------------------------------------------------------------------
452 
create(QLabel * & ret,QGridLayout * layout,QString name,int row,int span=3)453 void inline create(QLabel *&ret, QGridLayout *layout, QString name, int row,
454                    int span = 3) {
455   ret = new QLabel();
456   ret->setObjectName("TaskSheetItem");
457   QLabel *label = new QLabel(name);
458   label->setObjectName("TaskSheetItemLabel");
459   layout->addWidget(label, row, 0, 1, 2, Qt::AlignRight | Qt::AlignVCenter);
460   layout->addWidget(ret, row, 2, 1, span);
461 }
462 
463 //-----------------------------------------------------------------------------
464 
create(CheckBox * & ret,QGridLayout * layout,QString name,int row)465 void inline create(CheckBox *&ret, QGridLayout *layout, QString name, int row) {
466   ret = new CheckBox(name);
467   layout->addWidget(ret, row, 0, 1, 2, Qt::AlignLeft | Qt::AlignVCenter);
468 }
469 
470 //-----------------------------------------------------------------------------
471 
472 template <typename A, typename B>
create(A * & ret1,B * & ret2,QGridLayout * layout,QString name1,QString name2,QFlags<Qt::AlignmentFlag> align1,QFlags<Qt::AlignmentFlag> align2,int row)473 void inline create(A *&ret1, B *&ret2, QGridLayout *layout, QString name1,
474                    QString name2, QFlags<Qt::AlignmentFlag> align1,
475                    QFlags<Qt::AlignmentFlag> align2, int row) {
476   ret1           = new A();
477   ret2           = new B();
478   QLabel *label1 = new QLabel(name1);
479   QLabel *label2 = new QLabel(name2);
480   ret1->setObjectName("TaskSheetItem");
481   ret2->setObjectName("TaskSheetItem");
482   label1->setObjectName("TaskSheetItemLabel");
483   label2->setObjectName("TaskSheetItemLabel");
484   layout->addWidget(label1, row, 0, 1, 2, align1);
485   layout->addWidget(ret1, row, 2, 1, 1, Qt::AlignLeft);
486   layout->addWidget(label2, row, 3, align2);
487   layout->addWidget(ret2, row, 4, 1, 1, Qt::AlignLeft);
488 }
489 
490 }  // namespace
491 
492 //-----------------------------------------------------------------------------
493 
setChunkSize()494 void TaskSheet::setChunkSize() {
495   if (!m_task) return;
496   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
497     update(m_task);
498     return;
499   }
500 
501   int val = m_chunkSize->text().toInt();
502   if (val < 1) {
503     val = 1;
504     m_chunkSize->setText(QString::number(val));
505   } else if (val > m_task->m_stepCount) {
506     val = m_task->m_stepCount;
507     m_chunkSize->setText(QString::number(val));
508   }
509 
510   if (m_task->m_chunkSize != val) {
511     m_task->m_chunkSize = val;
512     BatchesController::instance()->setDirtyFlag(true);
513 
514     updateChunks(m_task);
515   }
516 
517   m_viewer->startTimer();
518 }
519 
520 //-----------------------------------------------------------------------------
521 
onFocusIn()522 void TaskSheet::onFocusIn() { m_viewer->stopTimer(); }
523 
524 //-----------------------------------------------------------------------------
525 
setName()526 void TaskSheet::setName() {
527   if (!m_task) return;
528 
529   m_task->m_name = m_name->text();
530   m_commandLine->setText(m_task->getCommandLine());
531 
532   BatchesController::instance()->setDirtyFlag(true);
533 
534   ((TaskTreeModel *)m_viewer->m_treeView->model())->update();
535   m_viewer->startTimer();
536 }
537 
538 //-----------------------------------------------------------------------------
539 
setFrom()540 void TaskSheet::setFrom() {
541   if (!m_task) return;
542   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
543     update(m_task);
544     return;
545   }
546 
547   int val = m_from->text().toInt();
548 
549   if (val < 1) {
550     val = 1;
551     m_from->setText(QString::number(val));
552   } else if (val > m_task->m_to) {
553     val = m_task->m_to;
554     m_from->setText(QString::number(val));
555   }
556 
557   if (m_task->m_from != val) {
558     m_task->m_from = val;
559     m_commandLine->setText(m_task->getCommandLine());
560     BatchesController::instance()->setDirtyFlag(true);
561 
562     updateChunks(m_task);
563   }
564 
565   m_viewer->startTimer();
566 }
567 
568 //-----------------------------------------------------------------------------
569 
setTo()570 void TaskSheet::setTo() {
571   if (!m_task) return;
572   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
573     update(m_task);
574     return;
575   }
576 
577   int val = m_to->text().toInt();
578 
579   if (val < m_task->m_from) {
580     val = m_task->m_from;
581     m_to->setText(QString::number(val));
582   } else if (val > m_task->m_stepCount) {
583     val = m_task->m_stepCount;
584     m_to->setText(QString::number(val));
585   }
586 
587   if (m_task->m_to != val) {
588     m_task->m_to = val;
589     m_commandLine->setText(m_task->getCommandLine());
590     BatchesController::instance()->setDirtyFlag(true);
591 
592     updateChunks(m_task);
593   }
594 
595   m_viewer->startTimer();
596 }
597 
598 //-----------------------------------------------------------------------------
599 
setShrink()600 void TaskSheet::setShrink() {
601   if (!m_task) return;
602   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
603     update(m_task);
604     return;
605   }
606 
607   int val = m_shrink->text().toInt();
608   if (val < 1)
609     m_shrink->setText(QString::number(m_task->m_shrink));
610   else if (m_task->m_shrink != val) {
611     m_task->m_shrink = val;
612     m_commandLine->setText(m_task->getCommandLine());
613     BatchesController::instance()->setDirtyFlag(true);
614 
615     // Update children tasks, if present.
616     TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
617     if (taskGroup) {
618       for (int i                        = 0; i < taskGroup->getTaskCount(); ++i)
619         taskGroup->getTask(i)->m_shrink = taskGroup->m_shrink;
620     }
621   }
622 
623   m_viewer->startTimer();
624 }
625 
626 //-----------------------------------------------------------------------------
627 
setStep()628 void TaskSheet::setStep() {
629   if (!m_task) return;
630   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
631     update(m_task);
632     return;
633   }
634 
635   int val = m_step->text().toInt();
636   if (val < 1)
637     m_step->setText(QString::number(m_task->m_step));
638   else if (m_task->m_step != val) {
639     m_task->m_step = val;
640     m_commandLine->setText(m_task->getCommandLine());
641     BatchesController::instance()->setDirtyFlag(true);
642 
643     // Update children tasks, if present.
644     TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
645     if (taskGroup) {
646       for (int i                      = 0; i < taskGroup->getTaskCount(); ++i)
647         taskGroup->getTask(i)->m_step = taskGroup->m_step;
648     }
649   }
650 
651   m_viewer->startTimer();
652 }
653 
654 //-----------------------------------------------------------------------------
655 
setPriority()656 void TaskSheet::setPriority() {
657   if (!m_task) return;
658 
659   int val = m_priority->text().toInt();
660   if (val < 0)
661     m_from->setText(QString::number(m_task->m_priority));
662   else {
663     m_task->m_priority = val;
664     BatchesController::instance()->setDirtyFlag(true);
665   }
666   m_viewer->startTimer();
667 }
668 
669 //-----------------------------------------------------------------------------
670 
setVisible(int)671 void TaskSheet::setVisible(int) {
672   if (!m_task) return;
673   if (m_task->m_onlyVisible == (m_visible->checkState() == Qt::Checked)) return;
674 
675   m_task->m_onlyVisible = (m_visible->checkState() == Qt::Checked);
676   m_commandLine->setText(m_task->getCommandLine());
677   BatchesController::instance()->setDirtyFlag(true);
678 }
679 
680 //-----------------------------------------------------------------------------
681 
setOverwrite(int index)682 void TaskSheet::setOverwrite(int index) {
683   if (!m_task) return;
684   if (m_task->m_overwrite == (OverwriteBehavior)index) return;
685 
686   m_task->m_overwrite = (OverwriteBehavior)index;
687   m_commandLine->setText(m_task->getCommandLine());
688   BatchesController::instance()->setDirtyFlag(true);
689 }
690 
691 //-----------------------------------------------------------------------------
692 
setMultimedia(int)693 void TaskSheet::setMultimedia(int) {
694   if (!m_task) return;
695   if (m_task->m_multimedia == m_multimedia->currentIndex()) return;
696   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
697     update(m_task);
698     return;
699   }
700 
701   m_task->m_multimedia = (m_multimedia->currentIndex());
702   m_commandLine->setText(m_task->getCommandLine());
703   BatchesController::instance()->setDirtyFlag(true);
704 
705   // Update children tasks, if present.
706   TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
707   if (taskGroup) {
708     for (int i = 0; i < taskGroup->getTaskCount(); ++i)
709       taskGroup->getTask(i)->m_multimedia = taskGroup->m_multimedia;
710   }
711 
712   m_viewer->startTimer();
713 }
714 
715 //-----------------------------------------------------------------------------
716 
setThreadsCombo(int)717 void TaskSheet::setThreadsCombo(int) {
718   if (!m_task) return;
719   if (m_task->m_threadsIndex == m_threadsCombo->currentIndex()) return;
720   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
721     update(m_task);
722     return;
723   }
724 
725   m_task->m_threadsIndex = (m_threadsCombo->currentIndex());
726   m_commandLine->setText(m_task->getCommandLine());
727   BatchesController::instance()->setDirtyFlag(true);
728 
729   // Update children tasks, if present.
730   TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
731   if (taskGroup) {
732     for (int i = 0; i < taskGroup->getTaskCount(); ++i)
733       taskGroup->getTask(i)->m_threadsIndex = taskGroup->m_threadsIndex;
734   }
735 
736   m_viewer->startTimer();
737 }
738 
739 //-----------------------------------------------------------------------------
740 
setGranularityCombo(int)741 void TaskSheet::setGranularityCombo(int) {
742   if (!m_task) return;
743   if (m_task->m_maxTileSizeIndex == m_rasterGranularityCombo->currentIndex())
744     return;
745   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
746     update(m_task);
747     return;
748   }
749 
750   m_task->m_maxTileSizeIndex = (m_rasterGranularityCombo->currentIndex());
751   m_commandLine->setText(m_task->getCommandLine());
752   BatchesController::instance()->setDirtyFlag(true);
753 
754   // Update children tasks, if present.
755   TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
756   if (taskGroup) {
757     for (int i = 0; i < taskGroup->getTaskCount(); ++i)
758       taskGroup->getTask(i)->m_maxTileSizeIndex = taskGroup->m_maxTileSizeIndex;
759   }
760 
761   m_viewer->startTimer();
762 }
763 
764 //-----------------------------------------------------------------------------
765 
setOutput()766 void TaskSheet::setOutput() {
767   if (!m_task) return;
768   if (m_task->m_outputPath == TFilePath(m_outputPath->text().toStdWString()))
769     return;
770 
771   if (m_task->m_completionDate.isNull() && !m_task->m_startDate.isNull()) {
772     update(m_task);
773     return;
774   }
775 
776   m_task->m_outputPath = TFilePath(m_outputPath->text().toStdWString());
777   m_commandLine->setText(m_task->getCommandLine());
778 
779   TFarmTaskGroup *taskGroup = dynamic_cast<TFarmTaskGroup *>(m_task);
780 
781   // Check chunk size consistency
782   if (taskGroup && !isMovieType(m_task->m_outputPath.getType())) {
783     m_chunkSize->setEnabled(true);
784     m_chunkSize->setText(QString::number(m_task->m_chunkSize));
785 
786     // Update children outputPaths
787     for (int i = 0; i < taskGroup->getTaskCount(); ++i)
788       taskGroup->getTask(i)->m_outputPath = taskGroup->m_outputPath;
789   } else {
790     m_chunkSize->setEnabled(false);
791     m_chunkSize->setText(QString::number(m_task->m_stepCount));
792 
793     setChunkSize();
794   }
795 
796   BatchesController::instance()->setDirtyFlag(true);
797   m_viewer->startTimer();
798 }
799 
800 //-----------------------------------------------------------------------------
801 
TaskSheet(TasksViewer * owner)802 TaskSheet::TaskSheet(TasksViewer *owner) : QScrollArea(owner) {
803   QFrame *contentWidget = new QFrame(this);
804   contentWidget->setMinimumWidth(300);
805   contentWidget->setMinimumHeight(400);
806 
807   setWidget(contentWidget);
808   setWidgetResizable(true);
809 
810   m_task   = 0;
811   int row  = 0;
812   m_viewer = owner;
813 
814   QGridLayout *layout = new QGridLayout(contentWidget);
815   layout->setMargin(15);
816   layout->setSpacing(8);
817   layout->setColumnStretch(3, 1);
818   layout->setColumnStretch(5, 1);
819   layout->setColumnStretch(6, 4);
820   layout->setColumnMinimumWidth(2, 70);
821   layout->setColumnMinimumWidth(3, 70);
822   layout->setColumnMinimumWidth(4, 40);
823   layout->setColumnMinimumWidth(5, 100);
824   contentWidget->setLayout(layout);
825   ::create(m_name, layout, tr("Name:"), row++);
826   ::create(m_status, layout, tr("Status:"), row++);
827   ::create(m_commandLine, layout, tr("Command Line:"), row++);
828   ::create(m_server, layout, tr("Server:"), row++);
829   ::create(m_submittedBy, layout, tr("Submitted By:"), row++);
830   ::create(m_submittedOn, layout, tr("Submitted On:"), row++);
831   ::create(m_submitDate, layout, tr("Submission Date:"), row++);
832   ::create(m_startDate, layout, tr("Start Date:"), row++);
833   ::create(m_complDate, layout, tr("Completion Date:"), row++);
834   ::create(m_duration, layout, tr("Duration:"), row++);
835   // m_duration->setMaximumWidth(38);
836   ::create(m_stepCount, layout, tr("Step Count:"), row++);
837   // m_stepCount->setMaximumWidth(38);
838   ::create(m_failedSteps, layout, tr("Failed Steps:"), row++);
839   // m_failedSteps->setMaximumWidth(38);
840   ::create(m_succSteps, layout, tr("Successful Steps:"), row++);
841   // m_succSteps->setMaximumWidth(38);
842   ::create(m_priority, layout, tr("Priority:"), row++);
843   // m_priority->setMaximumWidth(40);
844 
845   layout->setColumnStretch(0, 0);
846   layout->setColumnStretch(1, 0);
847   layout->setColumnStretch(2, 1);
848   layout->setColumnStretch(3, 1);
849   layout->setColumnStretch(4, 1);
850   layout->setColumnStretch(5, 1);
851 
852   int row1 = 0;
853 
854   m_boxComposer = new QFrame(contentWidget);
855   m_boxComposer->setMinimumHeight(150);
856   QGridLayout *layout1 = new QGridLayout(m_boxComposer);
857   layout1->setMargin(0);
858   layout1->setSpacing(8);
859   m_boxComposer->setLayout(layout1);
860   ::create(m_outputPath, layout1, tr("Output:"), row1++, 4);
861 
862   ::create(m_chunkSize, m_multimedia, layout1, tr("Frames per Chunk:"),
863            tr("Multimedia:"), Qt::AlignRight | Qt::AlignTop,
864            Qt::AlignLeft | Qt::AlignTop, row1++);
865   ::create(m_from, m_to, layout1, tr("From:"), tr("To:"),
866            Qt::AlignRight | Qt::AlignTop, Qt::AlignRight | Qt::AlignTop,
867            row1++);
868   ::create(m_step, m_shrink, layout1, tr("Step:"), tr("Shrink:"),
869            Qt::AlignRight | Qt::AlignTop, Qt::AlignRight | Qt::AlignTop,
870            row1++);
871 
872   layout1->setColumnMinimumWidth(2, 40);
873   QStringList multimediaTypes;
874   multimediaTypes << tr("None") << tr("Fx Schematic Flows")
875                   << tr("Fx Schematic Terminal Nodes");
876   m_multimedia->addItems(multimediaTypes);
877 
878   ::create(m_threadsCombo, layout1, tr("Dedicated CPUs:"), row1++, 3);
879   QStringList threadsTypes;
880   threadsTypes << tr("Single") << tr("Half") << tr("All");
881   m_threadsCombo->addItems(threadsTypes);
882 
883   ::create(m_rasterGranularityCombo, layout1, tr("Render Tile:"), row1++, 3);
884   QStringList granularityTypes;
885   granularityTypes << tr("None") << tr("Large") << tr("Medium") << tr("Small");
886   m_rasterGranularityCombo->addItems(granularityTypes);
887 
888   layout1->addWidget(new QWidget(), 0, 5);
889 
890   layout1->setColumnStretch(0, 0);
891   layout1->setColumnStretch(1, 0);
892   layout1->setColumnStretch(2, 0);
893   layout1->setColumnStretch(3, 0);
894   layout1->setColumnStretch(4, 0);
895   layout1->setColumnStretch(5, 1);
896 
897   layout->addWidget(m_boxComposer, row++, 0, 1,
898                     6 /*, Qt::AlignLeft|Qt::AlignTop*/);
899 
900   // tcleanupper Box
901   m_boxCleanup         = new QFrame(contentWidget);
902   QGridLayout *layout2 = new QGridLayout(m_boxCleanup);
903   layout2->setMargin(0);
904   layout2->setSpacing(8);
905   m_boxCleanup->setLayout(layout2);
906   ::create(m_visible, layout2, tr("Visible Only"), 0);
907   ::create(m_overwrite, layout2, tr("Overwrite"), 1, 1);
908 
909   QStringList overwriteOptions;
910   overwriteOptions << tr("All") << tr("NoPaint") << tr("Off");
911   m_overwrite->addItems(overwriteOptions);
912   m_overwrite->setCurrentIndex(2);  // do not overwrite by default
913 
914   layout2->setColumnStretch(0, 0);
915   layout2->setColumnStretch(1, 0);
916   layout2->setColumnStretch(2, 1);
917 
918   layout->addWidget(m_boxCleanup, row++, 2, 1, 1, Qt::AlignLeft | Qt::AlignTop);
919 
920   QLabel *label = new QLabel(tr("Dependencies:"), contentWidget);
921   label->setObjectName("TaskSheetItemLabel");
922   layout->addWidget(label, row, 0, 1, 2, Qt::AlignRight | Qt::AlignTop);
923 
924   m_addedBox = new QListWidget(contentWidget);
925   m_addedBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
926   m_addedBox->setObjectName("tasksRemoveBox");
927   m_addedBox->setFrameStyle(QFrame::StyledPanel);
928   layout->addWidget(m_addedBox, row, 2, 1, 2, Qt::AlignLeft | Qt::AlignTop);
929 
930   m_notAddedBox = new QListWidget(contentWidget);
931   m_notAddedBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
932   m_notAddedBox->setObjectName("tasksAddBox");
933   m_notAddedBox->setFrameStyle(QFrame::StyledPanel);
934   layout->addWidget(m_notAddedBox, row++, 4, 1, 2,
935                     Qt::AlignLeft | Qt::AlignTop);
936 
937   QPushButton *removeBtn = new QPushButton(tr("Remove >>"));
938   layout->addWidget(removeBtn, row, 2, 1, 2, Qt::AlignRight | Qt::AlignTop);
939 
940   QPushButton *addBtn = new QPushButton(tr("<< Add"));
941   layout->addWidget(addBtn, row++, 4, 1, 2, Qt::AlignLeft | Qt::AlignTop);
942 
943   bool ret = true;
944 
945   ret =
946       ret && connect(m_name, SIGNAL(editingFinished()), this, SLOT(setName()));
947   ret =
948       ret && connect(m_from, SIGNAL(editingFinished()), this, SLOT(setFrom()));
949   ret = ret && connect(m_to, SIGNAL(editingFinished()), this, SLOT(setTo()));
950   ret =
951       ret && connect(m_step, SIGNAL(editingFinished()), this, SLOT(setStep()));
952   ret = ret &&
953         connect(m_shrink, SIGNAL(editingFinished()), this, SLOT(setShrink()));
954   ret = ret && connect(m_outputPath, SIGNAL(editingFinished()), this,
955                        SLOT(setOutput()));
956   ret = ret && connect(m_chunkSize, SIGNAL(editingFinished()), this,
957                        SLOT(setChunkSize()));
958   ret = ret && connect(m_priority, SIGNAL(editingFinished()), this,
959                        SLOT(setPriority()));
960 
961   ret = ret && connect(m_name, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
962   ret = ret && connect(m_from, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
963   ret = ret && connect(m_to, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
964   ret = ret && connect(m_step, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
965   ret = ret && connect(m_shrink, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
966   ret =
967       ret && connect(m_outputPath, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
968   ret = ret && connect(m_chunkSize, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
969   ret = ret && connect(m_priority, SIGNAL(focusIn()), this, SLOT(onFocusIn()));
970 
971   ret = ret && connect(m_multimedia, SIGNAL(currentIndexChanged(int)), this,
972                        SLOT(setMultimedia(int)));
973   ret = ret && connect(m_visible, SIGNAL(stateChanged(int)), this,
974                        SLOT(setVisible(int)));
975 
976   ret = ret && connect(m_overwrite, SIGNAL(currentIndexChanged(int)), this,
977                        SLOT(setOverwrite(int)));
978 
979   ret =
980       ret && connect(m_addedBox, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
981                      this, SLOT(onRemovedItemDoubleClicked(QListWidgetItem *)));
982   ret = ret &&
983         connect(m_notAddedBox, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
984                 this, SLOT(onAddedItemDoubleClicked(QListWidgetItem *)));
985 
986   ret = ret &&
987         connect(removeBtn, SIGNAL(clicked(bool)), this, SLOT(onRemoved(bool)));
988   ret =
989       ret && connect(addBtn, SIGNAL(clicked(bool)), this, SLOT(onAdded(bool)));
990 
991   ret = ret && connect(m_threadsCombo, SIGNAL(currentIndexChanged(int)), this,
992                        SLOT(setThreadsCombo(int)));
993   ret =
994       ret && connect(m_rasterGranularityCombo, SIGNAL(currentIndexChanged(int)),
995                      this, SLOT(setGranularityCombo(int)));
996 
997   assert(ret);
998 
999   // The following instruction is needed in order to prevent contentWidget from
1000   // shrinking
1001   // beyond the tolerable size for its grid layout.
1002   contentWidget->setMinimumSize(layout->minimumSize());
1003 
1004   widget()->hide();
1005 }
1006 
1007 //=============================================================================
1008 
setSelected(TFarmTask * task)1009 void TasksViewer::setSelected(TFarmTask *task) { m_taskSheet->update(task); }
1010 
1011 //=============================================================================
1012 
1013 OpenFloatingPanel openTasksCommand(MI_OpenTasks, "Tasks", QObject::tr("Tasks"));
1014 
1015 //=============================================================================
1016 //=============================================================================
1017 //=============================================================================
1018 
TaskTreeView(TasksViewer * parent,TaskTreeModel * treeModel)1019 TaskTreeView::TaskTreeView(TasksViewer *parent, TaskTreeModel *treeModel)
1020     : TreeView(parent), m_mainViewer(parent) {
1021   if (!treeModel) treeModel = new TaskTreeModel(this);
1022   setModel(treeModel);
1023   setObjectName("taskeditortree");
1024   setIconSize(QSize(35, 18));
1025 
1026   // connect(this, SIGNAL(pressed      (const QModelIndex &) ), this,
1027   // SLOT(onActivated(const QModelIndex &)));
1028 }
1029 
1030 //----------------------------------------------------------------------------------------------------------------
1031 /*
1032 void TaskTreeView::mousePressEvent ( QMouseEvent * event )
1033 {
1034 QTreeView::mousePressEvent(event);
1035 if (selectedIndexes().empty()) return;
1036 QModelIndex index = selectedIndexes().at(0);
1037 if (!index.isValid()) return;
1038 
1039 ((TaskTreeModel*)model())->setSelected(index);
1040 
1041 m_mainViewer->setSelected(((TaskTreeModel::Item*)index.internalPointer())->getTask());
1042 
1043 if (event->button()==Qt::RightButton)
1044   ((TaskTreeModel*)model())->openContextMenu(event->globalPos());
1045 
1046 }
1047 */
1048 
onClick(TreeModel::Item * gItem,const QPoint & pos,QMouseEvent * e)1049 void TaskTreeView::onClick(TreeModel::Item *gItem, const QPoint &pos,
1050                            QMouseEvent *e) {
1051   TaskTreeModel::Item *item = static_cast<TaskTreeModel::Item *>(gItem);
1052   m_mainViewer->setSelected(item->getTask());
1053   // ((TaskTreeModel*)model())->setSelected(gItem->createIndex());
1054 }
1055 
openContextMenu(TreeModel::Item * gItem,const QPoint & globalPos)1056 void TaskTreeView::openContextMenu(TreeModel::Item *gItem,
1057                                    const QPoint &globalPos) {
1058   TaskTreeModel::Item *item = static_cast<TaskTreeModel::Item *>(gItem);
1059   TaskTreeModel *m          = static_cast<TaskTreeModel *>(model());
1060   // m->setSelected(item->createIndex());
1061   if (item->getDepth() == 1) {
1062     static QMenu globalMenu;
1063     if (globalMenu.isEmpty()) {
1064       const std::vector<QAction *> &actions = m_mainViewer->getActions();
1065       assert(!actions.empty());
1066       int i;
1067       for (i = 0; i < actions.size(); i++) {
1068         globalMenu.addAction(actions[i]);
1069         if (i == 1 || i == 3 || i == 6) globalMenu.addSeparator();
1070       }
1071     }
1072     globalMenu.exec(globalPos);
1073   } else if (item->getDepth() > 1) {
1074     static QMenu menu;
1075     if (menu.isEmpty()) {
1076       bool ret = true;
1077       QAction *action;
1078       action = new QAction(tr("Start"), this);
1079       ret =
1080           ret && connect(action, SIGNAL(triggered(bool)), m, SLOT(start(bool)));
1081       menu.addAction(action);
1082       action = new QAction(tr("Stop"), this);
1083       ret =
1084           ret && connect(action, SIGNAL(triggered(bool)), m, SLOT(stop(bool)));
1085       menu.addAction(action);
1086       action = new QAction(tr("Remove"), this);
1087       menu.addAction(action);
1088       ret = ret &&
1089             connect(action, SIGNAL(triggered(bool)), m, SLOT(remove(bool)));
1090       assert(ret);
1091     }
1092     menu.exec(globalPos);
1093   }
1094 }
1095 
1096 //----------------------------------------------------------------------------------------------------------------
1097 
1098 /*
1099  void TaskTreeView::onActivated(const QModelIndex &index)
1100  {
1101  if (!index.isValid())
1102    return;
1103 
1104 if (!isExpanded(index))
1105   {
1106   setExpanded(index, true);
1107   ((TaskTreeModel*)model())->onExpanded(index);
1108   }
1109 
1110 ((FunctionTreeModel*)model())->onActivated(index);
1111 }
1112 */
1113 
1114 //----------------------------------------------------------------------------------------------------------------
1115 
TaskTreeModel(TaskTreeView * parent)1116 TaskTreeModel::TaskTreeModel(TaskTreeView *parent)
1117     : TreeModel(parent), m_view(parent) {
1118   setupModelData();
1119   emit layoutChanged();
1120 }
1121 
1122 //------------------------------------------------------------------------------------------------------------------
1123 
data(const QModelIndex & index,int role) const1124 QVariant TaskTreeModel::data(const QModelIndex &index, int role) const {
1125   if (!index.isValid()) return QVariant();
1126 
1127   Item *item = (Item *)index.internalPointer();
1128 
1129   if (role == Qt::DecorationRole) {
1130     switch (item->getDepth()) {
1131     case 0:
1132       return QVariant();
1133     case 1:
1134       return createQIcon("tasks");
1135     case 2:
1136     case 3:
1137       TFarmTask *t         = item->getTask();
1138       bool sourceFileIsCLN = (t->m_taskFilePath.getType() == "cln");
1139       switch (t->m_status) {
1140       case Suspended:
1141         return QIcon(
1142             t->m_isComposerTask
1143                 ? getIconThemePath("actions/35/task_render_suspended.svg")
1144                 : (sourceFileIsCLN
1145                        ? getIconThemePath("actions/35/task_cln_suspended.svg")
1146                        : getIconThemePath(
1147                              "actions/35/task_cleanup_suspended.svg")));
1148       case Waiting:
1149         return QIcon(
1150             t->m_isComposerTask
1151                 ? getIconThemePath(
1152                       "actions/35/task_render_completed_with_errors.svg")
1153                 : (sourceFileIsCLN
1154                        ? getIconThemePath(
1155                              "actions/35/task_cln_completed_with_errors.svg")
1156                        : getIconThemePath(
1157                              "actions/35/"
1158                              "task_cleanup_completed_with_errors.svg")));
1159       case Running:
1160         return QIcon(
1161             t->m_isComposerTask
1162                 ? getIconThemePath("actions/35/task_render_computing.svg")
1163                 : (sourceFileIsCLN
1164                        ? getIconThemePath("actions/35/task_cln_computing.svg")
1165                        : getIconThemePath(
1166                              "actions/35/task_cleanup_computing.svg")));
1167       case Completed:
1168         return QIcon(
1169             t->m_isComposerTask
1170                 ? getIconThemePath("actions/35/task_render_completed.svg")
1171                 : (sourceFileIsCLN
1172                        ? getIconThemePath("actions/35/task_cln_completed.svg")
1173                        : getIconThemePath(
1174                              "actions/35/task_cleanup_completed.svg")));
1175       case Aborted:
1176       case TaskUnknown:
1177         return QIcon(
1178             t->m_isComposerTask
1179                 ? getIconThemePath("actions/35/task_render_failed.svg")
1180                 : (sourceFileIsCLN
1181                        ? getIconThemePath("actions/35/task_cln_failed.svg")
1182                        : getIconThemePath(
1183                              "actions/35/task_cleanup_failed.svg")));
1184       default:
1185         assert(false);
1186       }
1187     }
1188     assert(false);
1189   } else if (role == Qt::DisplayRole)
1190     return item->m_task ? item->m_task->m_name : item->m_name;
1191 
1192   return QVariant();
1193 }
1194 
1195 //------------------------------------------------------------------------------------------------------------------
1196 
save(bool)1197 void TaskTreeModel::save(bool) {
1198   BatchesController::instance()->save();
1199   // SaveTaskListPopup popup;
1200 }
1201 
1202 //------------------------------------------------------------------------------------------------------------------
1203 
saveas(bool)1204 void TaskTreeModel::saveas(bool) { BatchesController::instance()->saveas(); }
1205 
1206 //------------------------------------------------------------------------------------------------------------------
1207 
load(bool)1208 void TaskTreeModel::load(bool) { BatchesController::instance()->load(); }
1209 
1210 //------------------------------------------------------------------------------------------------------------------
1211 
addRenderTask(bool)1212 void TaskTreeModel::addRenderTask(bool) {
1213   BatchesController::instance()->loadTask(true);
1214 }
1215 
1216 //------------------------------------------------------------------------------------------------------------------
1217 
addCleanupTask(bool)1218 void TaskTreeModel::addCleanupTask(bool) {
1219   BatchesController::instance()->loadTask(false);
1220 }
1221 
1222 //------------------------------------------------------------------------------------------------------------------
1223 
start(bool)1224 void TaskTreeModel::start(bool) {
1225   try {
1226     TMsgCore::instance()->openConnection();
1227     QModelIndexList indexList = m_view->getSelectedIndexes();
1228     int i;
1229 
1230     if (indexList.size() == 0)
1231       BatchesController::instance()->startAll();
1232     else
1233       for (i = 0; i < indexList.size(); i++) {
1234         QModelIndex modelIndex = indexList.at(i);
1235         if (!modelIndex.isValid()) continue;
1236         TaskTreeModel::Item *item = (Item *)modelIndex.internalPointer();
1237         if (item->getDepth() == 1)  // significa che ho selezionato la radice!
1238         {
1239           assert(indexList.size() == 1);
1240           BatchesController::instance()->startAll();
1241           break;
1242         }
1243 
1244         TFarmTask *task = item->getTask();
1245         if (!task) continue;
1246         BatchesController::instance()->start(task->m_id);
1247       }
1248   } catch (TException &e) {
1249     DVGui::warning(QString::fromStdString(::to_string(e.getMessage())));
1250   }
1251 
1252   emit layoutChanged();
1253 }
1254 
1255 //------------------------------------------------------------------------------------------------------------------
1256 
stop(bool)1257 void TaskTreeModel::stop(bool) {
1258   try {
1259     QModelIndexList indexList = m_view->getSelectedIndexes();
1260     int i;
1261     if (indexList.empty())
1262       BatchesController::instance()->stopAll();
1263     else
1264       for (i = 0; i < indexList.size(); i++) {
1265         QModelIndex modelIndex = indexList.at(i);
1266         if (!modelIndex.isValid()) continue;
1267         TaskTreeModel::Item *item = (Item *)modelIndex.internalPointer();
1268         if (item->getDepth() == 1)  // significa che ho selezionato la radice!
1269         {
1270           assert(indexList.size() == 1);
1271           BatchesController::instance()->stopAll();
1272           break;
1273         }
1274         TFarmTask *task = item->getTask();
1275         if (!task) continue;
1276         BatchesController::instance()->stop(task->m_id);
1277       }
1278   } catch (TException &e) {
1279     DVGui::warning(QString::fromStdString(::to_string(e.getMessage())));
1280   }
1281 
1282   emit layoutChanged();
1283 }
1284 
1285 //------------------------------------------------------------------------------------------------------------------
1286 
remove(bool)1287 void TaskTreeModel::remove(bool) {
1288   QModelIndexList indexList = m_view->getSelectedIndexes();
1289   int i;
1290 
1291   if (indexList.size() == 0) {
1292     if (DVGui::MsgBox(tr("Are you sure you want to remove ALL tasks?"),
1293                       tr("Remove All"), tr("Cancel")) > 1)
1294       return;
1295 
1296     BatchesController::instance()->removeAllTasks();
1297   } else {
1298     for (i = 0; i < indexList.size(); i++) {
1299       QModelIndex modelIndex = indexList.at(i);
1300       if (!modelIndex.isValid()) continue;
1301       TaskTreeModel::Item *item = (Item *)modelIndex.internalPointer();
1302       if (item->getDepth() == 1)  // significa che ho selezionato la radice!
1303       {
1304         assert(indexList.size() == 1);
1305         BatchesController::instance()->removeAllTasks();
1306         break;
1307       }
1308       TFarmTask *task = item->getTask();
1309       if (!task) continue;
1310       BatchesController::instance()->removeTask(task->m_id);
1311     }
1312     TaskTreeView *p = (TaskTreeView *)QObject::parent();
1313 
1314     p->m_mainViewer->setSelected(0);
1315   }
1316 
1317   setupModelData();
1318   emit layoutChanged();
1319 }
1320 
1321 //------------------------------------------------------------------------------------------------------------------
1322 
1323 // WARNING: probabilmente non e' piu' necessaria
1324 // Se serve bisogna stare attenti: e' necessario emettere
1325 // prima layoutAboutToBeChanged() e poi aggiornare gli indici
1326 // permanenti
1327 
update()1328 void TaskTreeModel::update() {
1329   // assert(0);
1330   emit layoutChanged();
1331 }
1332 
1333 //---------------------------------------------------------------------------------------------------------------
1334 
1335 //! setupModelData() : refresh the model
setupModelData()1336 void TaskTreeModel::setupModelData() {
1337   beginRefresh();
1338 
1339   BatchesController *controller = BatchesController::instance();
1340   QString taskListName          = controller->getListName();
1341 
1342   // setup root and tasklist. set the taskList's name
1343   Item *root     = 0;
1344   Item *taskList = 0;
1345   if (!getRootItem()) {
1346     setRootItem(root = new Item("Root"));
1347     root->appendChild(taskList = new Item(taskListName));
1348   } else {
1349     root     = static_cast<Item *>(getRootItem());
1350     taskList = static_cast<Item *>(root->getChild(0));
1351     taskList->setName(taskListName);
1352     // TODO: emettere un dataChanged se il nome e' diverso?
1353   }
1354   assert(root);
1355   assert(root->getChildCount() == 1);
1356 
1357   // update the list of tasks
1358   int i, j;
1359   QList<TreeModel::Item *> tasks;
1360   for (i = 0; i < controller->getTaskCount(); i++) {
1361     TFarmTaskGroup *group =
1362         dynamic_cast<TFarmTaskGroup *>(controller->getTask(i));
1363     if (group) tasks.push_back(new Item(group));
1364   }
1365   taskList->setChildren(tasks);
1366 
1367   // foreach task (the new ones, but also the old ones) update
1368   // the list of sub-tasks
1369   for (i = 0; i < taskList->getChildCount(); i++) {
1370     TreeModel::Item *item = taskList->getChild(i);
1371     TFarmTaskGroup *group =
1372         static_cast<TFarmTaskGroup *>(item->getInternalPointer());
1373     QList<TreeModel::Item *> subTasks;
1374     for (j = 0; j < group->getTaskCount(); j++)
1375       subTasks.push_back(new Item(group->getTask(j)));
1376     item->setChildren(subTasks);
1377   }
1378   endRefresh();
1379   m_view->expandAll();
1380 }
1381 
1382 //------------------------------------------------------------------------------------------------------------------
1383 
Item(const QString & name)1384 TaskTreeModel::Item::Item(const QString &name)
1385     : TreeModel::Item(), m_name(name), m_task(0) {
1386   assert(!m_name.isEmpty());
1387 }
1388 /*
1389   TFarmTask*t = BatchesController::instance()->getTask(i);
1390 //------------------------------------------------------------------------------------------------------------------
1391   if (group = dynamic_cast<TFarmTaskGroup*>(t))
1392 */
1393 
Item(TFarmTask * task)1394 TaskTreeModel::Item::Item(TFarmTask *task)
1395     : TreeModel::Item(), m_name(), m_task(task) {
1396   assert(m_task);
1397 }
1398