ctkCmdLineModuleExplorerProgressWidget.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include <ctkCmdLineModuleFuture.h>
  16. #include "ctkCmdLineModuleExplorerProgressWidget.h"
  17. #include "ui_ctkCmdLineModuleExplorerProgressWidget.h"
  18. ctkCmdLineModuleExplorerProgressWidget::ctkCmdLineModuleExplorerProgressWidget(QWidget *parent)
  19. : QWidget(parent)
  20. , ui(new Ui::ctkCmdLineModuleExplorerProgressWidget)
  21. {
  22. ui->setupUi(this);
  23. ui->RemoveButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton));
  24. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  25. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  26. // resuming the future, after the "resume" signal has been emitted... we use
  27. // a polling aproach instead.
  28. PollPauseTimer.setInterval(300);
  29. connect(&PollPauseTimer, SIGNAL(timeout()), SLOT(checkModulePaused()));
  30. connect(&FutureWatcher, SIGNAL(started()), SLOT(moduleStarted()));
  31. connect(&FutureWatcher, SIGNAL(canceled()), SLOT(moduleCanceled()));
  32. connect(&FutureWatcher, SIGNAL(finished()), SLOT(moduleFinished()));
  33. connect(&FutureWatcher, SIGNAL(resumed()), SLOT(moduleResumed()));
  34. connect(&FutureWatcher, SIGNAL(progressRangeChanged(int,int)), SLOT(moduleProgressRangeChanged(int,int)));
  35. connect(&FutureWatcher, SIGNAL(progressTextChanged(QString)), ui->ProgressText, SLOT(setText(QString)));
  36. connect(&FutureWatcher, SIGNAL(progressValueChanged(int)), ui->ProgressBar, SLOT(setValue(int)));
  37. connect(ui->CancelButton, SIGNAL(clicked()), &this->FutureWatcher, SLOT(cancel()));
  38. PollPauseTimer.start();
  39. }
  40. ctkCmdLineModuleExplorerProgressWidget::~ctkCmdLineModuleExplorerProgressWidget()
  41. {
  42. delete ui;
  43. }
  44. void ctkCmdLineModuleExplorerProgressWidget::setFuture(const ctkCmdLineModuleFuture &future)
  45. {
  46. ui->PauseButton->setEnabled(future.canPause());
  47. ui->CancelButton->setEnabled(future.canCancel());
  48. ui->RemoveButton->setEnabled(!future.isRunning());
  49. FutureWatcher.setFuture(future);
  50. }
  51. void ctkCmdLineModuleExplorerProgressWidget::setTitle(const QString &title)
  52. {
  53. ui->ProgressTitle->setText(title);
  54. }
  55. void ctkCmdLineModuleExplorerProgressWidget::setHighlightStyle(bool highlight)
  56. {
  57. QPalette::ColorRole labelRole = highlight ? QPalette::NoRole : QPalette::Midlight;
  58. ui->ProgressTitle->setForegroundRole(labelRole);
  59. ui->ProgressText->setForegroundRole(labelRole);
  60. ui->ProgressBar->setEnabled(highlight);
  61. }
  62. void ctkCmdLineModuleExplorerProgressWidget::mouseReleaseEvent(QMouseEvent*)
  63. {
  64. emit clicked();
  65. }
  66. void ctkCmdLineModuleExplorerProgressWidget::on_PauseButton_toggled(bool toggled)
  67. {
  68. this->FutureWatcher.setPaused(toggled);
  69. }
  70. void ctkCmdLineModuleExplorerProgressWidget::on_RemoveButton_clicked()
  71. {
  72. this->deleteLater();
  73. }
  74. void ctkCmdLineModuleExplorerProgressWidget::moduleStarted()
  75. {
  76. this->ui->ProgressBar->setMaximum(0);
  77. }
  78. void ctkCmdLineModuleExplorerProgressWidget::moduleCanceled()
  79. {
  80. this->ui->PauseButton->setEnabled(false);
  81. this->ui->PauseButton->setChecked(false);
  82. this->ui->CancelButton->setEnabled(false);
  83. this->ui->RemoveButton->setEnabled(true);
  84. }
  85. void ctkCmdLineModuleExplorerProgressWidget::moduleFinished()
  86. {
  87. this->ui->PauseButton->setEnabled(false);
  88. this->ui->PauseButton->setChecked(false);
  89. this->ui->CancelButton->setEnabled(false);
  90. this->ui->RemoveButton->setEnabled(true);
  91. }
  92. void ctkCmdLineModuleExplorerProgressWidget::checkModulePaused()
  93. {
  94. if (this->FutureWatcher.future().isPaused())
  95. {
  96. if (!ui->PauseButton->isChecked())
  97. {
  98. ui->PauseButton->setChecked(true);
  99. }
  100. }
  101. else
  102. {
  103. if (ui->PauseButton->isChecked())
  104. {
  105. ui->PauseButton->setChecked(false);
  106. }
  107. }
  108. }
  109. void ctkCmdLineModuleExplorerProgressWidget::moduleResumed()
  110. {
  111. this->ui->PauseButton->setChecked(false);
  112. }
  113. void ctkCmdLineModuleExplorerProgressWidget::moduleProgressRangeChanged(int progressMin, int progressMax)
  114. {
  115. this->ui->ProgressBar->setMinimum(progressMin);
  116. this->ui->ProgressBar->setMaximum(progressMax);
  117. }
  118. void ctkCmdLineModuleExplorerProgressWidget::moduleProgressTextChanged(const QString& progressText)
  119. {
  120. Q_UNUSED(progressText)
  121. }
  122. void ctkCmdLineModuleExplorerProgressWidget::moduleProgressValueChanged(int progressValue)
  123. {
  124. Q_UNUSED(progressValue)
  125. }