ctkWorkflowGroupBox.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QString>
  16. #include <QVBoxLayout>
  17. #include <QDebug>
  18. #include <QList>
  19. // CTK includes
  20. #include "ctkWorkflowGroupBox.h"
  21. #include "ctkWorkflowWidget.h"
  22. #include "ctkWorkflowWidgetStep.h"
  23. #include "ctkFittedTextBrowser.h"
  24. #include "ui_ctkWorkflowGroupBox.h"
  25. //-----------------------------------------------------------------------------
  26. class ctkWorkflowGroupBoxPrivate: public Ui_ctkWorkflowGroupBox
  27. {
  28. public:
  29. ctkWorkflowGroupBoxPrivate();
  30. ~ctkWorkflowGroupBoxPrivate();
  31. QString TitleFormat;
  32. QString SubTitleFormat;
  33. QString ErrorTextFormat;
  34. bool HideWidgetsOfNonCurrentSteps;
  35. bool ErrorTextEnabled;
  36. ctkWorkflowStep* StepShownPreviously;
  37. ctkWorkflowStep* StepShownCurrently;
  38. void setupUi(ctkWorkflowGroupBox* widget);
  39. };
  40. // --------------------------------------------------------------------------
  41. // ctkWorkflowGroupBoxPrivate methods
  42. //---------------------------------------------------------------------------
  43. ctkWorkflowGroupBoxPrivate::ctkWorkflowGroupBoxPrivate()
  44. {
  45. this->TitleFormat = "{current:name}";
  46. this->SubTitleFormat = "{current:description}";
  47. this->ErrorTextFormat = "{current:statusText}";
  48. this->HideWidgetsOfNonCurrentSteps = false;
  49. this->ErrorTextEnabled = true;
  50. this->StepShownPreviously = 0;
  51. this->StepShownCurrently = 0;
  52. }
  53. //---------------------------------------------------------------------------
  54. ctkWorkflowGroupBoxPrivate::~ctkWorkflowGroupBoxPrivate()
  55. {
  56. }
  57. //---------------------------------------------------------------------------
  58. void ctkWorkflowGroupBoxPrivate::setupUi(ctkWorkflowGroupBox* widget)
  59. {
  60. this->Ui_ctkWorkflowGroupBox::setupUi(widget);
  61. }
  62. // --------------------------------------------------------------------------
  63. // ctkWorkflowGroupBoxMethods
  64. // --------------------------------------------------------------------------
  65. ctkWorkflowGroupBox::ctkWorkflowGroupBox(QWidget* _parent) : Superclass(_parent)
  66. , d_ptr(new ctkWorkflowGroupBoxPrivate)
  67. {
  68. Q_D(ctkWorkflowGroupBox);
  69. d->setupUi(this);
  70. }
  71. // --------------------------------------------------------------------------
  72. ctkWorkflowGroupBox::~ctkWorkflowGroupBox()
  73. {
  74. }
  75. // --------------------------------------------------------------------------
  76. CTK_GET_CPP(ctkWorkflowGroupBox, bool, hideWidgetsOfNonCurrentSteps, HideWidgetsOfNonCurrentSteps);
  77. CTK_SET_CPP(ctkWorkflowGroupBox, bool, setHideWidgetsOfNonCurrentSteps, HideWidgetsOfNonCurrentSteps);
  78. CTK_GET_CPP(ctkWorkflowGroupBox, bool, errorTextEnabled, ErrorTextEnabled);
  79. CTK_SET_CPP(ctkWorkflowGroupBox, bool, setErrorTextEnabled, ErrorTextEnabled);
  80. CTK_GET_CPP(ctkWorkflowGroupBox, QLayout*, clientAreaLayout, ClientAreaLayout);
  81. // --------------------------------------------------------------------------
  82. void ctkWorkflowGroupBox::updateGroupBox(ctkWorkflowStep* currentStep)
  83. {
  84. Q_D(ctkWorkflowGroupBox);
  85. d->StepShownPreviously = (currentStep != d->StepShownCurrently ?
  86. d->StepShownCurrently : d->StepShownPreviously);
  87. d->StepShownCurrently = currentStep;
  88. ctkWorkflowWidgetStep* currentWidgetStep = dynamic_cast<ctkWorkflowWidgetStep*>(currentStep);
  89. if (currentWidgetStep)
  90. {
  91. ctkWorkflowWidget::formatButton(d->CollapsibleButton, d->TitleFormat, currentWidgetStep);
  92. QString subTitleText = ctkWorkflowWidget::formatText(d->SubTitleFormat, currentWidgetStep);
  93. this->setSubTitle(subTitleText);
  94. QString errorText = ctkWorkflowWidget::formatText(d->ErrorTextFormat, currentWidgetStep);
  95. this->setErrorText(errorText);
  96. // don't show textual elements if they are empty
  97. d->SubTitleTextBrowser->setVisible(!this->subTitle().isEmpty());
  98. d->PreTextBrowser->setVisible(!this->preText().isEmpty());
  99. d->PostTextBrowser->setVisible(!this->postText().isEmpty());
  100. d->ErrorTextBrowser->setVisible(!this->errorText().isEmpty() && d->ErrorTextEnabled);
  101. }
  102. // disable/hide the previously shown step
  103. if (ctkWorkflowWidgetStep* prevStep = dynamic_cast<ctkWorkflowWidgetStep*>(d->StepShownPreviously))
  104. {
  105. //qDebug() << QString("updateClientArea - hiding %1").arg(prevStep->name());
  106. if (QWidget* stepArea = prevStep->stepArea())
  107. {
  108. stepArea->setEnabled(false);
  109. if (d->HideWidgetsOfNonCurrentSteps)
  110. {
  111. stepArea->hide();
  112. }
  113. }
  114. }
  115. // show/enable the current step
  116. if (currentWidgetStep)
  117. {
  118. currentWidgetStep->showUserInterface();
  119. if (QWidget* stepArea = currentWidgetStep->stepArea())
  120. {
  121. // add the step's client area to the widget if we haven't before
  122. if (!this->isAncestorOf(stepArea))
  123. {
  124. d->ClientAreaLayout->addWidget(stepArea);
  125. }
  126. stepArea->setEnabled(true);
  127. stepArea->show();
  128. }
  129. }
  130. }
  131. // --------------------------------------------------------------------------
  132. QString ctkWorkflowGroupBox::title()const
  133. {
  134. Q_D(const ctkWorkflowGroupBox);
  135. return d->CollapsibleButton->text();
  136. }
  137. // --------------------------------------------------------------------------
  138. QString ctkWorkflowGroupBox::subTitle()const
  139. {
  140. Q_D(const ctkWorkflowGroupBox);
  141. return d->SubTitleTextBrowser->toPlainText();
  142. }
  143. // --------------------------------------------------------------------------
  144. void ctkWorkflowGroupBox::setSubTitle(const QString& newSubTitleText)
  145. {
  146. Q_D(ctkWorkflowGroupBox);
  147. d->SubTitleTextBrowser->setPlainText(newSubTitleText);
  148. }
  149. // --------------------------------------------------------------------------
  150. QString ctkWorkflowGroupBox::preText()const
  151. {
  152. Q_D(const ctkWorkflowGroupBox);
  153. return d->PreTextBrowser->toPlainText();
  154. }
  155. // --------------------------------------------------------------------------
  156. void ctkWorkflowGroupBox::setPreText(const QString& newPreText)
  157. {
  158. Q_D(ctkWorkflowGroupBox);
  159. d->PreTextBrowser->setPlainText(newPreText);
  160. }
  161. // --------------------------------------------------------------------------
  162. QString ctkWorkflowGroupBox::postText()const
  163. {
  164. Q_D(const ctkWorkflowGroupBox);
  165. return d->PostTextBrowser->toPlainText();
  166. }
  167. // --------------------------------------------------------------------------
  168. void ctkWorkflowGroupBox::setPostText(const QString& newPostText)
  169. {
  170. Q_D(ctkWorkflowGroupBox);
  171. d->PostTextBrowser->setPlainText(newPostText);
  172. }
  173. // --------------------------------------------------------------------------
  174. QString ctkWorkflowGroupBox::errorText()const
  175. {
  176. Q_D(const ctkWorkflowGroupBox);
  177. return d->ErrorTextBrowser->toPlainText();
  178. }
  179. // --------------------------------------------------------------------------
  180. void ctkWorkflowGroupBox::setErrorText(const QString& newErrorText)
  181. {
  182. Q_D(ctkWorkflowGroupBox);
  183. d->ErrorTextBrowser->setPlainText(newErrorText);
  184. }
  185. // --------------------------------------------------------------------------
  186. QString ctkWorkflowGroupBox::titleFormat()const
  187. {
  188. Q_D(const ctkWorkflowGroupBox);
  189. return d->TitleFormat;
  190. }
  191. // --------------------------------------------------------------------------
  192. void ctkWorkflowGroupBox::setTitleFormat(const QString& format)
  193. {
  194. Q_D(ctkWorkflowGroupBox);
  195. d->TitleFormat = format;
  196. this->updateGroupBox(d->StepShownCurrently);
  197. }
  198. // --------------------------------------------------------------------------
  199. QString ctkWorkflowGroupBox::subTitleFormat()const
  200. {
  201. Q_D(const ctkWorkflowGroupBox);
  202. return d->SubTitleFormat;
  203. }
  204. // --------------------------------------------------------------------------
  205. void ctkWorkflowGroupBox::setSubTitleFormat(const QString& format)
  206. {
  207. Q_D(ctkWorkflowGroupBox);
  208. d->SubTitleFormat = format;
  209. this->updateGroupBox(d->StepShownCurrently);
  210. }
  211. // --------------------------------------------------------------------------
  212. QString ctkWorkflowGroupBox::errorTextFormat()const
  213. {
  214. Q_D(const ctkWorkflowGroupBox);
  215. return d->ErrorTextFormat;
  216. }
  217. // --------------------------------------------------------------------------
  218. void ctkWorkflowGroupBox::setErrorTextFormat(const QString& format)
  219. {
  220. Q_D(ctkWorkflowGroupBox);
  221. d->ErrorTextFormat = format;
  222. this->updateGroupBox(d->StepShownCurrently);
  223. }