ctkExpandButton.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <QIcon>
  16. #include <QStyleOption>
  17. // CTK includes
  18. #include "ctkExpandButton.h"
  19. class ctkExpandButtonPrivate
  20. {
  21. Q_DECLARE_PUBLIC(ctkExpandButton);
  22. protected:
  23. ctkExpandButton* const q_ptr;
  24. public:
  25. ctkExpandButtonPrivate(ctkExpandButton& object);
  26. void init();
  27. bool mirrorOnExpand;
  28. QPixmap defaultPixmap;
  29. Qt::Orientation orientation;
  30. Qt::LayoutDirection direction;
  31. };
  32. //-----------------------------------------------------------------------------
  33. ctkExpandButtonPrivate::ctkExpandButtonPrivate(ctkExpandButton &object)
  34. : q_ptr(&object)
  35. {
  36. this->mirrorOnExpand = false;
  37. this->orientation = Qt::Horizontal;
  38. this->direction = Qt::LeftToRight;
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkExpandButtonPrivate::init()
  42. {
  43. Q_Q(ctkExpandButton);
  44. q->setAutoRaise(true);
  45. q->setOrientation(Qt::Horizontal);
  46. q->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
  47. q->setCheckable(true);
  48. }
  49. //-----------------------------------------------------------------------------
  50. // ctkExpandButton methods
  51. //-----------------------------------------------------------------------------
  52. ctkExpandButton::ctkExpandButton(QWidget *_parent)
  53. :Superclass(_parent)
  54. , d_ptr(new ctkExpandButtonPrivate(*this))
  55. {
  56. Q_D(ctkExpandButton);
  57. d->init();
  58. }
  59. //-----------------------------------------------------------------------------
  60. ctkExpandButton::~ctkExpandButton()
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. void ctkExpandButton::setMirrorOnExpand(bool newBehavior)
  65. {
  66. Q_D(ctkExpandButton);
  67. d->mirrorOnExpand = newBehavior;
  68. this->updateIcon(d->direction);
  69. }
  70. //-----------------------------------------------------------------------------
  71. bool ctkExpandButton::mirrorOnExpand() const
  72. {
  73. Q_D(const ctkExpandButton);
  74. return d->mirrorOnExpand;
  75. }
  76. //-----------------------------------------------------------------------------
  77. QSize ctkExpandButton::sizeHint() const
  78. {
  79. int ext = this->style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent);
  80. return QSize(ext, ext);
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ctkExpandButton::setOrientation(Qt::Orientation newOrientation)
  84. {
  85. Q_D(ctkExpandButton);
  86. QStyleOption opt;
  87. opt.init(this);
  88. if(newOrientation == Qt::Horizontal)
  89. {
  90. d->defaultPixmap = this->style()->standardPixmap(
  91. QStyle::SP_ToolBarHorizontalExtensionButton, &opt);
  92. d->orientation = Qt::Horizontal;
  93. }
  94. else
  95. {
  96. d->defaultPixmap = this->style()->standardPixmap(
  97. QStyle::SP_ToolBarVerticalExtensionButton, &opt);
  98. d->orientation = Qt::Vertical;
  99. }
  100. this->updateIcon(d->direction);
  101. }
  102. //-----------------------------------------------------------------------------
  103. Qt::Orientation ctkExpandButton::orientation() const
  104. {
  105. Q_D(const ctkExpandButton);
  106. return d->orientation;
  107. }
  108. //-----------------------------------------------------------------------------
  109. void ctkExpandButton::updateIcon(Qt::LayoutDirection newDirection)
  110. {
  111. Q_D(ctkExpandButton);
  112. // If the orientation is vertical, UpToBottom is LeftToRight and
  113. // BottomToUp is RightToLeft. Rotate 90' clockwise.
  114. if(newDirection == Qt::LeftToRight)
  115. {
  116. this->setIcon(QIcon(d->defaultPixmap));
  117. d->direction = Qt::LeftToRight;
  118. }
  119. else
  120. {
  121. QImage mirrorImage =
  122. d->defaultPixmap.toImage().mirrored(d->orientation == Qt::Horizontal,
  123. d->orientation == Qt::Vertical);
  124. this->setIcon(QIcon(QPixmap::fromImage(mirrorImage)));
  125. d->direction = Qt::RightToLeft;
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. void ctkExpandButton::nextCheckState()
  130. {
  131. Q_D(ctkExpandButton);
  132. if (d->mirrorOnExpand)
  133. {
  134. Qt::LayoutDirection newDirection =
  135. this->isChecked() ? Qt::LeftToRight : Qt::RightToLeft;
  136. this->updateIcon(newDirection);
  137. }
  138. return this->Superclass::nextCheckState();
  139. }