Ver código fonte

Merge pull request #53 from benjaminlong/ctkCheckBox-FixSizeIcon

Add the function setIndicatorIconSize to ctkCheckBox.
It allows the icon of the checkbox is to be larger than the default checkbox indicator would be.
Julien Finet 13 anos atrás
pai
commit
7afa5521d7

+ 18 - 4
Libs/Widgets/Testing/Cpp/ctkCheckBoxTest1.cpp

@@ -20,6 +20,7 @@
 
 // Qt includes
 #include <QApplication>
+#include <QDebug>
 #include <QStyle>
 #include <QTimer>
 #include <QVBoxLayout>
@@ -48,10 +49,23 @@ int ctkCheckBoxTest1(int argc, char * argv [] )
   icon.addPixmap(QApplication::style()->standardPixmap(QStyle::SP_DesktopIcon),
                  QIcon::Normal,
                  QIcon::Off);
-  checkBoxWithoutIcon->setCheckIcon(icon);
-  checkBoxWithIcon->setCheckIcon(icon);
-  checkBoxWithIcon->setCheckIcon(QIcon());
-  checkBoxWithIconAndText->setCheckIcon(icon);
+  checkBoxWithoutIcon->setIndicatorIcon(icon);
+//  checkBoxWithoutIcon->setIndicatorIconSize(QSize(15,15));
+  checkBoxWithIcon->setIndicatorIcon(icon);
+  if (checkBoxWithIcon->indicatorIcon().isNull())
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with "
+        << "the function indicatorIcon()" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  icon.addPixmap(QApplication::style()->standardPixmap(QStyle::SP_FileLinkIcon),
+                 QIcon::Active,
+                 QIcon::On);
+
+  checkBoxWithIcon->setIndicatorIcon(QIcon());
+  checkBoxWithIconAndText->setIndicatorIcon(icon);
+  checkBoxWithIconAndText->setIndicatorIconSize(QSize(25,25));
   checkBoxWithIconAndText->setText("Test1");
   checkBoxWithIconAndText->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
 

+ 97 - 41
Libs/Widgets/ctkCheckBox.cpp

@@ -18,7 +18,6 @@
 
 =========================================================================*/
 // QT includes
-#include <QApplication>
 #include <QProxyStyle>
 #include <QStyleOption>
 
@@ -31,44 +30,83 @@
 class ctkCheckBoxStyle : public QProxyStyle
 {
   public:
-  ctkCheckBoxStyle(QStyle *parentStyle)
-    : QProxyStyle(parentStyle)
-    {}
-
-  void setCheckIcon(const QIcon& newIcon)
-  {
-    this->checkBoxIcon = newIcon;
-  }
-  QIcon checkIcon() const
-  {
-    return this->checkBoxIcon;
-  }
-
-  virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
-  {
-    if (pe == QStyle::PE_IndicatorCheckBox)
+  ctkCheckBoxStyle(QStyle *parentStyle);
+
+  virtual void drawPrimitive(QStyle::PrimitiveElement pe,
+                             const QStyleOption * opt,
+                             QPainter * p,
+                             const QWidget * widget = 0) const;
+
+  virtual int pixelMetric(QStyle::PixelMetric metric,
+                          const QStyleOption *option,
+                          const QWidget *widget = 0) const;
+
+  QIcon   indicatorIcon;
+  QSize   indicatorSize;
+};
+
+// ----------------------------------------------------------------------------
+//  Methods ctkCheckBoxStyle
+
+// ----------------------------------------------------------------------------
+ctkCheckBoxStyle::ctkCheckBoxStyle(QStyle *parentStyle)
+  : QProxyStyle(parentStyle)
+{
+}
+
+// ----------------------------------------------------------------------------
+void ctkCheckBoxStyle::drawPrimitive(QStyle::PrimitiveElement pe,
+                                     const QStyleOption * opt,
+                                     QPainter * p, const QWidget * widget) const
+{
+  if (pe == QStyle::PE_IndicatorCheckBox)
+    {
+    const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
+    if (checkBox && !indicatorIcon.isNull())
       {
-      const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
-      if (checkBox && !checkBoxIcon.isNull())
-        {
-        QIcon::Mode mode =
-            (checkBox->testAttribute(Qt::WA_Hover) && checkBox->underMouse()) ?
-              QIcon::Active : QIcon::Normal;
-        mode = checkBox->isDown() ? QIcon::Selected : mode;
-        mode = checkBox->isEnabled() ? mode : QIcon::Disabled;
-        this->drawItemPixmap(p, opt->rect, Qt::AlignHCenter,
-                             checkBoxIcon.pixmap(
-                               opt->rect.width(), opt->rect.height(),
-                               mode,
-                               checkBox->isChecked() ? QIcon::Off : QIcon::On));
-        return;
-        }
+      QIcon::Mode mode =
+          (opt->state & QStyle::State_MouseOver) == QStyle::State_MouseOver
+            ? QIcon::Active : QIcon::Normal;
+      mode = (opt->state & QStyle::State_Sunken) == QStyle::State_Sunken
+               ? QIcon::Selected : mode;
+      mode = (opt->state & QStyle::State_Enabled) == QStyle::State_Enabled
+               ? mode : QIcon::Disabled;
+      QIcon::State state =
+          (opt->state & QStyle::State_On) == QStyle::State_On
+            ?  QIcon::Off : QIcon::On;
+      state = (opt->state & QStyle::State_Sunken) == QStyle::State_Sunken
+              ? QIcon::Off : state ;
+
+      this->drawItemPixmap(p, opt->rect, Qt::AlignHCenter,
+                           this->indicatorIcon.pixmap(
+                             opt->rect.width(), opt->rect.height(),
+                             mode,
+                             state));
+      return;
       }
-    this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
-  }
-protected:
-  QIcon   checkBoxIcon;
-};
+    }
+  this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
+}
+
+// ----------------------------------------------------------------------------
+int ctkCheckBoxStyle::pixelMetric(QStyle::PixelMetric metric,
+                                  const QStyleOption *option,
+                                  const QWidget *widget) const
+{
+  const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
+  if (checkBox && !indicatorIcon.isNull())
+    {
+    if(metric == QStyle::PM_IndicatorHeight && !this->indicatorSize.isEmpty())
+      {
+      return this->indicatorIcon.actualSize(this->indicatorSize).height();
+      }
+    if(metric == QStyle::PM_IndicatorWidth && !this->indicatorSize.isEmpty())
+      {
+      return this->indicatorIcon.actualSize(this->indicatorSize).width();
+      }
+    }
+  return this->QProxyStyle::pixelMetric(metric, option, widget);
+}
 
 // ----------------------------------------------------------------------------
 class ctkCheckBoxPrivate
@@ -84,6 +122,9 @@ public:
 };
 
 // ----------------------------------------------------------------------------
+//  Methods ctkCheckBoxPrivate
+
+// ----------------------------------------------------------------------------
 ctkCheckBoxPrivate::ctkCheckBoxPrivate(ctkCheckBox &object)
   : q_ptr(&object)
 {
@@ -117,16 +158,31 @@ ctkCheckBox::~ctkCheckBox()
 }
 
 // ----------------------------------------------------------------------------
-void ctkCheckBox::setCheckIcon(const QIcon& newIcon)
+void ctkCheckBox::setIndicatorIcon(const QIcon& newIcon)
+{
+  Q_D(ctkCheckBox);
+  d->iconStyle->indicatorIcon = newIcon;
+  this->update();
+}
+
+// ----------------------------------------------------------------------------
+QIcon ctkCheckBox::indicatorIcon() const
+{
+  Q_D(const ctkCheckBox);
+  return d->iconStyle->indicatorIcon;
+}
+
+// ----------------------------------------------------------------------------
+void ctkCheckBox::setIndicatorIconSize(const QSize& newSize)
 {
   Q_D(ctkCheckBox);
-  d->iconStyle->setCheckIcon(newIcon);
+  d->iconStyle->indicatorSize = newSize;
   this->update();
 }
 
 // ----------------------------------------------------------------------------
-QIcon ctkCheckBox::checkIcon() const
+QSize ctkCheckBox::indicatorIconSize() const
 {
   Q_D(const ctkCheckBox);
-  return d->iconStyle->checkIcon();
+  return d->iconStyle->indicatorSize;
 }

+ 11 - 3
Libs/Widgets/ctkCheckBox.h

@@ -32,11 +32,13 @@ class ctkCheckBoxPrivate;
 /// ctkCheckBox is an advanced QCheckBox that gives more control
 /// over its look and feel.
 /// We can change the indicator check box by a new QIcon, with two mode On/Off.
+/// The indicator icon size can be controled. see setIndicatorIconSize()
 
 class CTK_WIDGETS_EXPORT ctkCheckBox : public QCheckBox
 {
   Q_OBJECT
-  Q_PROPERTY(QIcon checkIcon READ checkIcon WRITE setCheckIcon)
+  Q_PROPERTY(QIcon indicatorIcon READ indicatorIcon WRITE setIndicatorIcon)
+  Q_PROPERTY(QSize indicatorIconSize READ indicatorIconSize WRITE setIndicatorIconSize)
 
 public:
   typedef QCheckBox Superclass;
@@ -44,8 +46,14 @@ public:
   ctkCheckBox(QWidget *_parent);
   virtual ~ctkCheckBox();
 
-  void setCheckIcon(const QIcon& newIcon);
-  QIcon checkIcon() const;
+  void setIndicatorIcon(const QIcon& newIcon);
+  QIcon indicatorIcon() const;
+
+  /// Resize the indicator icon to Qsize.
+  /// If newSize is bigger than the indicator icon's maximum size,
+  /// The icon will get the icon's maximum size and not newSize.
+  void setIndicatorIconSize(const QSize& newSize);
+  QSize indicatorIconSize() const;
 
 protected:
   QScopedPointer<ctkCheckBoxPrivate> d_ptr;