diff --git a/cmake/qet_compilation_vars.cmake b/cmake/qet_compilation_vars.cmake index eab3ba0a7..9297115f7 100644 --- a/cmake/qet_compilation_vars.cmake +++ b/cmake/qet_compilation_vars.cmake @@ -865,6 +865,8 @@ set(QET_SRC_FILES ${QET_DIR}/sources/utils/conductorcreator.cpp ${QET_DIR}/sources/utils/conductorcreator.h + ${QET_DIR}/sources/utils/colordialogdoubleclick.cpp + ${QET_DIR}/sources/utils/colordialogdoubleclick.h ${QET_DIR}/sources/utils/macosxopenevent.cpp ${QET_DIR}/sources/utils/macosxopenevent.h ${QET_DIR}/sources/utils/qetsettings.cpp diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index d74e524b4..c7abe1f33 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -18,6 +18,7 @@ #include "diagramview.h" #include "cellruler.h" #include "lastusedstyle.h" +#include "utils/colordialogdoubleclick.h" #include "qetproject.h" #include "QPropertyUndoCommand/qpropertyundocommand.h" #include "diagramcommands.h" @@ -176,6 +177,7 @@ DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : color_dialog -> setWindowFlags(Qt::Sheet); #endif color_dialog->setCurrentColor(initial_properties.color); + ColorDialogDoubleClick::install(color_dialog); // asks the user what color he wishes to apply if (color_dialog->exec() == QDialog::Accepted) diff --git a/sources/ui/conductorcolortoolbutton.cpp b/sources/ui/conductorcolortoolbutton.cpp index e9b91dc08..f85ece30c 100644 --- a/sources/ui/conductorcolortoolbutton.cpp +++ b/sources/ui/conductorcolortoolbutton.cpp @@ -26,6 +26,7 @@ #include "../projectview.h" #include "../qetgraphicsitem/conductor.h" #include "../conductorproperties.h" +#include "../utils/colordialogdoubleclick.h" #include #include @@ -217,10 +218,11 @@ void ConductorColorToolButton::applyColor(const QColor &color) */ void ConductorColorToolButton::chooseOtherColor() { - const QColor c = QColorDialog::getColor(m_current, this, - tr("Choisir une couleur de conducteur")); - if (c.isValid()) { - applyColor(c); + QColorDialog dialog(m_current, this); + dialog.setWindowTitle(tr("Choisir une couleur de conducteur")); + ColorDialogDoubleClick::install(&dialog); + if (dialog.exec() == QDialog::Accepted && dialog.selectedColor().isValid()) { + applyColor(dialog.selectedColor()); } } diff --git a/sources/utils/colordialogdoubleclick.cpp b/sources/utils/colordialogdoubleclick.cpp new file mode 100644 index 000000000..3cde3a591 --- /dev/null +++ b/sources/utils/colordialogdoubleclick.cpp @@ -0,0 +1,77 @@ +/* + Copyright 2006-2026 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "colordialogdoubleclick.h" + +#include +#include +#include +#include + +/** + @brief ColorDialogDoubleClick::install + Watch the swatches of @a dialog. The filter is a child of the dialog + and is deleted with it. + @param dialog +*/ +void ColorDialogDoubleClick::install(QColorDialog *dialog) +{ + if (!dialog) { + return; + } + + //The swatches are looked for when the dialog is shown: Qt builds + //them only then when the platform offered a native dialog it + //could not show after all. + dialog->installEventFilter(new ColorDialogDoubleClick(dialog)); +} + +ColorDialogDoubleClick::ColorDialogDoubleClick(QColorDialog *dialog) : + QObject(dialog), + m_dialog(dialog) +{} + +/** + @brief ColorDialogDoubleClick::eventFilter + The first click of a double-click has already selected the swatch + (on its release), so the dialog's current colour is the one + double-clicked: accept it. Queued, so the swatch finishes handling + the second click before the dialog closes. +*/ +bool ColorDialogDoubleClick::eventFilter(QObject *watched, QEvent *event) +{ + if (watched == m_dialog && event->type() == QEvent::Show) { + //QWellArray is Qt's private class behind both swatch grids; + //its name is the only handle on them (in the QtPrivate + //namespace in recent Qt 6). If a Qt release renames it, + //nothing matches and double-click just selects, as before. + //Installing twice only moves the filter to the front. + const auto children = m_dialog->findChildren(); + for (QWidget *child : children) { + if (child->inherits("QtPrivate::QWellArray") + || child->inherits("QWellArray")) { + child->installEventFilter(this); + } + } + } + else if (event->type() == QEvent::MouseButtonDblClick + && static_cast(event)->button() == Qt::LeftButton) + { + QMetaObject::invokeMethod(m_dialog, &QDialog::accept, Qt::QueuedConnection); + } + return QObject::eventFilter(watched, event); +} diff --git a/sources/utils/colordialogdoubleclick.h b/sources/utils/colordialogdoubleclick.h new file mode 100644 index 000000000..62f9ea40a --- /dev/null +++ b/sources/utils/colordialogdoubleclick.h @@ -0,0 +1,50 @@ +/* + Copyright 2006-2026 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef COLOR_DIALOG_DOUBLE_CLICK_H +#define COLOR_DIALOG_DOUBLE_CLICK_H + +#include + +class QColorDialog; + +/** + @brief The ColorDialogDoubleClick class + Lets a double-click on one of a QColorDialog's colour swatches (the + basic colours and the custom colours) choose that colour and close + the dialog, as OK does. Qt's dialog only selects the swatch. + + Only Qt's own dialog has swatches to watch. Where the platform shows + its native dialog instead (macOS, some Linux desktops), this does + nothing and the native dialog behaves as it always has. +*/ +class ColorDialogDoubleClick : public QObject +{ + Q_OBJECT + + public: + static void install(QColorDialog *dialog); + + protected: + bool eventFilter(QObject *watched, QEvent *event) override; + + private: + explicit ColorDialogDoubleClick(QColorDialog *dialog); + QColorDialog *m_dialog; +}; + +#endif // COLOR_DIALOG_DOUBLE_CLICK_H