|
@@ -20,49 +20,95 @@
|
|
|
|
|
|
// Qt includes
|
|
|
#include <QColorDialog>
|
|
|
+#include <QDebug>
|
|
|
#include <QIcon>
|
|
|
#include <QPainter>
|
|
|
#include <QPixmap>
|
|
|
#include <QStyle>
|
|
|
+#include <QStyleOptionButton>
|
|
|
+#include <QStylePainter>
|
|
|
|
|
|
// CTK includes
|
|
|
#include "ctkColorPickerButton.h"
|
|
|
|
|
|
+class ctkColorPickerButtonPrivate
|
|
|
+{
|
|
|
+ Q_DECLARE_PUBLIC(ctkColorPickerButton);
|
|
|
+protected:
|
|
|
+ ctkColorPickerButton* const q_ptr;
|
|
|
+public:
|
|
|
+ ctkColorPickerButtonPrivate(ctkColorPickerButton& object);
|
|
|
+ void init();
|
|
|
+ void computeIcon();
|
|
|
+
|
|
|
+ QIcon Icon;
|
|
|
+ QColor Color;
|
|
|
+ bool DisplayColorName;
|
|
|
+ bool ShowAlpha;
|
|
|
+};
|
|
|
+
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
|
|
|
- :QPushButton(_parent)
|
|
|
+ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& object)
|
|
|
+ : q_ptr(&object)
|
|
|
{
|
|
|
- connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
|
|
|
- this->setColor(Qt::black);
|
|
|
+ this->Color = Qt::black;
|
|
|
+ this->DisplayColorName = true;
|
|
|
+ this->ShowAlpha = false;
|
|
|
+}
|
|
|
|
|
|
- this->setCheckable(true);
|
|
|
- this->setDisplayColorName(true);
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void ctkColorPickerButtonPrivate::init()
|
|
|
+{
|
|
|
+ Q_Q(ctkColorPickerButton);
|
|
|
+ q->setCheckable(true);
|
|
|
+ QObject::connect(q, SIGNAL(toggled(bool)),
|
|
|
+ q, SLOT(onToggled(bool)));
|
|
|
+ this->computeIcon();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
|
|
|
- :QPushButton(_text, _parent)
|
|
|
+void ctkColorPickerButtonPrivate::computeIcon()
|
|
|
+{
|
|
|
+ Q_Q(ctkColorPickerButton);
|
|
|
+ int _iconSize = q->style()->pixelMetric(QStyle::PM_SmallIconSize);
|
|
|
+ QPixmap pix(_iconSize, _iconSize);
|
|
|
+ pix.fill(q->palette().button().color());
|
|
|
+ QPainter p(&pix);
|
|
|
+ p.setPen(QPen(Qt::gray));
|
|
|
+ p.setBrush(this->Color);
|
|
|
+ p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
|
|
|
+
|
|
|
+ this->Icon = QIcon(pix);
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
|
|
|
+ : QPushButton(_parent)
|
|
|
+ , d_ptr(new ctkColorPickerButtonPrivate(*this))
|
|
|
{
|
|
|
- connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
|
|
|
- this->setColor(Qt::black);
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ d->init();
|
|
|
+}
|
|
|
|
|
|
- // Customize
|
|
|
- this->setCheckable(true);
|
|
|
- this->setDisplayColorName(true);
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
|
|
|
+ : QPushButton(_text, _parent)
|
|
|
+ , d_ptr(new ctkColorPickerButtonPrivate(*this))
|
|
|
+{
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ d->init();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
|
|
|
- const QString& _text,
|
|
|
- QWidget* _parent)
|
|
|
- :QPushButton(_text, _parent)
|
|
|
+ const QString& _text,
|
|
|
+ QWidget* _parent)
|
|
|
+ : QPushButton(_text, _parent)
|
|
|
+ , d_ptr(new ctkColorPickerButtonPrivate(*this))
|
|
|
{
|
|
|
- connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ d->init();
|
|
|
this->setColor(_color);
|
|
|
-
|
|
|
- // Customize
|
|
|
- this->setCheckable(true);
|
|
|
- this->setDisplayColorName(true);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
@@ -73,7 +119,21 @@ ctkColorPickerButton::~ctkColorPickerButton()
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ctkColorPickerButton::changeColor()
|
|
|
{
|
|
|
- this->setColor(QColorDialog::getColor(this->Color));
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ QColor res;
|
|
|
+ if (d->ShowAlpha)
|
|
|
+ {
|
|
|
+ res = QColorDialog::getColor(
|
|
|
+ d->Color, this, QString(""), QColorDialog::ShowAlphaChannel );
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ res = QColorDialog::getColor(d->Color);
|
|
|
+ }
|
|
|
+ if (res.isValid())
|
|
|
+ {
|
|
|
+ this->setColor(res);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
@@ -89,46 +149,66 @@ void ctkColorPickerButton::onToggled(bool change)
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
|
|
|
{
|
|
|
- this->DisplayColorName = displayColorName;
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ d->DisplayColorName = displayColorName;
|
|
|
+ this->update();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void ctkColorPickerButton::setColor(const QColor& newColor)
|
|
|
+bool ctkColorPickerButton::displayColorName()const
|
|
|
{
|
|
|
- if (newColor == this->Color)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
+ Q_D(const ctkColorPickerButton);
|
|
|
+ return d->DisplayColorName;
|
|
|
+}
|
|
|
|
|
|
- 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(newColor);
|
|
|
- p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void ctkColorPickerButton::setShowAlpha(bool show)
|
|
|
+{
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ d->ShowAlpha = show;
|
|
|
+}
|
|
|
|
|
|
- this->setIcon(QIcon(pix));
|
|
|
-
|
|
|
- // Update the button text to the color name, if selected
|
|
|
- if (this->DisplayColorName)
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+bool ctkColorPickerButton::showAlpha()const
|
|
|
+{
|
|
|
+ Q_D(const ctkColorPickerButton);
|
|
|
+ return d->ShowAlpha;
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void ctkColorPickerButton::setColor(const QColor& newColor)
|
|
|
+{
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ if (newColor == d->Color)
|
|
|
{
|
|
|
- this->setText(newColor.name());
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- this->Color = newColor;
|
|
|
- emit colorChanged(this->Color);
|
|
|
+ d->Color = newColor;
|
|
|
+ d->computeIcon();
|
|
|
+
|
|
|
+ this->update();
|
|
|
+ emit colorChanged(d->Color);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
QColor ctkColorPickerButton::color()const
|
|
|
{
|
|
|
- return this->Color;
|
|
|
+ Q_D(const ctkColorPickerButton);
|
|
|
+ return d->Color;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-bool ctkColorPickerButton::displayColorName()const
|
|
|
+void ctkColorPickerButton::paintEvent(QPaintEvent *)
|
|
|
{
|
|
|
- return this->DisplayColorName;
|
|
|
+ Q_D(ctkColorPickerButton);
|
|
|
+ QStylePainter p(this);
|
|
|
+ QStyleOptionButton option;
|
|
|
+ initStyleOption(&option);
|
|
|
+ if (d->DisplayColorName)
|
|
|
+ {
|
|
|
+ option.text = d->Color.name();
|
|
|
+ }
|
|
|
+ option.icon = d->Icon;
|
|
|
+ p.drawControl(QStyle::CE_PushButton, option);
|
|
|
}
|
|
|
-
|