/
logsettingsdialog.cpp
269 lines (229 loc) · 8.79 KB
/
logsettingsdialog.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/** @file logsettingsdialog.cpp Dialog for Log settings.
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program 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 General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "ui/dialogs/logsettingsdialog.h"
#include "clientapp.h"
#include <de/VariableChoiceWidget>
#include <de/VariableToggleWidget>
#include <de/FoldPanelWidget>
#include <de/SignalAction>
using namespace de;
struct DomainText {
char const *name;
char const *label;
};
static DomainText const domainText[] = {
{ "generic", "Minimum Level" },
{ "resource", "Resources" },
{ "map", "Map" },
{ "script", "Scripting" },
{ "gl", "Graphics" },
{ "audio", "Audio" },
{ "input", "Input" },
{ "network", "Network" }
};
#define NUM_DOMAINS (sizeof(domainText)/sizeof(domainText[0]))
DENG2_PIMPL(LogSettingsDialog)
, DENG2_OBSERVES(ToggleWidget, Toggle)
{
ui::ListData levels;
VariableToggleWidget *separately;
VariableToggleWidget *metadata;
FoldPanelWidget *fold;
GridLayout foldLayout;
IndirectRule *columnWidth; ///< Sync column width in and out of the fold.
struct DomainWidgets
{
LabelWidget *label;
VariableChoiceWidget *level;
VariableToggleWidget *dev;
VariableToggleWidget *alert;
};
DomainWidgets domWidgets[NUM_DOMAINS];
Instance(Public *i) : Base(i)
{
try
{
zap(domWidgets);
columnWidth = new IndirectRule;
self.area().add(separately =
new VariableToggleWidget(tr("Filter by Subsystem"),
App::config()["log.filterBySubsystem"]));
self.area().add(metadata =
new VariableToggleWidget(tr("Show Metadata in Log"),
App::config()["log.showMetadata"]));
levels << new ChoiceItem( tr("1 - X.Verbose"), LogEntry::XVerbose)
<< new ChoiceItem( tr("2 - Verbose"), LogEntry::Verbose )
<< new ChoiceItem( tr("3 - Message"), LogEntry::Message )
<< new ChoiceItem( tr("4 - Note"), LogEntry::Note )
<< new ChoiceItem(_E(D) + tr("5 - Warning"), LogEntry::Warning )
<< new ChoiceItem(_E(D) + tr("6 - Error"), LogEntry::Error )
<< new ChoiceItem(_E(D) + tr("7 - Critical"), LogEntry::Critical);
// Folding panel for the per-domain settings.
self.area().add(fold = new FoldPanelWidget);
fold->setContent(new GuiWidget);
foldLayout.setLeftTop(fold->content().rule().left(),
fold->content().rule().top());
foldLayout.setGridSize(4, 0);
foldLayout.setColumnFixedWidth(1, *columnWidth);
foldLayout.setColumnAlignment(0, ui::AlignRight);
for(uint i = 0; i < NUM_DOMAINS; ++i)
{
initDomain(domainText[i],
domWidgets[i],
i == 0? &self.area() : &fold->content());
}
// This'll keep the dialog's size fixed even though the choices change size.
columnWidth->setSource(domWidgets[0].level->maximumWidth());
separately->audienceForToggle() += this;
}
catch(Error const &er)
{
LOGDEV_ERROR("") << er.asText();
deinit();
throw;
}
}
~Instance()
{
deinit();
releaseRef(columnWidth);
}
void initDomain(DomainText const &dom, DomainWidgets &wgt, GuiWidget *parent)
{
// Text label.
wgt.label = LabelWidget::newWithText(tr(dom.label) + ":", parent);
// Minimum level for log entries.
parent->add(wgt.level =
new VariableChoiceWidget(App::config()[String("log.filter.%1.minLevel").arg(dom.name)]));
wgt.level->setItems(levels);
wgt.level->updateFromVariable();
QObject::connect(wgt.level, SIGNAL(selectionChangedByUser(uint)),
thisPublic, SLOT(updateLogFilter()));
// Developer messages?
parent->add(wgt.dev =
new VariableToggleWidget(tr("Dev"), App::config()[String("log.filter.%1.allowDev").arg(dom.name)]));
QObject::connect(wgt.dev, SIGNAL(stateChangedByUser(ToggleWidget::ToggleState)),
thisPublic, SLOT(updateLogFilter()));
// Raise alerts?
parent->add(wgt.alert =
new VariableToggleWidget(tr("Alerts"), App::config()[String("alert.") + dom.name]));
wgt.alert->setActiveValue(LogEntry::Warning);
wgt.alert->setInactiveValue(LogEntry::HighestLogLevel + 1);
// Lay out the folding panel's contents.
if(parent == &fold->content())
{
foldLayout << *wgt.label << *wgt.level << *wgt.dev << *wgt.alert;
}
}
void deinit()
{
// The common 'levels' will be deleted soon.
for(uint i = 0; i < NUM_DOMAINS; ++i)
{
if(domWidgets[i].level)
{
domWidgets[i].level->useDefaultItems();
}
}
}
void overrideWithGeneric()
{
Config &cfg = App::config();
LogFilter &logf = App::logFilter();
// Check the generic filter settings.
LogEntry::Level minLevel = LogEntry::Level(domWidgets[0].level->selectedItem().data().toInt());
bool allowDev = domWidgets[0].dev->isActive();
bool alerts = domWidgets[0].alert->isActive();
// Override the individual filters with the generic one.
logf.setMinLevel(LogEntry::AllDomains, minLevel);
logf.setAllowDev(LogEntry::AllDomains, allowDev);
// Update the variables (UI updated automatically).
logf.write(cfg.names().subrecord("log.filter"));
for(uint i = 0; i < NUM_DOMAINS; ++i)
{
char const *name = domainText[i].name;
cfg.set(String("alert.") + name, int(alerts? LogEntry::Warning : (LogEntry::HighestLogLevel + 1)));
}
}
void toggleStateChanged(ToggleWidget &toggle)
{
if(toggle.isActive())
{
overrideWithGeneric();
fold->open();
}
else
{
fold->close();
overrideWithGeneric();
}
}
void updateLogFilter()
{
if(separately->isInactive())
{
overrideWithGeneric();
}
// Re-read from Config, which has been changed via the widgets.
applyFilterFromConfig();
}
void applyFilterFromConfig()
{
App::logFilter().read(App::config().names().subrecord("log.filter"));
}
};
LogSettingsDialog::LogSettingsDialog(String const &name)
: DialogWidget(name, WithHeading), d(new Instance(this))
{
heading().setText(tr("Log Filter & Alerts"));
// Layout.
GridLayout layout(area().contentRule().left(), area().contentRule().top());
layout.setGridSize(4, 0);
layout.setColumnFixedWidth(1, *d->columnWidth);
layout.setColumnAlignment(0, ui::AlignRight);
layout << *d->domWidgets[0].label
<< *d->domWidgets[0].level
<< *d->domWidgets[0].dev
<< *d->domWidgets[0].alert
<< Const(0);
layout.append(*d->separately, 3);
layout.append(*d->fold, 4)
<< Const(0);
layout.append(*d->metadata, 3);
// Fold's layout is complete.
d->fold->content().rule().setSize(d->foldLayout.width(), d->foldLayout.height());
// Dialog content size.
area().setContentSize(layout.width(), layout.height());
buttons()
<< new DialogButtonItem(DialogWidget::Default | DialogWidget::Accept, tr("Close"))
<< new DialogButtonItem(DialogWidget::Action, tr("Reset to Defaults"),
new SignalAction(this, SLOT(resetToDefaults())));
if(d->separately->isActive())
{
d->fold->open();
}
}
void LogSettingsDialog::resetToDefaults()
{
ClientApp::logSettings().resetToDefaults();
d->applyFilterFromConfig();
}
void LogSettingsDialog::updateLogFilter()
{
d->updateLogFilter();
}