Choose a conductor colour by double-clicking it

In the F2 conductor colour dialog and the toolbar's "Other colour..."
dialog, a double-click on a basic or custom colour swatch now chooses it
and closes the dialog, as OK does (forum topic 3190). Qt's dialog only
selects the swatch on a double-click.

ColorDialogDoubleClick filters the dialog's QWellArray swatch grids,
found by class name once the dialog is shown, because Qt builds them
only then when a native dialog was offered but not shown. Where the
platform's native colour dialog is used instead (macOS, some Linux
desktops) there are no swatches to watch and nothing changes. The
toolbar dialog is now an instance rather than QColorDialog::getColor(),
so the filter can be installed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-09-30 13:47:45 +13:00
parent 63196bf4ac
commit 8b82e348f2
5 changed files with 137 additions and 4 deletions
+2
View File
@@ -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
+2
View File
@@ -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)
+6 -4
View File
@@ -26,6 +26,7 @@
#include "../projectview.h"
#include "../qetgraphicsitem/conductor.h"
#include "../conductorproperties.h"
#include "../utils/colordialogdoubleclick.h"
#include <QColorDialog>
#include <QMenu>
@@ -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());
}
}
+77
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "colordialogdoubleclick.h"
#include <QColorDialog>
#include <QEvent>
#include <QMouseEvent>
#include <QWidget>
/**
@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<QWidget *>();
for (QWidget *child : children) {
if (child->inherits("QtPrivate::QWellArray")
|| child->inherits("QWellArray")) {
child->installEventFilter(this);
}
}
}
else if (event->type() == QEvent::MouseButtonDblClick
&& static_cast<QMouseEvent *>(event)->button() == Qt::LeftButton)
{
QMetaObject::invokeMethod(m_dialog, &QDialog::accept, Qt::QueuedConnection);
}
return QObject::eventFilter(watched, event);
}
+50
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef COLOR_DIALOG_DOUBLE_CLICK_H
#define COLOR_DIALOG_DOUBLE_CLICK_H
#include <QObject>
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