support to auto change color when color dialog is modal

This commit is contained in:
wmayer 2016-10-02 19:57:01 +02:00
parent c833136cde
commit ddd4c4560c
2 changed files with 32 additions and 3 deletions

View File

@ -517,6 +517,7 @@ struct ColorButtonP
QColor old, col; QColor old, col;
QPointer<QColorDialog> cd; QPointer<QColorDialog> cd;
bool allowChange; bool allowChange;
bool autoChange;
bool drawFrame; bool drawFrame;
bool modal; bool modal;
bool dirty; bool dirty;
@ -524,6 +525,7 @@ struct ColorButtonP
ColorButtonP() ColorButtonP()
: cd(0) : cd(0)
, allowChange(true) , allowChange(true)
, autoChange(false)
, drawFrame(true) , drawFrame(true)
, modal(true) , modal(true)
, dirty(true) , dirty(true)
@ -604,6 +606,16 @@ bool ColorButton::isModal() const
return d->modal; return d->modal;
} }
void ColorButton::setAutoChangeColor(bool on)
{
d->autoChange = on;
}
bool ColorButton::autoChangeColor() const
{
return d->autoChange;
}
/** /**
* Draws the button label. * Draws the button label.
*/ */
@ -675,9 +687,23 @@ void ColorButton::onChooseColor()
#if QT_VERSION >= 0x040500 #if QT_VERSION >= 0x040500
if (d->modal) { if (d->modal) {
#endif #endif
QColor c = QColorDialog::getColor(d->col, this); QColor currentColor = d->col;
if (c.isValid()) { QColorDialog cd(d->col, this);
setColor(c);
if (d->autoChange) {
connect(&cd, SIGNAL(currentColorChanged(const QColor &)),
this, SLOT(onColorChosen(const QColor&)));
}
if (cd.exec() == QDialog::Accepted) {
QColor c = cd.selectedColor();
if (c.isValid()) {
setColor(c);
changed();
}
}
else if (d->autoChange) {
setColor(currentColor);
changed(); changed();
} }
#if QT_VERSION >= 0x040500 #if QT_VERSION >= 0x040500

View File

@ -191,6 +191,9 @@ public:
void setModal(bool); void setModal(bool);
bool isModal() const; bool isModal() const;
void setAutoChangeColor(bool);
bool autoChangeColor() const;
public Q_SLOTS: public Q_SLOTS:
void onChooseColor(); void onChooseColor();