-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferencesdialog.cpp
123 lines (99 loc) · 4.36 KB
/
preferencesdialog.cpp
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
114
115
116
117
118
119
120
121
122
123
#include "preferencesdialog.h"
#include "qdir.h"
#include "ui_preferencesdialog.h"
#include <QToolButton>
#include <QMenu>
#include <QAction>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QStyleFactory>
#include <QSettings>
#include "usersettings.h"
PreferencesDialog::PreferencesDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::PreferencesDialog)
{
ui->setupUi(this);
this->resize(100,100);
setWindowTitle("Preferences");
ui->windowPaletteToolButton->setMenu(new QMenu);
connect(ui->windowPaletteToolButton, &QToolButton::clicked, this, &PreferencesDialog::buildPaletteMenu);
QSettings settings;
ui->windowPaletteLabel->setText(settings.value("window_palette/name", "not set").toString());
bool useHighlightColorSetting = settings.value("window_palette/use_highlight_colors", 1).toBool();
ui->usePaletteHighlightColorCheckbox->setChecked(useHighlightColorSetting);
connect(ui->usePaletteHighlightColorCheckbox, &QCheckBox::stateChanged, this, [this](bool value){
usePaletteHighlightColorCheckboxStatusChanged(value);
});
}
PreferencesDialog::~PreferencesDialog()
{
delete ui;
}
// The key is the palette name, and the value is a QJsonObject representing the colors of the palette
QMap<QString, QJsonObject> palette_files;
void PreferencesDialog::buildPaletteMenu()
{
ui->windowPaletteToolButton->menu()->clear();
// get the list of JSON palette files
QString palettePath = ":/palettes/";
QDir paletteDir = palettePath;
QStringList paletteFiles = paletteDir.entryList(QStringList() << "*.json", QDir::Files);
// having references to the categories encountered and submenus created
// will be helpful when adding palettes to those category submenus
QStringList categories;
QMap<QString, QMenu*> submenus;
// iterate over each JSON file
for (const QString& jsonFile : paletteFiles) {
QJsonDocument doc = readJsonFile(palettePath, jsonFile);
// grab the name and category and whatever other data from these palettes
QJsonObject rootObj = doc.object();
QString name = rootObj.value("name").toString();
QString category = rootObj.value("category").toString();
// this is what we'll pass to setChosenPalette
QJsonObject colors = rootObj.value("colors").toObject();
// ...so that function can use lines like this.
// QString window = colors.value("window").toString();
// build a submenu for the category if it does not already exist
if(!categories.contains(category))
{
categories.append(category);
submenus.insert(category, ui->windowPaletteToolButton->menu()->addMenu(category));
}
// add the palette as an action in the submenu of its category
QAction *action = new QAction(name, this);
connect(action, &QAction::triggered, this, &PreferencesDialog::paletteActionTriggered);
submenus.value(category)->addAction(action);
palette_files.insert(name, colors);
}
// finally show the menu after building it, so we don't require
// them to click the tiny triangle on the side of the button
ui->windowPaletteToolButton->showMenu();
}
void PreferencesDialog::paletteActionTriggered()
{
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
QString paletteName = action->text();
// do stuff with the selected palette name
ui->windowPaletteLabel->setText(paletteName);
// check whether or not to use highlight colors
bool useHighlightColors = ui->usePaletteHighlightColorCheckbox->isChecked();
// apply the palette
setChosenPalette(palette_files.value(paletteName), useHighlightColors);
// set the palette as chosen in QSettings
saveUserWindowPalette(paletteName, palette_files.value(paletteName), useHighlightColors);
}
}
void PreferencesDialog::usePaletteHighlightColorCheckboxStatusChanged(int status)
{
bool useHighlightColors = status;
// if false, we're disabling the use of the palette's highlight color and highlight text color entries.
// if true, we're enabling their use.
QJsonObject palette = getSavedUserWindowPalette();
setChosenPalette(palette, useHighlightColors);
saveUserWindowPalette(palette.value("name").toString(), palette, useHighlightColors);
}