ctkFlowLayout.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 __ctkFlowLayout_h
  15. #define __ctkFlowLayout_h
  16. // Qt includes
  17. #include <QLayout>
  18. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkFlowLayoutPrivate;
  21. /// \ingroup Widgets
  22. /// Acts like a QBoxLayout but if the space is horizontally/vertically limited,
  23. /// it displays items ona a new row/column based on the orientation.
  24. /// Please note that using a Qt::Vertical orientation without the property
  25. /// alignItems set to true might result to weird layout behavior.
  26. class CTK_WIDGETS_EXPORT ctkFlowLayout : public QLayout
  27. {
  28. Q_OBJECT
  29. /// If orientation is Qt::Horizontal, items are layed out from left to right
  30. /// then top to bottom if there is no more horizontal space.
  31. /// If orientation is Qt::Vertical, items are layed out from top to bottom
  32. /// then left to right if there is no more vertical space.
  33. /// Qt::Horizontal by default
  34. /// \sa preferredExpandingDirections
  35. Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
  36. /// Indicates how the size hint of the layout should behave. The preferred
  37. /// expanding direction can be different than the \a orientation of the
  38. /// layout.
  39. /// It can be a combination of Qt::Horizontal and Qt::Vertical, in that case
  40. /// the layout will try to expand in a square shape (evenly distribute the
  41. /// number of rows and columns).
  42. /// Qt::Horizontal | Qt::Vertical by default.
  43. /// \sa orientation
  44. Q_PROPERTY(Qt::Orientations preferredExpandingDirections READ preferredExpandingDirections WRITE setPreferredExpandingDirections)
  45. /// Force the items to be horizontally aligned based on the largest item
  46. /// to display.
  47. /// True by default.
  48. /// \sa orientation
  49. Q_PROPERTY(bool alignItems READ alignItems WRITE setAlignItems)
  50. /// Horizontal space between items, if the spacing is <0, a default spacing
  51. /// set on the parent/style is used.
  52. /// -1 by default.
  53. /// \sa verticalSpacing
  54. Q_PROPERTY(int horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing)
  55. /// Vertical space between items, if the spacing is <0, a default spacing
  56. /// set on the parent/style is used.
  57. /// -1 by default.
  58. /// \sa horizontalSpacing
  59. Q_PROPERTY(int verticalSpacing READ verticalSpacing WRITE setVerticalSpacing)
  60. public:
  61. typedef QLayout Superclass;
  62. explicit ctkFlowLayout(Qt::Orientation orientation, QWidget* parent = 0);
  63. explicit ctkFlowLayout(QWidget* parent);
  64. explicit ctkFlowLayout();
  65. virtual ~ctkFlowLayout();
  66. void setOrientation(Qt::Orientation orientation);
  67. Qt::Orientation orientation()const;
  68. void setPreferredExpandingDirections(Qt::Orientations directions);
  69. Qt::Orientations preferredExpandingDirections()const;
  70. int horizontalSpacing() const;
  71. void setHorizontalSpacing(int);
  72. int verticalSpacing() const;
  73. void setVerticalSpacing(int);
  74. bool alignItems()const;
  75. void setAlignItems(bool);
  76. /// Replace the existing BoxLayout of the widget with a
  77. /// flow layout.
  78. /// When using the Designer, it is not possible to use a flow layout.
  79. /// Instead, you can use a H/VBoxLayout and replace it programatically
  80. /// in the setupUi() function with a flow layout. replaceLayout() makes
  81. /// the operation easier.
  82. /// \todo replaceLayout should take an existing layout instead of a widget,
  83. /// indeed, a layout can have another layout as a parent, not only a widget.
  84. static ctkFlowLayout* replaceLayout(QWidget* widget);
  85. /// When the orientation is Qt::Vertical, heightForWidth doesn't work
  86. /// correctly with ctkFlowLayout. Ideally widthForHeight should be used
  87. /// instead.
  88. /// \sa orientation widthForHeight hasHeightForWidth
  89. virtual bool hasWidthForHeight() const;
  90. virtual int widthForHeight(int) const;
  91. /// Reimplemented for internal reasons
  92. virtual void addItem(QLayoutItem *item);
  93. virtual Qt::Orientations expandingDirections() const;
  94. virtual bool hasHeightForWidth() const;
  95. virtual int heightForWidth(int) const;
  96. virtual int count() const;
  97. virtual QLayoutItem *itemAt(int index) const;
  98. virtual QSize minimumSize() const;
  99. virtual void setGeometry(const QRect &rect);
  100. virtual QSize sizeHint() const;
  101. virtual QLayoutItem *takeAt(int index);
  102. protected:
  103. QScopedPointer<ctkFlowLayoutPrivate> d_ptr;
  104. private:
  105. Q_DECLARE_PRIVATE(ctkFlowLayout);
  106. Q_DISABLE_COPY(ctkFlowLayout);
  107. };
  108. #endif