-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
Copy pathmainwindow.cpp
5317 lines (4507 loc) · 177 KB
/
mainwindow.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
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--
// This file is part of Sonic Pi: http://sonic-pi.net
// Full project source: https://github.com/samaaron/sonic-pi
// License: https://github.com/samaaron/sonic-pi/blob/main/LICENSE.md
//
// Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
// All rights reserved.
//
// Permission is granted for use, copying, modification, and
// distribution of modified versions of this work as long as this
// notice is included.
//++
// Standard stuff
#include <fstream>
#include <iostream>
#include <sstream>
// Qt stuff
#include <QtGlobal>
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QBoxLayout>
#include <QDesktopServices>
#include <QDialogButtonBox>
#include <QDockWidget>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QNetworkInterface>
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QScrollBar>
#include <QShortcut>
#include <QSplashScreen>
#include <QSplitter>
#include <QStatusBar>
#include <QStyle>
#include <QTextBrowser>
#include <QTextStream>
#include <QThread>
#include <QToolBar>
#include <QToolButton>
#include <QPushButton>
#include <QVBoxLayout>
#include "mainwindow.h"
// QScintilla stuff
#include <Qsci/qsciapis.h>
#include <Qsci/qsciscintilla.h>
#include "model/sonicpitheme.h"
#include "utils/scintilla_api.h"
#include "widgets/sonicpilexer.h"
#include "widgets/sonicpiscintilla.h"
#ifdef WITH_WEBENGINE
#include "widgets/phxwidget.h"
#endif
#include "utils/sonicpi_i18n.h"
#include "utils/borderlesslinksproxystyle.h"
#include "visualizer/scope_window.h"
#include "qt_api_client.h"
using namespace oscpkt; // OSC specific stuff
#include "model/settings.h"
#include "widgets/infowidget.h"
#include "widgets/settingswidget.h"
#include "widgets/sonicpicontext.h"
#include "widgets/sonicpilog.h"
#include "widgets/sonicpimetro.h"
#include "widgets/sonicpieditor.h"
#include "utils/ruby_help.h"
#include "dpi.h"
// Operating System Specific includes
#if defined(Q_OS_WIN)
#include <QtConcurrent/QtConcurrentRun>
#elif defined(Q_OS_MAC)
#include <QtConcurrent/QtConcurrentRun>
#else
//assuming Raspberry Pi
#include <QtConcurrentRun>
#include <cmath>
#endif
#if QT_VERSION >= 0x050400
// Requires Qt5
#include <QWindow>
#endif
using namespace std::chrono;
using namespace SonicPi;
MainWindow::MainWindow(QApplication& app, QSplashScreen* splash)
{
app.installEventFilter(this);
app.processEvents();
connect(&app, SIGNAL(aboutToQuit()), this, SLOT(onExitCleanup()));
printAsciiArtLogo();
QApplication::instance()->setAttribute(Qt::AA_DontShowIconsInMenus, true);
this->splash = splash;
// API and Client
m_spClient = std::make_shared<QtAPIClient>(this);
m_spAPI = std::make_shared<SonicPiAPI>(m_spClient.get(), APIProtocol::UDP, LogOption::File);
this->piSettings = new SonicPiSettings();
startup_error_reported = new QCheckBox;
startup_error_reported->setChecked(false);
hash_salt = "Secret Hash ;-)";
updated_dark_mode_for_help = false;
updated_dark_mode_for_prefs = false;
loaded_workspaces = false;
is_recording = false;
show_rec_icon_a = false;
restoreDocPane = false;
focusMode = false;
version = "5.0.0";
latest_version = "";
version_num = 0;
latest_version_num = 0;
bool startupOK = false;
APIInitResult init_success = m_spAPI->Init(rootPath().toStdString());
if(init_success == APIInitResult::Successful) {
} else if (init_success == APIInitResult::HomePathNotWritableError) {
std::cout << "[GUI] - API HomePath Not Writable" << std::endl;
homeDirWriteError();
} else {
std::cout << "[GUI] - API Init failed" << std::endl;
}
initPaths();
readSettings();
bool noScsynthInputs = !piSettings->enable_scsynth_inputs;
APIBootResult boot_success = m_spAPI->Boot(noScsynthInputs);
if(boot_success == APIBootResult::Successful) {
std::cout << "[GUI] - API Boot successful" << std::endl;
} else if (boot_success == APIBootResult::ScsynthBootError) {
std::cout << "[GUI] - API Scsynth Boot Failed" << std::endl;
scsynthBootError();
} else {
std::cout << "[GUI] - API Boot failed" << std::endl;
}
const QRect rect = this->geometry();
m_appWindowSizeRect = std::make_shared<QRect>(rect);
guiID = m_spAPI->GetGuid();
this->sonicPii18n = new SonicPii18n(rootPath());
std::cout << "[GUI] - Language setting: " << piSettings->language.toUtf8().constData() << std::endl;
std::cout << "[GUI] - System language: " << QLocale::system().name().toStdString() << std::endl;
this->ui_language = sonicPii18n->determineUILanguage(piSettings->language);
std::cout << "[GUI] - Using language: " << ui_language.toUtf8().constData() << std::endl;
this->i18n = sonicPii18n->loadTranslations(ui_language);
if (i18n)
{
std::cout << "[GUI] - translations available " << std::endl;
}
else
{
std::cout << "[GUI] - translations unavailable (using EN)" << std::endl;
}
std::cout << "[GUI] - hiding main window" << std::endl;
hide();
setupTheme();
lexer = new SonicPiLexer(theme);
QPalette p = theme->createPalette();
QApplication::setPalette(p);
setupWindowStructure();
loadUserShortcuts();
createStatusBar();
createInfoPane();
setWindowTitle(tr("Sonic Pi"));
createToolBar();
updateShortcuts();
updateTabsVisibility();
updateButtonVisibility();
updateLogVisibility();
updateCuesVisibility();
// The implementation of this method is dynamically generated and can
// be found in ruby_help.h:
std::cout << "[GUI] - initialising documentation window" << std::endl;
initDocsWindow();
//setup autocompletion
autocomplete->loadSamples(QString::fromStdString(m_spAPI->GetPath(SonicPiPath::SamplePath)));
QThreadPool::globalInstance()->setMaxThreadCount(3);
startupOK = m_spAPI->WaitUntilReady();
if (startupOK)
{
// We have a connection! Finish up loading app...
#ifdef WITH_WEBENGINE
QUrl phxUrl;
phxUrl.setUrl("http://localhost");
phxUrl.setPort(m_spAPI->GetPort(SonicPiPortId::phx_http));
std::cout << "[GUI] - loading up web view with URL: " << phxUrl.toString().toStdString() << std::endl;
// load phoenix webview
phxWidget->connectToTauPhx(phxUrl);
#endif
scopeWindow->Booted();
std::cout << "[GUI] - restore windows" << std::endl;
restoreWindows();
std::cout << "[GUI] - honour prefs" << std::endl;
honourPrefs();
std::cout << "[GUI] - update prefs icon" << std::endl;
updatePrefsIcon();
std::cout << "[GUI] - toggle icons" << std::endl;
toggleIcons();
std::cout << "[GUI] - full screen" << std::endl;
updateFullScreenMode();
updateColourTheme();
std::cout << "[GUI] - load workspaces" << std::endl;
loadWorkspaces();
std::cout << "[GUI] - load request Version" << std::endl;
requestVersion();
changeSystemPreAmp(piSettings->main_volume, 1);
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(heartbeatOSC()));
timer->start(1000);
emit settingsChanged();
splashClose();
focusEditor();
showWindow();
app.processEvents();
std::cout << "[GUI] - boot sequence completed." << std::endl;
}
else
{
std::cout << "[GUI] - Critical Error. Unable to connect to server.." << std::endl;
startupError("GUI was unable to connect to the Ruby server.");
}
toggleOSCServer(1);
editorTabWidget->currentWidget()->activateWindow();
showWelcomeScreen();
std::cout << "[GUI] - MainWindow initialisation completed." << std::endl;
}
void MainWindow::initPaths()
{
QString settings_path = sonicPiConfigPath() + QDir::separator() + "gui-settings.ini";
gui_settings = new QSettings(settings_path, QSettings::IniFormat);
QString root_path = rootPath();
qt_app_theme_path = QDir::toNativeSeparators(root_path + "/app/gui/qt/theme/app.qss");
qt_browser_dark_css = QDir::toNativeSeparators(root_path + "/app/gui/qt/theme/dark/doc-styles.css");
qt_browser_light_css = QDir::toNativeSeparators(root_path + "/app/gui/qt/theme/light/doc-styles.css");
qt_browser_hc_css = QDir::toNativeSeparators(root_path + "/app/gui/qt/theme/high_contrast/doc-styles.css");
}
void MainWindow::checkForStudioMode()
{
// Studio mode should always be enabled on linux
#if defined(Q_OS_LINUX)
studio_mode->setChecked(true);
return;
#else
// other operating systems need to support the project
//to enable studio mode
studio_mode->setChecked(false);
#endif
QString queryStr;
queryStr = QString("%1")
.arg(QString(QCryptographicHash::hash(QString(user_token->text() + hash_salt).toUtf8(), QCryptographicHash::Sha256).toHex()));
QStringList studioHashList = QStringList();
std::cout << "[GUI] - Fetching Studio hashes" << std::endl;
QProcess* fetchStudioHashes = new QProcess();
QStringList fetch_studio_hashes_send_args;
fetch_studio_hashes_send_args << QString::fromStdString(m_spAPI->GetPath(SonicPiPath::FetchUrlPath)) << "http://sonic-pi.net/static/info/studio-hashes.txt";
fetchStudioHashes->start(QString::fromStdString(m_spAPI->GetPath(SonicPiPath::RubyPath)), fetch_studio_hashes_send_args);
fetchStudioHashes->waitForFinished();
QTextStream stream(fetchStudioHashes->readAllStandardOutput().trimmed());
QString line = stream.readLine();
while (!line.isNull())
{
studioHashList << line;
line = stream.readLine();
};
if (studioHashList.contains(queryStr))
{
std::cout << "[GUI] - Found Studio Hash Match" << std::endl;
std::cout << "[GUI] - Enabling Studio Mode..." << std::endl;
std::cout << "[GUI] - Thank-you for supporting Sonic Pi's continued development :-)" << std::endl;
statusBar()->showMessage(tr("Studio Mode Enabled. Thank-you for supporting Sonic Pi."), 5000);
studio_mode->setChecked(true);
}
else
{
std::cout << "[GUI] - No Studio Hash Match Found" << std::endl;
statusBar()->showMessage(tr("No Matching Studio Hash Found..."), 1000);
studio_mode->setChecked(false);
}
}
void MainWindow::showWelcomeScreen()
{
if (gui_settings->value("first_time", 1).toInt() == 1)
{
QTextBrowser* startupPane = new QTextBrowser;
startupPane->setFixedSize(ScaleHeightForDPI(600), ScaleHeightForDPI(650));
startupPane->setWindowIcon(QIcon(":images/icon-smaller.png"));
startupPane->setWindowTitle(tr("Welcome to Sonic Pi"));
addUniversalCopyShortcuts(startupPane);
QString styles = ScalePxInStyleSheet(readFile(":/theme/light/doc-styles.css"));
startupPane->document()->setDefaultStyleSheet(styles);
QFile file(":/html/startup.html");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream st(&file);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
st.setEncoding(QStringConverter::Utf8);
#else
st.setCodec("UTF-8");
#endif
QString source = st.readAll();
source = source.replace("214dx", QString("%1").arg(ScaleHeightForDPI(214)));
source = source.replace("262dx", QString("%1").arg(ScaleHeightForDPI(262)));
source = source.replace("50dx", QString("%1px").arg(ScaleHeightForDPI(32)));
startupPane->setHtml(source);
docWidget->show();
docsNavTabs->setCurrentIndex(0);
helpLists[0]->setCurrentRow(0);
startupPane->show();
startupPane->raise();
startupPane->activateWindow();
incomingPane->setFixedWidth(ScaleWidthForDPI(600));
incomingPane->setFixedHeight(ScaleHeightForDPI(50));
outputPane->verticalScrollBar()->setValue(0);
}
}
void MainWindow::setupTheme()
{
// Syntax highlighting
QString themeFilename = sonicPiConfigPath() + QDir::separator() + "colour-theme.properties";
this->theme = new SonicPiTheme(this, themeFilename, rootPath());
}
void MainWindow::setupWindowStructure()
{
std::cout << "[GUI] - setting up window structure" << std::endl;
setUnifiedTitleAndToolBarOnMac(true);
setWindowIcon(QIcon(":images/icon-smaller.png"));
rec_flash_timer = new QTimer(this);
connect(rec_flash_timer, SIGNAL(timeout()), this, SLOT(toggleRecordingOnIcon()));
// Setup output and error panes
outputPane = new SonicPiLog;
incomingPane = new SonicPiLog;
errorPane = new QTextBrowser;
metroPane = new SonicPiMetro(m_spClient, m_spAPI, theme, this);
connect(metroPane, SIGNAL(linkEnabled()), this, SLOT(checkEnableLinkMenu()));
connect(metroPane, SIGNAL(linkDisabled()), this, SLOT(uncheckEnableLinkMenu()));
errorPane->setOpenExternalLinks(true);
// Window layout
editorTabWidget = new QTabWidget();
editorTabWidget->setTabsClosable(false);
editorTabWidget->setMovable(false);
editorTabWidget->setTabPosition(QTabWidget::South);
lexer->setAutoIndentStyle(SonicPiScintilla::AiMaintain);
// create workspaces and add them to the tabs
// workspace shortcuts
signalMapper = new QSignalMapper(this);
QVBoxLayout* prefsLayout = new QVBoxLayout;
prefsWidget = new QWidget;
prefsWidget->setParent(this);
prefsWidget->hide();
settingsWidget = new SettingsWidget(m_spAPI->GetPort(SonicPiPortId::tau_osc_cues), i18n, piSettings, sonicPii18n, this);
settingsWidget->setObjectName("settings");
settingsWidget->setAttribute(Qt::WA_StyledBackground, true);
connect(settingsWidget, SIGNAL(restartApp()), this, SLOT(restartApp()));
connect(settingsWidget, SIGNAL(volumeChanged(int)), this, SLOT(changeSystemPreAmp(int)));
connect(settingsWidget, SIGNAL(mixerSettingsChanged()), this, SLOT(mixerSettingsChanged()));
connect(settingsWidget, SIGNAL(enableScsynthInputsChanged()), this, SLOT(changeEnableScsynthInputs()));
connect(settingsWidget, SIGNAL(midiSettingsChanged()), this, SLOT(toggleMidi()));
connect(settingsWidget, SIGNAL(resetMidi()), this, SLOT(resetMidi()));
connect(settingsWidget, SIGNAL(oscSettingsChanged()), this, SLOT(toggleOSCServer()));
connect(settingsWidget, SIGNAL(showLineNumbersChanged()), this, SLOT(changeShowLineNumbers()));
connect(settingsWidget, SIGNAL(showAutoCompletionChanged()), this, SLOT(changeShowAutoCompletion()));
connect(settingsWidget, SIGNAL(showLogChanged()), this, SLOT(updateLogVisibility()));
connect(settingsWidget, SIGNAL(showCuesChanged()), this, SLOT(updateCuesVisibility()));
connect(settingsWidget, SIGNAL(showMetroChanged()), this, SLOT(updateMetroVisibility()));
connect(settingsWidget, SIGNAL(showButtonsChanged()), this, SLOT(updateButtonVisibility()));
connect(settingsWidget, SIGNAL(showFullscreenChanged()), this, SLOT(updateFullScreenMode()));
connect(settingsWidget, SIGNAL(showTabsChanged()), this, SLOT(updateTabsVisibility()));
connect(settingsWidget, SIGNAL(logAutoScrollChanged()), this, SLOT(updateLogAutoScroll()));
connect(settingsWidget, SIGNAL(themeChanged()), this, SLOT(updateColourTheme()));
connect(settingsWidget, SIGNAL(scopeChanged()), this, SLOT(scope()));
connect(settingsWidget, SIGNAL(scopeChanged(QString)), this, SLOT(changeScopeKindVisibility(QString)));
connect(settingsWidget, SIGNAL(scopeLabelsChanged()), this, SLOT(changeScopeLabels()));
connect(settingsWidget, SIGNAL(titlesChanged()), this, SLOT(changeTitleVisibility()));
connect(settingsWidget, SIGNAL(hideMenuBarInFullscreenChanged()), this, SLOT(changeMenuBarInFullscreenVisibility()));
connect(settingsWidget, SIGNAL(transparencyChanged(int)), this, SLOT(changeGUITransparency(int)));
connect(settingsWidget, SIGNAL(checkUpdatesChanged()), this, SLOT(update_check_updates()));
connect(settingsWidget, SIGNAL(forceCheckUpdates()), this, SLOT(check_for_updates_now()));
connect(settingsWidget, SIGNAL(showContextChanged()), this, SLOT(changeShowContext()));
connect(settingsWidget, SIGNAL(checkArgsChanged()), this, SLOT(changeAudioSafeMode()));
connect(settingsWidget, SIGNAL(synthTriggerTimingGuaranteesChanged()), this, SLOT(changeAudioTimingGuarantees()));
connect(settingsWidget, SIGNAL(enableExternalSynthsChanged()), this, SLOT(changeEnableExternalSynths()));
connect(settingsWidget, SIGNAL(midiDefaultChannelChanged()), this, SLOT(changeMidiDefaultChannel()));
connect(settingsWidget, SIGNAL(logCuesChanged()), this, SLOT(changeLogCues()));
connect(settingsWidget, SIGNAL(logSynthsChanged()), this, SLOT(changeLogSynths()));
connect(settingsWidget, SIGNAL(clearOutputOnRunChanged()), this, SLOT(changeClearOutputOnRun()));
connect(settingsWidget, SIGNAL(autoIndentOnRunChanged()), this, SLOT(changeAutoIndentOnRun()));
connect(this, SIGNAL(settingsChanged()), settingsWidget, SLOT(settingsChanged()));
scopeWindow = new ScopeWindow(m_spClient, m_spAPI, this);
scopeWindow->Pause();
scopeWindow->setObjectName("scopes");
restoreScopeState(scopeWindow->GetScopeCategories());
settingsWidget->updateScopeNames(scopeWindow->GetScopeCategories());
QHBoxLayout* prefsLabelLayout = new QHBoxLayout;
QLabel* prefsLabel = new QLabel(tr("Preferences"));
prefsLabelLayout->addStretch(1);
prefsLabelLayout->addWidget(prefsLabel);
prefsLabelLayout->addStretch(1);
prefsLayout->addLayout(prefsLabelLayout);
prefsLayout->addWidget(settingsWidget, 2) ;
QHBoxLayout* prefsButtonLayout = new QHBoxLayout;
QPushButton* prefsHidePushButton = new QPushButton(tr("Close"));
prefsHidePushButton->setObjectName("prefsHideButton");
prefsButtonLayout->addStretch(1);
prefsButtonLayout->addWidget(prefsHidePushButton);
prefsLayout->addLayout(prefsButtonLayout);
prefsWidget->setObjectName("prefs");
prefsWidget->setLayout(prefsLayout);
prefsWidget->setMinimumHeight(settingsWidget->height() + ScaleHeightForDPI(240));
prefsWidget->setMinimumWidth(settingsWidget->width() + ScaleWidthForDPI(200));
QSizePolicy prefsSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
prefsWidget->setSizePolicy(prefsSizePolicy) ;
connect(prefsHidePushButton, &QPushButton::clicked, this, [=]() {
togglePrefs();
});
bool auto_indent = piSettings->auto_indent_on_run;
for (int ws = 0; ws < workspace_max; ws++)
{
std::string s;
QString fileName = QString("workspace_") + QString::fromStdString(number_name(ws));
//TODO: this is only here to ensure auto_indent_on_run is
// initialised before using it to construct the
// workspaces. Strongly consider how to clean this up in a way
// that nicely scales for more properties such as this. This
// should only be considered an interim solution necessary to
// fix the return issue on Japanese keyboards.
SonicPiScintilla* workspace = new SonicPiScintilla(lexer, theme, fileName, auto_indent);
connect(workspace,
&SonicPiScintilla::bufferNewlineAndIndent,
this,
[this](int point_line, int point_index, int first_line, const std::string& code, const std::string& fileName)
{
m_spAPI->BufferNewLineAndIndent(point_line, point_index, first_line, code, fileName);
});
workspace->setObjectName(QString("Buffer %1").arg(ws));
//tab completion when in list
auto indentLine = new QShortcut(QKeySequence(Qt::Key_Tab), workspace);
connect(indentLine, &QShortcut::activated, this, [this, workspace]() {
completeSnippetListOrIndentLine(workspace);
});
//escape
QString w = QString(tr("| %1 |")).arg(QString::number(ws));
workspaces[ws] = workspace;
SonicPiEditor *editor = new SonicPiEditor(workspace, theme, this);
editorTabWidget->addTab(editor, w);
connect(workspace, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(updateContext(int, int)));
}
connect(signalMapper, SIGNAL(mappedInt(int)), this, SLOT(changeTab(int)));
QFont font("Monospace");
font.setStyleHint(QFont::Monospace);
lexer->setDefaultFont(font);
autocomplete = new ScintillaAPI(lexer);
// adding universal shortcuts to outputpane seems to
// steal events from doc system!?
// addUniversalCopyShortcuts(outputPane);
addUniversalCopyShortcuts(errorPane);
outputPane->setReadOnly(true);
outputPane->setLineWrapMode(QPlainTextEdit::NoWrap);
outputPane->setFontFamily("Hack");
incomingPane->setReadOnly(true);
incomingPane->setLineWrapMode(QPlainTextEdit::NoWrap);
incomingPane->setFontFamily("Hack");
errorPane->setReadOnly(true);
if (!theme->font("LogFace").isEmpty())
{
outputPane->setFontFamily(theme->font("LogFace"));
incomingPane->setFontFamily(theme->font("LogFace"));
}
outputPane->document()->setMaximumBlockCount(1000);
incomingPane->document()->setMaximumBlockCount(1000);
errorPane->document()->setMaximumBlockCount(1000);
outputPane->setTextColor(QColor(theme->color("LogForeground")));
outputPane->appendPlainText("\n");
incomingPane->setTextColor(QColor(theme->color("LogForeground")));
incomingPane->appendPlainText("\n");
errorPane->zoomIn(1);
errorPane->setFixedHeight(ScaleHeightForDPI(200));
// hudPane = new QTextBrowser;
// hudPane->setMinimumHeight(130);
// hudPane->setHtml("<center><img src=\":/images/logo.png\" height=\"113\" width=\"138\"></center>");
// hudWidget = new QDockWidget(this);
// hudWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
// hudWidget->setAllowedAreas(Qt::RightDockWidgetArea);
// hudWidget->setTitleBarWidget(new QWidget());
// addDockWidget(Qt::RightDockWidgetArea, hudWidget);
// hudWidget->setWidget(hudPane);
// hudWidget->setObjectName("hud");
scopeWidget = new QDockWidget(tr("Scope"), this);
scopeWidget->setFocusPolicy(Qt::NoFocus);
scopeWidget->setAllowedAreas(Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
scopeWidget->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
scopeWidget->setWidget(scopeWindow);
scopeWidget->setObjectName("Scope");
scopeWidget->setMinimumHeight(ScaleHeightForDPI(100));
addDockWidget(Qt::RightDockWidgetArea, scopeWidget);
connect(scopeWidget, SIGNAL(visibilityChanged(bool)), this, SLOT(scopeVisibilityChanged()));
outputWidget = new QDockWidget(tr("Log"), this);
outputWidget->setFocusPolicy(Qt::NoFocus);
outputWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
outputWidget->setAllowedAreas(Qt::RightDockWidgetArea);
outputWidget->setWidget(outputPane);
incomingWidget = new QDockWidget(tr("Cues"), this);
incomingWidget->setFocusPolicy(Qt::NoFocus);
incomingWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
incomingWidget->setAllowedAreas(Qt::RightDockWidgetArea);
incomingWidget->setWidget(incomingPane);
metroWidget = new QDockWidget(tr("Link Metronome & Global Time Warp"), this);
metroWidget->setFocusPolicy(Qt::NoFocus);
metroWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
metroWidget->setAllowedAreas(Qt::RightDockWidgetArea);
metroWidget->setMaximumHeight(ScaleHeightForDPI(110));
metroWidget->setWidget(metroPane);
addDockWidget(Qt::RightDockWidgetArea, outputWidget);
addDockWidget(Qt::RightDockWidgetArea, incomingWidget);
addDockWidget(Qt::RightDockWidgetArea, metroWidget);
outputWidget->setObjectName("output");
incomingWidget->setObjectName("input");
metroWidget->setObjectName("metro");
blankWidgetOutput = new QWidget();
blankWidgetIncoming = new QWidget();
blankWidgetScope = new QWidget();
blankWidgetDoc = new QWidget();
blankWidgetMetro = new QWidget();
docsNavTabs = new QTabWidget;
docsNavTabs->setFocusPolicy(Qt::NoFocus);
docsNavTabs->setTabsClosable(false);
docsNavTabs->setMovable(false);
docsNavTabs->setTabPosition(QTabWidget::South);
QShortcut* left = new QShortcut(Qt::Key_Left, docsNavTabs);
left->setContext(Qt::WidgetWithChildrenShortcut);
connect(left, SIGNAL(activated()), this, SLOT(docPrevTab()));
QShortcut* right = new QShortcut(Qt::Key_Right, docsNavTabs);
right->setContext(Qt::WidgetWithChildrenShortcut);
connect(right, SIGNAL(activated()), this, SLOT(docNextTab()));
#ifdef WITH_WEBENGINE
phxWidget = new PhxWidget(this);
#endif
docPane = new QTextBrowser;
QSizePolicy policy = docPane->sizePolicy();
policy.setHorizontalStretch(QSizePolicy::Maximum);
docPane->setSizePolicy(policy);
docPane->setMinimumHeight(100);
docPane->setOpenLinks(false);
docPane->setOpenExternalLinks(true);
docPane->setStyle(new BorderlessLinksProxyStyle);
connect(docPane, SIGNAL(anchorClicked(const QUrl&)), this, SLOT(docLinkClicked(const QUrl&)));
docPane->setSource(QUrl("qrc:///html/doc.html"));
addUniversalCopyShortcuts(docPane);
docsplit = new QSplitter;
docsplit->addWidget(docsNavTabs);
docsplit->addWidget(docPane);
southTabs = new QTabWidget;
southTabs->setTabPosition(QTabWidget::West);
southTabs->setTabsClosable(false);
southTabs->setMovable(false);
southTabs->addTab(docsplit, "Docs");
southTabs->setAttribute(Qt::WA_StyledBackground, true);
#ifdef WITH_WEBENGINE
southTabs->addTab(phxWidget, "Tau");
#endif
docWidget = new QDockWidget(tr("Help"), this);
docWidget->setFocusPolicy(Qt::NoFocus);
docWidget->setAllowedAreas(Qt::BottomDockWidgetArea);
docWidget->setWidget(southTabs);
docWidget->setObjectName("help");
addDockWidget(Qt::BottomDockWidgetArea, docWidget);
docWidget->hide();
//Currently causes a segfault when dragging doc pane out of main
//window:
connect(docWidget, SIGNAL(visibilityChanged(bool)), this, SLOT(toggleHelpIcon()));
mainWidgetLayout = new QVBoxLayout;
mainWidgetLayout->addWidget(editorTabWidget);
mainWidgetLayout->addWidget(errorPane);
mainWidget = new QWidget;
mainWidget->setFocusPolicy(Qt::NoFocus);
errorPane->hide();
mainWidget->setLayout(mainWidgetLayout);
mainWidget->setObjectName("mainWidget");
setCentralWidget(mainWidget);
}
void MainWindow::docLinkClicked(const QUrl& url)
{
QString link = url.toDisplayString();
std::cout << "[GUI] Link clicked: " << link.toStdString() << std::endl;
if (url.scheme() == "sonicpi")
{
handleCustomUrl(url);
}
else if (url.isRelative() || url.isLocalFile() || url.scheme() == "qrc")
{
docPane->setSource(url);
}
else
{
QDesktopServices::openUrl(url);
}
}
void MainWindow::handleCustomUrl(const QUrl& url)
{
if (url.host() == "play-sample")
{
QString sample = url.path();
sample.remove(QRegularExpression("^/"));
QString code = "use_debug false\n"
"use_real_time\n"
"sample :"
+ sample;
Message msg("/run-code");
msg.pushInt32(guiID);
msg.pushStr(code.toStdString());
if (sendOSC(msg))
{
statusBar()->showMessage(tr("Playing Sample..."), 1000);
}
}
}
void MainWindow::escapeWorkspaces()
{
errorPane->hide();
for (int w = 0; w < workspace_max; w++)
{
workspaces[w]->escapeAndCancelSelection();
workspaces[w]->clearLineMarkers();
}
getCurrentWorkspace()->setFocus();
}
void MainWindow::changeTab(int id)
{
editorTabWidget->setCurrentIndex(id);
}
void MainWindow::toggleFullScreenMode()
{
piSettings->full_screen = !piSettings->full_screen;
emit settingsChanged();
updateFullScreenMode();
}
void MainWindow::fullScreenMenuChanged()
{
piSettings->full_screen = fullScreenAct->isChecked();
emit settingsChanged();
updateFullScreenMode();
}
void MainWindow::shortcutModeMenuChanged(int modeID)
{
if (modeID == 2)
{
piSettings->shortcut_mode = 2;
}
else if (modeID == 3)
{
piSettings->shortcut_mode = 3;
}
else if (modeID == 4)
{
piSettings->shortcut_mode = 4;
}
else
{
// default
piSettings->shortcut_mode = 1;
}
emit settingsChanged();
updateShortcuts();
}
void MainWindow::blankTitleBars()
{
statusBar()->showMessage(tr("Hiding pane titles..."), 2000);
outputWidget->setTitleBarWidget(blankWidgetOutput);
incomingWidget->setTitleBarWidget(blankWidgetIncoming);
scopeWidget->setTitleBarWidget(blankWidgetScope);
docWidget->setTitleBarWidget(blankWidgetDoc);
metroWidget->setTitleBarWidget(blankWidgetMetro);
}
void MainWindow::namedTitleBars()
{
statusBar()->showMessage(tr("Showing pane titles..."), 2000);
outputWidget->setTitleBarWidget(0);
incomingWidget->setTitleBarWidget(0);
scopeWidget->setTitleBarWidget(0);
docWidget->setTitleBarWidget(0);
metroWidget->setTitleBarWidget(0);
}
void MainWindow::updateFullScreenMode()
{
QSignalBlocker blocker(fullScreenAct);
fullScreenAct->setChecked(piSettings->full_screen);
if (piSettings->full_screen && !fullScreenMode)
{
//switch to full screen mode
std::cout << "[GUI] - switch into full screen mode." << std::endl;
#if defined(Q_OS_WIN)
QRect rect = this->geometry();
m_appWindowSizeRect.reset(new QRect(rect));
QRect screenRect = this->screen()->availableGeometry();
this->setGeometry(screenRect.x()-1, screenRect.y()-1, screenRect.width()+2, screenRect.height()+2);
this->setWindowFlags(Qt::FramelessWindowHint);
#else
this->showFullScreen();
#endif
fullScreenMode = true;
}
else if (!piSettings->full_screen && fullScreenMode)
{
//switch out of full screen mode
std::cout << "[GUI] - switch out of full screen mode." << std::endl;
menuBar()->show();
#ifdef Q_OS_WIN
this->setWindowFlags(Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
this->setWindowFlags(windowFlags() & ~Qt::FramelessWindowHint);
this->setGeometry(*m_appWindowSizeRect.get());
this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
#else
this->showNormal();
#endif
statusBar()->showMessage(tr("Full screen mode off."), 2000);
fullScreenMode = false;
}
changeMenuBarInFullscreenVisibility();
this->show();
}
void MainWindow::toggleFocusMode()
{
focusMode = !focusMode;
updateFocusMode();
}
void MainWindow::updateFocusMode()
{
if (focusMode)
{
piSettings->full_screen = true;
piSettings->show_tabs = false;
piSettings->show_buttons = false;
piSettings->show_log = false;
piSettings->show_cues = false;
}
else
{
piSettings->full_screen = false;
piSettings->show_tabs = true;
piSettings->show_buttons = true;
piSettings->show_cues = true;
}
emit settingsChanged();
updateFullScreenMode();
updateTabsVisibility();
updateButtonVisibility();
updateLogVisibility();
updateCuesVisibility();
}
void MainWindow::toggleScopePaused()
{
scopeWindow->TogglePause();
}
void MainWindow::allJobsCompleted()
{
scopeWindow->Pause();
// re-enable log text selection
incomingPane->setTextInteractionFlags(Qt::TextSelectableByMouse);
outputPane->setTextInteractionFlags(Qt::TextSelectableByMouse);
}
void MainWindow::toggleLogVisibility()
{
piSettings->show_log = !piSettings->show_log;
emit settingsChanged();
updateLogVisibility();
}
void MainWindow::toggleCuesVisibility()
{
piSettings->show_cues = !piSettings->show_cues;
emit settingsChanged();
updateCuesVisibility();
}
void MainWindow::updateLogVisibility()
{
QSignalBlocker blocker(showLogAct);
showLogAct->setChecked(piSettings->show_log);
if (piSettings->show_log)
{
outputWidget->show();
}
else
{
outputWidget->hide();
}
}
void MainWindow::showCuesMenuChanged()
{
piSettings->show_cues = showCuesAct->isChecked();
emit settingsChanged();
updateCuesVisibility();
}
void MainWindow::showMetroChanged()
{
piSettings->show_metro = showMetroAct->isChecked();
emit settingsChanged();
updateMetroVisibility();
}
void MainWindow::showLogMenuChanged()
{
piSettings->show_log = showLogAct->isChecked();
emit settingsChanged();
updateLogVisibility();
}
void MainWindow::updateCuesVisibility()
{
QSignalBlocker blocker(showCuesAct);
showCuesAct->setChecked(piSettings->show_cues);
if (piSettings->show_cues)
{
incomingWidget->show();
}
else
{
incomingWidget->hide();
}
}
void MainWindow::updateMetroVisibility()
{
QSignalBlocker blocker(showMetroAct);
showMetroAct->setChecked(piSettings->show_metro);
if (piSettings->show_metro)
{
metroWidget->show();
}
else