Repository navigation
Expand file tree
/
Copy pathpanes.cpp
More file actions
802 lines (690 loc) · 23.3 KB
/
Copy pathpanes.cpp
File metadata and controls
802 lines (690 loc) · 23.3 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#include "panes.h"
#include <QVBoxLayout>
#include <QStackedLayout>
#include <QLabel>
#include <QResizeEvent>
#include <QTextBlock>
const QString StatusPane::SETTINGS_CATEGORY="StatusPane";
QMediaPlayer* BuildPlayer(QObject *parent,qreal initialVolume)
{
QMediaPlayer *player=new QMediaPlayer(parent);
player->setAudioOutput(new QAudioOutput(parent));
player->audioOutput()->setVolume(initialVolume);
return player;
}
StatusPane::StatusPane(QWidget *parent) : PersistentPane(parent),
output(this),
settingFont(SETTINGS_CATEGORY,"Font","Droid Sans Mono"),
settingFontSize(SETTINGS_CATEGORY,"FontSize",10),
settingForegroundColor(SETTINGS_CATEGORY,"ForegroundColor","#ffffffff"),
settingBackgroundColor(SETTINGS_CATEGORY,"BackgroundColor","#ff000000")
{
output.setStyleSheet(StyleSheet::Colors<StaticTextEdit>(settingForegroundColor,settingBackgroundColor));
output.setFontFamily(settingFont);
output.setFontPointSize(settingFontSize);
output.document()->setDocumentMargin(16);
output.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
output.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
output.setFrameStyle(QFrame::NoFrame);
connect(&output,&StaticTextEdit::ContextMenu,this,&StatusPane::ContextMenu);
setLayout(new QVBoxLayout(this));
layout()->setContentsMargins(0,0,0,0);
layout()->addWidget(&output);
}
void StatusPane::EnableScrollBar()
{
output.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}
void StatusPane::Print(const QString &text)
{
output.insertPlainText(text);
output.insertPlainText("\n");
output.ensureCursorVisible();
}
ApplicationSetting& StatusPane::Font()
{
return settingFont;
}
ApplicationSetting& StatusPane::FontSize()
{
return settingFontSize;
}
ApplicationSetting& StatusPane::ForegroundColor()
{
return settingForegroundColor;
}
ApplicationSetting& StatusPane::BackgroundColor()
{
return settingBackgroundColor;
}
const QString ChatPane::SETTINGS_CATEGORY="ChatPane";
ChatPane::ChatPane(QWidget *parent) : PersistentPane(parent),
agenda(nullptr),
chat(nullptr),
status(nullptr),
settingFont(SETTINGS_CATEGORY,"Font","Copperplate Gothic Bold"),
settingFontSize(SETTINGS_CATEGORY,"FontSize",12),
settingForegroundColor(SETTINGS_CATEGORY,"ForegroundColor","#ffffffff"),
settingBackgroundColor(SETTINGS_CATEGORY,"BackgroundColor","#ff000000"),
settingStatusInterval(SETTINGS_CATEGORY,"StatusInterval",5000)
{
setLayout(new QVBoxLayout(this));
layout()->setContentsMargins(0,0,0,0);
layout()->setSpacing(0);
agenda=new QLabel(this);
agenda->setStyleSheet(StyleSheet::Colors<QLabel>(settingForegroundColor,settingBackgroundColor));
agenda->setFont(QFont(settingFont,static_cast<qreal>(settingFontSize)*1.666,QFont::Bold));
agenda->setMargin(16);
agenda->setAlignment(Qt::AlignCenter);
agenda->setWordWrap(true);
agenda->hide();
layout()->addWidget(agenda);
chat=new PinnedTextEdit(this);
chat->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
chat->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
chat->setFrameStyle(QFrame::NoFrame);
chat->setCursorWidth(0);
layout()->addWidget(chat);
connect(chat,&PinnedTextEdit::ContextMenu,this,&ChatPane::ContextMenu);
status=new QLabel(this);
status->setMargin(16);
status->setAlignment(Qt::AlignCenter);
status->setWordWrap(true);
status->setTextFormat(Qt::RichText);
status->hide();
layout()->addWidget(status);
statusClock.setInterval(static_cast<std::chrono::milliseconds>(settingStatusInterval).count());
connect(&statusClock,&QTimer::timeout,this,&ChatPane::DismissStatus);
Format();
}
void ChatPane::SetAgenda(const QString &text)
{
if (text.isEmpty())
{
agenda->clear();
agenda->hide();
}
else
{
agenda->setText(text);
agenda->show();
}
}
void ChatPane::Format()
{
chat->setStyleSheet(StyleSheet::Colors<PinnedTextEdit>(settingForegroundColor,settingBackgroundColor));
chat->setFontFamily(settingFont);
chat->setFontPointSize(settingFontSize);
chat->document()->setDefaultStyleSheet(QString("div.user { font-family: '%1'; font-size: %2pt; } div.message, span.message { font-family: '%1'; font-size: %3pt; }").arg(static_cast<QString>(settingFont),StringConvert::Integer(static_cast<int>(settingFontSize)*1.333),StringConvert::Integer(static_cast<int>(settingFontSize))));
chat->document()->setDocumentMargin(static_cast<qreal>(settingFontSize)*1.333);
status->setStyleSheet(StyleSheet::Colors<QLabel>(settingForegroundColor,settingBackgroundColor));
status->setFont(QFont(settingFont,static_cast<qreal>(settingFontSize)*0.833)); // QLabel doesn't have setFontFamily()
}
void ChatPane::Refresh()
{
Format();
chat->viewport()->update();
}
void ChatPane::Message(std::shared_ptr<Chat::Message> message) const
{
const QString imageTemplate(R"(<img style="vertical-align: middle;" src="%1" />)");
const QStringList &badgeList=message->badges;
QString badges;
for (const QString &icon : badgeList) badges.append(QString{"<img style='vertical-align: middle;' src='%1' /> "}.arg(icon));
int reserve=0;
for (const Chat::Emote &emote : message->emotes) reserve+=imageTemplate.size()+emote.path.size()-emote.name.size();
message->text.reserve(reserve);
Chat::EmoteList::const_reverse_iterator emote=message->emotes.crbegin();
for (int index=message->text.size()-1; index >= 0; index--)
{
// replace entities
if (!message->html)
{
if (message->text.at(index) == QLatin1Char('<'))
message->text.replace(index,1,"<");
else if (message->text.at(index) == QLatin1Char('>'))
message->text.replace(index,1,">");
else if (message->text.at(index) == QLatin1Char('&'))
message->text.replace(index,1,"&");
else if (message->text.at(index) == QLatin1Char('"'))
message->text.replace(index,1,""");
}
// replace emotes
if (emote != message->emotes.crend() && index == emote->start)
{
message->text.replace(index,emote->name.size(),imageTemplate.arg(emote->path));
emote++;
}
}
if (message->action)
chat->Append(QString("<div>%4</div><div class='user' style='color: %3;'>%1 <span class='message'>%2</span></div>").arg(message->displayName,message->text,message->color.isValid() ? message->color.name() : settingForegroundColor,badges),message->id);
else
chat->Append(QString("<div>%4</div><div class='user' style='color: %3;'>%1</div><div class='message'>%2</div>").arg(message->displayName,message->text,message->color.isValid() ? message->color.name() : settingForegroundColor,badges),message->id);
}
void ChatPane::DeleteMessage(const QString &id)
{
chat->Remove(id);
}
void ChatPane::Print(const QString &text)
{
statuses.push(text);
status->setText(statuses.front());
status->show();
if (!statusClock.isActive()) statusClock.start();
}
void ChatPane::DismissStatus()
{
statuses.pop();
if (!statuses.empty())
{
status->setText(statuses.front());
return;
}
status->clear();
status->hide();
statusClock.stop();
}
ApplicationSetting& ChatPane::Font()
{
return settingFont;
}
ApplicationSetting& ChatPane::FontSize()
{
return settingFontSize;
}
ApplicationSetting& ChatPane::ForegroundColor()
{
return settingForegroundColor;
}
ApplicationSetting& ChatPane::BackgroundColor()
{
return settingBackgroundColor;
}
ApplicationSetting& ChatPane::StatusInterval()
{
return settingStatusInterval;
}
EphemeralPane::EphemeralPane(QWidget *parent,int priority): QWidget(parent), expired(false), priority(priority)
{
connect(this,&EphemeralPane::Finished,this,&EphemeralPane::Expire);
}
bool EphemeralPane::operator<(const EphemeralPane &other) const
{
return priority < other.priority;
}
int EphemeralPane::Priority() const
{
return priority;
}
void EphemeralPane::Expire()
{
// I think some panes, particularly the ones relying on QMediaPlayer signals
// can fire Finished() more than once. This was my workaround for that.
if (expired) return;
expired=true;
hide(); // so as not to confuse layout counts in the pane host
emit Expired();
deleteLater();
}
void EphemeralPane::showEvent(QShowEvent *event)
{
emit Presented();
QWidget::showEvent(event);
}
void EphemeralPane::hideEvent(QHideEvent *event)
{
if (!expired) Interrupt();
QWidget::hideEvent(event);
}
bool EphemeralPane::event(QEvent *event)
{
if (event->type() == QEvent::Polish) setVisible(false);
return QWidget::event(event);
}
const int PaneHost::HIGH_PRIORITY_THRESHOLD=50;
PaneHost::PaneHost(const QColor &backgroundColor,QWidget *parent): QWidget(parent), livePane(nullptr), persistentPane(nullptr)
{
auto gridLayout=new QGridLayout(this);
gridLayout->setContentsMargins(0,0,0,0);
setLayout(gridLayout);
setStyleSheet(QString("background-color: rgba(%1,%2,%3,%4);").arg(
StringConvert::Integer(backgroundColor.red()),
StringConvert::Integer(backgroundColor.green()),
StringConvert::Integer(backgroundColor.blue()),
StringConvert::Integer(backgroundColor.alpha()))
);
}
PersistentPane* PaneHost::ReplacePersistent(PersistentPane *candidate)
{
auto replacedPane=persistentPane;
persistentPane=candidate;
layout()->addWidget(candidate);
connect(this,&PaneHost::Print,candidate,&PersistentPane::Print);
return replacedPane;
}
void PaneHost::StageEphemeral(EphemeralPane *candidate,bool bypassQueue)
{
if (persistentPane) persistentPane->hide();
layout()->addWidget(candidate);
connect(candidate,&EphemeralPane::Expired,this,&PaneHost::RestoreOrder);
connect(candidate,&EphemeralPane::Expired,this,&PaneHost::ReflowLayout);
connect(candidate,&EphemeralPane::Presented,this,&PaneHost::ReflowLayout);
if (bypassQueue)
{
if (livePane && livePane->Priority() < HIGH_PRIORITY_THRESHOLD)
{
livePane->hide();
ephemeralPanes.push(livePane);
livePane=nullptr;
}
// there's is a subtle bug here where the music will stay suppressed until
// everything in the host is clear, not just the chaos mode pane, which I'm okay with
emit HighPriority();
candidate->show();
return;
}
connect(candidate,&EphemeralPane::Expired,this,&PaneHost::PresentNextEphemeral);
ephemeralPanes.push(candidate);
if (Chaos()) return;
if (livePane)
{
if (*livePane < *candidate) // interrupt lower priority panes
{
livePane->hide();
ephemeralPanes.push(livePane);
livePane=nullptr;
PresentNextEphemeral();
}
}
else
{
PresentNextEphemeral();
}
}
void PaneHost::PresentNextEphemeral()
{
if (ephemeralPanes.empty())
{
livePane=nullptr;
return;
}
livePane=ephemeralPanes.top();
ephemeralPanes.pop();
livePane->show();
if (livePane->Priority() > HIGH_PRIORITY_THRESHOLD)
{
emit HighPriority();
}
else
{
if (!Chaos()) emit LowPriority();
}
}
bool PaneHost::Chaos()
{
int ephemeralPaneCount=0;
for (const auto &child : children())
{
auto pane=qobject_cast<EphemeralPane*>(child);
if (pane && pane->isVisible())
{
if (++ephemeralPaneCount > 1) return true;
}
}
return false;
}
void PaneHost::Flush()
{
while (!ephemeralPanes.empty())
{
auto pane=ephemeralPanes.top();
ephemeralPanes.pop();
disconnect(pane,&EphemeralPane::Expired,this,&PaneHost::PresentNextEphemeral);
pane->show();
}
}
void PaneHost::RestoreOrder()
{
if (ephemeralPanes.empty())
{
int ephemeralPaneCount=0;
for (const auto &child : children())
{
if (qobject_cast<EphemeralPane*>(child))
{
if (++ephemeralPaneCount > 1) return;
}
}
if (persistentPane) persistentPane->show();
emit LowPriority();
}
else
{
PresentNextEphemeral();
}
}
void PaneHost::ReflowLayout()
{
// count how many total visible panes we have
int visiblePaneCount=0;
for (const auto &child : children())
{
if (auto pane=qobject_cast<EphemeralPane*>(child); pane && pane->isVisible()) visiblePaneCount++;
}
if (visiblePaneCount < 4) return;
// determine how many rows and columns we need to make a square of them
int paneCountSquareRoot=static_cast<int>(std::sqrt(visiblePaneCount));
if (paneCountSquareRoot*paneCountSquareRoot == visiblePaneCount && layout()->count() >= visiblePaneCount)
{
auto candidateLayout=new QGridLayout();
std::vector<QWidget*> visibleEphemeralPanes;
visibleEphemeralPanes.reserve(visiblePaneCount);
std::vector<QWidget*> inactivePanes;
inactivePanes.reserve(layout()->count()-visiblePaneCount);
// remove panes from old layout
QLayoutItem *candidateLayoutItem;
while ((candidateLayoutItem=layout()->takeAt(0)) != nullptr)
{
auto widget=candidateLayoutItem->widget();
if (auto pane=qobject_cast<EphemeralPane*>(widget); pane && pane->isVisible())
{
visibleEphemeralPanes.push_back(widget);
}
else
{
inactivePanes.push_back(widget);
}
delete candidateLayoutItem;
}
// add visible panes to the new layout in the correct row and column
for (int y=0; y < paneCountSquareRoot; y++)
{
for (int x=0; x < paneCountSquareRoot; x++)
{
int subscript=y*paneCountSquareRoot+x;
candidateLayout->addWidget(visibleEphemeralPanes.at(subscript),x,y);
}
}
// add the remaining inactive panes to the new layout in a generic position
for (auto pane : inactivePanes) candidateLayout->addWidget(pane,0,0);
// replace the old layout with the new one
delete layout();
setLayout(candidateLayout);
}
}
VideoPane::VideoPane(const QString &path,QWidget *parent,int priority) noexcept(false): EphemeralPane(parent,priority), videoPlayer(BuildPlayer(this,1)), viewport(new QVideoWidget(this))
{
if (!QFile(path).exists()) throw std::runtime_error(QString{"Video doesn't exist ("+path+")"}.toStdString());
videoPlayer->setVideoOutput(viewport);
videoPlayer->setSource(QUrl::fromLocalFile(path));
connect(videoPlayer,&QMediaPlayer::playbackStateChanged,this,[this](QMediaPlayer::PlaybackState state) {
if (state == QMediaPlayer::StoppedState) emit Finished();
});
setLayout(new QVBoxLayout(this));
layout()->setContentsMargins(0,0,0,0);
layout()->addWidget(viewport);
}
void VideoPane::showEvent(QShowEvent *event)
{
videoPlayer->play();
EphemeralPane::showEvent(event);
}
void VideoPane::Interrupt()
{
videoPlayer->pause();
}
QString VideoPane::Subsystem()
{
return u"video pane"_s;
}
const QString ScrollingPane::SETTINGS_CATEGORY="ScrollingPane";
ScrollingPane::ScrollingPane(const QString &text,QWidget *parent) : EphemeralPane(parent,false),
commands(new ScrollingTextEdit(this)),
settingFont(SETTINGS_CATEGORY,"Font","Copperplate Gothic Bold"),
settingFontSize(SETTINGS_CATEGORY,"FontSize",20),
settingForegroundColor(SETTINGS_CATEGORY,"ForegroundColor","#ffffffff"),
settingBackgroundColor(SETTINGS_CATEGORY,"BackgroundColor","#ff000000")
{
QGridLayout *gridLayout=new QGridLayout(this);
setLayout(gridLayout);
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->setSpacing(0);
commands->setStyleSheet(StyleSheet::Colors<ScrollingTextEdit>(settingForegroundColor,settingBackgroundColor));
commands->setFontFamily(settingFont);
commands->setFontPointSize(settingFontSize);
commands->document()->setDefaultStyleSheet(QString("div.name { font-family: '%1'; font-size: %2pt; } span.description, span.aliases { font-family: '%1'; font-size: %3pt; }").arg(static_cast<QString>(settingFont),StringConvert::Integer(static_cast<int>(settingFontSize)),StringConvert::Integer(static_cast<int>(settingFontSize)*0.7)));
commands->document()->setDocumentMargin(static_cast<qreal>(settingFontSize)*1.333);
commands->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
commands->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
commands->setFrameStyle(QFrame::NoFrame);
commands->setCursorWidth(0);
commands->setText(text);
gridLayout->addWidget(commands);
connect(commands,&ScrollingTextEdit::Finished,this,&ScrollingPane::Finished);
}
QString ScrollingPane::Subsystem()
{
return u"scrolling pane"_s;
}
const QString AnnouncePane::SETTINGS_CATEGORY="AnnouncePane";
AnnouncePane::AnnouncePane(const Lines &lines,QWidget *parent,int priority): EphemeralPane(parent,priority),
lines(lines),
output(new QLabel(this)),
settingDuration(SETTINGS_CATEGORY,"Duration",5000),
settingFont(SETTINGS_CATEGORY,"Font","Copperplate Gothic Bold"),
settingFontSize(SETTINGS_CATEGORY,"FontSize",20),
settingForegroundColor(SETTINGS_CATEGORY,"ForegroundColor","#ffffffff"),
settingBackgroundColor(SETTINGS_CATEGORY,"BackgroundColor","#ff000000"),
settingAccentColor(SETTINGS_CATEGORY,"AccentColor","#ff000000")
{
QVBoxLayout *verticalLayout=new QVBoxLayout(this);
setLayout(verticalLayout);
verticalLayout->setContentsMargins(0,0,0,0);
verticalLayout->setSpacing(0);
output->setStyleSheet(StyleSheet::Colors<QLabel>(settingForegroundColor,settingBackgroundColor));
output->setFont(QFont(settingFont,settingFontSize,QFont::Bold));
output->setMargin(16);
output->setAlignment(Qt::AlignCenter);
output->setWordWrap(true);
output->setTextFormat(Qt::RichText);
clock.setSingleShot(true);
connect(&clock,&QTimer::timeout,this,&AnnouncePane::Finished);
connect(this,&AnnouncePane::Resized,this,&AnnouncePane::AdjustText,Qt::QueuedConnection);
}
AnnouncePane::AnnouncePane(const QString &text,QWidget *parent,int priority): AnnouncePane(Lines{},parent,priority)
{
SingleLine(text);
}
void AnnouncePane::SingleLine(const QString &text)
{
lines.emplace_back(text,1);
}
bool AnnouncePane::event(QEvent *event)
{
if (event->type() == QEvent::Polish) Polish();
return EphemeralPane::event(event);
}
void AnnouncePane::showEvent(QShowEvent *event)
{
clock.setInterval(static_cast<std::chrono::milliseconds>(settingDuration).count());
clock.start();
EphemeralPane::showEvent(event);
}
void AnnouncePane::Interrupt()
{
clock.stop();
}
void AnnouncePane::resizeEvent(QResizeEvent *event)
{
emit Resized(event->size().width());
QWidget::resizeEvent(event);
}
void AnnouncePane::AdjustText(int width)
{
output->setText(BuildParagraph(width));
}
void AnnouncePane::Polish()
{
layout()->addWidget(output);
}
QString AnnouncePane::BuildParagraph(int width)
{
QString paragraph;
QFont font=output->font();
for (const Line &line : lines)
{
font.setPointSizeF(output->font().pointSizeF()*line.size);
int pointSize=line.text.contains(QChar{32}) ? font.pointSize() : StringConvert::RestrictFontWidth(font,line.text,width-output->margin()*2);
if (line.size == 1 && font.pointSizeF() == output->font().pointSizeF())
paragraph.append(line.text);
else
paragraph.append(QString(R"(<span style="font-size: %2pt;">%1</span>)").arg(line.text,StringConvert::Integer(pointSize)));
paragraph.append("<br>");
}
return QString(R"(<div style="line-height: 1.25;">%1</div>)").arg(paragraph);
}
ApplicationSetting& AnnouncePane::Font()
{
return settingFont;
}
ApplicationSetting& AnnouncePane::FontSize()
{
return settingFontSize;
}
ApplicationSetting& AnnouncePane::ForegroundColor()
{
return settingForegroundColor;
}
ApplicationSetting& AnnouncePane::BackgroundColor()
{
return settingBackgroundColor;
}
ApplicationSetting& AnnouncePane::AccentColor()
{
return settingAccentColor;
}
ApplicationSetting& AnnouncePane::Duration()
{
return settingDuration;
}
QString AnnouncePane::Subsystem()
{
return u"announce pane"_s;
}
AudioAnnouncePane::AudioAnnouncePane(const Lines &lines,const QString &path,QWidget *parent,int priority): AnnouncePane(lines,parent,priority), audioPlayer(BuildPlayer(this,1)), path(path)
{
if (!QFile(path).exists()) throw std::runtime_error(QString{"Audio doesn't exist ("+path+")"}.toStdString());
connect(audioPlayer,&QMediaPlayer::playbackStateChanged,this,[this](QMediaPlayer::PlaybackState state) {
if (state == QMediaPlayer::StoppedState) emit Finished();
});
connect(audioPlayer,&QMediaPlayer::durationChanged,this,&AudioAnnouncePane::DurationAvailable);
connect(audioPlayer,&QMediaPlayer::errorOccurred,this,[this](QMediaPlayer::Error error,const QString &errorString) {
Q_UNUSED(error)
emit Print(QString("Failed to play audio: %1").arg(errorString),"play audio",Subsystem());
emit Finished();
});
}
AudioAnnouncePane::AudioAnnouncePane(const QString &text,const QString &path,QWidget *parent,int priority): AudioAnnouncePane(Lines{},path,parent,priority)
{
SingleLine(text);
}
void AudioAnnouncePane::showEvent(QShowEvent *event)
{
connect(audioPlayer,&QMediaPlayer::mediaStatusChanged,this,[this,event](QMediaPlayer::MediaStatus status) {
if (status == QMediaPlayer::LoadedMedia)
{
audioPlayer->play();
EphemeralPane::showEvent(event);
return;
}
if (status == QMediaPlayer::InvalidMedia)
{
emit Print(QString("Failed to load audio: %1").arg(audioPlayer->errorString()),"load audio",Subsystem());
emit Finished();
}
event->ignore();
});
audioPlayer->setSource(QUrl::fromLocalFile(path));
}
void AudioAnnouncePane::Interrupt()
{
audioPlayer->pause();
}
QString AudioAnnouncePane::Subsystem()
{
return u"audible announce pane"_s;
}
ImageAnnouncePane::ImageAnnouncePane(const Lines &lines,const QImage &image,QWidget *parent,int priority): AnnouncePane(lines,parent,priority), view(nullptr), stack(nullptr), shadow(nullptr), image(image), pixmap(nullptr)
{
stack=new QStackedWidget(this);
qobject_cast<QStackedLayout*>(stack->layout())->setStackingMode(QStackedLayout::StackAll);
scene=new QGraphicsScene(this);
view=new QGraphicsView(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
pixmap=new QGraphicsPixmapItem(); // the scene takes ownership of the pixmap pointer
scene->addItem(pixmap);
shadow=new QGraphicsDropShadowEffect();
shadow->setBlurRadius(50); // TODO: abstract this out to a setting
shadow->setOffset(0,0);
output->setGraphicsEffect(shadow);
}
ImageAnnouncePane::ImageAnnouncePane(const QString &text,const QImage &image,QWidget *parent,int priority): ImageAnnouncePane(Lines{},image,parent,priority)
{
SingleLine(text);
}
void ImageAnnouncePane::resizeEvent(QResizeEvent *event)
{
if (!expired)
{
int coverSize=std::max(event->size().width(),event->size().height());
if (!image.isNull()) pixmap->setPixmap(QPixmap::fromImage(QImage(image).scaled(QSize(coverSize,coverSize))));
}
AnnouncePane::resizeEvent(event);
}
void ImageAnnouncePane::Polish()
{
output->setStyleSheet(StyleSheet::Colors<QLabel>(settingForegroundColor,"#00000000")); // clear background color behind text
shadow->setColor(settingAccentColor);
stack->addWidget(output);
stack->addWidget(view);
layout()->addWidget(stack);
}
QString ImageAnnouncePane::Subsystem()
{
return u"visual announcement pane"_s;
}
MultimediaAnnouncePane::MultimediaAnnouncePane(const QString &path,QWidget *parent,int priority): AnnouncePane(Lines{},parent,priority), imagePane(nullptr)
{
audioPane=new AudioAnnouncePane(Lines{},path,this,priority);
connect(audioPane,&AudioAnnouncePane::Finished,this,&MultimediaAnnouncePane::Finished);
connect(audioPane,&AudioAnnouncePane::Print,this,&MultimediaAnnouncePane::Print);
connect(imagePane,&ImageAnnouncePane::Print,this,&MultimediaAnnouncePane::Print);
}
MultimediaAnnouncePane::MultimediaAnnouncePane(const Lines &lines,const QImage &image,const QString &path,QWidget *parent,int priority): MultimediaAnnouncePane(path,parent,priority)
{
imagePane=new ImageAnnouncePane(lines,image,this,priority);
}
MultimediaAnnouncePane::MultimediaAnnouncePane(const QString &text,const QImage &image,const QString &path,QWidget *parent,int priority): MultimediaAnnouncePane(path,parent,priority)
{
imagePane=new ImageAnnouncePane(text,image,this,priority);
}
void MultimediaAnnouncePane::Polish()
{
layout()->addWidget(imagePane);
}
void MultimediaAnnouncePane::Interrupt()
{
audioPane->hide();
imagePane->hide();
}
void MultimediaAnnouncePane::showEvent(QShowEvent *event)
{
audioPane->show();
imagePane->show();
EphemeralPane::showEvent(event); // don't call AnnouncePane's show event because that will make it do AnnouncePane things which will conflict with the AudioAnnouncePane's timing
}
QString MultimediaAnnouncePane::Subsystem()
{
return u"audible and visual announcement pane"_s;
}