mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-04 09:54:13 +02:00
Add saving and loading the configuration to a file (discussion #610)
Revives #640, closed only to clear a review backlog. Two entries in the Configuration menu: "Enregistrer la configuration sous..." saves the settings to a .conf file, "Charger une configuration..." replaces them with one. The file is always ini format, so a profile made on one platform loads on the others. Changes from #640: - Window sizes, positions and dock layouts (".../geometry", ".../state") and the recent-files lists stay out of a profile and are kept on load: they describe this computer, not a preference. - A saved file carries a format marker; loading refuses files that QElectroTech did not save. - Loading closes QElectroTech and asks the user to start it again, instead of starting a second copy: a second copy started while this one is still running hands its arguments over and exits (main.cpp). - The colour dialog's custom colours are reloaded from the profile before quitting, since ~QETApp() saves them over the loaded ones. - Both actions are registered with ShortcutManager, so they appear in command search and can be given a shortcut. - The copy rules live in the header-only utils/configprofile.h and are unit tested (tst_configprofile). Loading still replaces rather than merges: a setting the profile does not have is removed, or switching profiles would keep settings from the previous one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include "qetpalette.h"
|
||||
#include "qetstyle.h"
|
||||
#include "utils/qetutils.h"
|
||||
#include "utils/configprofile.h"
|
||||
#include "qetmessagebox.h"
|
||||
#include "qetproject.h"
|
||||
#include "qtextorientationspinboxwidget.h"
|
||||
@@ -55,9 +56,11 @@
|
||||
#include <iostream>
|
||||
#define QUOTE(x) STRINGIFY(x)
|
||||
#define STRINGIFY(x) #x
|
||||
#include <QFileDialog>
|
||||
#include <QFontDatabase>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QRegularExpression>
|
||||
#include <QSettings>
|
||||
#include <QStyleFactory>
|
||||
#include <QStyleHints>
|
||||
#ifdef BUILD_WITHOUT_KF
|
||||
@@ -2242,6 +2245,109 @@ void QETApp::configureQET()
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QETApp::exportConfiguration
|
||||
Save the settings of QElectroTech to a file the user chooses, to keep
|
||||
them as a named profile or copy them to another computer (discussion
|
||||
#610). The file is always written in the ini format, whatever the
|
||||
platform stores its live settings in, so a profile saved on Windows
|
||||
loads on Linux and macOS. See ConfigProfile for the keys left out.
|
||||
*/
|
||||
void QETApp::exportConfiguration()
|
||||
{
|
||||
QWidget *parent_widget = qApp->activeWindow();
|
||||
|
||||
QString path = QFileDialog::getSaveFileName(
|
||||
parent_widget,
|
||||
tr("Enregistrer la configuration sous...", "dialog title"),
|
||||
QString(),
|
||||
tr("Configurations QElectroTech (*.conf)", "file dialog filter"));
|
||||
if (path.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!path.endsWith(QLatin1String(".conf"), Qt::CaseInsensitive)) {
|
||||
path += QLatin1String(".conf");
|
||||
}
|
||||
|
||||
QSettings live_settings;
|
||||
QSettings file_settings(path, QSettings::IniFormat);
|
||||
ConfigProfile::exportTo(live_settings, file_settings);
|
||||
|
||||
if (file_settings.status() != QSettings::NoError) {
|
||||
QET::QetMessageBox::critical(
|
||||
parent_widget,
|
||||
tr("Erreur", "message box title"),
|
||||
tr("Impossible d'enregistrer la configuration dans « %1 ».").arg(path));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QETApp::importConfiguration
|
||||
Replace the settings of QElectroTech with a file saved by
|
||||
exportConfiguration(), then close QElectroTech.
|
||||
|
||||
The settings are read by each part of QElectroTech when it starts, and
|
||||
there is no signal telling all of them that a setting changed, so the
|
||||
new settings are applied by starting QElectroTech again. That restart
|
||||
is left to the user: a second copy started from here would find this
|
||||
one still running, hand its arguments over to it and exit (main.cpp).
|
||||
*/
|
||||
void QETApp::importConfiguration()
|
||||
{
|
||||
QWidget *parent_widget = qApp->activeWindow();
|
||||
|
||||
const QString path = QFileDialog::getOpenFileName(
|
||||
parent_widget,
|
||||
tr("Charger une configuration...", "dialog title"),
|
||||
QString(),
|
||||
tr("Configurations QElectroTech (*.conf)", "file dialog filter"));
|
||||
if (path.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings file_settings(path, QSettings::IniFormat);
|
||||
if (file_settings.status() != QSettings::NoError
|
||||
|| !ConfigProfile::isProfile(file_settings))
|
||||
{
|
||||
QET::QetMessageBox::critical(
|
||||
parent_widget,
|
||||
tr("Erreur", "message box title"),
|
||||
tr("« %1 » n'est pas une configuration enregistrée par QElectroTech.").arg(path));
|
||||
return;
|
||||
}
|
||||
|
||||
//Said before anything closes: once the last window is closed,
|
||||
//QElectroTech quits by itself (checkRemainingWindows()).
|
||||
const auto answer = QET::QetMessageBox::question(
|
||||
parent_widget,
|
||||
tr("Charger une configuration", "message box title"),
|
||||
tr("Cette configuration va remplacer vos réglages actuels, sauf la "
|
||||
"disposition des fenêtres et la liste des fichiers récents.\n\n"
|
||||
"QElectroTech va ensuite se fermer. Relancez-le pour utiliser "
|
||||
"la nouvelle configuration.\n\n"
|
||||
"Voulez-vous continuer ?"),
|
||||
QMessageBox::Yes | QMessageBox::No,
|
||||
QMessageBox::No);
|
||||
if (answer != QMessageBox::Yes) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Asks to save any modified project, as quitQET() does. The settings
|
||||
//are only replaced once every editor is closed, so that the editors
|
||||
//saving their own settings on close cannot overwrite them, and a
|
||||
//cancelled close leaves them untouched.
|
||||
if (!closeEveryEditor()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings live_settings;
|
||||
ConfigProfile::importFrom(file_settings, live_settings);
|
||||
//~QETApp() saves the colour dialog's custom colours on exit: load
|
||||
//the profile's into it first, or the old ones overwrite them
|
||||
QET::loadCustomColors();
|
||||
qApp->quit();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QETApp::aboutQET
|
||||
Open the dialog about qet.
|
||||
|
||||
Reference in New Issue
Block a user