Selaa lähdekoodia

Merge branch 'cim-CheckablePushButton_not_user_checkable_checkbox' into cim

Miklos Espak 13 vuotta sitten
vanhempi
commit
86b66ad86c

+ 24 - 0
Libs/Widgets/Testing/Cpp/ctkCheckablePushButtonTest1.cpp

@@ -45,6 +45,12 @@ int ctkCheckablePushButtonTest1(int argc, char * argv [] )
   ctkCheckablePushButton button5(QObject::tr("Button5"));
   ctkCheckablePushButton button6(QObject::tr("Button6"));
   ctkCheckablePushButton button7(QObject::tr("Checkable PushButton"));
+  ctkCheckablePushButton button8(QObject::tr("Checkbox Not User Checkable (On)"));
+  ctkCheckablePushButton button9(QObject::tr("CheckBox Not User Checkable (Off)"));
+  ctkCheckablePushButton button10(QObject::tr("User Checkable Bi-state"));
+  ctkCheckablePushButton button11(QObject::tr("User Checkable Tri-state"));
+  ctkCheckablePushButton button12(QObject::tr("Checkbox Not User Checkable\nButton Checkable"));
+  ctkCheckablePushButton button13(QObject::tr("Checkbox and Buttun User Checkable"));
 
   QVBoxLayout *layout= new QVBoxLayout;
   layout->addWidget(&button1);
@@ -54,6 +60,12 @@ int ctkCheckablePushButtonTest1(int argc, char * argv [] )
   layout->addWidget(&button5);
   layout->addWidget(&button6);
   layout->addWidget(&button7);
+  layout->addWidget(&button8);
+  layout->addWidget(&button9);
+  layout->addWidget(&button10);
+  layout->addWidget(&button11);
+  layout->addWidget(&button12);
+  layout->addWidget(&button13);
   topLevel.setLayout(layout);
 
   topLevel.show();
@@ -89,6 +101,18 @@ int ctkCheckablePushButtonTest1(int argc, char * argv [] )
   button7.setButtonTextAlignment(Qt::AlignCenter);
   button7.setIndicatorAlignment(Qt::AlignLeft);
   
+  button8.setCheckBoxFlags(Qt::ItemIsEnabled);
+  button8.setCheckState(Qt::Checked);
+  button9.setCheckBoxFlags(Qt::ItemIsEnabled);
+  button9.setCheckState(Qt::Unchecked);
+  button10.setCheckBoxFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
+  button11.setCheckBoxFlags(Qt::ItemIsEnabled |
+      Qt::ItemIsUserCheckable | Qt::ItemIsTristate);
+  button12.setCheckBoxFlags(Qt::ItemIsEnabled);
+  button12.setCheckable(true);
+  button13.setCheckBoxFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
+  button13.setCheckable(true);
+
   if (argc < 2 || QString(argv[1]) != "-I" )
     {
     QTimer::singleShot(200, &app, SLOT(quit()));

+ 80 - 6
Libs/Widgets/ctkCheckablePushButton.cpp

@@ -53,6 +53,8 @@ public:
   // Tuning of the button look&feel
   Qt::Alignment TextAlignment;
   Qt::Alignment IndicatorAlignment;
+  Qt::ItemFlags CheckBoxFlags;
+  Qt::CheckState CheckState;
 };
 
 //-----------------------------------------------------------------------------
@@ -61,6 +63,8 @@ ctkCheckablePushButtonPrivate::ctkCheckablePushButtonPrivate(ctkCheckablePushBut
 {
   this->TextAlignment = Qt::AlignLeft | Qt::AlignVCenter;
   this->IndicatorAlignment = Qt::AlignLeft | Qt::AlignVCenter;
+  this->CheckBoxFlags = Qt::NoItemFlags;
+  this->CheckState = Qt::Unchecked;
 }
 
 //-----------------------------------------------------------------------------
@@ -218,6 +222,36 @@ Qt::Alignment ctkCheckablePushButton::indicatorAlignment()const
 }
 
 //-----------------------------------------------------------------------------
+void ctkCheckablePushButton::setCheckState(Qt::CheckState checkState)
+{
+  Q_D(ctkCheckablePushButton);
+  d->CheckState = checkState;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+Qt::CheckState ctkCheckablePushButton::checkState()const
+{
+  Q_D(const ctkCheckablePushButton);
+  return d->CheckState;
+}
+
+//-----------------------------------------------------------------------------
+void ctkCheckablePushButton::setCheckBoxFlags(Qt::ItemFlags checkBoxFlags)
+{
+  Q_D(ctkCheckablePushButton);
+  d->CheckBoxFlags = checkBoxFlags;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+Qt::ItemFlags ctkCheckablePushButton::checkBoxFlags()const
+{
+  Q_D(const ctkCheckablePushButton);
+  return d->CheckBoxFlags;
+}
+
+//-----------------------------------------------------------------------------
 QSize ctkCheckablePushButton::minimumSizeHint()const
 {
   Q_D(const ctkCheckablePushButton);
@@ -267,13 +301,31 @@ void ctkCheckablePushButton::paintEvent(QPaintEvent * _event)
   // Draw Indicator
   QStyleOption indicatorOpt;
   indicatorOpt.init(this);
-  if (this->isCheckable())
+  if (d->CheckBoxFlags == Qt::NoItemFlags)
     {
-    indicatorOpt.state |= QStyle::State_On;
+    if (this->isCheckable())
+      {
+      indicatorOpt.state |= QStyle::State_On;
+      }
+    else
+      {
+      indicatorOpt.state |= QStyle::State_Off;
+      }
     }
-  else
+  else if (d->CheckBoxFlags & Qt::ItemIsEnabled)
     {
-    indicatorOpt.state |= QStyle::State_Off;
+    if (d->CheckState == Qt::Checked)
+      {
+      indicatorOpt.state |= QStyle::State_On;
+      }
+    else if (d->CheckState == Qt::PartiallyChecked)
+      {
+      indicatorOpt.state |= QStyle::State_NoChange;
+      }
+    else
+      {
+      indicatorOpt.state |= QStyle::State_Off;
+      }
     }
   indicatorOpt.rect = d->checkboxRect();
   this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &indicatorOpt, &p, 0);
@@ -350,8 +402,30 @@ void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
     }
   if (d->checkboxRect().contains(e->pos()))
     {
-    //check the checkbox
-    this->setCheckable(!this->isCheckable());
+    Qt::ItemFlags cbf = d->CheckBoxFlags;
+    if (cbf == Qt::NoItemFlags)
+      {
+      //check the checkbox
+      this->setCheckable(!this->isCheckable());
+      }
+    else if (d->CheckBoxFlags & Qt::ItemIsUserCheckable)
+      {
+      switch (d->CheckState)
+        {
+        case Qt::Unchecked:
+          d->CheckState =
+              d->CheckBoxFlags & Qt::ItemIsTristate
+              ? Qt::PartiallyChecked
+              : Qt::Checked;
+          break;
+        case Qt::PartiallyChecked:
+          d->CheckState = Qt::Checked;
+          break;
+        case Qt::Checked:
+          d->CheckState = Qt::Unchecked;
+          break;
+        }
+      }
     this->update();
     e->accept();
     }

+ 16 - 1
Libs/Widgets/ctkCheckablePushButton.h

@@ -33,12 +33,21 @@ class ctkCheckablePushButtonPrivate;
 
 /// Description
 /// ctkCheckablePushButton is a QPushButton with a checkbox that controls the
-/// checkable property of the button
+/// checkable property of the button.
+/// You can change this behavior by assigning flags to the checkbox by the
+/// setCheckBoxFlags function. The Qt::ItemIsEnabled flag causes that the
+/// checkbox can be used
+/// independently from the containing button. (It will not control the checkable
+/// property of the button.) The following flags are supported:
+/// Qt::ItemIsEnabled, Qt::ItemIsUserCheckable, Qt::ItemIsTriState.
+/// In default, no flags are set (Qt::NoItemFlags) that means the original behavior.
 class CTK_WIDGETS_EXPORT ctkCheckablePushButton : public QPushButton
 {
   Q_OBJECT
   Q_PROPERTY(Qt::Alignment buttonTextAlignment READ buttonTextAlignment WRITE setButtonTextAlignment)
   Q_PROPERTY(Qt::Alignment indicatorAlignment READ indicatorAlignment WRITE setIndicatorAlignment)
+  Q_PROPERTY(Qt::ItemFlags checkBoxFlags READ checkBoxFlags WRITE setCheckBoxFlags)
+  Q_PROPERTY(Qt::CheckState checkState READ checkState WRITE setCheckState)
 
 public:
   ctkCheckablePushButton(QWidget *parent = 0);
@@ -60,6 +69,12 @@ public:
   virtual QSize minimumSizeHint()const;
   virtual QSize sizeHint()const;
 
+  virtual Qt::ItemFlags checkBoxFlags()const;
+  virtual void setCheckBoxFlags(Qt::ItemFlags checkBoxFlags);
+
+  virtual Qt::CheckState checkState()const;
+  virtual void setCheckState(Qt::CheckState checkState);
+
 protected:
   /// Reimplemented for internal reasons
   virtual void paintEvent(QPaintEvent*);