Browse Source

ENH: Add elideMode property to ctkComboBox for display

ctkComboBox::elideMode can add an ellipsis to the current item text in case
the text is too long. Qt::ElideNone by default
Julien Finet 14 years ago
parent
commit
7c2e2914ec
2 changed files with 131 additions and 101 deletions
  1. 124 101
      Libs/Widgets/ctkComboBox.cpp
  2. 7 0
      Libs/Widgets/ctkComboBox.h

+ 124 - 101
Libs/Widgets/ctkComboBox.cpp

@@ -36,6 +36,7 @@ public:
   QString DefaultText;
   QString DefaultText;
   QIcon   DefaultIcon;
   QIcon   DefaultIcon;
   bool    ForceDefault;
   bool    ForceDefault;
+  Qt::TextElideMode ElideMode;
 
 
   mutable QSize MinimumSizeHint;
   mutable QSize MinimumSizeHint;
   mutable QSize SizeHint;
   mutable QSize SizeHint;
@@ -46,107 +47,7 @@ ctkComboBoxPrivate::ctkComboBoxPrivate()
 {
 {
   this->DefaultText = "Select an item...";
   this->DefaultText = "Select an item...";
   this->ForceDefault = false;
   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);
+  this->ElideMode = Qt::ElideNone;
 }
 }
 
 
 // -------------------------------------------------------------------------
 // -------------------------------------------------------------------------
@@ -255,4 +156,126 @@ void ctkComboBoxPrivate::initStyleOption(QStyleOptionComboBox* opt)const
     opt->currentText = this->DefaultText;
     opt->currentText = this->DefaultText;
     opt->currentIcon = this->DefaultIcon;
     opt->currentIcon = this->DefaultIcon;
     }
     }
+  QRect textRect = p->style()->subControlRect(
+    QStyle::CC_ComboBox, opt, QStyle::SC_ComboBoxEditField, p);
+  // TODO substract icon size
+  opt->currentText = opt->fontMetrics.elidedText(opt->currentText,
+                                                 this->ElideMode,
+                                                 textRect.width());
+}
+
+
+// -------------------------------------------------------------------------
+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->update();
+}
+
+// -------------------------------------------------------------------------
+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->update();
+}
+
+// -------------------------------------------------------------------------
+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();
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::setElideMode(const Qt::TextElideMode& newMode)
+{
+  CTK_D(ctkComboBox);
+  d->ElideMode = newMode;
+  this->update();
+}
+// -------------------------------------------------------------------------
+Qt::TextElideMode ctkComboBox::elideMode()const
+{
+  CTK_D(const ctkComboBox);
+  return d->ElideMode;
+}
+
+// -------------------------------------------------------------------------
+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);
 }
 }

+ 7 - 0
Libs/Widgets/ctkComboBox.h

@@ -35,20 +35,27 @@ class CTK_WIDGETS_EXPORT ctkComboBox : public QComboBox
   Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
   Q_PROPERTY(QString defaultText READ defaultText WRITE setDefaultText)
   Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
   Q_PROPERTY(QIcon defaultIcon READ defaultIcon WRITE setDefaultIcon)
   Q_PROPERTY(bool forceDefault READ isDefaultForced WRITE forceDefault)
   Q_PROPERTY(bool forceDefault READ isDefaultForced WRITE forceDefault)
+  Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode)
 
 
 public:
 public:
   explicit ctkComboBox(QWidget* parent = 0);
   explicit ctkComboBox(QWidget* parent = 0);
   virtual ~ctkComboBox();
   virtual ~ctkComboBox();
 
 
+  /// Empty by default (same behavior as QComboBox)
   void setDefaultText(const QString&);
   void setDefaultText(const QString&);
   QString defaultText()const;
   QString defaultText()const;
 
 
+  /// Empty by default (same behavior as QComboBox)
   void setDefaultIcon(const QIcon&);
   void setDefaultIcon(const QIcon&);
   QIcon defaultIcon()const;
   QIcon defaultIcon()const;
 
 
   void forceDefault(bool forceDefault);
   void forceDefault(bool forceDefault);
   bool isDefaultForced()const;
   bool isDefaultForced()const;
 
 
+  /// Qt::ElideNone by default (same behavior as QComboBox)
+  void setElideMode(const Qt::TextElideMode& newMode);
+  Qt::TextElideMode elideMode()const;
+
   virtual QSize minimumSizeHint()const;
   virtual QSize minimumSizeHint()const;
   virtual QSize sizeHint()const;
   virtual QSize sizeHint()const;