ctkFlowLayout.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
  30. Q_PROPERTY(int horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing)
  31. Q_PROPERTY(int verticalSpacing READ verticalSpacing WRITE setVerticalSpacing)
  32. Q_PROPERTY(bool alignItems READ alignItems WRITE setAlignItems)
  33. Q_PROPERTY(Qt::Orientations preferredExpandingDirections READ preferredExpandingDirections WRITE setPreferredExpandingDirections)
  34. public:
  35. typedef QLayout Superclass;
  36. explicit ctkFlowLayout(Qt::Orientation orientation, QWidget* parent = 0);
  37. explicit ctkFlowLayout(QWidget* parent);
  38. explicit ctkFlowLayout();
  39. virtual ~ctkFlowLayout();
  40. /// If orientation is Qt::Horizontal, items are layed out from left to right
  41. /// then top to bottom. If orientation is Qt::Vertical, items are layed out
  42. /// from top to bottom then left to right.
  43. void setOrientation(Qt::Orientation orientation);
  44. Qt::Orientation orientation()const;
  45. /// Indicates how the size hint of the layout should behave. The preferred
  46. /// expanding direction can be different than the orientation of the layout.
  47. /// It can be a combination of Qt::Horizontal and Qt::Vertical, in that case
  48. /// the layout will try to expand in a square shape (evenly distribute the
  49. /// number of rows and columns).
  50. void setPreferredExpandingDirections(Qt::Orientations directions);
  51. Qt::Orientations preferredExpandingDirections()const;
  52. /// Horizontal space between items, if the spacing is <0, a default spacing
  53. /// set on the parent/style will be used.
  54. int horizontalSpacing() const;
  55. void setHorizontalSpacing(int);
  56. /// Vertical space between items, if the spacing is <0, a default spacing
  57. /// set on the parent/style will be used.
  58. int verticalSpacing() const;
  59. void setVerticalSpacing(int);
  60. /// Force the items to be horizontally aligned based on the largest item
  61. /// to display.
  62. /// True by default.
  63. bool alignItems()const;
  64. void setAlignItems(bool);
  65. /// Replace the existing BoxLayout of the widget with a
  66. /// flow layout.
  67. /// When using the Designer, it is not possible to use a flow layout.
  68. /// Instead, you can use a H/VBoxLayout and replace it programatically
  69. /// in the setupUi() function with a flow layout. replaceLayout() makes
  70. /// the operation easier.
  71. /// \todo replaceLayout should take an existing layout instead of a widget,
  72. /// indeed, a layout can have another layout as a parent, not only a widget.
  73. static ctkFlowLayout* replaceLayout(QWidget* widget);
  74. /// Reimplemented for internal reasons
  75. virtual void addItem(QLayoutItem *item);
  76. virtual Qt::Orientations expandingDirections() const;
  77. virtual bool hasHeightForWidth() const;
  78. virtual int heightForWidth(int) const;
  79. virtual int count() const;
  80. virtual QLayoutItem *itemAt(int index) const;
  81. virtual QSize minimumSize() const;
  82. virtual void setGeometry(const QRect &rect);
  83. virtual QSize sizeHint() const;
  84. virtual QLayoutItem *takeAt(int index);
  85. protected:
  86. QScopedPointer<ctkFlowLayoutPrivate> d_ptr;
  87. private:
  88. Q_DECLARE_PRIVATE(ctkFlowLayout);
  89. Q_DISABLE_COPY(ctkFlowLayout);
  90. };
  91. #endif