mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-09 21:54:13 +02:00
Persist QColorDialog custom colors across application restarts
Qt's QColorDialog loads custom colors from QSettings on startup but never writes them back, so user-defined colors in the color picker are lost when the application exits. Save and load the 16 custom color slots explicitly via QSettings in QETApp's constructor (after initStyle) and destructor (before other settings are flushed). This covers every QColorDialog usage in the application transparently. Also fixes a QColorDialog memory leak in DiagramView.
This commit is contained in:
@@ -40,6 +40,7 @@
|
||||
#include "NameList/nameslist.h"
|
||||
#include "elementdialog.h"
|
||||
#include <QDropEvent>
|
||||
#include <QPointer>
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@@ -122,7 +123,7 @@ DiagramView::DiagramView(Diagram *diagram, QWidget *parent) :
|
||||
ConductorProperties initial_properties = edited_conductor->properties();
|
||||
|
||||
// prepare a color dialog showing the initial conductor color
|
||||
QColorDialog *color_dialog = new QColorDialog(this);
|
||||
QPointer<QColorDialog> color_dialog = new QColorDialog(this);
|
||||
color_dialog->setWindowTitle(tr("Choisir la nouvelle couleur de ce conducteur"));
|
||||
#ifdef Q_OS_MACOS
|
||||
color_dialog -> setWindowFlags(Qt::Sheet);
|
||||
@@ -150,6 +151,8 @@ DiagramView::DiagramView(Diagram *diagram, QWidget *parent) :
|
||||
LastUsedStyle::setConductorColor(new_color);
|
||||
}
|
||||
}
|
||||
if (color_dialog)
|
||||
delete color_dialog;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,13 @@
|
||||
#include "shortcutmanager.h"
|
||||
|
||||
#include <limits>
|
||||
#include <QBuffer>
|
||||
#include <QColorDialog>
|
||||
#include <QGraphicsSceneContextMenuEvent>
|
||||
#include <QAction>
|
||||
#include <QFileInfo>
|
||||
#include <QSaveFile>
|
||||
#include <QSettings>
|
||||
#include <QTextStream>
|
||||
#include <QRegularExpression>
|
||||
#include <QActionGroup>
|
||||
@@ -845,3 +848,55 @@ bool QET::writeToFile(QDomDocument &xml_doc, QFile *file, QString *error_message
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QET::saveCustomColors
|
||||
Save the 16 QColorDialog custom colors to QSettings so they persist
|
||||
across application restarts.
|
||||
*/
|
||||
void QET::saveCustomColors()
|
||||
{
|
||||
QByteArray ba;
|
||||
QBuffer buf(&ba);
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
QDataStream s(&buf);
|
||||
s.setVersion(QDataStream::Qt_6_0);
|
||||
for (int i = 0; i < 16; i++)
|
||||
s << QColorDialog::customColor(i);
|
||||
QSettings settings;
|
||||
settings.setValue(QStringLiteral("color/customColors"), ba);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QET::loadCustomColors
|
||||
Load the 16 QColorDialog custom colors from QSettings into Qt's
|
||||
internal custom color array. A short or corrupt buffer is ignored
|
||||
so that unread slots keep their default rather than turning black.
|
||||
*/
|
||||
void QET::loadCustomColors()
|
||||
{
|
||||
QSettings settings;
|
||||
QByteArray ba = settings.value(QStringLiteral("color/customColors")).toByteArray();
|
||||
|
||||
// Fall back to the legacy ungrouped key used by earlier versions.
|
||||
if (ba.isEmpty())
|
||||
ba = settings.value(QStringLiteral("customColors")).toByteArray();
|
||||
|
||||
if (ba.isEmpty())
|
||||
return;
|
||||
|
||||
QBuffer buf(&ba);
|
||||
buf.open(QIODevice::ReadOnly);
|
||||
QDataStream s(&buf);
|
||||
s.setVersion(QDataStream::Qt_6_0);
|
||||
|
||||
QColor colors[16];
|
||||
for (int i = 0; i < 16; i++)
|
||||
s >> colors[i];
|
||||
|
||||
if (s.status() != QDataStream::Ok)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
QColorDialog::setCustomColor(i, colors[i]);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#ifndef _QET_H
|
||||
#define _QET_H
|
||||
|
||||
#include <QColor>
|
||||
#include <QDomElement>
|
||||
#include <QFile>
|
||||
#include <QObject>
|
||||
@@ -185,6 +186,8 @@ namespace QET {
|
||||
bool writeToFile (QDomDocument &xml_doc, QFile *file, QString *error_message = nullptr);
|
||||
bool eachStrIsEqual (const QStringList &qsl);
|
||||
QActionGroup *depthActionGroup(QObject *parent = nullptr);
|
||||
void saveCustomColors();
|
||||
void loadCustomColors();
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(QET::DepthOption)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "qetapp.h"
|
||||
|
||||
#include "configdialog.h"
|
||||
#include "qet.h"
|
||||
#include "ui/configpage/configpages.h"
|
||||
#include "editor/ui/qetelementeditor.h"
|
||||
#include "elementscollectioncache.h"
|
||||
@@ -124,6 +125,7 @@ QETApp::QETApp() :
|
||||
QET::Icons::initIcons();
|
||||
initFonts();
|
||||
initStyle();
|
||||
QET::loadCustomColors();
|
||||
initSplashScreen();
|
||||
initSystemTray();
|
||||
|
||||
@@ -168,6 +170,7 @@ QETApp::QETApp() :
|
||||
*/
|
||||
QETApp::~QETApp()
|
||||
{
|
||||
QET::saveCustomColors();
|
||||
m_elements_recent_files->save();
|
||||
m_projects_recent_files->save();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user