ctkButtonGroup.cpp 2.6 KB

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