ctkWorkflowWidgetStep.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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 <QWidget>
  16. #include <QList>
  17. #include <QDebug>
  18. // CTK includes
  19. #include "ctkWorkflowWidgetStep.h"
  20. #include "ctkWorkflowWidget.h"
  21. #include "ctkWorkflow.h"
  22. #include "ctkWorkflowButtonBoxWidget.h"
  23. #include "ctkLogger.h"
  24. // STD includes
  25. #include <iostream>
  26. //--------------------------------------------------------------------------
  27. static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowWidgetStep");
  28. //--------------------------------------------------------------------------
  29. //-----------------------------------------------------------------------------
  30. class ctkWorkflowWidgetStepPrivate : public ctkPrivate<ctkWorkflowWidgetStep>
  31. {
  32. public:
  33. CTK_DECLARE_PUBLIC(ctkWorkflowWidgetStep);
  34. ctkWorkflowWidgetStepPrivate();
  35. ~ctkWorkflowWidgetStepPrivate(){}
  36. QWidget* stepArea;
  37. QList<QWidget*> stepWidgetsList;
  38. QList<ctkWorkflowStep*> finishStepsToHaveButtonsInStepArea;
  39. // orientation for layout of the step area and button area (for use
  40. // with QBoxLayouy only)
  41. QBoxLayout::Direction stepAreaDirection;
  42. int hasPopulateStepWidgetsListCommand;
  43. int hasShowUserInterfaceCommand;
  44. // names for the buttons
  45. QString backButtonText;
  46. QString nextButtonText;
  47. // QList<QString> finishButtonTexts;
  48. ctkWorkflowButtonBoxWidget* buttonBoxWidget;
  49. bool hasButtonBoxWidget;
  50. ctkWorkflowWidgetStep::ButtonBoxHints ButtonBoxHints;
  51. };
  52. //-----------------------------------------------------------------------------
  53. // ctkWorkflowWidgetStepPrivate methods
  54. //-----------------------------------------------------------------------------
  55. ctkWorkflowWidgetStepPrivate::ctkWorkflowWidgetStepPrivate()
  56. {
  57. this->stepArea = 0;
  58. this->buttonBoxWidget = 0;
  59. this->hasButtonBoxWidget = false;
  60. stepAreaDirection = QBoxLayout::TopToBottom;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // ctkWorkflowWidgetStep methods
  64. //-----------------------------------------------------------------------------
  65. ctkWorkflowWidgetStep::ctkWorkflowWidgetStep(ctkWorkflow* newWorkflow, const QString& newId) : Superclass(newWorkflow, newId)
  66. {
  67. CTK_INIT_PRIVATE(ctkWorkflowWidgetStep);
  68. CTK_D(ctkWorkflowWidgetStep);
  69. d->hasPopulateStepWidgetsListCommand = 0;
  70. d->hasShowUserInterfaceCommand = 0;
  71. QObject::connect(this, SIGNAL(populateStepWidgetsListComplete()), this, SLOT(evaluatePopulateStepWidgetsListResults()));
  72. }
  73. //-----------------------------------------------------------------------------
  74. CTK_GET_CXX(ctkWorkflowWidgetStep, QBoxLayout::Direction, stepAreaDirection, stepAreaDirection);
  75. CTK_GET_CXX(ctkWorkflowWidgetStep, int, hasPopulateStepWidgetsListCommand, hasPopulateStepWidgetsListCommand);
  76. CTK_SET_CXX(ctkWorkflowWidgetStep, int, setHasPopulateStepWidgetsListCommand, hasPopulateStepWidgetsListCommand);
  77. CTK_GET_CXX(ctkWorkflowWidgetStep, int, hasShowUserInterfaceCommand, hasShowUserInterfaceCommand);
  78. CTK_SET_CXX(ctkWorkflowWidgetStep, int, setHasShowUserInterfaceCommand, hasShowUserInterfaceCommand);
  79. CTK_GET_CXX(ctkWorkflowWidgetStep, QString, backButtonText, backButtonText);
  80. CTK_SET_CXX(ctkWorkflowWidgetStep, const QString&, setBackButtonText, backButtonText);
  81. CTK_GET_CXX(ctkWorkflowWidgetStep, QString, nextButtonText, nextButtonText);
  82. CTK_SET_CXX(ctkWorkflowWidgetStep, const QString&, setNextButtonText, nextButtonText);
  83. // CTK_GET_CXX(ctkWorkflowWidgetStep, QList<QString>, finishButtonTexts, finishButtonTexts);
  84. // CTK_SET_CXX(ctkWorkflowWidgetStep, QList<QString>, setFinishButtonTexts, finishButtonTexts);
  85. CTK_GET_CXX(ctkWorkflowWidgetStep, bool, hasButtonBoxWidget, hasButtonBoxWidget);
  86. CTK_SET_CXX(ctkWorkflowWidgetStep, bool, setHasButtonBoxWidget, hasButtonBoxWidget);
  87. //-----------------------------------------------------------------------------
  88. CTK_GET_CXX(ctkWorkflowWidgetStep, ctkWorkflowWidgetStep::ButtonBoxHints,
  89. buttonBoxHints, ButtonBoxHints);
  90. CTK_SET_CXX(ctkWorkflowWidgetStep, ctkWorkflowWidgetStep::ButtonBoxHints,
  91. setButtonBoxHints, ButtonBoxHints);
  92. //-----------------------------------------------------------------------------
  93. // void ctkWorkflowWidgetStep::setFinishButtonText(const QString& name)
  94. // {
  95. // QList<QString> names;
  96. // names << name;
  97. // this->setFinishButtonTexts(names);
  98. // }
  99. //-----------------------------------------------------------------------------
  100. QWidget* ctkWorkflowWidgetStep::stepArea()
  101. {
  102. CTK_D(ctkWorkflowWidgetStep);
  103. if (!d->stepArea)
  104. {
  105. d->stepArea = new QWidget();
  106. d->stepArea->hide();
  107. }
  108. return d->stepArea;
  109. }
  110. //-----------------------------------------------------------------------------
  111. ctkWorkflowButtonBoxWidget* ctkWorkflowWidgetStep::buttonBoxWidget()
  112. {
  113. CTK_D(ctkWorkflowWidgetStep);
  114. if (!d->buttonBoxWidget)
  115. {
  116. if (!this->workflow())
  117. {
  118. logger.error("buttonBoxWidget - Cannot create buttonBoxWidget without a workflow");
  119. return 0;
  120. }
  121. d->buttonBoxWidget = new ctkWorkflowButtonBoxWidget(this->workflow());
  122. d->stepArea->layout()->addWidget(d->buttonBoxWidget);
  123. }
  124. return d->buttonBoxWidget;
  125. }
  126. // --------------------------------------------------------------------------
  127. void ctkWorkflowWidgetStep::changeWidgetDirection(QWidget* widget, const QBoxLayout::Direction& direction)
  128. {
  129. // easy to change layout direction if we don't already have a layout
  130. if (!widget->layout())
  131. {
  132. widget->setLayout(new QBoxLayout(direction));
  133. }
  134. // do nothing if the page's layout cannot be cast to a QBoxLayout
  135. else if (QBoxLayout* widgetLayout = qobject_cast<QBoxLayout*>(widget->layout()))
  136. {
  137. widgetLayout->setDirection(direction);
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. void ctkWorkflowWidgetStep::setStepAreaDirection(const QBoxLayout::Direction& direction)
  142. {
  143. this->changeWidgetDirection(this->stepArea(), direction);
  144. CTK_D(ctkWorkflowWidgetStep);
  145. d->stepAreaDirection = direction;
  146. }
  147. //-----------------------------------------------------------------------------
  148. // reimplement in derived classes
  149. void ctkWorkflowWidgetStep::showUserInterface()
  150. {
  151. CTK_D(ctkWorkflowWidgetStep);
  152. // use the user's showUserInterfaceCommand if given
  153. if (d->hasShowUserInterfaceCommand)
  154. {
  155. emit invokeShowUserInterfaceCommand();
  156. return;
  157. }
  158. // otherwise we provide an implementation here
  159. logger.debug(QString("showUserInterface - showing %1").arg(this->name()));
  160. QWidget* stepArea = this->stepArea();
  161. // create the default step area layout, if there isn't one set
  162. if (!stepArea->layout())
  163. {
  164. d->stepArea->setLayout(new QBoxLayout(QBoxLayout::TopToBottom));
  165. }
  166. // add the step-specific widgets - only runs the first time this
  167. // method is called
  168. if (d->stepWidgetsList.isEmpty())
  169. {
  170. this->populateStepWidgetsList(d->stepWidgetsList);
  171. // actually enabling/showing the stepArea is handled by evaluatePopulateStepWidgetsListResults
  172. }
  173. else
  174. {
  175. this->addButtons();
  176. }
  177. }
  178. //-----------------------------------------------------------------------------
  179. void ctkWorkflowWidgetStep::addButtons()
  180. {
  181. CTK_D(ctkWorkflowWidgetStep);
  182. if (d->hasButtonBoxWidget)
  183. {
  184. ctkWorkflowButtonBoxWidget* buttonBoxWidget = this->buttonBoxWidget();
  185. buttonBoxWidget->updateButtons();
  186. }
  187. emit showUserInterfaceComplete();
  188. }
  189. //-----------------------------------------------------------------------------
  190. // reimplement in derived classes
  191. void ctkWorkflowWidgetStep::populateStepWidgetsList(QList<QWidget*>& stepWidgetsList)
  192. {
  193. CTK_D(ctkWorkflowWidgetStep);
  194. if (d->hasPopulateStepWidgetsListCommand)
  195. {
  196. emit invokePopulateStepWidgetsListCommand(stepWidgetsList);
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. void ctkWorkflowWidgetStep::evaluatePopulateStepWidgetsListResults()
  201. {
  202. // iterate through the list of widgets, add them to the layout
  203. CTK_D(ctkWorkflowWidgetStep);
  204. if (!d->stepArea)
  205. {
  206. logger.error("evaluatePopulateStepWidgetsListResults - stepArea is Null");
  207. return;
  208. }
  209. foreach(QWidget* widget, d->stepWidgetsList)
  210. {
  211. if (widget)
  212. {
  213. logger.debug("evaluatePopulateStepWidgetsListResults - Adding widget to layout");
  214. d->stepArea->layout()->addWidget(widget);
  215. }
  216. }
  217. this->addButtons();
  218. }