ctkMenuButton.cpp 6.7 KB

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