ctkWorkflowGroupBox.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "ctkLogger.h"
  26. //--------------------------------------------------------------------------
  27. static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowGroupBox");
  28. //--------------------------------------------------------------------------
  29. //-----------------------------------------------------------------------------
  30. class ctkWorkflowGroupBoxPrivate: public Ui_ctkWorkflowGroupBox
  31. {
  32. public:
  33. ctkWorkflowGroupBoxPrivate();
  34. ~ctkWorkflowGroupBoxPrivate();
  35. QString TitleFormat;
  36. QString SubTitleFormat;
  37. QString ErrorTextFormat;
  38. bool HideWidgetsOfNonCurrentSteps;
  39. bool ErrorTextEnabled;
  40. ctkWorkflowStep* StepShownPreviously;
  41. ctkWorkflowStep* StepShownCurrently;
  42. void setupUi(ctkWorkflowGroupBox* widget);
  43. };
  44. // --------------------------------------------------------------------------
  45. // ctkWorkflowGroupBoxPrivate methods
  46. //---------------------------------------------------------------------------
  47. ctkWorkflowGroupBoxPrivate::ctkWorkflowGroupBoxPrivate()
  48. {
  49. this->TitleFormat = "{current:name}";
  50. this->SubTitleFormat = "{current:description}";
  51. this->ErrorTextFormat = "{current:statusText}";
  52. this->HideWidgetsOfNonCurrentSteps = false;
  53. this->ErrorTextEnabled = true;
  54. this->StepShownPreviously = 0;
  55. this->StepShownCurrently = 0;
  56. }
  57. //---------------------------------------------------------------------------
  58. ctkWorkflowGroupBoxPrivate::~ctkWorkflowGroupBoxPrivate()
  59. {
  60. }
  61. //---------------------------------------------------------------------------
  62. void ctkWorkflowGroupBoxPrivate::setupUi(ctkWorkflowGroupBox* widget)
  63. {
  64. this->Ui_ctkWorkflowGroupBox::setupUi(widget);
  65. }
  66. // --------------------------------------------------------------------------
  67. // ctkWorkflowGroupBoxMethods
  68. // --------------------------------------------------------------------------
  69. ctkWorkflowGroupBox::ctkWorkflowGroupBox(QWidget* _parent) : Superclass(_parent)
  70. , d_ptr(new ctkWorkflowGroupBoxPrivate)
  71. {
  72. Q_D(ctkWorkflowGroupBox);
  73. d->setupUi(this);
  74. }
  75. // --------------------------------------------------------------------------
  76. ctkWorkflowGroupBox::~ctkWorkflowGroupBox()
  77. {
  78. }
  79. // --------------------------------------------------------------------------
  80. CTK_GET_CPP(ctkWorkflowGroupBox, bool, hideWidgetsOfNonCurrentSteps, HideWidgetsOfNonCurrentSteps);
  81. CTK_SET_CPP(ctkWorkflowGroupBox, bool, setHideWidgetsOfNonCurrentSteps, HideWidgetsOfNonCurrentSteps);
  82. CTK_GET_CPP(ctkWorkflowGroupBox, bool, errorTextEnabled, ErrorTextEnabled);
  83. CTK_SET_CPP(ctkWorkflowGroupBox, bool, setErrorTextEnabled, ErrorTextEnabled);
  84. CTK_GET_CPP(ctkWorkflowGroupBox, QLayout*, clientAreaLayout, ClientAreaLayout);
  85. // --------------------------------------------------------------------------
  86. void ctkWorkflowGroupBox::updateGroupBox(ctkWorkflowStep* currentStep)
  87. {
  88. Q_D(ctkWorkflowGroupBox);
  89. d->StepShownPreviously = (currentStep != d->StepShownCurrently ?
  90. d->StepShownCurrently : d->StepShownPreviously);
  91. d->StepShownCurrently = currentStep;
  92. ctkWorkflowWidgetStep* currentWidgetStep = dynamic_cast<ctkWorkflowWidgetStep*>(currentStep);
  93. if (currentWidgetStep)
  94. {
  95. ctkWorkflowWidget::formatButton(d->CollapsibleButton, d->TitleFormat, currentWidgetStep);
  96. QString subTitleText = ctkWorkflowWidget::formatText(d->SubTitleFormat, currentWidgetStep);
  97. this->setSubTitle(subTitleText);
  98. QString errorText = ctkWorkflowWidget::formatText(d->ErrorTextFormat, currentWidgetStep);
  99. this->setErrorText(errorText);
  100. // don't show textual elements if they are empty
  101. d->SubTitleTextBrowser->setVisible(!this->subTitle().isEmpty());
  102. d->PreTextBrowser->setVisible(!this->preText().isEmpty());
  103. d->PostTextBrowser->setVisible(!this->postText().isEmpty());
  104. d->ErrorTextBrowser->setVisible(!this->errorText().isEmpty() && d->ErrorTextEnabled);
  105. }
  106. // disable/hide the previously shown step
  107. if (ctkWorkflowWidgetStep* prevStep = dynamic_cast<ctkWorkflowWidgetStep*>(d->StepShownPreviously))
  108. {
  109. logger.debug(QString("updateClientArea - hiding %1").arg(prevStep->name()));
  110. if (QWidget* stepArea = prevStep->stepArea())
  111. {
  112. stepArea->setEnabled(false);
  113. if (d->HideWidgetsOfNonCurrentSteps)
  114. {
  115. stepArea->hide();
  116. }
  117. }
  118. }
  119. // show/enable the current step
  120. if (currentWidgetStep)
  121. {
  122. currentWidgetStep->showUserInterface();
  123. if (QWidget* stepArea = currentWidgetStep->stepArea())
  124. {
  125. // add the step's client area to the widget if we haven't before
  126. if (!this->isAncestorOf(stepArea))
  127. {
  128. d->ClientAreaLayout->addWidget(stepArea);
  129. }
  130. stepArea->setEnabled(true);
  131. stepArea->show();
  132. }
  133. }
  134. }
  135. // --------------------------------------------------------------------------
  136. QString ctkWorkflowGroupBox::title()const
  137. {
  138. Q_D(const ctkWorkflowGroupBox);
  139. return d->CollapsibleButton->text();
  140. }
  141. // --------------------------------------------------------------------------
  142. QString ctkWorkflowGroupBox::subTitle()const
  143. {
  144. Q_D(const ctkWorkflowGroupBox);
  145. return d->SubTitleTextBrowser->toPlainText();
  146. }
  147. // --------------------------------------------------------------------------
  148. void ctkWorkflowGroupBox::setSubTitle(const QString& newSubTitleText)
  149. {
  150. Q_D(ctkWorkflowGroupBox);
  151. d->SubTitleTextBrowser->setPlainText(newSubTitleText);
  152. }
  153. // --------------------------------------------------------------------------
  154. QString ctkWorkflowGroupBox::preText()const
  155. {
  156. Q_D(const ctkWorkflowGroupBox);
  157. return d->PreTextBrowser->toPlainText();
  158. }
  159. // --------------------------------------------------------------------------
  160. void ctkWorkflowGroupBox::setPreText(const QString& newPreText)
  161. {
  162. Q_D(ctkWorkflowGroupBox);
  163. d->PreTextBrowser->setPlainText(newPreText);
  164. }
  165. // --------------------------------------------------------------------------
  166. QString ctkWorkflowGroupBox::postText()const
  167. {
  168. Q_D(const ctkWorkflowGroupBox);
  169. return d->PostTextBrowser->toPlainText();
  170. }
  171. // --------------------------------------------------------------------------
  172. void ctkWorkflowGroupBox::setPostText(const QString& newPostText)
  173. {
  174. Q_D(ctkWorkflowGroupBox);
  175. d->PostTextBrowser->setPlainText(newPostText);
  176. }
  177. // --------------------------------------------------------------------------
  178. QString ctkWorkflowGroupBox::errorText()const
  179. {
  180. Q_D(const ctkWorkflowGroupBox);
  181. return d->ErrorTextBrowser->toPlainText();
  182. }
  183. // --------------------------------------------------------------------------
  184. void ctkWorkflowGroupBox::setErrorText(const QString& newErrorText)
  185. {
  186. Q_D(ctkWorkflowGroupBox);
  187. d->ErrorTextBrowser->setPlainText(newErrorText);
  188. }
  189. // --------------------------------------------------------------------------
  190. QString ctkWorkflowGroupBox::titleFormat()const
  191. {
  192. Q_D(const ctkWorkflowGroupBox);
  193. return d->TitleFormat;
  194. }
  195. // --------------------------------------------------------------------------
  196. void ctkWorkflowGroupBox::setTitleFormat(const QString& format)
  197. {
  198. Q_D(ctkWorkflowGroupBox);
  199. d->TitleFormat = format;
  200. this->updateGroupBox(d->StepShownCurrently);
  201. }
  202. // --------------------------------------------------------------------------
  203. QString ctkWorkflowGroupBox::subTitleFormat()const
  204. {
  205. Q_D(const ctkWorkflowGroupBox);
  206. return d->SubTitleFormat;
  207. }
  208. // --------------------------------------------------------------------------
  209. void ctkWorkflowGroupBox::setSubTitleFormat(const QString& format)
  210. {
  211. Q_D(ctkWorkflowGroupBox);
  212. d->SubTitleFormat = format;
  213. this->updateGroupBox(d->StepShownCurrently);
  214. }
  215. // --------------------------------------------------------------------------
  216. QString ctkWorkflowGroupBox::errorTextFormat()const
  217. {
  218. Q_D(const ctkWorkflowGroupBox);
  219. return d->ErrorTextFormat;
  220. }
  221. // --------------------------------------------------------------------------
  222. void ctkWorkflowGroupBox::setErrorTextFormat(const QString& format)
  223. {
  224. Q_D(ctkWorkflowGroupBox);
  225. d->ErrorTextFormat = format;
  226. this->updateGroupBox(d->StepShownCurrently);
  227. }