ctkMenuButton.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QDesktopWidget>
  18. #include <QLayout>
  19. #include <QMouseEvent>
  20. #include <QMenu>
  21. #include <QPainter>
  22. #include <QPointer>
  23. #include <QPushButton>
  24. #include <QStyle>
  25. #include <QStyleOptionButton>
  26. #include <QStylePainter>
  27. #include <QToolBar>
  28. // CTK includes
  29. #include "ctkMenuButton.h"
  30. //-----------------------------------------------------------------------------
  31. class ctkMenuButtonPrivate
  32. {
  33. Q_DECLARE_PUBLIC(ctkMenuButton);
  34. protected:
  35. ctkMenuButton* const q_ptr;
  36. public:
  37. ctkMenuButtonPrivate(ctkMenuButton& object);
  38. QRect indicatorRect() const;
  39. bool ShowMenu;
  40. };
  41. //-----------------------------------------------------------------------------
  42. ctkMenuButtonPrivate::ctkMenuButtonPrivate(ctkMenuButton& object)
  43. :q_ptr(&object)
  44. {
  45. this->ShowMenu = false;
  46. }
  47. //-----------------------------------------------------------------------------
  48. QRect ctkMenuButtonPrivate::indicatorRect()const
  49. {
  50. Q_Q(const ctkMenuButton);
  51. QStyleOptionButton option;
  52. q->initStyleOption(&option);
  53. QRect downArrowRect = q->style()->visualRect(option.direction, option.rect, option.rect);
  54. downArrowRect.setRect(downArrowRect.right() - 13, downArrowRect.top(),
  55. 14, downArrowRect.height());
  56. downArrowRect = q->style()->visualRect(option.direction, option.rect, downArrowRect);
  57. return downArrowRect;
  58. }
  59. //-----------------------------------------------------------------------------
  60. ctkMenuButton::ctkMenuButton(QWidget* _parent)
  61. :QPushButton(_parent)
  62. , d_ptr(new ctkMenuButtonPrivate(*this))
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. ctkMenuButton::ctkMenuButton(const QString& title, QWidget* _parent)
  67. :QPushButton(title, _parent)
  68. , d_ptr(new ctkMenuButtonPrivate(*this))
  69. {
  70. }
  71. //-----------------------------------------------------------------------------
  72. ctkMenuButton::~ctkMenuButton()
  73. {
  74. }
  75. //-----------------------------------------------------------------------------
  76. QSize ctkMenuButton::minimumSizeHint()const
  77. {
  78. QSize min = QPushButton::minimumSizeHint();
  79. return QSize(min.width() + 13, min.height());
  80. }
  81. //-----------------------------------------------------------------------------
  82. QSize ctkMenuButton::sizeHint()const
  83. {
  84. QSize buttonSizeHint = QPushButton::sizeHint();
  85. return QSize(buttonSizeHint.width() + 13, buttonSizeHint.height());
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkMenuButton::paintEvent(QPaintEvent * _event)
  89. {
  90. Q_UNUSED(_event);
  91. Q_D(ctkMenuButton);
  92. QStylePainter painter(this);
  93. QStyleOptionButton option;
  94. initStyleOption(&option);
  95. bool drawIndicatorBackground =
  96. option.state & QStyle::State_Sunken ||
  97. option.state & QStyle::State_On;
  98. // Draw button
  99. option.features &= ~QStyleOptionButton::HasMenu;
  100. if (this->menu() && (this->menu()->isVisible() || d->ShowMenu))
  101. {
  102. option.state &= ~QStyle::State_Sunken;
  103. option.state |= QStyle::State_Raised;
  104. }
  105. painter.drawControl(QStyle::CE_PushButtonBevel, option);
  106. // is PE_PanelButtonCommand better ?
  107. //painter.drawPrimitive(QStyle::PE_PanelButtonCommand, option);
  108. QRect downArrowRect = d->indicatorRect();
  109. if (drawIndicatorBackground)
  110. {
  111. // if the button is down, draw the part under the indicator up
  112. QPixmap cache = QPixmap(option.rect.size());
  113. cache.fill(Qt::transparent);
  114. QPainter cachePainter(&cache);
  115. option.state &= ~QStyle::State_Sunken;
  116. option.state |= QStyle::State_Raised;
  117. option.state &= ~QStyle::State_On;
  118. option.state |= QStyle::State_Off;
  119. //option.state &= ~QStyle::State_HasFocus;
  120. option.state &= ~QStyle::State_MouseOver;
  121. this->style()->drawControl(QStyle::CE_PushButtonBevel, &option, &cachePainter, this);
  122. painter.drawItemPixmap(downArrowRect, Qt::AlignLeft | Qt::AlignTop, cache.copy(downArrowRect));
  123. }
  124. // Separator
  125. // Freely inspired by the painting of CC_ComboBox in qcleanlooksstyle.cpp
  126. QColor buttonColor = this->palette().button().color();
  127. QColor darkColor;
  128. darkColor.setHsv(buttonColor.hue(),
  129. qMin(255, static_cast<int>(buttonColor.saturation()*1.9)),
  130. qMin(255, static_cast<int>(buttonColor.value()*0.7)));
  131. painter.setPen(buttonColor.darker(130));
  132. int borderSize = 2;
  133. painter.drawLine(QPoint(downArrowRect.left() - 1, downArrowRect.top() + borderSize),
  134. QPoint(downArrowRect.left() - 1, downArrowRect.bottom() - borderSize));
  135. painter.setPen(this->palette().light().color());
  136. painter.drawLine(QPoint(downArrowRect.left(), downArrowRect.top() + borderSize),
  137. QPoint(downArrowRect.left(), downArrowRect.bottom() - borderSize));
  138. // Draw arrow
  139. QStyleOption indicatorOpt;
  140. indicatorOpt.init(this);
  141. indicatorOpt.rect = downArrowRect.adjusted(borderSize, borderSize, -borderSize, -borderSize);
  142. painter.drawPrimitive(QStyle::PE_IndicatorArrowDown, indicatorOpt);
  143. // Draw Icon & Text
  144. option.rect.setRight( downArrowRect.left());
  145. painter.drawControl(QStyle::CE_PushButtonLabel, option);
  146. }
  147. //-----------------------------------------------------------------------------
  148. bool ctkMenuButton::hitButton(const QPoint & _pos)const
  149. {
  150. Q_D(const ctkMenuButton);
  151. return !d->indicatorRect().contains(_pos)
  152. && this->QPushButton::hitButton(_pos);
  153. }
  154. //-----------------------------------------------------------------------------
  155. void ctkMenuButton::initStyleOption(QStyleOptionButton* option)const
  156. {
  157. this->QPushButton::initStyleOption(option);
  158. }
  159. //-----------------------------------------------------------------------------
  160. void ctkMenuButton::mousePressEvent(QMouseEvent *e)
  161. {
  162. Q_D(ctkMenuButton);
  163. // we don't want to open the menu if the mouse is clicked anywhere on
  164. // the button, only if it's clicked on the indecator
  165. this->disconnect(this,SIGNAL(pressed()), this, SLOT(_q_popupPressed()));
  166. this->QPushButton::mousePressEvent(e);
  167. if (e->isAccepted())
  168. {
  169. return;
  170. }
  171. if (d->indicatorRect().contains(e->pos()))
  172. {
  173. d->ShowMenu = true;
  174. this->showMenu();
  175. d->ShowMenu = false;
  176. e->accept();
  177. }
  178. }