ctkLayoutFactory.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <QDebug>
  16. #include <QList>
  17. #include <QWidget>
  18. // CTK includes
  19. #include "ctkLayoutFactory.h"
  20. #include "ctkLayoutManager_p.h"
  21. #include "ctkLayoutViewFactory.h"
  22. //-----------------------------------------------------------------------------
  23. class ctkLayoutFactoryPrivate: public ctkLayoutManagerPrivate
  24. {
  25. public:
  26. ctkLayoutFactoryPrivate(ctkLayoutManager& object);
  27. /// List of factories to generate views from XML element.
  28. QList<ctkLayoutViewFactory*> ViewFactories;
  29. /// Save the factory that was used to generate the view
  30. QHash<QWidget*, ctkLayoutViewFactory*> ViewFactory;
  31. };
  32. //-----------------------------------------------------------------------------
  33. ctkLayoutFactoryPrivate::ctkLayoutFactoryPrivate(ctkLayoutManager& object)
  34. : ctkLayoutManagerPrivate(object)
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. // ctkLayoutFactory
  39. //-----------------------------------------------------------------------------
  40. ctkLayoutFactory::ctkLayoutFactory(QObject* parentObject)
  41. : ctkLayoutManager(new ctkLayoutFactoryPrivate(*this), 0, parentObject)
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. ctkLayoutFactory::ctkLayoutFactory(QWidget* viewport, QObject* parentObject)
  46. : ctkLayoutManager(new ctkLayoutFactoryPrivate(*this), viewport, parentObject)
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. ctkLayoutFactory::~ctkLayoutFactory()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. void ctkLayoutFactory::registerViewFactory(ctkLayoutViewFactory* factory)
  55. {
  56. Q_D(ctkLayoutFactory);
  57. if (!factory)
  58. {
  59. return;
  60. }
  61. if (factory->parent() == 0)
  62. {
  63. factory->setParent(this);
  64. }
  65. d->ViewFactories.push_front(factory);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ctkLayoutFactory::unregisterViewFactory(ctkLayoutViewFactory* factory)
  69. {
  70. Q_D(ctkLayoutFactory);
  71. bool removed = d->ViewFactories.removeOne(factory);
  72. if (removed && factory->parent() == this)
  73. {
  74. factory->deleteLater();
  75. }
  76. }
  77. //-----------------------------------------------------------------------------
  78. QList<ctkLayoutViewFactory*> ctkLayoutFactory::registeredViewFactories()const
  79. {
  80. Q_D(const ctkLayoutFactory);
  81. return d->ViewFactories;
  82. }
  83. //-----------------------------------------------------------------------------
  84. void ctkLayoutFactory::setupLayout()
  85. {
  86. Q_D(ctkLayoutFactory);
  87. foreach(ctkLayoutViewFactory* factory, d->ViewFactories)
  88. {
  89. factory->beginSetupLayout();
  90. }
  91. this->ctkLayoutManager::setupLayout();
  92. foreach(ctkLayoutViewFactory* factory, d->ViewFactories)
  93. {
  94. factory->endSetupLayout();
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. QWidget* ctkLayoutFactory::viewFromXML(QDomElement viewElement)
  99. {
  100. Q_D(ctkLayoutFactory);
  101. QWidget* res = 0;
  102. QList<ctkLayoutViewFactory*> factories = this->viewFactories(viewElement);
  103. foreach(ctkLayoutViewFactory* factory, factories)
  104. {
  105. res = factory->viewFromXML(viewElement);
  106. if (res)
  107. {
  108. d->ViewFactory[res] = factory;
  109. break;
  110. }
  111. }
  112. return res;
  113. }
  114. //-----------------------------------------------------------------------------
  115. QList<QWidget*> ctkLayoutFactory::viewsFromXML(QDomElement viewElement)
  116. {
  117. QWidgetList res;
  118. QList<ctkLayoutViewFactory*> factories = this->viewFactories(viewElement);
  119. foreach(ctkLayoutViewFactory* factory, factories)
  120. {
  121. res = factory->viewsFromXML(viewElement);
  122. if (!res.isEmpty())
  123. {
  124. break;
  125. }
  126. }
  127. if (res.isEmpty())
  128. {
  129. res = this->ctkLayoutManager::viewsFromXML(viewElement);
  130. }
  131. return res;
  132. }
  133. //-----------------------------------------------------------------------------
  134. void ctkLayoutFactory::setupView(QDomElement viewElement, QWidget* view)
  135. {
  136. Q_D(ctkLayoutFactory);
  137. ctkLayoutViewFactory* factory = d->ViewFactory[view];
  138. if (factory)
  139. {
  140. factory->setupView(viewElement, view);
  141. d->Views.insert(view);
  142. }
  143. else
  144. {
  145. this->ctkLayoutManager::setupView(viewElement, view);
  146. }
  147. }
  148. //-----------------------------------------------------------------------------
  149. QList<ctkLayoutViewFactory*> ctkLayoutFactory::viewFactories(QDomElement viewElement)const
  150. {
  151. Q_D(const ctkLayoutFactory);
  152. QList<ctkLayoutViewFactory*> res;
  153. foreach(ctkLayoutViewFactory* factory, d->ViewFactories)
  154. {
  155. if (factory->isElementSupported(viewElement))
  156. {
  157. res << factory;
  158. }
  159. }
  160. return res;
  161. }