ctkButtonGroup.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. //Qt includes
  11. #include <QAbstractButton>
  12. #include <QDebug>
  13. #include <QWeakPointer>
  14. // CTK includes
  15. #include "ctkButtonGroup.h"
  16. //-----------------------------------------------------------------------------
  17. class ctkButtonGroupPrivate : public ctkPrivate<ctkButtonGroup>
  18. {
  19. public:
  20. CTK_DECLARE_PUBLIC(ctkButtonGroup);
  21. bool IsLastButtonPressedChecked;
  22. };
  23. //------------------------------------------------------------------------------
  24. ctkButtonGroup::ctkButtonGroup(QObject* _parent)
  25. :QButtonGroup(_parent)
  26. {
  27. CTK_INIT_PRIVATE(ctkButtonGroup);
  28. connect(this, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
  29. connect(this, SIGNAL(buttonPressed(QAbstractButton*)), this, SLOT(onButtonPressed(QAbstractButton*)));
  30. }
  31. //------------------------------------------------------------------------------
  32. void ctkButtonGroup::onButtonClicked(QAbstractButton *clickedButton)
  33. {
  34. CTK_D(ctkButtonGroup);
  35. if (!this->exclusive() || !d->IsLastButtonPressedChecked)
  36. {
  37. return;
  38. }
  39. this->removeButton(clickedButton);
  40. clickedButton->setChecked(false);
  41. this->addButton(clickedButton);
  42. }
  43. //------------------------------------------------------------------------------
  44. void ctkButtonGroup::onButtonPressed(QAbstractButton *pressedButton)
  45. {
  46. CTK_D(ctkButtonGroup);
  47. Q_ASSERT(pressedButton);
  48. d->IsLastButtonPressedChecked = pressedButton->isChecked();
  49. }