ctkCollapsibleButton.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.commontk.org/LICENSE
  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 __ctkCollapsibleButton_h
  15. #define __ctkCollapsibleButton_h
  16. // Qt includes
  17. #include <QAbstractButton>
  18. #include <QFrame>
  19. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkCollapsibleButtonPrivate;
  23. class QStyleOptionButton;
  24. /// A collapsible button that shows/hides its children depending on its
  25. /// checked/collapsed property.
  26. /// Warning: <old behavior> As ctkCollapsibleButton forces the Visiblity of its children to
  27. /// true when it get expanded, any child Visibility property is lost. All the widgets
  28. /// will then be visible. To avoid this behavior, use an intermediate widget that
  29. /// contains all the children (they would become grandchildren and their Visibility property
  30. /// will remain relative to their parent, ctkCollapsibleButton's unique child widget.</old behavior>
  31. /// Note: The user QAbstractButton::icon is not visible (it's placeholder is used to display the
  32. /// collapsible state
  33. class CTK_WIDGETS_EXPORT ctkCollapsibleButton : public QAbstractButton
  34. {
  35. Q_OBJECT
  36. Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed DESIGNABLE isCheckable NOTIFY contentsCollapsed)
  37. Q_PROPERTY(int collapsedHeight READ collapsedHeight WRITE setCollapsedHeight)
  38. Q_PROPERTY(QFrame::Shape contentsFrameShape READ contentsFrameShape WRITE setContentsFrameShape)
  39. Q_PROPERTY(QFrame::Shadow contentsFrameShadow READ contentsFrameShadow WRITE setContentsFrameShadow)
  40. Q_PROPERTY(int contentsLineWidth READ contentsLineWidth WRITE setContentsLineWidth)
  41. Q_PROPERTY(int contentsMidLineWidth READ contentsMidLineWidth WRITE setContentsMidLineWidth)
  42. Q_PROPERTY(Qt::Alignment buttonTextAlignment READ buttonTextAlignment WRITE setButtonTextAlignment)
  43. Q_PROPERTY(Qt::Alignment indicatorAlignment READ indicatorAlignment WRITE setIndicatorAlignment)
  44. public:
  45. ctkCollapsibleButton(QWidget *parent = 0);
  46. ctkCollapsibleButton(const QString& text, QWidget *parent = 0);
  47. virtual ~ctkCollapsibleButton();
  48. ///
  49. /// Property that describes if the widget is collapsed or not.
  50. /// When collapsed, the children are invisible and the widget
  51. /// has a sized defined by CollapsedHeight
  52. void setCollapsed(bool);
  53. bool collapsed()const;
  54. ///
  55. /// Height used when the widget is collapsed
  56. void setCollapsedHeight(int);
  57. int collapsedHeight()const;
  58. ///
  59. /// Set the frame shape of the contents
  60. /// Hint: StyledPanel is probably the shape you are looking for
  61. QFrame::Shape contentsFrameShape() const;
  62. void setContentsFrameShape(QFrame::Shape);
  63. ///
  64. /// Set the frame shadow of the contents
  65. /// Hint: Raised is probably the shadow you are looking for
  66. QFrame::Shadow contentsFrameShadow() const;
  67. void setContentsFrameShadow(QFrame::Shadow);
  68. ///
  69. /// Set the line width of the frame around the contents
  70. int contentsLineWidth() const;
  71. void setContentsLineWidth(int);
  72. ///
  73. /// Set the mid line width of the frame around the contents
  74. int contentsMidLineWidth() const;
  75. void setContentsMidLineWidth(int);
  76. ///
  77. /// Set the alignment of the text on the button,
  78. /// Qt::Left|Qt::VCenter by default.
  79. void setButtonTextAlignment(Qt::Alignment textAlignment);
  80. Qt::Alignment buttonTextAlignment()const;
  81. ///
  82. /// Set the alignment of the indicator (arrow) on the button,
  83. /// Qt::Left|Qt::VCenter by default.
  84. void setIndicatorAlignment(Qt::Alignment indicatorAlignment);
  85. Qt::Alignment indicatorAlignment()const;
  86. ///
  87. /// Reimplemented for internal reasons
  88. virtual QSize minimumSizeHint()const;
  89. ///
  90. /// Reimplemented for internal reasons
  91. virtual QSize sizeHint()const;
  92. /// Reimplemented to update the size of the button in case of font/style
  93. /// change
  94. virtual bool event(QEvent* event);
  95. /// Reimplmented for internal reasons
  96. /// Catch when a child widget's visibility is externally changed
  97. virtual bool eventFilter(QObject* child, QEvent* e);
  98. /// Reimplemented for internal reasons
  99. /// Don't process Show/Hide events of children when it is
  100. /// ctkCollapsibleButton that generate them.
  101. virtual void setVisible(bool);
  102. signals:
  103. ///
  104. /// Signal emitted when the widget is collapsed or expanded.
  105. /// See signal toggled(bool) for the opposite.
  106. void contentsCollapsed(bool);
  107. protected slots:
  108. ///
  109. /// Perform the collapse.
  110. virtual void collapse(bool c);
  111. virtual void onToggled(bool clicked = false);
  112. protected:
  113. virtual void paintEvent(QPaintEvent*);
  114. //virtual void mousePressEvent(QMouseEvent* event);
  115. //virtual void mouseReleaseEvent(QMouseEvent* event);
  116. virtual void childEvent(QChildEvent* c);
  117. virtual bool hitButton(const QPoint & pos) const;
  118. /// Compute the size hint of the head button only. The sizehint depends on the text.
  119. virtual QSize buttonSizeHint() const;
  120. /// Initialize option with the values from this ctkCollapsibleButton.
  121. /// This method is useful for subclasses when they need a QStyleOptionButton,
  122. /// but don't want to fill in all the information themselves.
  123. virtual void initStyleOption(QStyleOptionButton* option)const;
  124. protected:
  125. QScopedPointer<ctkCollapsibleButtonPrivate> d_ptr;
  126. private:
  127. Q_DECLARE_PRIVATE(ctkCollapsibleButton);
  128. Q_DISABLE_COPY(ctkCollapsibleButton);
  129. };
  130. #endif