Browse Source

ENH: Rename ctkTitleComboBox into ctkComboBox

ctkTitleComboBox was a too limited. We replace it with ctkComboBox, a
QComboBox with a few more extra features. When no current item is selected
in the combo box, the property defaultText/defaultIcon are shown (instead
of a blank content with QComboBox). This defaultText/defaultIcon can be
forced to stay shown even if an item is selected.
Julien Finet 15 years ago
parent
commit
3fc95f0488

+ 4 - 4
Libs/Widgets/CMakeLists.txt

@@ -9,7 +9,7 @@ SET(KIT_export_directive "CTK_WIDGETS_EXPORT")
 # Additional directories to include
 # Additional directories to include
 SET(KIT_include_directories
 SET(KIT_include_directories
   )
   )
-  
+
 # Source files
 # Source files
 SET(KIT_SRCS
 SET(KIT_SRCS
   ctkAddRemoveComboBox.cpp
   ctkAddRemoveComboBox.cpp
@@ -20,6 +20,8 @@ SET(KIT_SRCS
   ctkCheckableHeaderView.h
   ctkCheckableHeaderView.h
   ctkCheckBoxPixmaps.cpp
   ctkCheckBoxPixmaps.cpp
   ctkCheckBoxPixmaps.h
   ctkCheckBoxPixmaps.h
+  ctkComboBox.cpp
+  ctkComboBox.h
   ctkCollapsibleButton.cpp
   ctkCollapsibleButton.cpp
   ctkCollapsibleButton.h
   ctkCollapsibleButton.h
   ctkCollapsibleGroupBox.cpp
   ctkCollapsibleGroupBox.cpp
@@ -54,8 +56,6 @@ SET(KIT_SRCS
   ctkSliderSpinBoxWidget.h
   ctkSliderSpinBoxWidget.h
   ctkTestApplication.cpp
   ctkTestApplication.cpp
   ctkTestApplication.h
   ctkTestApplication.h
-  ctkTitleComboBox.cpp
-  ctkTitleComboBox.h
   ctkTransferFunctionControlPointsItem.cpp
   ctkTransferFunctionControlPointsItem.cpp
   ctkTransferFunctionControlPointsItem.h
   ctkTransferFunctionControlPointsItem.h
   ctkTransferFunctionGradientItem.cpp
   ctkTransferFunctionGradientItem.cpp
@@ -78,6 +78,7 @@ SET(KIT_MOC_SRCS
   ctkButtonGroup.h
   ctkButtonGroup.h
   ctkCheckableHeaderView.h
   ctkCheckableHeaderView.h
   ctkCheckBoxPixmaps.h
   ctkCheckBoxPixmaps.h
+  ctkComboBox.h
   ctkCollapsibleButton.h
   ctkCollapsibleButton.h
   ctkCollapsibleGroupBox.h
   ctkCollapsibleGroupBox.h
   ctkColorPickerButton.h
   ctkColorPickerButton.h
@@ -95,7 +96,6 @@ SET(KIT_MOC_SRCS
   ctkSettings.h
   ctkSettings.h
   ctkSliderSpinBoxWidget.h
   ctkSliderSpinBoxWidget.h
   ctkTestApplication.h
   ctkTestApplication.h
-  ctkTitleComboBox.h
   ctkTransferFunctionControlPointsItem.h
   ctkTransferFunctionControlPointsItem.h
   ctkTransferFunctionGradientItem.h
   ctkTransferFunctionGradientItem.h
   ctkTransferFunctionBarsItem.h
   ctkTransferFunctionBarsItem.h

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

@@ -13,6 +13,8 @@ SET(PLUGIN_SRCS
   ctkWidgetsAbstractPlugin.cpp
   ctkWidgetsAbstractPlugin.cpp
   ctkWidgetsAbstractPlugin.h
   ctkWidgetsAbstractPlugin.h
 
 
+  ctkComboBoxPlugin.cpp
+  ctkComboBoxPlugin.h
   ctkCollapsibleButtonPlugin.cpp
   ctkCollapsibleButtonPlugin.cpp
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleGroupBoxPlugin.cpp
   ctkCollapsibleGroupBoxPlugin.cpp
@@ -41,8 +43,6 @@ SET(PLUGIN_SRCS
   ctkRangeSliderPlugin.h
   ctkRangeSliderPlugin.h
   ctkRangeWidgetPlugin.cpp
   ctkRangeWidgetPlugin.cpp
   ctkRangeWidgetPlugin.h
   ctkRangeWidgetPlugin.h
-  ctkTitleComboBoxPlugin.cpp
-  ctkTitleComboBoxPlugin.h
   ctkTransferFunctionWidgetPlugin.cpp
   ctkTransferFunctionWidgetPlugin.cpp
   ctkTransferFunctionWidgetPlugin.h
   ctkTransferFunctionWidgetPlugin.h
   ctkTreeComboBoxPlugin.cpp
   ctkTreeComboBoxPlugin.cpp
@@ -55,6 +55,7 @@ SET(PLUGIN_SRCS
 SET(PLUGIN_MOC_SRCS
 SET(PLUGIN_MOC_SRCS
   ctkWidgetsPlugins.h
   ctkWidgetsPlugins.h
 
 
+  ctkComboBoxPlugin.h
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleButtonPlugin.h
   ctkCollapsibleGroupBoxPlugin.h
   ctkCollapsibleGroupBoxPlugin.h
   ctkColorPickerButtonPlugin.h
   ctkColorPickerButtonPlugin.h
@@ -69,7 +70,6 @@ SET(PLUGIN_MOC_SRCS
   ctkMenuButtonPlugin.h
   ctkMenuButtonPlugin.h
   ctkRangeSliderPlugin.h
   ctkRangeSliderPlugin.h
   ctkRangeWidgetPlugin.h
   ctkRangeWidgetPlugin.h
-  ctkTitleComboBoxPlugin.h
   ctkTransferFunctionWidgetPlugin.h
   ctkTransferFunctionWidgetPlugin.h
   ctkTreeComboBoxPlugin.h
   ctkTreeComboBoxPlugin.h
   ctkSliderSpinBoxWidgetPlugin.h
   ctkSliderSpinBoxWidgetPlugin.h

+ 15 - 15
Libs/Widgets/Plugins/ctkTitleComboBoxPlugin.cpp

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,55 +15,55 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
 // CTK includes
 // CTK includes
-#include "ctkTitleComboBoxPlugin.h"
-#include "ctkTitleComboBox.h"
+#include "ctkComboBoxPlugin.h"
+#include "ctkComboBox.h"
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-ctkTitleComboBoxPlugin::ctkTitleComboBoxPlugin(QObject *_parent)
+ctkComboBoxPlugin::ctkComboBoxPlugin(QObject *_parent)
         : QObject(_parent)
         : QObject(_parent)
 {
 {
 
 
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-QWidget *ctkTitleComboBoxPlugin::createWidget(QWidget *_parent)
+QWidget *ctkComboBoxPlugin::createWidget(QWidget *_parent)
 {
 {
-  ctkTitleComboBox* _widget = new ctkTitleComboBox(_parent);
+  ctkComboBox* _widget = new ctkComboBox(_parent);
   return _widget;
   return _widget;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-QString ctkTitleComboBoxPlugin::domXml() const
+QString ctkComboBoxPlugin::domXml() const
 {
 {
-  return "<widget class=\"ctkTitleComboBox\" \
+  return "<widget class=\"ctkComboBox\" \
           name=\"CTKTitleComboBox\">\n"
           name=\"CTKTitleComboBox\">\n"
           "</widget>\n";
           "</widget>\n";
 }
 }
 
 
 // --------------------------------------------------------------------------
 // --------------------------------------------------------------------------
-QIcon ctkTitleComboBoxPlugin::icon() const
+QIcon ctkComboBoxPlugin::icon() const
 {
 {
   return QIcon(":/Icons/combobox.png");
   return QIcon(":/Icons/combobox.png");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-QString ctkTitleComboBoxPlugin::includeFile() const
+QString ctkComboBoxPlugin::includeFile() const
 {
 {
-  return "ctkTitleComboBox.h";
+  return "ctkComboBox.h";
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-bool ctkTitleComboBoxPlugin::isContainer() const
+bool ctkComboBoxPlugin::isContainer() const
 {
 {
   return false;
   return false;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-QString ctkTitleComboBoxPlugin::name() const
+QString ctkComboBoxPlugin::name() const
 {
 {
-  return "ctkTitleComboBox";
+  return "ctkComboBox";
 }
 }

+ 7 - 7
Libs/Widgets/Plugins/ctkTitleComboBoxPlugin.h

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,24 +15,24 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
-#ifndef __ctkTitleComboBoxPlugin_h
-#define __ctkTitleComboBoxPlugin_h
+#ifndef __ctkComboBoxPlugin_h
+#define __ctkComboBoxPlugin_h
 
 
 // CTK includes
 // CTK includes
 #include "ctkWidgetsAbstractPlugin.h"
 #include "ctkWidgetsAbstractPlugin.h"
 
 
-class CTK_WIDGETS_PLUGINS_EXPORT ctkTitleComboBoxPlugin :
+class CTK_WIDGETS_PLUGINS_EXPORT ctkComboBoxPlugin :
   public QObject,
   public QObject,
   public ctkWidgetsAbstractPlugin
   public ctkWidgetsAbstractPlugin
 {
 {
   Q_OBJECT
   Q_OBJECT
 
 
 public:
 public:
-  ctkTitleComboBoxPlugin(QObject *_parent = 0);
-  
+  ctkComboBoxPlugin(QObject *_parent = 0);
+
   QWidget *createWidget(QWidget *_parent);
   QWidget *createWidget(QWidget *_parent);
   QString  domXml() const;
   QString  domXml() const;
   QIcon    icon() const;
   QIcon    icon() const;

+ 5 - 5
Libs/Widgets/Plugins/ctkWidgetsPlugins.h

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +15,7 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
 #ifndef __ctkWidgetsPlugins_h
 #ifndef __ctkWidgetsPlugins_h
@@ -26,6 +26,7 @@
 
 
 // CTK includes
 // CTK includes
 #include "CTKWidgetsPluginsExport.h"
 #include "CTKWidgetsPluginsExport.h"
+#include "ctkComboBoxPlugin.h"
 #include "ctkCollapsibleButtonPlugin.h"
 #include "ctkCollapsibleButtonPlugin.h"
 #include "ctkCollapsibleGroupBoxPlugin.h"
 #include "ctkCollapsibleGroupBoxPlugin.h"
 #include "ctkColorPickerButtonPlugin.h"
 #include "ctkColorPickerButtonPlugin.h"
@@ -39,7 +40,6 @@
 #include "ctkMenuButtonPlugin.h"
 #include "ctkMenuButtonPlugin.h"
 #include "ctkRangeSliderPlugin.h"
 #include "ctkRangeSliderPlugin.h"
 #include "ctkRangeWidgetPlugin.h"
 #include "ctkRangeWidgetPlugin.h"
-#include "ctkTitleComboBoxPlugin.h"
 #include "ctkTransferFunctionWidgetPlugin.h"
 #include "ctkTransferFunctionWidgetPlugin.h"
 #include "ctkTreeComboBoxPlugin.h"
 #include "ctkTreeComboBoxPlugin.h"
 #include "ctkSliderSpinBoxWidgetPlugin.h"
 #include "ctkSliderSpinBoxWidgetPlugin.h"
@@ -55,7 +55,8 @@ public:
   QList<QDesignerCustomWidgetInterface*> customWidgets() const
   QList<QDesignerCustomWidgetInterface*> customWidgets() const
     {
     {
     QList<QDesignerCustomWidgetInterface *> plugins;
     QList<QDesignerCustomWidgetInterface *> plugins;
-    plugins << new ctkCollapsibleButtonPlugin
+    plugins << new ctkComboBoxPlugin
+            << new ctkCollapsibleButtonPlugin
             << new ctkCollapsibleGroupBoxPlugin
             << new ctkCollapsibleGroupBoxPlugin
             << new ctkColorPickerButtonPlugin
             << new ctkColorPickerButtonPlugin
             << new ctkCoordinatesWidgetPlugin
             << new ctkCoordinatesWidgetPlugin
@@ -68,7 +69,6 @@ public:
             << new ctkMenuButtonPlugin
             << new ctkMenuButtonPlugin
             << new ctkRangeSliderPlugin
             << new ctkRangeSliderPlugin
             << new ctkRangeWidgetPlugin
             << new ctkRangeWidgetPlugin
-            << new ctkTitleComboBoxPlugin
             << new ctkTransferFunctionWidgetPlugin
             << new ctkTransferFunctionWidgetPlugin
             << new ctkTreeComboBoxPlugin
             << new ctkTreeComboBoxPlugin
             << new ctkSliderSpinBoxWidgetPlugin;
             << new ctkSliderSpinBoxWidgetPlugin;

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

@@ -8,6 +8,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkCollapsibleButtonTest1.cpp
   ctkCollapsibleButtonTest1.cpp
   ctkCollapsibleGroupBoxTest1.cpp
   ctkCollapsibleGroupBoxTest1.cpp
   ctkColorPickerButtonTest1.cpp
   ctkColorPickerButtonTest1.cpp
+  ctkComboBoxTest1.cpp
   ctkCoordinatesWidgetTest1.cpp
   ctkCoordinatesWidgetTest1.cpp
   ctkDoubleRangeSliderTest1.cpp
   ctkDoubleRangeSliderTest1.cpp
   ctkDoubleSliderTest1.cpp
   ctkDoubleSliderTest1.cpp
@@ -16,7 +17,6 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkRangeSliderTest1.cpp
   ctkRangeSliderTest1.cpp
   ctkRangeWidgetTest1.cpp
   ctkRangeWidgetTest1.cpp
   ctkSliderSpinBoxWidgetTest1.cpp
   ctkSliderSpinBoxWidgetTest1.cpp
-  ctkTitleComboBoxTest1.cpp
   ctkTreeComboBoxTest1.cpp
   ctkTreeComboBoxTest1.cpp
   )
   )
 
 
@@ -49,6 +49,7 @@ SIMPLE_TEST( ctkCheckableHeaderViewTest1 )
 SIMPLE_TEST( ctkCollapsibleButtonTest1 )
 SIMPLE_TEST( ctkCollapsibleButtonTest1 )
 SIMPLE_TEST( ctkCollapsibleGroupBoxTest1 )
 SIMPLE_TEST( ctkCollapsibleGroupBoxTest1 )
 SIMPLE_TEST( ctkColorPickerButtonTest1 )
 SIMPLE_TEST( ctkColorPickerButtonTest1 )
+SIMPLE_TEST( ctkComboBoxTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkDoubleRangeSliderTest1 )
 SIMPLE_TEST( ctkDoubleRangeSliderTest1 )
 SIMPLE_TEST( ctkDoubleSliderTest1 )
 SIMPLE_TEST( ctkDoubleSliderTest1 )
@@ -57,5 +58,4 @@ SIMPLE_TEST( ctkMatrixWidgetTest1 )
 SIMPLE_TEST( ctkRangeSliderTest1 )
 SIMPLE_TEST( ctkRangeSliderTest1 )
 SIMPLE_TEST( ctkRangeWidgetTest1 )
 SIMPLE_TEST( ctkRangeWidgetTest1 )
 SIMPLE_TEST( ctkSliderSpinBoxWidgetTest1 )
 SIMPLE_TEST( ctkSliderSpinBoxWidgetTest1 )
-SIMPLE_TEST( ctkTitleComboBoxTest1 )
 SIMPLE_TEST( ctkTreeComboBoxTest1 )
 SIMPLE_TEST( ctkTreeComboBoxTest1 )

+ 5 - 6
Libs/Widgets/Testing/Cpp/ctkTitleComboBoxTest1.cpp

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,26 +15,25 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
 // Qt includes
 // Qt includes
 #include <QApplication>
 #include <QApplication>
 
 
 // CTK includes
 // CTK includes
-#include "ctkTitleComboBox.h"
+#include "ctkComboBox.h"
 
 
 // STD includes
 // STD includes
 #include <stdlib.h>
 #include <stdlib.h>
 #include <iostream>
 #include <iostream>
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int ctkTitleComboBoxTest1(int argc, char * argv [] )
+int ctkComboBoxTest1(int argc, char * argv [] )
 {
 {
   QApplication app(argc, argv);
   QApplication app(argc, argv);
 
 
-  ctkTitleComboBox ctkObject;
-
+  ctkComboBox ctkObject;
 
 
   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
 }
 }

+ 258 - 0
Libs/Widgets/ctkComboBox.cpp

@@ -0,0 +1,258 @@
+/*=========================================================================
+
+  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 <QStylePainter>
+#include <QApplication>
+#include <QDebug>
+
+// CTK includes
+#include "ctkComboBox.h"
+
+// -------------------------------------------------------------------------
+class ctkComboBoxPrivate: public ctkPrivate<ctkComboBox>
+{
+public:
+  ctkComboBoxPrivate();
+  void initStyleOption(QStyleOptionComboBox* opt)const;
+  QSize recomputeSizeHint(QSize &sh) const;
+  QString DefaultText;
+  QIcon   DefaultIcon;
+  bool    ForceDefault;
+
+  mutable QSize MinimumSizeHint;
+  mutable QSize SizeHint;
+};
+
+// -------------------------------------------------------------------------
+ctkComboBoxPrivate::ctkComboBoxPrivate()
+{
+  this->DefaultText = "Select an item...";
+  this->ForceDefault = false;
+}
+
+// -------------------------------------------------------------------------
+ctkComboBox::ctkComboBox(QWidget* _parent)
+  : QComboBox(_parent)
+{
+  CTK_INIT_PRIVATE(ctkComboBox);
+}
+
+// -------------------------------------------------------------------------
+ctkComboBox::~ctkComboBox()
+{
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::setDefaultText(const QString& newDefaultText)
+{
+  CTK_D(ctkComboBox);
+  d->DefaultText = newDefaultText;
+  d->SizeHint = QSize();
+  this->updateGeometry();
+}
+
+// -------------------------------------------------------------------------
+QString ctkComboBox::defaultText()const
+{
+  CTK_D(const ctkComboBox);
+  return d->DefaultText;
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::setDefaultIcon(const QIcon& newIcon)
+{
+  CTK_D(ctkComboBox);
+  d->DefaultIcon = newIcon;
+  d->SizeHint = QSize();
+  this->updateGeometry();
+}
+
+// -------------------------------------------------------------------------
+QIcon ctkComboBox::defaultIcon()const
+{
+  CTK_D(const ctkComboBox);
+  return d->DefaultIcon;
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::forceDefault(bool newForceDefault)
+{
+  CTK_D(ctkComboBox);
+  if (newForceDefault == d->ForceDefault)
+    {
+    return;
+    }
+  d->ForceDefault = newForceDefault;
+  d->SizeHint = QSize();
+  this->updateGeometry();
+}
+
+// -------------------------------------------------------------------------
+bool ctkComboBox::isDefaultForced()const
+{
+  CTK_D(const ctkComboBox);
+  return d->ForceDefault;
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::paintEvent(QPaintEvent*)
+{
+  CTK_D(ctkComboBox);
+  QStylePainter painter(this);
+  painter.setPen(palette().color(QPalette::Text));
+
+  QStyleOptionComboBox opt;
+  d->initStyleOption(&opt);
+
+  // draw the combobox frame, focusrect and selected etc.
+  painter.drawComplexControl(QStyle::CC_ComboBox, opt);
+  // draw the icon and text
+  painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
+}
+
+// -------------------------------------------------------------------------
+QSize ctkComboBox::minimumSizeHint() const
+{
+  CTK_D(const ctkComboBox);
+  return d->recomputeSizeHint(d->MinimumSizeHint);
+}
+
+// -------------------------------------------------------------------------
+/*!
+    \reimp
+
+    This implementation caches the size hint to avoid resizing when
+    the contents change dynamically. To invalidate the cached value
+    change the \l sizeAdjustPolicy.
+*/
+QSize ctkComboBox::sizeHint() const
+{
+  CTK_D(const ctkComboBox);
+  return d->recomputeSizeHint(d->SizeHint);
+}
+
+// -------------------------------------------------------------------------
+QSize ctkComboBoxPrivate::recomputeSizeHint(QSize &sh) const
+{
+  CTK_P(const ctkComboBox);
+  if (sh.isValid())
+    {
+    return sh.expandedTo(QApplication::globalStrut());
+    }
+
+  bool hasIcon = false;
+  int count = p->count();
+  QSize iconSize = p->iconSize();
+  const QFontMetrics &fm = p->fontMetrics();
+
+  // text width
+  if (&sh == &this->SizeHint || p->minimumContentsLength() == 0)
+    {
+    switch (p->sizeAdjustPolicy())
+      {
+      case QComboBox::AdjustToContents:
+      case QComboBox::AdjustToContentsOnFirstShow:
+        if (count == 0 || this->ForceDefault)
+          {
+          sh.rwidth() = this->DefaultText.isEmpty() ?
+            7 * fm.width(QLatin1Char('x')) :
+            fm.boundingRect(this->DefaultText).width();
+          if (!this->DefaultIcon.isNull())
+            {
+            hasIcon = true;
+            sh.rwidth() += iconSize.width() + 4;
+            }
+          }
+        for (int i = 0; i < count; ++i)
+          {
+          if (!p->itemIcon(i).isNull())
+            {
+            hasIcon = true;
+            sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width() + iconSize.width() + 4));
+            }
+          else
+            {
+            sh.setWidth(qMax(sh.width(), fm.boundingRect(p->itemText(i)).width()));
+            }
+          }
+        break;
+      case QComboBox::AdjustToMinimumContentsLength:
+        if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
+          {
+          hasIcon = true;
+          }
+        for (int i = 0; i < count && !hasIcon; ++i)
+          {
+          hasIcon = !p->itemIcon(i).isNull();
+          }
+        break;
+      case QComboBox::AdjustToMinimumContentsLengthWithIcon:
+        hasIcon = true;
+        break;
+      default:
+        break;
+      }
+    }
+  else // minimumsizehint is computing and minimumcontentslenght is > 0
+    {
+    if ((count == 0 || this->ForceDefault) && !this->DefaultIcon.isNull())
+      {
+      hasIcon = true;
+      }
+
+    for (int i = 0; i < count && !hasIcon; ++i)
+      {
+      hasIcon = !p->itemIcon(i).isNull();
+      }
+    }
+  if (p->minimumContentsLength() > 0)
+    {
+    sh.setWidth(qMax(sh.width(),
+                     p->minimumContentsLength() * fm.width(QLatin1Char('X'))
+                     + (hasIcon ? iconSize.width() + 4 : 0)));
+    }
+
+  // height
+  sh.setHeight(qMax(fm.height(), 14) + 2);
+  if (hasIcon)
+    {
+    sh.setHeight(qMax(sh.height(), iconSize.height() + 2));
+    }
+
+  // add style and strut values
+  QStyleOptionComboBox opt;
+  this->initStyleOption(&opt);
+  sh = p->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, p);
+  return sh.expandedTo(QApplication::globalStrut());
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBoxPrivate::initStyleOption(QStyleOptionComboBox* opt)const
+{
+  CTK_P(const ctkComboBox);
+  p->initStyleOption(opt);
+  if (p->currentIndex() == -1 ||
+      this->ForceDefault)
+    {
+    opt->currentText = this->DefaultText;
+    opt->currentIcon = this->DefaultIcon;
+    }
+}

+ 21 - 20
Libs/Widgets/ctkTitleComboBox.h

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,47 +15,48 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
-#ifndef __ctkTitleComboBox_h
-#define __ctkTitleComboBox_h
+#ifndef __ctkComboBox_h
+#define __ctkComboBox_h
 
 
 // Qt includes
 // Qt includes
 #include <QComboBox>
 #include <QComboBox>
 
 
 // CTK includes
 // CTK includes
+#include "ctkPimpl.h"
 #include "CTKWidgetsExport.h"
 #include "CTKWidgetsExport.h"
+class ctkComboBoxPrivate;
 
 
-class CTK_WIDGETS_EXPORT ctkTitleComboBox : public QComboBox
+class CTK_WIDGETS_EXPORT ctkComboBox : public QComboBox
 {
 {
   Q_OBJECT
   Q_OBJECT
-  Q_PROPERTY(QString title READ title WRITE setTitle)
-  Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
+  Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
+  Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
+  Q_PROPERTY(bool forceDefault READ isDefaultForced WRITE forceDefault)
 
 
 public:
 public:
-  explicit ctkTitleComboBox(QWidget* parent = 0);
-  virtual ~ctkTitleComboBox();
+  explicit ctkComboBox(QWidget* parent = 0);
+  virtual ~ctkComboBox();
+
+  void setDefaultText(const QString&);
+  QString defaultText()const;
+
+  void setDefaultIcon(const QIcon&);
+  QIcon defaultIcon()const;
 
 
-  void setTitle(const QString&);
-  QString title()const;
-  
-  void setIcon(const QIcon&);
-  QIcon icon()const;
+  void forceDefault(bool forceDefault);
+  bool isDefaultForced()const;
 
 
   virtual QSize minimumSizeHint()const;
   virtual QSize minimumSizeHint()const;
   virtual QSize sizeHint()const;
   virtual QSize sizeHint()const;
 
 
 protected:
 protected:
   virtual void paintEvent(QPaintEvent*);
   virtual void paintEvent(QPaintEvent*);
-  virtual QSize recomputeSizeHint(QSize &sh)const;
 
 
-  QString Title;
-  QIcon   Icon;
-  
 private:
 private:
-  mutable QSize MinimumSizeHint;
-  mutable QSize SizeHint;
+  CTK_DECLARE_PRIVATE(ctkComboBox);
 };
 };
 
 
 #endif
 #endif

+ 0 - 149
Libs/Widgets/ctkTitleComboBox.cpp

@@ -1,149 +0,0 @@
-/*=========================================================================
-
-  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 <QStylePainter>
-#include <QApplication>
-#include <QDebug>
-
-// CTK includes
-#include "ctkTitleComboBox.h"
-
-// -------------------------------------------------------------------------
-ctkTitleComboBox::ctkTitleComboBox(QWidget* _parent)
-  : QComboBox(_parent)
-{
-  this->Title = "Select an item...";
-}
-
-// -------------------------------------------------------------------------
-ctkTitleComboBox::~ctkTitleComboBox()
-{
-  
-}
-
-// -------------------------------------------------------------------------
-void ctkTitleComboBox::setTitle(const QString& _title)
-{
-  this->Title = _title;
-  this->SizeHint = QSize();
-  this->updateGeometry();
-}
-
-// -------------------------------------------------------------------------
-QString ctkTitleComboBox::title()const
-{
-  return this->Title;
-}
-
-// -------------------------------------------------------------------------
-void ctkTitleComboBox::setIcon(const QIcon& _icon)
-{
-  this->Icon = _icon;
-  this->SizeHint = QSize();
-  this->updateGeometry();
-}
-
-// -------------------------------------------------------------------------
-QIcon ctkTitleComboBox::icon()const
-{
-  return this->Icon;
-}
-
-// -------------------------------------------------------------------------
-void ctkTitleComboBox::paintEvent(QPaintEvent*)
-{
-  QStylePainter painter(this);
-  painter.setPen(palette().color(QPalette::Text));
-
-  QStyleOptionComboBox opt;
-  initStyleOption(&opt);
-
-  opt.currentText = this->Title;
-  opt.currentIcon = this->Icon;
-  if (!this->Icon.isNull() && !this->Icon.availableSizes().empty())
-    {
-    QList<QSize> sizes = this->Icon.availableSizes();
-    // TODO: pick the best size (based on the pixelMetric maybe)
-    //int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
-    opt.iconSize = sizes[0];
-    }
-
-  // draw the combobox frame, focusrect and selected etc.
-  painter.drawComplexControl(QStyle::CC_ComboBox, opt);
-  // draw the icon and text
-  painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
-}
-
-// -------------------------------------------------------------------------
-QSize ctkTitleComboBox::minimumSizeHint() const
-{
-  return this->recomputeSizeHint(this->SizeHint);
-}
-
-// -------------------------------------------------------------------------
-/*!
-    \reimp
-
-    This implementation caches the size hint to avoid resizing when
-    the contents change dynamically. To invalidate the cached value
-    change the \l sizeAdjustPolicy.
-*/
-QSize ctkTitleComboBox::sizeHint() const
-{
-  return this->recomputeSizeHint(this->SizeHint);
-}
-
-// -------------------------------------------------------------------------
-QSize ctkTitleComboBox::recomputeSizeHint(QSize &sh) const
-{
-  if (sh.isValid()) 
-    {
-    return sh.expandedTo(QApplication::globalStrut());
-    }
-  const QFontMetrics &fm = this->fontMetrics();
-  QList<QSize> iconSizes = this->Icon.availableSizes();
-  if (!this->Icon.isNull() && !iconSizes.empty()) 
-    {
-    // TODO: pick the best size (based on the pixelMetric maybe)
-    QSize _iconSize = iconSizes[0];
-    sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width() + _iconSize.width() + 4));
-    }
-  else
-    {
-    sh.setWidth(qMax(sh.width(), fm.boundingRect(this->Title).width()));
-    }
-
-  // height
-  sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
-  if (!this->Icon.isNull() && !iconSizes.empty()) 
-    {
-    // TODO: pick the best size (based on the pixelMetric maybe)
-    QSize _iconSize = iconSizes[0];
-    sh.setHeight(qMax(sh.height(), _iconSize.height() + 2));
-    }
-
-  // add style and strut values
-  QStyleOptionComboBox opt;
-  this->initStyleOption(&opt);
-  sh = this->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
-
-  return sh.expandedTo(QApplication::globalStrut());
-}