Preference Packs: Add revert to backup option

This commit is contained in:
Chris Hennes
2022-02-10 23:07:05 -06:00
parent a21b99fed2
commit a7e356c865
12 changed files with 6733 additions and 6 deletions

View File

@@ -328,6 +328,7 @@ SET(Gui_UIC_SRCS
DlgProjectUtility.ui
DlgPropertyLink.ui
DlgReportView.ui
DlgRevertToBackupConfig.ui
DlgSettings3DView.ui
DlgSettingsCacheDirectory.ui
DlgSettingsNavigation.ui
@@ -423,6 +424,7 @@ SET(Dialog_CPP_SRCS
DlgProjectInformationImp.cpp
DlgProjectUtility.cpp
DlgPropertyLink.cpp
DlgRevertToBackupConfigImp.cpp
DlgExpressionInput.cpp
TaskDlgRelocation.cpp
DlgCheckableMessageBox.cpp
@@ -462,6 +464,7 @@ SET(Dialog_HPP_SRCS
DlgProjectInformationImp.h
DlgProjectUtility.h
DlgPropertyLink.h
DlgRevertToBackupConfigImp.h
DlgCheckableMessageBox.h
DlgExpressionInput.h
TaskDlgRelocation.h
@@ -506,6 +509,7 @@ SET(Dialog_SRCS
DlgProjectInformation.ui
DlgProjectUtility.ui
DlgPropertyLink.ui
DlgRevertToBackupConfig.ui
DlgCheckableMessageBox.ui
DlgTreeWidget.ui
DlgExpressionInput.ui

View File

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>425</width>
<height>598</height>
<width>660</width>
<height>930</height>
</rect>
</property>
<property name="windowTitle">
@@ -239,6 +239,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="RevertToSavedConfig">
<property name="text">
<string>Revert...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
@@ -342,7 +349,7 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<item>
<widget class="QCheckBox" name="tiledBackground">
<property name="toolTip">
<string>Background of the main window will consist of tiles of a special image.
@@ -353,7 +360,7 @@ See the FreeCAD Wiki for details about the image.</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item>
<widget class="Gui::PrefCheckBox" name="EnableCursorBlinking">
<property name="toolTip">
<string>The text cursor will be blinking</string>

View File

@@ -45,6 +45,7 @@
#include "DlgCreateNewPreferencePackImp.h"
#include "DlgPreferencePackManagementImp.h"
#include "DlgRevertToBackupConfigImp.h"
using namespace Gui::Dialog;
@@ -96,6 +97,14 @@ DlgGeneralImp::DlgGeneralImp( QWidget* parent )
ui->ManagePreferencePacks->setToolTip(tr("Manage preference packs"));
connect(ui->ManagePreferencePacks, &QPushButton::clicked, this, &DlgGeneralImp::onManagePreferencePacksClicked);
// If there are any saved config file backs, show the revert button, otherwise hide it:
const auto & backups = Application::Instance->prefPackManager()->configBackups();
if (backups.empty())
ui->RevertToSavedConfig->setEnabled(false);
else
ui->RevertToSavedConfig->setEnabled(true);
connect(ui->RevertToSavedConfig, &QPushButton::clicked, this, &DlgGeneralImp::revertToSavedConfig);
}
/**
@@ -379,6 +388,17 @@ void DlgGeneralImp::saveAsNewPreferencePack()
newPreferencePackDialog->open();
}
void DlgGeneralImp::revertToSavedConfig()
{
revertToBackupConfigDialog = std::make_unique<DlgRevertToBackupConfigImp>(this);
connect(revertToBackupConfigDialog.get(), &DlgRevertToBackupConfigImp::accepted, [this]() {
auto parentDialog = qobject_cast<DlgPreferencesImp*> (this->window());
if (parentDialog)
parentDialog->reload();
});
revertToBackupConfigDialog->open();
}
void DlgGeneralImp::newPreferencePackDialogAccepted()
{
auto preferencePackTemplates = Application::Instance->prefPackManager()->templateFiles();

View File

@@ -34,6 +34,7 @@ namespace Dialog {
class Ui_DlgGeneral;
class DlgCreateNewPreferencePackImp;
class DlgPreferencePackManagementImp;
class DlgRevertToBackupConfigImp;
/** This class implements the settings for the application.
* You can change window style, size of pixmaps, size of recent file list and so on
@@ -62,11 +63,13 @@ protected Q_SLOTS:
private:
void setRecentFileSize();
void saveAsNewPreferencePack();
void revertToSavedConfig();
private:
std::unique_ptr<Ui_DlgGeneral> ui;
std::unique_ptr<DlgCreateNewPreferencePackImp> newPreferencePackDialog;
std::unique_ptr<DlgPreferencePackManagementImp> preferencePackManagementDialog;
std::unique_ptr<DlgRevertToBackupConfigImp> revertToBackupConfigDialog;
};
} // namespace Dialog

View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Gui::Dialog::DlgRevertToBackupConfig</class>
<widget class="QDialog" name="Gui::Dialog::DlgRevertToBackupConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>610</width>
<height>471</height>
</rect>
</property>
<property name="windowTitle">
<string>Revert to Backup Config</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>WARNING: this process will undo any preference changes made since the specified date, and will also reset your Recent files and Macros to their state on that date.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Available backup files:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Gui::Dialog::DlgRevertToBackupConfig</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>304</x>
<y>450</y>
</hint>
<hint type="destinationlabel">
<x>304</x>
<y>235</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Gui::Dialog::DlgRevertToBackupConfig</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>304</x>
<y>450</y>
</hint>
<hint type="destinationlabel">
<x>304</x>
<y>235</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,117 @@
/***************************************************************************
* Copyright (c) 2022 Chris Hennes <chennes@pioneerlibrarysystem.org> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QMessageBox>
# include <QPushButton>
# include <QDateTime>
#endif
#include "DlgRevertToBackupConfigImp.h"
#include "ui_DlgRevertToBackupConfig.h"
#include <Gui/Application.h>
#include <Gui/PreferencePackManager.h>
#include <boost/filesystem.hpp>
using namespace Gui;
using namespace Gui::Dialog;
namespace fs = boost::filesystem;
/* TRANSLATOR Gui::Dialog::DlgRevertToBackupConfigImp */
DlgRevertToBackupConfigImp::DlgRevertToBackupConfigImp(QWidget* parent)
: QDialog(parent)
, ui(new Ui_DlgRevertToBackupConfig)
{
ui->setupUi(this);
connect(ui->listWidget, &QListWidget::itemSelectionChanged, this, &DlgRevertToBackupConfigImp::onItemSelectionChanged);
}
DlgRevertToBackupConfigImp::~DlgRevertToBackupConfigImp()
{
}
void Gui::Dialog::DlgRevertToBackupConfigImp::onItemSelectionChanged()
{
auto items = ui->listWidget->selectedItems();
if (items.count() == 1)
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(true);
else
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false);
}
/**
* Sets the strings of the subwidgets using the current language.
*/
void DlgRevertToBackupConfigImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
}
else {
QWidget::changeEvent(e);
}
}
void DlgRevertToBackupConfigImp::showEvent(QShowEvent* event)
{
ui->listWidget->clear();
const auto& backups = Application::Instance->prefPackManager()->configBackups();
for (const auto& backup : backups) {
auto filename = backup.filename().string();
auto modification_date = QDateTime::fromTime_t(fs::last_write_time(backup));
auto item = new QListWidgetItem(QLocale().toString(modification_date));
item->setData(Qt::UserRole, QString::fromStdString(backup.string()));
ui->listWidget->addItem(item);
}
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false);
QDialog::showEvent(event);
}
void DlgRevertToBackupConfigImp::accept()
{
auto items = ui->listWidget->selectedItems();
if (items.count() != 1) {
Base::Console().Error(tr("No selection in dialog, cannot load backup file").toStdString().c_str());
return;
}
auto item = items[0];
auto path = item->data(Qt::UserRole).toString().toStdString();
if (fs::exists(path)) {
ParameterManager newParameters;
newParameters.LoadDocument(path.c_str());
auto baseAppGroup = App::GetApplication().GetUserParameter().GetGroup("BaseApp");
newParameters.GetGroup("BaseApp")->copyTo(baseAppGroup);
}
else {
Base::Console().Error("Preference Pack Internal Error: Invalid backup file location");
}
QDialog::accept();
}
#include "moc_DlgRevertToBackupConfigImp.cpp"

View File

@@ -0,0 +1,60 @@
/***************************************************************************
* Copyright (c) 2022 Chris Hennes <chennes@pioneerlibrarysystem.org> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef GUI_DIALOG_DLG_REVERT_TO_BACKUP_CONFIG_IMP
#define GUI_DIALOG_DLG_REVERT_TO_BACKUP_CONFIG_IMP
#include <memory>
#include <QDialog>
namespace Gui {
namespace Dialog {
class Ui_DlgRevertToBackupConfig;
/** The DlgRevertToBackupConfigImp class
* \author Chris Hennes
*/
class DlgRevertToBackupConfigImp : public QDialog
{
Q_OBJECT
public:
DlgRevertToBackupConfigImp( QWidget* parent = 0 );
~DlgRevertToBackupConfigImp();
public Q_SLOTS:
void accept() override;
void onItemSelectionChanged();
protected:
void changeEvent(QEvent *e);
void showEvent(QShowEvent* event) override;
private:
std::unique_ptr<Ui_DlgRevertToBackupConfig> ui;
};
} // namespace Dialog
} // namespace Gui
#endif //GUI_DIALOG_DLG_REVERT_TO_BACKUP_CONFIG_IMP

View File

@@ -130,8 +130,6 @@ void PreferencePack::applyConfigChanges() const
}
}
PreferencePackManager::PreferencePackManager()
{
auto modPath = fs::path(App::Application::getUserAppDataDir()) / "Mod";
@@ -526,4 +524,16 @@ void Gui::PreferencePackManager::DeleteOldBackups() const
}
}
}
}
std::vector<boost::filesystem::path> Gui::PreferencePackManager::configBackups() const
{
std::vector<boost::filesystem::path> results;
auto backupDirectory = fs::path(App::Application::getUserAppDataDir()) / "SavedPreferencePacks" / "Backups";
if (fs::exists(backupDirectory) && fs::is_directory(backupDirectory)) {
for (const auto& backup : fs::directory_iterator(backupDirectory)) {
results.push_back(backup);
}
}
return results;
}

View File

@@ -184,6 +184,11 @@ namespace Gui {
std::vector<TemplateFile> templateFiles(bool rescan = false);
/**
* Get a list of all available config file backups. Backups are currently stored for one week.
*/
std::vector<boost::filesystem::path> configBackups() const;
private:
void FindPreferencePacksInPackage(const boost::filesystem::path& mod);