/
alertdialog.cpp
396 lines (334 loc) · 11.7 KB
/
alertdialog.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/** @file alertdialog.cpp Dialog for listing recent alerts.
*
* @authors Copyright (c) 2013 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/alertdialog.h"
#include "ui/dialogs/logsettingsdialog.h"
#include "ui/clientwindow.h"
#include "clientapp.h"
#include <de/FIFO>
#include <de/App>
#include <de/ChoiceWidget>
#include <de/DialogContentStylist>
#include <de/NotificationAreaWidget>
#include <de/SequentialLayout>
#include <de/SignalAction>
#include <de/ui/ListData>
#include <de/ui/ActionItem>
#include <QTimer>
using namespace de;
static String const VAR_AUTOHIDE = "alert.autoHide";
DENG_GUI_PIMPL(AlertDialog)
, DENG2_OBSERVES(ChildWidgetOrganizer, WidgetCreation)
, DENG2_OBSERVES(ChildWidgetOrganizer, WidgetUpdate)
, DENG2_OBSERVES(Variable, Change)
{
/// Data model item representing an alert in the list.
class AlertItem : public ui::ActionItem
{
public:
AlertItem(String const &msg, Level level)
: ui::ActionItem(ShownAsLabel, msg)
, _level(level)
{}
Level level() const { return _level; }
private:
Level _level;
};
/// Rich font styling for alert labels.
struct TextStyling : public Font::RichFormat::IStyle
{
Color richStyleColor(int /*index*/) const
{
return Color(255, 255, 255, 255); // not used by LabelWidget
}
void richStyleFormat(int contentStyle, float &sizeFactor,
Font::RichFormat::Weight &fontWeight,
Font::RichFormat::Style &fontStyle,
int &colorIndex) const
{
ClientApp::windowSystem().style()
.richStyleFormat(contentStyle, sizeFactor, fontWeight, fontStyle, colorIndex);
if(contentStyle == Font::RichFormat::MajorStyle ||
contentStyle == Font::RichFormat::MajorMetaStyle)
{
// Keep the major style normal-weight.
fontWeight = Font::RichFormat::Normal;
}
}
};
ButtonWidget *notification;
MenuWidget *alerts;
bool clearOnDismiss;
TextStyling styling;
QTimer hideTimer; ///< Automatically hides the notification.
ChoiceWidget *hideTimes;
DialogContentStylist stylist;
dsize maxCount;
typedef FIFO<AlertItem> Pending;
Pending pending;
Instance(Public *i)
: Base(i)
, clearOnDismiss(false)
, maxCount(100)
{
notification = new ButtonWidget;
notification->setSizePolicy(ui::Expand, ui::Expand);
notification->setImage(style().images().image("alert"));
notification->setOverrideImageSize(style().fonts().font("default").height().value());
notification->setAction(new SignalAction(thisPublic, SLOT(showListOfAlerts())));
// The menu expands with all the alerts, and the dialog's scroll area allows
// browsing it up and down.
ScrollAreaWidget &area = self.area();
alerts = new MenuWidget;
alerts->enableScrolling(false);
alerts->setGridSize(1, ui::Expand, 0, ui::Expand);
alerts->rule().setLeftTop(area.contentRule().left(), area.contentRule().top());
area.setContentSize(alerts->rule().width(), alerts->rule().height());
area.add(alerts);
area.enableIndicatorDraw(true);
alerts->organizer().audienceForWidgetCreation() += this;
alerts->organizer().audienceForWidgetUpdate() += this;
// Set up the automatic hide timer.
QObject::connect(&hideTimer, SIGNAL(timeout()), thisPublic, SLOT(hideNotification()));
hideTimer.setSingleShot(true);
App::config(VAR_AUTOHIDE).audienceForChange() += this;
}
~Instance()
{
App::config(VAR_AUTOHIDE).audienceForChange() -= this;
}
NotificationAreaWidget ¬ifs()
{
return ClientWindow::main().notifications();
}
int autoHideAfterSeconds() const
{
return App::config().geti(VAR_AUTOHIDE, 3 * 60);
}
void variableValueChanged(Variable &, Value const &)
{
// Update the auto-hide timer.
if(!autoHideAfterSeconds())
{
// Never autohide.
hideTimer.stop();
}
else
{
hideTimer.setInterval(autoHideAfterSeconds() * 1000);
if(!hideTimer.isActive()) hideTimer.start();
}
}
void queueAlert(String const &msg, Level level)
{
pending.put(new AlertItem(msg, level));
}
bool addPendingAlerts()
{
bool changed = false;
while(AlertItem *alert = pending.take())
{
add(alert);
changed = true;
}
// Remove excess alerts.
while(alerts->items().size() > maxCount)
{
alerts->items().remove(alerts->items().size() - 1);
changed = true;
}
return changed;
}
void add(AlertItem *alert)
{
DENG2_ASSERT_IN_MAIN_THREAD();
// If we already have this, don't re-add.
for(ui::Data::Pos i = 0; i < alerts->items().size(); ++i)
{
if(!alerts->items().at(i).label().compareWithoutCase(alert->label()))
return;
}
alerts->items().insert(0, alert);
}
void widgetCreatedForItem(GuiWidget &widget, ui::Item const &item)
{
// Background is provided by the popup.
widget.set(Background());
LabelWidget &label = widget.as<LabelWidget>();
// Each alert has an icon identifying the originating subsystem and the level
// of the alert.
label.setBehavior(ContentClipping); // allow clip-culling
label.setTextStyle(&styling);
label.setMaximumTextWidth(style().rules().rule("alerts.width").valuei());
label.setSizePolicy(ui::Expand, ui::Expand);
label.setAppearanceAnimation(LabelWidget::AppearGrowVertically, 0.5);
label.setAlignment(ui::AlignBottom);
label.setImage(style().images().image("alert"));
label.setOverrideImageSize(style().fonts().font("default").height().value());
label.setImageAlignment(ui::AlignTop);
label.setTextAlignment(ui::AlignRight);
label.margins()
.setLeft("")
.setRight("")
.setBottom("");
AlertItem const &alert = item.as<AlertItem>();
switch(alert.level())
{
case Minor:
label.setImageColor(Vector4f(style().colors().colorf("text"), .5f));
break;
case Normal:
label.setImageColor(style().colors().colorf("text"));
break;
case Major:
label.setTextStyle(0); // use default styling with bold weights
label.setImageColor(style().colors().colorf("accent"));
break;
}
}
void widgetUpdatedForItem(GuiWidget &widget, ui::Item const &item)
{
DENG2_UNUSED(widget);
DENG2_UNUSED(item);
}
void showNotification()
{
// Change color to indicate new alerts.
notification->setImageColor(style().colors().colorf("accent"));
notifs().showOrHide(notification, true);
// Restart the autohiding timer.
if(autoHideAfterSeconds() > 0)
{
hideTimer.start(autoHideAfterSeconds() * 1000);
}
}
void hideNotification()
{
notifs().hideChild(*notification);
}
/**
* @returns @c true, if the notification was hidden due to the alerts list being
* empty.
*/
bool hideIfEmpty()
{
if(alerts->items().isEmpty())
{
// No alerts to show.
hideNotification();
return true; // was hidden
}
return false;
}
void updateHideTimeSelection()
{
int const time = autoHideAfterSeconds();
ui::DataPos pos = hideTimes->items().findData(time);
if(pos != ui::Data::InvalidPos)
{
hideTimes->setSelected(pos);
}
else
{
hideTimes->setSelected(hideTimes->items().findData(0));
}
}
};
AlertDialog::AlertDialog(String const &/*name*/) : d(new Instance(this))
{
// The dialog is connected to the notification icon.
setAnchorAndOpeningDirection(d->notification->rule(), ui::Down);
buttons() << new DialogButtonItem(DialogWidget::Accept | DialogWidget::Default,
tr("Clear All"))
<< new DialogButtonItem(DialogWidget::Action | DialogWidget::Id1,
style().images().image("gear"),
new SignalAction(this, SLOT(showLogFilterSettings())));
// Auto-hide setting, positioned next to the Gear button.
// These are not part of the dialog proper, but should be styled like regular
// dialog contents.
d->stylist.setContainer(*this);
ButtonWidget const &gearButton = *buttonWidget(DialogWidget::Id1);
auto *lab = LabelWidget::newWithText(tr("Hide After:"), this);
add(d->hideTimes = new ChoiceWidget);
d->hideTimes->items()
<< new ChoiceItem(tr("1 min"), 60)
<< new ChoiceItem(tr("3 mins"), 3 * 60)
<< new ChoiceItem(tr("5 mins"), 5 * 60)
<< new ChoiceItem(tr("10 mins"), 10 * 60)
<< new ChoiceItem(tr("Never"), 0);
d->updateHideTimeSelection();
lab->rule()
.setInput(Rule::Left, gearButton.rule().right())
.setInput(Rule::AnchorY, gearButton.rule().midY())
.setAnchorPoint(Vector2f(0, .5f));
d->hideTimes->rule()
.setInput(Rule::Left, lab->rule().right())
.setInput(Rule::Top, lab->rule().top());
connect(d->hideTimes, SIGNAL(selectionChangedByUser(uint)), this, SLOT(hideTimeChanged()));
}
void AlertDialog::newAlert(String const &message, Level level)
{
d->queueAlert(message, level);
}
void AlertDialog::update()
{
DialogWidget::update();
if(d->addPendingAlerts())
{
d->showNotification();
}
}
void AlertDialog::showListOfAlerts()
{
if(isOpen() || d->hideIfEmpty()) return;
// Restore the normal color.
d->notification->setImageColor(style().colors().colorf("text"));
area().scrollToTop(0);
open();
}
void AlertDialog::showLogFilterSettings()
{
LogSettingsDialog *st = new LogSettingsDialog;
st->setAnchorAndOpeningDirection(buttonWidget(DialogWidget::Id1)->rule(), ui::Left);
st->setDeleteAfterDismissed(true);
connect(this, SIGNAL(closed()), st, SLOT(close()));
st->exec(root());
}
void AlertDialog::hideNotification()
{
d->hideNotification();
}
void AlertDialog::hideTimeChanged()
{
App::config().set(VAR_AUTOHIDE, d->hideTimes->selectedItem().data().toInt());
}
void AlertDialog::finish(int result)
{
DialogWidget::finish(result);
// The alerts will be cleared if the dialog is accepted.
d->clearOnDismiss = (result != 0);
}
void AlertDialog::panelDismissed()
{
if(d->clearOnDismiss)
{
d->clearOnDismiss = false;
d->alerts->items().clear();
d->hideIfEmpty();
}
DialogWidget::panelDismissed();
}