-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappsettings.cpp
More file actions
113 lines (105 loc) · 4.18 KB
/
appsettings.cpp
File metadata and controls
113 lines (105 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/////////////////////////////////////////////////////////////////////////////
// Name: appsettings.cpp
// Purpose: Mangement of application settings (singleton)
// Author: Jan Buchholz
// Created: 2025-11-12
/////////////////////////////////////////////////////////////////////////////
#include "appsettings.h"
#include <wx/stdpaths.h>
#include <wx/window.h>
#include "json.hpp"
#include "definitions.h"
#include "preferencesdialog.h"
#include <fstream>
#include "jbconversion.hpp"
#include "mainwindow.h"
using json = nlohmann::json;
AppSettings& AppSettings::instance(MainWindow* mainWin) {
static AppSettings settings(mainWin);
return settings;
}
// private constructor with optional mainWindow
AppSettings::AppSettings(MainWindow* mainWin) : m_mainWindow(mainWin) {
m_prefs = {};
m_prefs.extensions = {
{".h", true},
{".hpp", true},
{".hxx", false},
{".c", true},
{".cpp", true},
{".cxx", false}
};
m_prefsPath = toStdString(wxStandardPaths::Get().GetUserConfigDir());
}
void AppSettings::savePreferences() {
const auto pos = m_mainWindow->GetPosition();
const auto size = m_mainWindow->GetSize();
const auto sashpos = m_mainWindow->m_splitter->GetSashPosition();
m_prefs.coordinates = {pos.x, pos.y, size.x, size.y, m_mainWindow->IsMaximized(), sashpos};
const std::string fullPath = m_prefsPath + getPathSeparator() + APP_VENDOR + "." + APP_NAME + FILE_EXT_JSON;
json j;
j[CONFIG_WINDOW]["x"] = m_prefs.coordinates.pos_x;
j[CONFIG_WINDOW]["y"] = m_prefs.coordinates.pos_y;
j[CONFIG_WINDOW]["w"] = m_prefs.coordinates.size_w;
j[CONFIG_WINDOW]["h"] = m_prefs.coordinates.size_h;
j[CONFIG_WINDOW]["m"] = m_prefs.coordinates.maximized;
j[CONFIG_WINDOW]["s"] = m_prefs.coordinates.sashpos;
j[CONFIG_COMMON][CONFIG_COMMON_EXCLUSION] = m_prefs.exclude_folders;
for (const auto& ex : m_prefs.extensions) {
j[CONFIG_COMMON][CONFIG_COMMON_EXTENSION + ex.first] = ex.second;
}
std::ofstream ofs(fullPath);
if (ofs.is_open()) {
ofs << std::setw(4) << j << std::endl;
ofs.close();
}
}
void AppSettings::loadPreferences() {
const std::string fullPath = m_prefsPath + getPathSeparator() + APP_VENDOR + "." + APP_NAME + FILE_EXT_JSON;
std::ifstream ifs(fullPath);
if (ifs.is_open()) {
try {
json j = json::parse(ifs);
ifs.close();
m_prefs.exclude_folders = j[CONFIG_COMMON][CONFIG_COMMON_EXCLUSION];
m_prefs.coordinates.pos_x = j[CONFIG_WINDOW]["x"];
m_prefs.coordinates.pos_y = j[CONFIG_WINDOW]["y"];
m_prefs.coordinates.size_w = j[CONFIG_WINDOW]["w"];
m_prefs.coordinates.size_h = j[CONFIG_WINDOW]["h"];
m_prefs.coordinates.maximized = j[CONFIG_WINDOW]["m"];
m_prefs.coordinates.sashpos = j[CONFIG_WINDOW]["s"];
for (auto& ex : m_prefs.extensions) {
ex.second = j[CONFIG_COMMON][CONFIG_COMMON_EXTENSION + ex.first];
}
}
catch (json::parse_error&) {}
catch (json::type_error&) {}
catch (json::exception&) {}
}
if (m_prefs.coordinates.size_h == 0 || m_prefs.coordinates.size_w == 0) {
m_prefs.coordinates.size_h = c_windowHeight;
m_prefs.coordinates.size_w = c_windowWidth;
}
if (m_prefs.coordinates.pos_x < 0 || m_prefs.coordinates.pos_y <= 0) {
m_prefs.coordinates.pos_x = c_windowPosX;
m_prefs.coordinates.pos_y = c_windowsPosY;
}
if (m_prefs.coordinates.sashpos <= 0) {
m_prefs.coordinates.sashpos = c_sashPos;
}
const wxPoint pt(m_prefs.coordinates.pos_x, m_prefs.coordinates.pos_y);
m_mainWindow->Move(pt);
m_mainWindow->SetSize(wxSize(m_prefs.coordinates.size_w, m_prefs.coordinates.size_h));
m_mainWindow->Maximize(m_prefs.coordinates.maximized);
m_mainWindow->m_splitter->SetSashPosition(m_prefs.coordinates.sashpos);
}
void AppSettings::openPreferencesDialog() {
auto *dlg = new PreferencesDialog(m_mainWindow);
if (dlg->showDialog(m_prefs) == wxID_OK) {
m_prefs = dlg->getPreferences();
};
delete dlg;
}
mo_app_settings AppSettings::getAppSettings() {
return m_prefs;
}