Explorar el Código

ENH: Added class ctkColorPickerButton (and corresponding test skeleton)

Jean-Christophe Fillion-Robin hace 15 años
padre
commit
0528a961d7

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

@@ -1,6 +1,7 @@
 SET(KIT ${PROJECT_NAME})
 
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
+  ctkColorPickerButtonTest1.cxx
   ctkMatrixWidgetTest1.cxx
   #EXTRA_INCLUDE TestingMacros.h
   )
@@ -27,4 +28,5 @@ ENDMACRO( SIMPLE_TEST  )
 # Add Tests
 #
 
+SIMPLE_TEST( ctkColorPickerButtonTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest1 )

+ 34 - 0
Libs/Widgets/Testing/Cpp/ctkColorPickerButtonTest1.cxx

@@ -0,0 +1,34 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+
+// CTK includes
+#include "ctkColorPickerButton.h"
+
+// STD includes
+#include <stdlib.h>
+#include <iostream>
+
+int ctkColorPickerButtonTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkColorPickerButton ctkObject;
+
+
+  return EXIT_SUCCESS;
+}
+

+ 97 - 0
Libs/Widgets/ctkColorPickerButton.cpp

@@ -0,0 +1,97 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+// Qt includes
+#include <QColorDialog>
+#include <QIcon>
+#include <QPainter>
+#include <QPixmap>
+#include <QStyle>
+
+// CTK includes
+#include "ctkColorPickerButton.h"
+
+//-----------------------------------------------------------------------------
+ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
+  :QPushButton(_parent)
+{
+  connect(this, SIGNAL(toggled(bool)), this, SLOT(changeColor(bool)));
+  this->setColor(Qt::black);
+
+  this->setCheckable(true);
+}
+
+//-----------------------------------------------------------------------------
+ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
+  :QPushButton(_text, _parent)
+{
+  connect(this, SIGNAL(clicked), this, SLOT(changeColor));
+  this->setColor(Qt::black);
+
+  // Customize
+  this->setCheckable(true);
+}
+
+//-----------------------------------------------------------------------------
+ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
+                                             const QString& _text,
+                                             QWidget* _parent)
+  :QPushButton(_text, _parent)
+{
+  connect(this, SIGNAL(clicked), this, SLOT(changeColor));
+  this->setColor(_color);
+
+  // Customize
+  this->setCheckable(true);
+}
+
+//-----------------------------------------------------------------------------
+void ctkColorPickerButton::changeColor(bool change)
+{
+  if (change)
+    {
+    this->setColor(QColorDialog::getColor(this->Color));
+
+    this->setChecked(false);
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkColorPickerButton::setColor(const QColor& _color)
+{
+  if (_color == this->Color)
+    {
+    return;
+    }
+
+  int _iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
+  QPixmap pix(_iconSize, _iconSize);
+  pix.fill(palette().button().color());
+  QPainter p(&pix);
+  p.setPen(QPen(Qt::gray));
+  p.setBrush(_color);
+  p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
+
+  this->setIcon(QIcon(pix));
+  this->setText(_color.name());
+  
+  this->Color = _color;
+  emit colorChanged(this->Color);
+}
+
+//-----------------------------------------------------------------------------
+QColor ctkColorPickerButton::color()const
+{
+  return this->Color;
+}

+ 48 - 0
Libs/Widgets/ctkColorPickerButton.h

@@ -0,0 +1,48 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc. 
+  All rights reserved.
+  Distributed under a BSD License. See LICENSE.txt file.
+
+  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+  See the above copyright notice for more information.
+
+=========================================================================*/
+
+#ifndef __ctkColorPickerButton_h
+#define __ctkColorPickerButton_h
+
+// Qt includes
+#include <QPushButton>
+#include <QColor>
+
+// CTK includes
+#include "CTKWidgetsExport.h"
+
+class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
+{
+  Q_OBJECT
+  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
+public:
+  explicit ctkColorPickerButton(QWidget* parent = 0);
+  explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
+  explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
+  virtual ~ctkColorPickerButton(){}
+  
+  QColor color()const;
+
+public slots:
+  void setColor(const QColor& color);
+  void changeColor(bool change = true);
+
+signals:
+  void colorChanged(QColor);
+
+protected:
+  QColor Color;
+};
+
+#endif