ctkLayoutViewFactory.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef __ctkLayoutViewFactory_h
  15. #define __ctkLayoutViewFactory_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QDomDocument>
  19. class QLayoutItem;
  20. class QWidgetItem;
  21. // CTK includes
  22. #include "ctkWidgetsExport.h"
  23. class ctkLayoutViewFactoryPrivate;
  24. /// \ingroup Widgets
  25. /// ctkLayoutViewFactory is an abstract class that creates and setups widgets for a
  26. /// specific view XML element from ctkLayoutManager.
  27. /// See ctkTemplateLayoutViewFactory for a factory that can instantiate
  28. /// any Qt widget.
  29. /// This class is meant to be derived with at least the method createViewFromXML()
  30. /// being overwritten.
  31. /// \sa ctkLayoutManager, ctkTemplateLayoutViewFactory, createViewFromXML()
  32. class CTK_WIDGETS_EXPORT ctkLayoutViewFactory: public QObject
  33. {
  34. Q_OBJECT
  35. /// This property controls whether the views are cached and reused
  36. /// from a previous layout. True by default.
  37. /// \sa useCachedViews(), setUseCachedViews()
  38. Q_PROPERTY(bool useCachedViews READ useCachedViews WRITE setUseCachedViews);
  39. public:
  40. /// Constructor
  41. ctkLayoutViewFactory(QObject* parent = 0);
  42. /// Destructor
  43. virtual ~ctkLayoutViewFactory();
  44. /// Returns the list of element names supported by the factory (e.g. "view",
  45. /// "myview"...). Returns ["view"] by default.
  46. /// Can be reimplemented to support other element names.
  47. /// \warning It is not possible to support the "layout" and "item" XML
  48. /// elements.
  49. /// \sa isSupportedElement()
  50. virtual QStringList supportedElementNames()const;
  51. /// Returns true if the layout element can be created and setup by the
  52. /// factory. By default, returns true if the layout element name is
  53. /// contained in supportedElementNames().
  54. /// \sa supportedElementNames()
  55. virtual bool isElementSupported(QDomElement layoutElement)const;
  56. /// Return the useCachedViews property value.
  57. /// \sa useCachedViews
  58. bool useCachedViews()const;
  59. /// Set the useCachedViews property value.
  60. /// \sa useCachedViews
  61. void setUseCachedViews(bool cache);
  62. /// Called by the layout factory before a layout is setup.
  63. /// \sa endSetupLayout()
  64. virtual void beginSetupLayout();
  65. /// Called by the layout factory after a layout is setup.
  66. /// \sa beginSetupLayout()
  67. virtual void endSetupLayout();
  68. /// Method is called each time a view is made visible into a layout.
  69. /// This method can be reimplemented. Sets the widget visibility to true
  70. /// and register the view by default.
  71. /// \sa viewsFromXML()
  72. virtual void setupView(QDomElement layoutElement, QWidget* view);
  73. /// Virtual method that returns a widget from a "view" layout element.
  74. /// You are ensured that the tagName of the element is one of the element
  75. /// from the supportedElementNames() list.
  76. /// The XML element can contain an arbitrary number of XML attributes.
  77. /// Returns previously registered or cached view if any, otherwise create
  78. /// a new view using createViewFromXML(). Must be reimplemented.
  79. /// \sa viewsFromXML(), setupView(), registeredViews(), useCachedView,
  80. /// createViewFromXML()
  81. virtual QWidget* viewFromXML(QDomElement layoutElement);
  82. /// Virtual method that returns a list of widgets from a "view" layout
  83. /// element.
  84. /// If the parent "item" element has a "multiple=true" XML attribute,
  85. /// the "view" layout element can describe many widgets instead of just one
  86. /// widget.
  87. /// The returned widgets will automatically be layout into their parent
  88. /// layout (e.g. boxlayout).
  89. /// Returns previously registered or cached views if any, otherwise create
  90. /// new views using createViewsFromXML().
  91. /// \sa viewFromXML(), registeredViews(),
  92. virtual QList<QWidget*> viewsFromXML(QDomElement layoutElement);
  93. /// Return all the widgets that have been registered.
  94. /// Used internally for testing.
  95. /// \sa registeredViews()
  96. QList<QWidget*> registeredViews()const;
  97. protected:
  98. QScopedPointer<ctkLayoutViewFactoryPrivate> d_ptr;
  99. /// Create a new view from a layoutElement. Returns 0 by default
  100. /// \sa viewFromXML
  101. virtual QWidget* createViewFromXML(QDomElement layoutElement);
  102. /// Create new views from a layoutElement. Returns createViewFromXML()
  103. /// by default.
  104. /// \sa viewsFromXML(), createViewFromXML()
  105. virtual QList<QWidget*> createViewsFromXML(QDomElement layoutElement);
  106. /// Return the list of widgets that have already been created by
  107. /// view(s)FromXML().
  108. /// \sa registerView(), unregisterView()
  109. QList<QWidget*> registeredViews(const QDomElement& layoutElement)const;
  110. /// Save the view to reuse it later on (in view(s)FromXML() ).
  111. /// \sa registerView(), registeredViews()
  112. virtual void registerView(QDomElement layoutElement, QWidget* view);
  113. /// Forget about the view. The view is not being deleted.
  114. /// \sa registerView(), registeredViews()
  115. virtual void unregisterView(QDomElement layoutElement, QWidget* view);
  116. /// Forget about the view. The view is not being deleted.
  117. /// \sa registerView(), unregisterView()
  118. virtual void unregisterView(QWidget* view);
  119. /// Return the layout element registered with the view or a null element
  120. /// if the view is not registered.
  121. /// \sa registerView(), unregisterView()
  122. QDomElement layoutElement(QWidget* view)const;
  123. private:
  124. Q_DECLARE_PRIVATE(ctkLayoutViewFactory);
  125. Q_DISABLE_COPY(ctkLayoutViewFactory);
  126. };
  127. /// \ingroup Widgets
  128. /// Instantiate a template QWidget anytime a view element is listed
  129. /// inside a layout element.
  130. /// \code
  131. /// QString tabLayout(
  132. /// "<layout type=\"tab\">"
  133. /// " <item><view name=\"tab1\"/></item>"
  134. /// " <item><view name=\"tab2\"/></item>"
  135. /// " <item><view name=\"tab3\"/></item>"
  136. /// "</layout>");
  137. /// QWidget tab;
  138. /// tab.setWindowTitle("Tab Layout");
  139. /// ctkTemplateLayoutViewFactory<QPushButton>* buttonFactory =
  140. /// new ctkTemplateLayoutViewFactory<QPushButton>(&tab);
  141. /// ctkLayoutFactory tabLayoutManager;
  142. /// tabLayoutManager.registerViewFactory(buttonFactory);
  143. /// tabToSimpleLayoutManager.setLayout(tabLayoutDoc);
  144. /// tabToSimpleLayoutManager.setViewport(&tab);
  145. /// tabToSimple.show();
  146. /// \endcode
  147. /// \sa ctkLayoutViewFactory
  148. template<class T>
  149. class ctkTemplateLayoutViewFactory: public ctkLayoutViewFactory
  150. {
  151. public:
  152. ctkTemplateLayoutViewFactory(QObject* parent = 0)
  153. : ctkLayoutViewFactory(parent)
  154. {
  155. this->setUseCachedViews(true);
  156. }
  157. virtual QWidget* createViewFromXML(QDomElement layoutElement){
  158. Q_UNUSED(layoutElement);
  159. return new T;
  160. }
  161. };
  162. #endif