浏览代码

Display ctkColorPickerButton as a ToolButton when no text is given

Julien Finet 14 年之前
父节点
当前提交
fcd98c9266

+ 12 - 2
Libs/Widgets/Testing/Cpp/ctkColorPickerButtonTest1.cpp

@@ -39,21 +39,30 @@ int ctkColorPickerButtonTest1(int argc, char * argv [] )
   ctkColorPickerButton colorPicker1;
   ctkColorPickerButton colorPicker2("Select a color");
   ctkColorPickerButton colorPicker3(Qt::red,"text");
+  ctkColorPickerButton colorPicker4(Qt::yellow,"");
   
+  QHBoxLayout* subLayout = new QHBoxLayout;
+  subLayout->addStretch(1);
+  subLayout->addWidget(&colorPicker4);
+  subLayout->addStretch(1);
+
   QVBoxLayout* layout = new QVBoxLayout;
   layout->addWidget(&colorPicker1);
   layout->addWidget(&colorPicker2);
   layout->addWidget(&colorPicker3);
+  layout->addLayout(subLayout);
   topLevel.setLayout(layout);
 
   if (!colorPicker1.text().isEmpty() ||
        colorPicker2.text() != "Select a color" ||
-       colorPicker3.text() != "text")
+       colorPicker3.text() != "text" ||
+       colorPicker4.text() != "")
     {
     std::cerr << "ctkColorPickerButton::ctkColorPickerButton wrong default text"
               << colorPicker1.text().toStdString() << " "
               << colorPicker2.text().toStdString() << " "
-              << colorPicker3.text().toStdString() << std::endl;
+              << colorPicker3.text().toStdString() << " "
+              << colorPicker4.text().toStdString() << std::endl;
     return EXIT_FAILURE;
     }
 
@@ -67,6 +76,7 @@ int ctkColorPickerButtonTest1(int argc, char * argv [] )
     }
   
   colorPicker3.setDisplayColorName(false);
+  colorPicker4.setDisplayColorName(false);
   
   if (colorPicker3.displayColorName() ||
       colorPicker3.text() != "text")

+ 33 - 1
Libs/Widgets/ctkColorPickerButton.cpp

@@ -19,6 +19,7 @@
 =========================================================================*/
 
 // Qt includes
+#include <QApplication>
 #include <QColorDialog>
 #include <QDebug>
 #include <QIcon>
@@ -46,6 +47,7 @@ public:
   QColor Color;
   bool   DisplayColorName;
   ctkColorPickerButton::ColorDialogOptions DialogOptions;
+  mutable QSize CachedSizeHint;
 };
 
 //-----------------------------------------------------------------------------
@@ -158,7 +160,9 @@ void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
 {
   Q_D(ctkColorPickerButton);
   d->DisplayColorName = displayColorName;
+  d->CachedSizeHint = QSize();
   this->update();
+  this->updateGeometry();
 }
 
 //-----------------------------------------------------------------------------
@@ -211,7 +215,7 @@ void ctkColorPickerButton::paintEvent(QPaintEvent *)
   Q_D(ctkColorPickerButton);
   QStylePainter p(this);
   QStyleOptionButton option;
-  initStyleOption(&option);
+  this->initStyleOption(&option);
   if (d->DisplayColorName)
     {
     option.text = d->Color.name();
@@ -219,3 +223,31 @@ void ctkColorPickerButton::paintEvent(QPaintEvent *)
   option.icon = d->Icon;
   p.drawControl(QStyle::CE_PushButton, option);
 }
+
+//-----------------------------------------------------------------------------
+QSize ctkColorPickerButton::sizeHint()const
+{
+  Q_D(const ctkColorPickerButton);
+  if (d->DisplayColorName || !this->text().isEmpty())
+    {
+    return this->QPushButton::sizeHint();
+    }
+  if (d->CachedSizeHint.isValid())
+    {
+    return d->CachedSizeHint;
+    }
+
+  QStyleOptionButton pushButtonOpt;
+  this->initStyleOption(&pushButtonOpt);
+  QStyleOptionToolButton opt;
+  (&opt)->QStyleOption::operator=(pushButtonOpt);
+  opt.arrowType = Qt::NoArrow;
+  opt.icon = d->Icon;
+  int iconSize = this->style()->pixelMetric(QStyle::PM_SmallIconSize);
+  opt.iconSize = QSize(iconSize, iconSize);
+  opt.rect.setSize(opt.iconSize); // PM_MenuButtonIndicator depends on the height
+  d->CachedSizeHint = this->style()->sizeFromContents(
+    QStyle::CT_ToolButton, &opt, opt.iconSize, this).
+    expandedTo(QApplication::globalStrut());
+  return d->CachedSizeHint;
+}

+ 4 - 0
Libs/Widgets/ctkColorPickerButton.h

@@ -76,6 +76,10 @@ public:
   void setDialogOptions(const ColorDialogOptions& options);
   const ColorDialogOptions& dialogOptions() const;
 
+  ///
+  /// Reimplemented to return a toolbutton sizehint when no text is displayed
+  /// in the button.
+  virtual QSize sizeHint()const;
 
 public slots:
   ///