Browse Source

Merge pull request #51 from benjaminlong/ctkCheckBox

Add ctkCheckBox, an advanced QCheckBox.
Julien Finet 13 years ago
parent
commit
99abbdf026

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -24,6 +24,8 @@ SET(KIT_SRCS
   ctkCheckableComboBox.h
   ctkCheckableHeaderView.cpp
   ctkCheckableHeaderView.h
+  ctkCheckBox.cpp
+  ctkCheckBox.h
   ctkCheckBoxPixmaps.cpp
   ctkCheckBoxPixmaps.h
   ctkCheckablePushButton.cpp
@@ -173,6 +175,7 @@ SET(KIT_MOC_SRCS
   ctkCheckableComboBox.h
   ctkCheckableHeaderView.h
   ctkCheckablePushButton.h
+  ctkCheckBox.h
   ctkCheckBoxPixmaps.h
   ctkComboBox.h
   ctkCompleter.h

+ 3 - 0
Libs/Widgets/Plugins/CMakeLists.txt

@@ -18,6 +18,8 @@ SET(PLUGIN_SRCS
   ctkAxesWidgetPlugin.h
   ctkCheckableComboBoxPlugin.cpp
   ctkCheckableComboBoxPlugin.h
+  ctkCheckBoxPlugin.cpp
+  ctkCheckBoxPlugin.h
   ctkCollapsibleButtonPlugin.cpp
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleGroupBoxPlugin.cpp
@@ -91,6 +93,7 @@ SET(PLUGIN_MOC_SRCS
   ctkActionsWidgetPlugin.h
   ctkAxesWidgetPlugin.h
   ctkCheckableComboBoxPlugin.h
+  ctkCheckBoxPlugin.h
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleGroupBoxPlugin.h
   ctkColorPickerButtonPlugin.h

+ 69 - 0
Libs/Widgets/Plugins/ctkCheckBoxPlugin.cpp

@@ -0,0 +1,69 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkCheckBoxPlugin.h"
+#include "ctkCheckBox.h"
+
+//-----------------------------------------------------------------------------
+ctkCheckBoxPlugin::ctkCheckBoxPlugin(QObject* pluginParent)
+  : QObject(pluginParent)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+QWidget *ctkCheckBoxPlugin::createWidget(QWidget* parentForWidget)
+{
+  ctkCheckBox* newWidget = new ctkCheckBox(parentForWidget);
+  return newWidget;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkCheckBoxPlugin::domXml() const
+{
+  return "<widget class=\"ctkCheckBox\" \
+          name=\"CheckBox\">\n"
+          "</widget>\n";
+}
+
+// --------------------------------------------------------------------------
+QIcon ctkCheckBoxPlugin::icon() const
+{
+  return QIcon();
+}
+
+//-----------------------------------------------------------------------------
+QString ctkCheckBoxPlugin::includeFile() const
+{
+  return "ctkCheckBox.h";
+}
+
+//-----------------------------------------------------------------------------
+bool ctkCheckBoxPlugin::isContainer() const
+{
+  return false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkCheckBoxPlugin::name() const
+{
+  return "ctkCheckBox";
+}

+ 45 - 0
Libs/Widgets/Plugins/ctkCheckBoxPlugin.h

@@ -0,0 +1,45 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkCheckBoxPlugin_h
+#define __ctkCheckBoxPlugin_h
+
+// CTK includes
+#include "ctkWidgetsAbstractPlugin.h"
+
+class CTK_WIDGETS_PLUGINS_EXPORT ctkCheckBoxPlugin :
+  public QObject,
+  public ctkWidgetsAbstractPlugin
+{
+  Q_OBJECT
+
+public:
+  ctkCheckBoxPlugin(QObject *_parent = 0);
+
+  QWidget *createWidget(QWidget *_parent);
+  QString  domXml() const;
+  QIcon    icon() const;
+  QString  includeFile() const;
+  bool     isContainer() const;
+  QString  name() const;
+
+};
+
+#endif

+ 2 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -6,6 +6,7 @@ SET(TEST_SOURCES
   ctkAxesWidgetTest1.cpp
   ctkButtonGroupTest1.cpp
   ctkCheckBoxPixmapsTest1.cpp
+  ctkCheckBoxTest1.cpp
   ctkCheckableComboBoxTest1.cpp
   ctkCheckableHeaderViewTest1.cpp
   ctkCheckableHeaderViewTest2.cpp
@@ -133,6 +134,7 @@ SIMPLE_TEST( ctkAddRemoveComboBoxTest1 )
 SIMPLE_TEST( ctkAxesWidgetTest1 )
 SIMPLE_TEST( ctkButtonGroupTest1 )
 SIMPLE_TEST( ctkCheckBoxPixmapsTest1 )
+SIMPLE_TEST( ctkCheckBoxTest1 )
 SIMPLE_TEST( ctkCheckableComboBoxTest1 )
 SIMPLE_TEST( ctkCheckableHeaderViewTest1 )
 SIMPLE_TEST( ctkCheckableHeaderViewTest2 )

+ 82 - 0
Libs/Widgets/Testing/Cpp/ctkCheckBoxTest1.cpp

@@ -0,0 +1,82 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QStyle>
+#include <QTimer>
+#include <QVBoxLayout>
+
+// CTK includes
+#include "ctkCheckBox.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkCheckBoxTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  QWidget topLevel;
+  ctkCheckBox* checkBoxWithoutIcon = new ctkCheckBox(0);
+  ctkCheckBox* checkBoxWithIcon = new ctkCheckBox(0);
+  checkBoxWithIcon->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
+  ctkCheckBox* checkBoxWithIconAndText = new ctkCheckBox(0);
+  QIcon icon;
+  icon.addPixmap(QApplication::style()->standardPixmap(QStyle::SP_DriveCDIcon),
+                 QIcon::Normal,
+                 QIcon::On);
+  icon.addPixmap(QApplication::style()->standardPixmap(QStyle::SP_DesktopIcon),
+                 QIcon::Normal,
+                 QIcon::Off);
+  checkBoxWithoutIcon->setCheckIcon(icon);
+  checkBoxWithIcon->setCheckIcon(icon);
+  checkBoxWithIcon->setCheckIcon(QIcon());
+  checkBoxWithIconAndText->setCheckIcon(icon);
+  checkBoxWithIconAndText->setText("Test1");
+  checkBoxWithIconAndText->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
+
+  ctkCheckBox* checkBoxWithoutIcon2 = new ctkCheckBox(0);
+  ctkCheckBox* checkBoxWithIcon2 = new ctkCheckBox(0);
+  checkBoxWithIcon2->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
+  ctkCheckBox* checkBoxWithIconAndText2 = new ctkCheckBox(0);
+  checkBoxWithIconAndText2->setText("Test1");
+  checkBoxWithIconAndText2->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
+
+  QVBoxLayout* layout = new QVBoxLayout;
+  layout->addWidget(checkBoxWithoutIcon);
+  layout->addWidget(checkBoxWithIcon);
+  layout->addWidget(checkBoxWithIconAndText);
+  layout->addWidget(checkBoxWithoutIcon2);
+  layout->addWidget(checkBoxWithIcon2);
+  layout->addWidget(checkBoxWithIconAndText2);
+  topLevel.setLayout(layout);
+  topLevel.show();
+
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}

+ 132 - 0
Libs/Widgets/ctkCheckBox.cpp

@@ -0,0 +1,132 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+// QT includes
+#include <QApplication>
+#include <QProxyStyle>
+#include <QStyleOption>
+
+// CTK includes
+#include "ctkCheckBox.h"
+
+#include <iostream>
+
+// ----------------------------------------------------------------------------
+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)
+      {
+      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;
+        }
+      }
+    this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
+  }
+protected:
+  QIcon   checkBoxIcon;
+};
+
+// ----------------------------------------------------------------------------
+class ctkCheckBoxPrivate
+{
+  Q_DECLARE_PUBLIC(ctkCheckBox);
+protected:
+  ctkCheckBox* const q_ptr;
+public:
+  ctkCheckBoxPrivate(ctkCheckBox& object);
+  void init();
+
+  ctkCheckBoxStyle* iconStyle;
+};
+
+// ----------------------------------------------------------------------------
+ctkCheckBoxPrivate::ctkCheckBoxPrivate(ctkCheckBox &object)
+  : q_ptr(&object)
+{
+  this->iconStyle = 0;
+}
+
+// ----------------------------------------------------------------------------
+void ctkCheckBoxPrivate::init()
+{
+  Q_Q(ctkCheckBox);
+  this->iconStyle = new ctkCheckBoxStyle(0);
+  this->iconStyle->setParent(q);
+  q->setStyle(this->iconStyle);
+}
+
+// ----------------------------------------------------------------------------
+//  Methods ctkCheckBox
+
+// ----------------------------------------------------------------------------
+ctkCheckBox::ctkCheckBox(QWidget *_parent)
+  :Superclass(_parent)
+  , d_ptr(new ctkCheckBoxPrivate(*this))
+{
+  Q_D(ctkCheckBox);
+  d->init();
+}
+
+// ----------------------------------------------------------------------------
+ctkCheckBox::~ctkCheckBox()
+{
+}
+
+// ----------------------------------------------------------------------------
+void ctkCheckBox::setCheckIcon(const QIcon& newIcon)
+{
+  Q_D(ctkCheckBox);
+  d->iconStyle->setCheckIcon(newIcon);
+  this->update();
+}
+
+// ----------------------------------------------------------------------------
+QIcon ctkCheckBox::checkIcon() const
+{
+  Q_D(const ctkCheckBox);
+  return d->iconStyle->checkIcon();
+}

+ 58 - 0
Libs/Widgets/ctkCheckBox.h

@@ -0,0 +1,58 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkCheckBox_h
+#define __ctkCheckBox_h
+
+// QT includes
+#include <QCheckBox>
+
+// CTK includes
+#include "ctkWidgetsExport.h"
+
+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.
+
+class CTK_WIDGETS_EXPORT ctkCheckBox : public QCheckBox
+{
+  Q_OBJECT
+  Q_PROPERTY(QIcon checkIcon READ checkIcon WRITE setCheckIcon)
+
+public:
+  typedef QCheckBox Superclass;
+
+  ctkCheckBox(QWidget *_parent);
+  virtual ~ctkCheckBox();
+
+  void setCheckIcon(const QIcon& newIcon);
+  QIcon checkIcon() const;
+
+protected:
+  QScopedPointer<ctkCheckBoxPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkCheckBox);
+  Q_DISABLE_COPY(ctkCheckBox);
+};
+
+#endif