ctkButtonGroup.cpp 2.6 KB

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