123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /*=========================================================================
- Library: CTK
- Copyright (c) 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.apache.org/licenses/LICENSE-2.0.txt
- 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 <QApplication>
- #include <QColorDialog>
- #include <QDebug>
- #include <QIcon>
- #include <QPainter>
- #include <QPixmap>
- #include <QStyle>
- #include <QStyleOptionButton>
- #include <QStylePainter>
- // CTK includes
- #include "ctkColorDialog.h"
- #include "ctkColorPickerButton.h"
- class ctkColorPickerButtonPrivate
- {
- Q_DECLARE_PUBLIC(ctkColorPickerButton);
- protected:
- ctkColorPickerButton* const q_ptr;
- public:
- ctkColorPickerButtonPrivate(ctkColorPickerButton& object);
- void init();
- void computeIcon();
- QString text()const;
- QIcon Icon;
- QColor Color;
- QString ColorName;
- bool DisplayColorName;
- ctkColorPickerButton::ColorDialogOptions DialogOptions;
- mutable QSize CachedSizeHint;
- };
- //-----------------------------------------------------------------------------
- ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& object)
- : q_ptr(&object)
- {
- this->Color = Qt::black;
- this->ColorName = QString();
- this->DisplayColorName = true;
- this->DialogOptions = 0;
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButtonPrivate::init()
- {
- Q_Q(ctkColorPickerButton);
- q->setCheckable(true);
- QObject::connect(q, SIGNAL(toggled(bool)),
- q, SLOT(onToggled(bool)));
- this->computeIcon();
- }
- //-----------------------------------------------------------------------------
- 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);
- }
- //-----------------------------------------------------------------------------
- QString ctkColorPickerButtonPrivate::text()const
- {
- Q_Q(const ctkColorPickerButton);
- if (!this->DisplayColorName)
- {
- return q->text();
- }
- if (this->ColorName.isEmpty())
- {
- return this->Color.name();
- }
- else
- {
- return this->ColorName;
- }
- }
- //-----------------------------------------------------------------------------
- ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
- : QPushButton(_parent)
- , d_ptr(new ctkColorPickerButtonPrivate(*this))
- {
- Q_D(ctkColorPickerButton);
- d->init();
- }
- //-----------------------------------------------------------------------------
- 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)
- , d_ptr(new ctkColorPickerButtonPrivate(*this))
- {
- Q_D(ctkColorPickerButton);
- d->init();
- this->setColor(_color);
- }
- //-----------------------------------------------------------------------------
- ctkColorPickerButton::~ctkColorPickerButton()
- {
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::changeColor()
- {
- Q_D(ctkColorPickerButton);
- QColor newColor;
- QString newColorName;
- QColorDialog::ColorDialogOptions options;
- options |= QColorDialog::ColorDialogOption(
- static_cast<int>(d->DialogOptions & ShowAlphaChannel));
- options |= QColorDialog::ColorDialogOption(
- static_cast<int>(d->DialogOptions & NoButtons));
- options |= QColorDialog::ColorDialogOption(
- static_cast<int>(d->DialogOptions & DontUseNativeDialog));
- if (d->DialogOptions & UseCTKColorDialog)
- {
- newColor = ctkColorDialog::getColor(d->Color, this, QString(""),options);
- newColorName = ctkColorDialog::getColorName();
- }
- else
- {
- newColor = QColorDialog::getColor(d->Color, this, QString(""), options);
- }
- if (newColor.isValid())
- {
- this->setColor(newColor);
- this->setColorName(newColorName);
- }
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::onToggled(bool change)
- {
- if (change)
- {
- this->changeColor();
- this->setChecked(false);
- }
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
- {
- Q_D(ctkColorPickerButton);
- d->DisplayColorName = displayColorName;
- d->CachedSizeHint = QSize();
- this->update();
- this->updateGeometry();
- }
- //-----------------------------------------------------------------------------
- bool ctkColorPickerButton::displayColorName()const
- {
- Q_D(const ctkColorPickerButton);
- return d->DisplayColorName;
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::setDialogOptions(const ColorDialogOptions& options)
- {
- Q_D(ctkColorPickerButton);
- d->DialogOptions = options;
- }
- //-----------------------------------------------------------------------------
- const ctkColorPickerButton::ColorDialogOptions& ctkColorPickerButton::dialogOptions()const
- {
- Q_D(const ctkColorPickerButton);
- return d->DialogOptions;
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::setColor(const QColor& newColor)
- {
- Q_D(ctkColorPickerButton);
- if (newColor == d->Color)
- {
- return;
- }
- d->Color = newColor;
- d->computeIcon();
- this->update();
- emit colorChanged(d->Color);
- }
- //-----------------------------------------------------------------------------
- QColor ctkColorPickerButton::color()const
- {
- Q_D(const ctkColorPickerButton);
- return d->Color;
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::setColorName(const QString& newColorName)
- {
- Q_D(ctkColorPickerButton);
- if (newColorName == d->ColorName)
- {
- return;
- }
- d->ColorName = newColorName;
- d->CachedSizeHint = QSize();
- this->update();
- this->updateGeometry();
- emit colorNameChanged(d->ColorName);
- }
- //-----------------------------------------------------------------------------
- QString ctkColorPickerButton::colorName()const
- {
- Q_D(const ctkColorPickerButton);
- return d->ColorName;
- }
- //-----------------------------------------------------------------------------
- void ctkColorPickerButton::paintEvent(QPaintEvent *)
- {
- Q_D(ctkColorPickerButton);
- QStylePainter p(this);
- QStyleOptionButton option;
- this->initStyleOption(&option);
- option.text = d->text();
- 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;
- }
- // If no text, the sizehint is a QToolButton sizeHint
- QStyleOptionButton pushButtonOpt;
- this->initStyleOption(&pushButtonOpt);
- pushButtonOpt.text = d->text();
- int iconSize = this->style()->pixelMetric(QStyle::PM_SmallIconSize);
- if (pushButtonOpt.text == QString())
- {
- QStyleOptionToolButton opt;
- (&opt)->QStyleOption::operator=(pushButtonOpt);
- opt.arrowType = Qt::NoArrow;
- opt.icon = d->Icon;
- 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());
- }
- else
- {
- pushButtonOpt.icon = d->Icon;
- pushButtonOpt.iconSize = QSize(iconSize, iconSize);
- pushButtonOpt.rect.setSize(pushButtonOpt.iconSize); // PM_MenuButtonIndicator depends on the height
- d->CachedSizeHint = (style()->sizeFromContents(
- QStyle::CT_PushButton, &pushButtonOpt, pushButtonOpt.iconSize, this).
- expandedTo(QApplication::globalStrut()));
- }
- return d->CachedSizeHint;
- }
|