ctkMenuButton.cpp 6.8 KB

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