ctkLayoutManager.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\" split=\"true\">
  35. /// <item splitSize=\"500\"><view/></item>
  36. /// <item splitSize=\"250\">
  37. /// <layout type=\"vertical\">
  38. /// <item><view verticalStretch=\"1\"/></item>
  39. /// <item><view verticalStretch=\"2\"/></item>
  40. /// <item>
  41. /// <layout type=\"grid\">
  42. /// <item row=\"0\" column=\"1\"><view verticalStretch=\"1\"/></item>
  43. /// <item row=\"1\" column=\"0\"><view verticalStretch=\"1\"/></item>
  44. /// </layout>
  45. /// </item>
  46. /// </layout>
  47. /// </item>
  48. /// <item splitSize=\"250\"><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 multiple
  56. /// items. Arrangement of items are specified by type attribute of the layout element;
  57. /// supported values: vertical, horizontal, grid, tab.
  58. /// The item elements describe widgets or layouts that are children of
  59. /// layouts.
  60. /// The view elements can be any type of QWidget. viewFromXML() must be
  61. /// reimplemented to return the type(s) of QWidget(s) to use wherever the view
  62. /// element is listed in the layout. The XML element can contain any XML
  63. /// attribute to be parsed by viewFromXML() method.
  64. ///
  65. /// For horizontal and vertical layouts, setting split attribute to "true" makes the
  66. /// views resizeable. Default size can be set using splitSize attribute of child items.
  67. ///
  68. /// Relative size of views can be adjusted by specifying stretch factors in
  69. /// horizontalStretch and verticalStretch attributes. The stretch factor must be an
  70. /// integer in the range of [0,255].
  71. ///
  72. /// \sa ctkSimpleLayoutManager, ctkLayoutViewFactory
  73. class CTK_WIDGETS_EXPORT ctkLayoutManager: public QObject
  74. {
  75. Q_OBJECT
  76. /// Spacing between the widgets in all the layouts.
  77. /// \sa spacing(), setSpacing()
  78. Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
  79. public:
  80. /// Constructor
  81. ctkLayoutManager(QObject* parent = 0);
  82. explicit ctkLayoutManager(QWidget* viewport, QObject* parent);
  83. /// Destructor
  84. virtual ~ctkLayoutManager();
  85. Q_INVOKABLE void setViewport(QWidget* widget);
  86. Q_INVOKABLE QWidget* viewport()const;
  87. /// Return the spacing property value.
  88. /// \sa spacing
  89. int spacing()const;
  90. /// Set the spacing property value.
  91. /// \sa spacing
  92. void setSpacing(int spacing);
  93. void refresh();
  94. public Q_SLOTS:
  95. Q_SIGNALS:
  96. void layoutChanged();
  97. protected:
  98. QScopedPointer<ctkLayoutManagerPrivate> d_ptr;
  99. ctkLayoutManager(ctkLayoutManagerPrivate* ptr, QWidget* viewport, QObject* parent);
  100. virtual void onViewportChanged();
  101. void clearLayout();
  102. virtual void setupLayout();
  103. virtual void setLayout(const QDomDocument& newLayout);
  104. const QDomDocument layout()const;
  105. /// Create the QLayoutItem for an XML element (e.g. "layout", "view"...)
  106. /// and its nested elements.
  107. /// \sa processLayoutElement()
  108. virtual QLayoutItem* processElement(QDomElement element);
  109. /// Create the QLayoutItem for a "layout" XML element and its nested elements.
  110. /// \sa processElement(), layoutFromXML(), processItemElement(), addChildItemToLayout()
  111. virtual QLayoutItem* processLayoutElement(QDomElement layoutElement);
  112. /// Create the QLayoutItem for a "layout" XML element.
  113. /// \sa processLayoutElement()
  114. virtual QLayoutItem* layoutFromXML(QDomElement layoutElement);
  115. /// Create the QLayoutItem(s) of the "item" XML element.
  116. /// \sa processItemElement()
  117. void processItemElement(QDomElement layoutElement, QLayoutItem* layoutItem);
  118. /// Insert a child item into a layout.
  119. /// \sa processLayoutElement()
  120. virtual void addChildItemToLayout(QDomElement itemElement, QLayoutItem* childItem, QLayoutItem* layoutItem);
  121. /// Utility method that creates, setups and wraps into a QWidgetItem the widget
  122. /// of a view XML element.
  123. /// \sa widgetsItemsFromXML(), viewFromXML()
  124. QWidgetItem* widgetItemFromXML(QDomElement layoutElement);
  125. /// Method is called each time a view is made visible into a layout.
  126. /// This method can be reimplemented. Sets the widget visibility to true
  127. /// by default.
  128. /// \sa viewsFromXML()
  129. virtual void setupView(QDomElement layoutElement, QWidget* view);
  130. /// Create, setup and wrap into QWidgetItems the widgets of a view XML
  131. /// element.
  132. QList<QLayoutItem*> widgetItemsFromXML(QDomElement layoutElement);
  133. /// Virtual method that returns a widget from a "view" layout element.
  134. /// You are ensured that the tagName of the element is "view".
  135. /// The XML element can contain an arbitrary number of XML attributes.
  136. /// Create the widget if needed or reuse it from a previous call.
  137. /// \sa viewsFromXML(), setupView()
  138. virtual QWidget* viewFromXML(QDomElement layoutElement) = 0;
  139. /// Virtual method that returns a list of widgets from a "view" layout
  140. /// element.
  141. /// If the parent "item" element has a "multiple=true" XML attribute,
  142. /// the "view" layout element can describe many widgets instead of just one
  143. /// widget.
  144. /// The returned widgets will automatically be layout into their parent
  145. /// layout (e.g. boxlayout).
  146. /// This method can be reimplemented. Returns viewFromXML() by default.
  147. /// \sa viewFromXML(),
  148. virtual QList<QWidget*> viewsFromXML(QDomElement layoutElement);
  149. private:
  150. Q_DECLARE_PRIVATE(ctkLayoutManager);
  151. Q_DISABLE_COPY(ctkLayoutManager);
  152. };
  153. #endif