Gui: create modal color dialog on the heap if its parent widget is used in the property editor

For more details see: https://forum.freecadweb.org/viewtopic.php?f=23&t=70655
This commit is contained in:
wmayer
2022-10-22 21:42:21 +02:00
parent c5a7654285
commit 4c6e123c00
2 changed files with 51 additions and 32 deletions

View File

@@ -760,29 +760,42 @@ void ColorButton::paintEvent (QPaintEvent * e)
QPushButton::paintEvent(e);
}
/**
* Opens a QColorDialog to set a new color.
*/
void ColorButton::onChooseColor()
void ColorButton::showModeless()
{
if (!d->allowChange)
return;
if (d->modal) {
QColor currentColor = d->col;
QColorDialog cd(d->col, this);
if (d->cd.isNull()) {
d->old = d->col;
QColorDialog* dlg = new QColorDialog(d->col, this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
if (DialogOptions::dontUseNativeColorDialog())
cd.setOptions(QColorDialog::DontUseNativeDialog);
cd.setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
dlg->setOptions(QColorDialog::DontUseNativeDialog);
dlg->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
connect(dlg, &QColorDialog::rejected, this, &ColorButton::onRejected);
connect(dlg, &QColorDialog::currentColorChanged, this, &ColorButton::onColorChosen);
d->cd = dlg;
}
d->cd->show();
}
if (d->autoChange) {
connect(&cd, SIGNAL(currentColorChanged(const QColor &)),
this, SLOT(onColorChosen(const QColor&)));
}
void ColorButton::showModal()
{
QColor currentColor = d->col;
QColorDialog* dlg = new QColorDialog(d->col, this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
if (DialogOptions::dontUseNativeColorDialog())
dlg->setOptions(QColorDialog::DontUseNativeDialog);
dlg->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
cd.setCurrentColor(currentColor);
cd.adjustSize();
if (cd.exec() == QDialog::Accepted) {
QColor c = cd.selectedColor();
if (d->autoChange) {
connect(dlg, &QColorDialog::currentColorChanged, this, &ColorButton::onColorChosen);
}
dlg->setCurrentColor(currentColor);
dlg->adjustSize();
connect(dlg, &QColorDialog::finished, this, [&](int result) {
if (result == QDialog::Accepted) {
QColor c = dlg->selectedColor();
if (c.isValid()) {
setColor(c);
Q_EMIT changed();
@@ -792,21 +805,23 @@ void ColorButton::onChooseColor()
setColor(currentColor);
Q_EMIT changed();
}
});
dlg->exec();
}
/**
* Opens a QColorDialog to set a new color.
*/
void ColorButton::onChooseColor()
{
if (!d->allowChange)
return;
if (d->modal) {
showModal();
}
else {
if (d->cd.isNull()) {
d->old = d->col;
d->cd = new QColorDialog(d->col, this);
if (DialogOptions::dontUseNativeColorDialog())
d->cd->setOptions(QColorDialog::DontUseNativeDialog);
d->cd->setOption(QColorDialog::ColorDialogOption::ShowAlphaChannel, d->allowTransparency);
d->cd->setAttribute(Qt::WA_DeleteOnClose);
connect(d->cd, SIGNAL(rejected()),
this, SLOT(onRejected()));
connect(d->cd, SIGNAL(currentColorChanged(const QColor &)),
this, SLOT(onColorChosen(const QColor&)));
}
d->cd->show();
showModeless();
}
}

View File

@@ -267,6 +267,10 @@ Q_SIGNALS:
protected:
void paintEvent (QPaintEvent*) override;
private:
void showModeless();
void showModal();
private:
struct ColorButtonP *d;
};