ctkButtonGroup.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <QAbstractButton>
  16. #include <QDebug>
  17. #include <QWeakPointer>
  18. // CTK includes
  19. #include "ctkButtonGroup.h"
  20. //-----------------------------------------------------------------------------
  21. class ctkButtonGroupPrivate
  22. {
  23. public:
  24. bool IsLastButtonPressedChecked;
  25. };
  26. //------------------------------------------------------------------------------
  27. ctkButtonGroup::ctkButtonGroup(QObject* _parent)
  28. :QButtonGroup(_parent)
  29. , d_ptr(new ctkButtonGroupPrivate)
  30. {
  31. Q_D(ctkButtonGroup);
  32. d->IsLastButtonPressedChecked = false;
  33. // we need to connect to button{Clicked,Pressed}(int) instead of
  34. // button{Clicked,Pressed}(QAbstractButton*) in order to be first to catch the
  35. // signals
  36. connect(this, SIGNAL(buttonClicked(int)), this, SLOT(onButtonClicked(int)));
  37. connect(this, SIGNAL(buttonPressed(int)), this, SLOT(onButtonPressed(int)));
  38. }
  39. //------------------------------------------------------------------------------
  40. ctkButtonGroup::~ctkButtonGroup()
  41. {
  42. }
  43. //------------------------------------------------------------------------------
  44. void ctkButtonGroup::onButtonClicked(int buttonId)
  45. {
  46. Q_D(ctkButtonGroup);
  47. QAbstractButton* clickedButton = this->button(buttonId);
  48. Q_ASSERT(clickedButton);
  49. if (!this->exclusive() || !d->IsLastButtonPressedChecked)
  50. {
  51. return;
  52. }
  53. // here the button is clicked and we click it again... so we want to
  54. // uncheck, a behavior not supported by QButtonGroup.
  55. // The only way to uncheck the button is to remove it from the group, and put it back
  56. this->removeButton(clickedButton);
  57. clickedButton->setChecked(false);
  58. this->addButton(clickedButton);
  59. d->IsLastButtonPressedChecked = false;
  60. }
  61. //------------------------------------------------------------------------------
  62. void ctkButtonGroup::onButtonPressed(int buttonId)
  63. {
  64. Q_D(ctkButtonGroup);
  65. QAbstractButton* pressedButton = this->button(buttonId);
  66. Q_ASSERT(pressedButton);
  67. d->IsLastButtonPressedChecked = pressedButton->isChecked();
  68. }