-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrefs.cpp
More file actions
193 lines (162 loc) · 4.67 KB
/
Prefs.cpp
File metadata and controls
193 lines (162 loc) · 4.67 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "Prefs.hpp"
#include "App.hpp"
#include "ByteArray.hpp"
#include "io/io.hh"
#include "io/SaveFile.hpp"
#include "gui/Tab.hpp"
#include "gui/Table.hpp"
#include "gui/TreeView.hpp"
#include <QApplication>
#include <QHeaderView>
#include <fcntl.h>
namespace cornus {
int NormalFontSize()
{
const QFont f = QApplication::font();
return (f.pixelSize() > 0) ? f.pixelSize() : f.pointSize();
}
Prefs::Prefs(App *app): app_(app) {}
Prefs::~Prefs()
{
delete left_bytes_;
left_bytes_ = nullptr;
}
void Prefs::ApplyTableHeight(gui::Table *table, int max)
{
if (table_size_.empty())
return;
auto *vh = table->verticalHeader();
QFont f = table->font();
if (table_size_.pixels > 0) {
f.setPixelSize(table_size_.pixels);
} else {
f.setPointSize(table_size_.points);
}
table->setFont(f);
vh->setSectionResizeMode(QHeaderView::Fixed);
vh->setMinimumSectionSize((table_size_.pixels > 0)
? table_size_.pixels : table_size_.points);
vh->setMaximumSectionSize(max);
vh->setDefaultSectionSize(max);
table->UpdateColumnSizes();
}
void Prefs::ApplyTreeViewHeight()
{
if (table_size_.empty())
return;
gui::TreeView *view = app_->tree_view();
QFont f = view->font();
if (table_size_.pixels > 0) {
f.setPixelSize(table_size_.pixels);
} else {
f.setPointSize(table_size_.points);
}
view->setFont(f);
}
bool Prefs::Load()
{
const QString full_path = prefs::QueryAppConfigPath() + '/'
+ prefs::PrefsFileName + QString::number(prefs::PrefsFormatVersion);
io::ReadParams read_params = {};
read_params.can_rely = CanRelyOnStatxSize::Yes;
read_params.print_errors = PrintErrors::Yes;
ByteArray buf;
MTL_CHECK(io::ReadFile(full_path, buf, read_params));
MTL_CHECK(!buf.is_empty());
u16 version = buf.next_u16();
MTL_CHECK(version == prefs::PrefsFormatVersion);
table_size_.pixels = buf.next_i16();
table_size_.points = buf.next_i16();
const i8 col_start = buf.next_i8();
const i8 col_end = buf.next_i8();
for (i8 i = col_start; i < col_end; i++) {
cols_visibility_[i] = buf.next_i8() == 1 ? true : false;
}
bool_ = buf.next_u64();
side_pane_width_ = buf.next_i32();
editor_tab_size_ = buf.next_i8();
splitter_sizes_.append(buf.next_i32());
splitter_sizes_.append(buf.next_i32());
win_w_ = buf.next_i32();
win_h_ = buf.next_i32();
// ABI: this line must be the last one to save unknown data to @left_bytes
// to avoid casual abi version bumps which would entail losing prefs.
left_bytes_ = buf.CloneFromHere();
return true;
}
void Prefs::Save() const
{
QString parent_dir = prefs::QueryAppConfigPath();
parent_dir.append('/');
MTL_CHECK_VOID(!parent_dir.isEmpty());
auto filename = prefs::PrefsFileName + QString::number(prefs::PrefsFormatVersion);
io::SaveFile save_file(parent_dir, filename);
ByteArray buf;
buf.add_u16(prefs::PrefsFormatVersion);
buf.add_i16(table_size_.pixels);
buf.add_i16(table_size_.points);
auto *hh = app_->tab()->table()->horizontalHeader();
const i8 col_start = (int)gui::Column::FileName + 1;
const i8 col_end = int(gui::Column::Count);
buf.add_i8(col_start);
buf.add_i8(col_end);
for (i8 i = col_start; i < col_end; i++)
{
i8 b = hh->isSectionHidden(i) ? 0 : 1;
buf.add_i8(b);
}
buf.add_u64(bool_);
buf.add_i32(side_pane_width_);
buf.add_i8(editor_tab_size_);
QList<int> sizes = app_->main_splitter()->sizes();
buf.add_i32(sizes[0]);
buf.add_i32(sizes[1]);
const auto sz = app_->size();
buf.add_i32(sz.width());
buf.add_i32(sz.height());
// ABI: this line must be the last one to add data to @buf
buf.add(left_bytes_, From::Start);
if (!io::WriteToFile(save_file.GetPathToWorkWith(), buf.data(), buf.size()))
{
mtl_trace("Failed to save bookmarks");
}
save_file.Commit();
}
void Prefs::UpdateTableSizes()
{
i32 str_h = (table_size_.pixels > 0) ? table_size_.pixels : table_size_.points;
gui::Table *table = app_->tab()->table();
if (table_size_.ratio < 0) {
QFont f = table->font();
int rh = table->verticalHeader()->defaultSectionSize();
int orig_sz = (f.pixelSize() > 0) ? f.pixelSize() : f.pointSize();
table_size_.ratio = float(rh) / float(orig_sz);
}
i32 max = str_h * table_size_.ratio;
ApplyTableHeight(table, max);
ApplyTreeViewHeight();
}
void Prefs::WheelEventFromMainView(const Zoom zoom)
{
gui::Table *table = app_->tab()->table();
if (table_size_.empty()) {
QFont f = table->font();
if (f.pixelSize() > 0) {
table_size_.pixels = f.pixelSize();
} else {
table_size_.points = f.pointSize();
}
}
auto &value = (table_size_.pixels > 0) ? table_size_.pixels : table_size_.points;
if (zoom == Zoom::In) {
if (value < 50)
value++;
} else if (zoom == Zoom::Out) {
if (value > 8)
value--;
} else if (zoom == Zoom::Reset) {
value = NormalFontSize();
}
UpdateTableSizes();
}
}