52
52
#include " widgets/sonicpilexer.h"
53
53
#include " widgets/sonicpiscintilla.h"
54
54
55
+ #include " utils/sonic_pi_i18n.h"
56
+
55
57
#include " utils/borderlesslinksproxystyle.h"
56
58
57
59
// OSC stuff
@@ -105,9 +107,9 @@ using namespace SonicPi;
105
107
#endif
106
108
107
109
#ifdef Q_OS_MAC
108
- MainWindow::MainWindow (QApplication& app, bool i18n, QMainWindow* splash)
110
+ MainWindow::MainWindow (QApplication& app, QMainWindow* splash)
109
111
#else
110
- MainWindow::MainWindow (QApplication& app, bool i18n, QSplashScreen* splash)
112
+ MainWindow::MainWindow (QApplication& app, QSplashScreen* splash)
111
113
#endif
112
114
{
113
115
app.installEventFilter (this );
@@ -118,9 +120,6 @@ MainWindow::MainWindow(QApplication& app, bool i18n, QSplashScreen* splash)
118
120
119
121
this ->piSettings = new SonicPiSettings ();
120
122
121
- this ->splash = splash;
122
- this ->i18n = i18n;
123
-
124
123
#ifdef QT_OLD_API
125
124
sonicPiOSCServer = NULL ;
126
125
#else
@@ -153,7 +152,6 @@ MainWindow::MainWindow(QApplication& app, bool i18n, QSplashScreen* splash)
153
152
version_num = 0 ;
154
153
latest_version_num = 0 ;
155
154
this ->splash = splash;
156
- this ->i18n = i18n;
157
155
#ifdef QT_OLD_API
158
156
guiID = QUuid::createUuid ().toString ();
159
157
#endif
@@ -163,6 +161,10 @@ MainWindow::MainWindow(QApplication& app, bool i18n, QSplashScreen* splash)
163
161
initPaths ();
164
162
165
163
bool startupOK = false ;
164
+ this ->sonicPii18n = new SonicPii18n (rootPath ());
165
+ this ->ui_language = sonicPii18n->determineUILanguage (piSettings->language );
166
+ std::cout << " Using language: " << ui_language.toUtf8 ().constData () << std::endl;
167
+ this ->i18n = sonicPii18n->loadTranslations (ui_language);
166
168
167
169
#ifdef QT_OLD_API
168
170
// Clear out old tasks from previous sessions if they still exist
@@ -579,7 +581,8 @@ void MainWindow::setupWindowStructure()
579
581
prefsWidget->setAllowedAreas (Qt::RightDockWidgetArea);
580
582
prefsWidget->setFeatures (QDockWidget::DockWidgetClosable);
581
583
582
- settingsWidget = new SettingsWidget (server_osc_cues_port, piSettings, this );
584
+ settingsWidget = new SettingsWidget (server_osc_cues_port, i18n, piSettings, sonicPii18n, this );
585
+ connect (settingsWidget, SIGNAL (uiLanguageChanged (QString)), this , SLOT (changeUILanguage (QString)));
583
586
connect (settingsWidget, SIGNAL (volumeChanged (int )), this , SLOT (changeSystemPreAmp (int )));
584
587
connect (settingsWidget, SIGNAL (mixerSettingsChanged ()), this , SLOT (mixerSettingsChanged ()));
585
588
connect (settingsWidget, SIGNAL (midiSettingsChanged ()), this , SLOT (toggleMidi ()));
@@ -2179,6 +2182,43 @@ void MainWindow::changeSystemPreAmp(int val, int silent)
2179
2182
statusBar ()->showMessage (tr (" Updating System Volume..." ), 2000 );
2180
2183
}
2181
2184
2185
+ // TODO: Implement real-time language switching
2186
+ void MainWindow::changeUILanguage (QString lang) {
2187
+ if (lang != piSettings->language ) {
2188
+ std::cout << " Current language: " << piSettings->language .toUtf8 ().constData () << std::endl;
2189
+ std::cout << " New language selected: " << lang.toUtf8 ().constData () << std::endl;
2190
+ QString old_lang = sonicPii18n->getNativeLanguageName (piSettings->language );
2191
+ QString new_lang = sonicPii18n->getNativeLanguageName (lang);
2192
+
2193
+ // Load new language
2194
+ // QString language = sonicPii18n->determineUILanguage(lang);
2195
+ // sonicPii18n->loadTranslations(language);
2196
+ // QString title_new = tr("Updated the UI language from %s to %s").arg();
2197
+
2198
+ QMessageBox msgBox (this );
2199
+ msgBox.setText (QString (tr (" You've selected a new language: %1" )).arg (new_lang));
2200
+ msgBox.setInformativeText (tr (" Do you want to apply this language?\n Applying the new language will restart Sonic Pi." ));
2201
+ QPushButton *restartButton = msgBox.addButton (tr (" Apply and Restart" ), QMessageBox::ActionRole);
2202
+ QPushButton *dismissButton = msgBox.addButton (tr (" Cancel" ), QMessageBox::RejectRole);
2203
+ msgBox.setDefaultButton (restartButton);
2204
+ msgBox.setIcon (QMessageBox::Information);
2205
+ msgBox.exec ();
2206
+
2207
+ if (msgBox.clickedButton () == (QAbstractButton*)restartButton) {
2208
+ piSettings->language = lang;
2209
+ writeSettings ();
2210
+ restartApp ();
2211
+ // statusBar()->showMessage(tr("Updated UI language setting, please restart Sonic Pi to apply it"), 2000);
2212
+ } else if (msgBox.clickedButton () == (QAbstractButton*)dismissButton) {
2213
+ // Don't apply the new language settings
2214
+ settingsWidget->updateSelectedUILanguage (piSettings->language );
2215
+ }
2216
+
2217
+ // Load previously set language
2218
+ // sonicPii18n->loadTranslations(ui_language);
2219
+ }
2220
+ }
2221
+
2182
2222
void MainWindow::changeScopeKindVisibility (QString name)
2183
2223
{
2184
2224
foreach (QAction* action, scopeKindVisibilityMenu->actions ())
@@ -3500,6 +3540,8 @@ void MainWindow::readSettings()
3500
3540
QSettings settings (QSettings::IniFormat, QSettings::UserScope, " sonic-pi.net" , " gui-settings" );
3501
3541
3502
3542
// Read in preferences from previous session
3543
+ piSettings->language = settings.value (" prefs/language" , " system_locale" ).toString ();
3544
+
3503
3545
piSettings->show_buttons = settings.value (" prefs/show-buttons" , true ).toBool ();
3504
3546
piSettings->show_tabs = settings.value (" prefs/show-tabs" , true ).toBool ();
3505
3547
piSettings->show_log = settings.value (" prefs/show-log" , true ).toBool ();
@@ -3549,6 +3591,7 @@ void MainWindow::writeSettings()
3549
3591
{
3550
3592
std::cout << " [GUI] - writing settings" << std::endl;
3551
3593
QSettings settings (QSettings::IniFormat, QSettings::UserScope, " sonic-pi.net" , " gui-settings" );
3594
+ settings.setValue (" prefs/language" , piSettings->language );
3552
3595
settings.setValue (" pos" , pos ());
3553
3596
settings.setValue (" size" , size ());
3554
3597
settings.setValue (" first_time" , 0 );
@@ -3732,6 +3775,31 @@ void MainWindow::onExitCleanup()
3732
3775
#endif
3733
3776
}
3734
3777
3778
+ void MainWindow::restartApp () {
3779
+ QApplication* app = dynamic_cast <QApplication*>(parent ());
3780
+ statusBar ()->showMessage (tr (" Restarting Sonic Pi..." ), 10000 );
3781
+ // Save settings and perform some cleanup
3782
+ writeSettings ();
3783
+ onExitCleanup ();
3784
+ sleep (1 );
3785
+ std::cout << " Performing application restart... please wait..." << std::endl;
3786
+ // this->hide(); // So it doesn't look like the app's frozen or crashed
3787
+ sleep (4 ); // Allow cleanup to complete
3788
+ // Create new process
3789
+ QStringList args = qApp->arguments ();
3790
+ args.removeFirst ();
3791
+ QProcess process;
3792
+ bool restart_success = process.startDetached (qApp->arguments ()[0 ], args);
3793
+ if (restart_success) {
3794
+ std::cout << " Successfully restarted sonic-pi" << std::endl;
3795
+ } else {
3796
+ std::cout << " Failed to restart sonic-pi" << std::endl;
3797
+ }
3798
+ // Quit
3799
+ app->exit (0 );
3800
+ exit (0 );
3801
+ }
3802
+
3735
3803
#ifdef QT_OLD_API
3736
3804
void MainWindow::cleanupRunningProcesses ()
3737
3805
{
0 commit comments