Procházet zdrojové kódy

ENH: ctkCheckBoxPixmaps now supports empty parents. Add test for ctkCheckBoxPixmaps.

Julien Finet před 15 roky
rodič
revize
c2864f3b3a

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

@@ -3,6 +3,7 @@ SET(KIT ${PROJECT_NAME})
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkAddRemoveComboBoxTest1.cpp
   ctkButtonGroupTest1.cpp
+  ctkCheckBoxPixmapsTest1.cpp
   ctkCheckableHeaderViewTest1.cpp
   ctkCollapsibleButtonTest1.cpp
   ctkCollapsibleGroupBoxTest1.cpp
@@ -43,6 +44,7 @@ ENDMACRO( SIMPLE_TEST  )
 
 SIMPLE_TEST( ctkAddRemoveComboBoxTest1 )
 SIMPLE_TEST( ctkButtonGroupTest1 )
+SIMPLE_TEST( ctkCheckBoxPixmapsTest1 )
 SIMPLE_TEST( ctkCheckableHeaderViewTest1 )
 SIMPLE_TEST( ctkCollapsibleButtonTest1 )
 SIMPLE_TEST( ctkCollapsibleGroupBoxTest1 )

+ 90 - 0
Libs/Widgets/Testing/Cpp/ctkCheckBoxPixmapsTest1.cpp

@@ -0,0 +1,90 @@
+/*=========================================================================
+
+  Library:   CTK
+ 
+  Copyright (c) 2010  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.commontk.org/LICENSE
+
+  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>
+
+// CTK includes
+#include "ctkCheckBoxPixmaps.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkCheckBoxPixmapsTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkCheckBoxPixmaps checkBoxPixmaps;
+  // make sure all the pixmaps are valid
+  if (checkBoxPixmaps.pixmap(Qt::Checked, true).isNull())
+    {
+    std::cerr << "Failed to create active Qt::Checked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (checkBoxPixmaps.pixmap(Qt::PartiallyChecked, true).isNull())
+    {
+    std::cerr << "Failed to create active Qt::PartiallyChecked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (checkBoxPixmaps.pixmap(Qt::Unchecked, true).isNull())
+    {
+    std::cerr << "Failed to create active Qt::Unchecked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (checkBoxPixmaps.pixmap(Qt::Checked, false).isNull())
+    {
+    std::cerr << "Failed to create unactive Qt::Checked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (checkBoxPixmaps.pixmap(Qt::PartiallyChecked, false).isNull())
+    {
+    std::cerr << "Failed to create unactive Qt::PartiallyChecked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (checkBoxPixmaps.pixmap(Qt::Unchecked, false).isNull())
+    {
+    std::cerr << "Failed to create unactive Qt::Unchecked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  // check the int version of the ctkCheckBoxPixmaps::pixmap()
+  if (checkBoxPixmaps.pixmap(0, false).isNull())
+    {
+    std::cerr << "Failed to create unactive Qt::Checked pixmap" << std::endl;
+    return EXIT_FAILURE;
+    }
+  // Same pixmap ?
+  const QPixmap& p1 = checkBoxPixmaps.pixmap(Qt::PartiallyChecked, false);
+  const QPixmap& p2 = checkBoxPixmaps.pixmap(Qt::PartiallyChecked, false);
+  if ( &p1 != &p2)
+    {
+    std::cerr << "The returned pixmap should be the same" << std::endl;
+    return EXIT_FAILURE;
+    }
+  // Same pixmap ?
+  if (&checkBoxPixmaps.pixmap(Qt::Unchecked, false) == 
+      &checkBoxPixmaps.pixmap(Qt::Unchecked, true))
+    {
+    std::cerr << "The returned pixmaps should not be the same" << std::endl;
+    return EXIT_FAILURE;
+    }
+  return EXIT_SUCCESS;
+}

+ 43 - 17
Libs/Widgets/ctkCheckBoxPixmaps.cpp

@@ -50,21 +50,42 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ========================================================================*/
 
 // Qt includes
-#include <QWidget>
+#include <QApplication>
 #include <QPainter>
 #include <QPixmap>
 #include <QStyle>
 #include <QStyleOptionButton>
+#include <QWidget>
 
 // CTK includes
 #include "ctkCheckBoxPixmaps.h"
 
+class ctkCheckBoxPixmapsPrivate: public ctkPrivate<ctkCheckBoxPixmaps>
+{
+public:
+  
+  enum PixmapStateIndex
+    {
+    Checked                 = 0,
+    PartiallyChecked        = 1,
+    UnChecked               = 2,
+    
+    // All active states in lower half
+    Checked_Active          = 3,
+    PartiallyChecked_Active = 4,
+    UnChecked_Active        = 5,
+   
+    PixmapCount             = 6
+    };
+  QPixmap Pixmaps[7];
+};
+
 //-----------------------------------------------------------------------------
 ctkCheckBoxPixmaps::ctkCheckBoxPixmaps(QWidget* parentWidget)
   : Superclass(parentWidget)
 {
-  Q_ASSERT(parentWidget != 0);
-
+  CTK_INIT_PRIVATE(ctkCheckBoxPixmaps);
+  CTK_D(ctkCheckBoxPixmaps);
   // Initialize the pixmaps. The following style array should
   // correspond to the PixmapStateIndex enum.
   const QStyle::State PixmapStyle[] =
@@ -78,37 +99,42 @@ ctkCheckBoxPixmaps::ctkCheckBoxPixmaps(QWidget* parentWidget)
     };
 
   QStyleOptionButton option;
-  QRect r = parentWidget->style()->subElementRect(
+  QStyle* style = parentWidget ? parentWidget->style() : qApp->style();
+  QRect r = style->subElementRect(
       QStyle::SE_CheckBoxIndicator, &option, parentWidget);
   option.rect = QRect(QPoint(0,0), r.size());
-  for(int i = 0; i < ctkCheckBoxPixmaps::PixmapCount; i++)
+  for(int i = 0; i < ctkCheckBoxPixmapsPrivate::PixmapCount; i++)
     {
-    this->Pixmaps[i] = QPixmap(r.size());
-    this->Pixmaps[i].fill(QColor(0, 0, 0, 0));
-    QPainter painter(&this->Pixmaps[i]);
+    d->Pixmaps[i] = QPixmap(r.size());
+    d->Pixmaps[i].fill(QColor(0, 0, 0, 0));
+    QPainter painter(&d->Pixmaps[i]);
     option.state = PixmapStyle[i];
-    parentWidget->style()->drawPrimitive(
+    style->drawPrimitive(
         QStyle::PE_IndicatorCheckBox, &option, &painter, parentWidget);
     }
 }
 
 //-----------------------------------------------------------------------------
-QPixmap ctkCheckBoxPixmaps::pixmap(Qt::CheckState state, bool active) const
+const QPixmap& ctkCheckBoxPixmaps::pixmap(Qt::CheckState state, bool active) const
 {
+  CTK_D(const ctkCheckBoxPixmaps);
   int offset = active ? 3 : 0;
   switch (state)
     {
-  case Qt::Checked:
-    return this->Pixmaps[offset + Checked];
+    case Qt::Checked:
+      return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::Checked];
+
+    case Qt::Unchecked:
+      return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::UnChecked];
 
-  case Qt::Unchecked:
-    return this->Pixmaps[offset + UnChecked];
+    case Qt::PartiallyChecked:
+      return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::PartiallyChecked];
 
-  case Qt::PartiallyChecked:
-    return this->Pixmaps[offset + PartiallyChecked];
+    default:
+      return d->Pixmaps[ctkCheckBoxPixmapsPrivate::PixmapCount];
     }
 
-  return QPixmap();
+  return d->Pixmaps[ctkCheckBoxPixmapsPrivate::PixmapCount];
 }
 
 

+ 20 - 25
Libs/Widgets/ctkCheckBoxPixmaps.h

@@ -56,9 +56,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <QPixmap>
 
 /// CTK includes
+#include "ctkPimpl.h"
 #include "CTKWidgetsExport.h"
 
+class QPixmap;
 class QWidget;
+class ctkCheckBoxPixmapsPrivate;
 
 ///
 /// ctkCheckBoxPixmaps is a helper class that can used to create pixmaps for
@@ -69,36 +72,28 @@ class CTK_WIDGETS_EXPORT ctkCheckBoxPixmaps : public QObject
   typedef QObject Superclass;
 
 public:
-  /// parent cannot be NULL.
-  ctkCheckBoxPixmaps(QWidget* parent);
+  ///
+  /// The widget is used to retrieve the style of the checkboxes
+  /// If the widget is 0 (not recommended) use the QApplication style.
+  ctkCheckBoxPixmaps(QWidget* parent = 0);
 
+  ///
   /// Returns a pixmap for the given state .
-  QPixmap pixmap(Qt::CheckState state, bool active) const;
-  QPixmap pixmap(int state, bool active) const
-    {
-    return this->pixmap(static_cast<Qt::CheckState>(state), active);
-    }
-
+  /// The pixmaps have been cached so the cost of the function is minimum.
+  const QPixmap& pixmap(Qt::CheckState state, bool active) const;
+  ///
+  /// Utility function that can take an int for the state.
+  /// Best to be avoided.
+  inline const QPixmap& pixmap(int state, bool active) const;
 
 private:
   Q_DISABLE_COPY(ctkCheckBoxPixmaps)
-
-  enum PixmapStateIndex
-    {
-    Checked                 = 0,
-    PartiallyChecked        = 1,
-    UnChecked               = 2,
-    
-    // All active states in lower half
-    Checked_Active          = 3,
-    PartiallyChecked_Active = 4,
-    UnChecked_Active        = 5,
-   
-    PixmapCount             = 6
-    };
-  QPixmap Pixmaps[6];
+  CTK_DECLARE_PRIVATE(ctkCheckBoxPixmaps);
 };
 
-#endif
-
+const QPixmap& ctkCheckBoxPixmaps::pixmap(int state, bool active) const
+{
+  return this->pixmap(static_cast<Qt::CheckState>(state), active);
+}
 
+#endif