ctkLayoutManager.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 __ctkLayoutManager_h
  15. #define __ctkLayoutManager_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 ctkLayoutManagerPrivate;
  24. class ctkLayoutViewFactory;
  25. /// \ingroup Widgets
  26. /// ctkLayoutManager is a layout manager that populates a widget (viewport)
  27. /// with widgets described into an XML document.
  28. /// To be used, ctkLayoutManager class must be derived and a subset of virtual
  29. /// methods must be reimplemented to support custom views.
  30. /// See below an example of layout XML document:
  31. /// \code
  32. /// <layout type=\"tab\">
  33. /// <item>
  34. /// <layout type=\"horizontal\">
  35. /// <item><view/></item>
  36. /// <item>
  37. /// <layout type=\"vertical\">
  38. /// <item><view/></item>
  39. /// <item><view/></item>
  40. /// <item>
  41. /// <layout type=\"grid\">
  42. /// <item row=\"0\" column=\"1\"><view/></item>
  43. /// <item row=\"1\" column=\"0\"><view/></item>
  44. /// </layout>
  45. /// </item>
  46. /// </layout>
  47. /// </item>
  48. /// <item><view/></item>
  49. /// </layout>
  50. /// </item>
  51. /// <item><view name=\"tab2\"/></item>
  52. /// <item><view name=\"tab3\"/></item>
  53. /// </layout>
  54. /// \endcode
  55. /// The layout elements describe widget containers that embed one or mulitple
  56. /// items.
  57. /// The item elements describe widgets or layouts that are children of
  58. /// layouts.
  59. /// The view elements can be any type of QWidget. viewFromXML() must be
  60. /// reimplemented to return the type(s) of QWidget(s) to use wherever the view
  61. /// element is listed in the layout. The XML element can contain any XML
  62. /// attribute to be parsed by viewFromXML() method.
  63. /// \sa ctkSimpleLayoutManager, ctkLayoutViewFactory
  64. class CTK_WIDGETS_EXPORT ctkLayoutManager: public QObject
  65. {
  66. Q_OBJECT
  67. /// Spacing between the widgets in all the layouts.
  68. /// \sa spacing(), setSpacing()
  69. Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
  70. public:
  71. /// Constructor
  72. ctkLayoutManager(QObject* parent = 0);
  73. explicit ctkLayoutManager(QWidget* viewport, QObject* parent);
  74. /// Destructor
  75. virtual ~ctkLayoutManager();
  76. void setViewport(QWidget* widget);
  77. Q_INVOKABLE QWidget* viewport()const;
  78. /// Return the spacing property value.
  79. /// \sa spacing
  80. int spacing()const;
  81. /// Set the spacing property value.
  82. /// \sa spacing
  83. void setSpacing(int spacing);
  84. void refresh();
  85. public Q_SLOTS:
  86. Q_SIGNALS:
  87. void layoutChanged();
  88. protected:
  89. QScopedPointer<ctkLayoutManagerPrivate> d_ptr;
  90. ctkLayoutManager(ctkLayoutManagerPrivate* ptr, QWidget* viewport, QObject* parent);
  91. virtual void onViewportChanged();
  92. void clearLayout();
  93. virtual void setupLayout();
  94. virtual void setLayout(const QDomDocument& newLayout);
  95. const QDomDocument layout()const;
  96. /// Create the QLayoutItem for an XML element (e.g. "layout", "view"...)
  97. /// and its nested elements.
  98. /// \sa processLayoutElement()
  99. virtual QLayoutItem* processElement(QDomElement element);
  100. /// Create the QLayoutItem for a "layout" XML element and its nested elements.
  101. /// \sa processElement(), layoutFromXML(), processItemElement(), addChildItemToLayout()
  102. virtual QLayoutItem* processLayoutElement(QDomElement layoutElement);
  103. /// Create the QLayoutItem for a "layout" XML element.
  104. /// \sa processLayoutElement()
  105. virtual QLayoutItem* layoutFromXML(QDomElement layoutElement);
  106. /// Create the QLayoutItem(s) of the "item" XML element.
  107. /// \sa processItemElement()
  108. void processItemElement(QDomElement layoutElement, QLayoutItem* layoutItem);
  109. /// Insert a child item into a layout.
  110. /// \sa processLayoutElement()
  111. virtual void addChildItemToLayout(QDomElement itemElement, QLayoutItem* childItem, QLayoutItem* layoutItem);
  112. /// Utility method that creates, setups and wraps into a QWidgetItem the widget
  113. /// of a view XML element.
  114. /// \sa widgetsItemsFromXML(), viewFromXML()
  115. QWidgetItem* widgetItemFromXML(QDomElement layoutElement);
  116. /// Method is called each time a view is made visible into a layout.
  117. /// This method can be reimplemented. Sets the widget visibility to true
  118. /// by default.
  119. /// \sa viewsFromXML()
  120. virtual void setupView(QDomElement layoutElement, QWidget* view);
  121. /// Create, setup and wrap into QWidgetItems the widgets of a view XML
  122. /// element.
  123. QList<QLayoutItem*> widgetItemsFromXML(QDomElement layoutElement);
  124. /// Virtual method that returns a widget from a "view" layout element.
  125. /// You are ensured that the tagName of the element is "view".
  126. /// The XML element can contain an arbitrary number of XML attributes.
  127. /// Create the widget if needed or reuse it from a previous call.
  128. /// \sa viewsFromXML(), setupView()
  129. virtual QWidget* viewFromXML(QDomElement layoutElement) = 0;
  130. /// Virtual method that returns a list of widgets from a "view" layout
  131. /// element.
  132. /// If the parent "item" element has a "multiple=true" XML attribute,
  133. /// the "view" layout element can describe many widgets instead of just one
  134. /// widget.
  135. /// The returned widgets will automatically be layout into their parent
  136. /// layout (e.g. boxlayout).
  137. /// This method can be reimplemented. Returns viewFromXML() by default.
  138. /// \sa viewFromXML(),
  139. virtual QList<QWidget*> viewsFromXML(QDomElement layoutElement);
  140. private:
  141. Q_DECLARE_PRIVATE(ctkLayoutManager);
  142. Q_DISABLE_COPY(ctkLayoutManager);
  143. };
  144. #endif