-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorSettings.cpp
More file actions
92 lines (79 loc) · 3.52 KB
/
Copy pathEditorSettings.cpp
File metadata and controls
92 lines (79 loc) · 3.52 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
#include "settings/EditorSettings.h"
#include "editor/DocumentManager.h"
#include "editor/EditorWidget.h"
#include "settings/SettingsService.h"
#include <QApplication>
#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QPlainTextEdit>
#include <QStyle>
#include <QStyleFactory>
namespace cold {
void EditorSettings::applyTo(EditorWidget* editor, const SettingsService& settings) {
if (!editor) {
return;
}
QFont font = editor->font();
const QString family = settings.value(kFontFamily);
if (!family.isEmpty()) {
font.setFamily(family);
}
bool ok = false;
const int size = settings.value(kFontSize, QStringLiteral("11")).toInt(&ok);
if (ok && size > 0) {
font.setPointSize(size);
}
font.setStyleHint(QFont::Monospace);
editor->setFont(font);
const int tabWidth = settings.value(kTabWidth, QStringLiteral("4")).toInt(&ok);
const bool insertSpaces =
settings.value(kInsertSpaces, QStringLiteral("true")) != QStringLiteral("false");
const int spaces = ok && tabWidth > 0 ? tabWidth : 4;
const QFontMetrics fm(font);
const qreal stop = insertSpaces
? static_cast<qreal>(spaces) * fm.horizontalAdvance(QLatin1Char(' '))
: static_cast<qreal>(spaces) * fm.horizontalAdvance(QLatin1Char('\t'));
editor->setTabStopDistance(stop);
const bool wrap = settings.value(kWordWrap, QStringLiteral("false")) == QStringLiteral("true");
editor->setLineWrapMode(wrap ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
}
void EditorSettings::applyToAllEditors(DocumentManager& documents,
const SettingsService& settings) {
documents.forEachEditor([&settings](EditorWidget* editor) { applyTo(editor, settings); });
}
void EditorSettings::applyTheme(QApplication& app, const QString& theme) {
if (theme == QStringLiteral("light")) {
app.setStyle(QStyleFactory::create(QStringLiteral("Fusion")));
QPalette palette;
palette.setColor(QPalette::Window, QColor(245, 245, 245));
palette.setColor(QPalette::WindowText, QColor(30, 30, 30));
palette.setColor(QPalette::Base, QColor(255, 255, 255));
palette.setColor(QPalette::AlternateBase, QColor(240, 240, 240));
palette.setColor(QPalette::Text, QColor(30, 30, 30));
palette.setColor(QPalette::Button, QColor(240, 240, 240));
palette.setColor(QPalette::ButtonText, QColor(30, 30, 30));
palette.setColor(QPalette::Highlight, QColor(0, 120, 215));
palette.setColor(QPalette::HighlightedText, Qt::white);
app.setPalette(palette);
return;
}
if (theme == QStringLiteral("dark")) {
app.setStyle(QStyleFactory::create(QStringLiteral("Fusion")));
QPalette palette;
palette.setColor(QPalette::Window, QColor(30, 30, 30));
palette.setColor(QPalette::WindowText, QColor(212, 212, 212));
palette.setColor(QPalette::Base, QColor(30, 30, 30));
palette.setColor(QPalette::AlternateBase, QColor(45, 45, 45));
palette.setColor(QPalette::Text, QColor(212, 212, 212));
palette.setColor(QPalette::Button, QColor(45, 45, 45));
palette.setColor(QPalette::ButtonText, QColor(212, 212, 212));
palette.setColor(QPalette::Highlight, QColor(38, 79, 120));
palette.setColor(QPalette::HighlightedText, Qt::white);
app.setPalette(palette);
return;
}
app.setStyle(nullptr);
app.setPalette(app.style()->standardPalette());
}
} // namespace cold