/
mainwindow.cpp
1399 lines (1010 loc) · 49.9 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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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 3 of the License, or
(at your option) any later version.
OpenCOR 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/>.
*******************************************************************************/
//==============================================================================
// Main window
//==============================================================================
#include "centralwidget.h"
#include "checkforupdatesdialog.h"
#include "cliutils.h"
#include "coreinterface.h"
#include "guiapplication.h"
#include "guiinterface.h"
#include "guiutils.h"
#include "i18ninterface.h"
#include "mainwindow.h"
#include "plugininterface.h"
#include "pluginmanager.h"
#include "pluginsdialog.h"
#include "preferencesdialog.h"
#include "preferencesinterface.h"
#include "viewinterface.h"
#include "windowinterface.h"
#include "windowwidget.h"
//==============================================================================
#include "ui_mainwindow.h"
//==============================================================================
#ifdef Q_OS_WIN
#include <windows.h>
#endif
//==============================================================================
#include <QAction>
#include <QCloseEvent>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QLocale>
#include <QMenu>
#include <QMenuBar>
#include <QRect>
#include <QSettings>
#include <QShortcut>
#include <QUrl>
#include <QWindow>
//==============================================================================
#ifdef Q_OS_WIN
#include <shlobj.h>
#endif
//==============================================================================
#ifdef Q_OS_MAC
#include <CoreServices/CoreServices.h>
#endif
//==============================================================================
namespace OpenCOR {
//==============================================================================
static const auto EnglishLocale = QStringLiteral("en");
static const auto FrenchLocale = QStringLiteral("fr");
//==============================================================================
MainWindow::MainWindow(const QString &pApplicationDate) :
QMainWindow(),
mGui(new Ui::MainWindow),
mApplicationDate(pApplicationDate),
mLoadedPluginPlugins(Plugins()),
mLoadedI18nPlugins(Plugins()),
mLoadedGuiPlugins(Plugins()),
mLoadedPreferencesPlugins(Plugins()),
mLoadedWindowPlugins(Plugins()),
mRawLocale(QString()),
mMenus(QMap<QString, QMenu *>()),
mFileNewMenu(0),
mViewWindowsMenu(0),
mViewSeparator(0),
mViewPlugin(0),
mDockedWindowsVisible(true),
mDockedWindowsState(QByteArray())
{
// Make sure that OpenCOR can handle a file opening request (from the
// operating system), as well as a message sent by another instance of
// itself
QObject::connect(qApp, SIGNAL(fileOpenRequest(const QString &)),
this, SLOT(openFileOrHandleUrl(const QString &)));
QObject::connect(qApp, SIGNAL(messageReceived(const QString &)),
this, SLOT(handleMessage(const QString &)));
// Handle OpenCOR URLs
// Note: we should, through our GuiApplication class (see main.cpp), be able
// to handle OpenCOR URLs (not least because we make sure that the
// OpenCOR URL scheme is set; see the call to
// registerOpencorUrlScheme() below), but our URL handler ensures that
// it will work whether the OpenCOR URL scheme is set or not (in case
// it can't be set on a given platform)...
QDesktopServices::setUrlHandler("opencor", this, "handleUrl");
// Register our OpenCOR URL scheme
registerOpencorUrlScheme();
// Create our settings object
mSettings = new QSettings();
// Create our plugin manager (which will automatically load our various
// plugins)
mPluginManager = new PluginManager();
// Retrieve some categories of plugins
foreach (Plugin *plugin, mPluginManager->loadedPlugins()) {
if (qobject_cast<PluginInterface *>(plugin->instance()))
mLoadedPluginPlugins << plugin;
if (qobject_cast<I18nInterface *>(plugin->instance()))
mLoadedI18nPlugins << plugin;
if (qobject_cast<GuiInterface *>(plugin->instance()))
mLoadedGuiPlugins << plugin;
if (qobject_cast<PreferencesInterface *>(plugin->instance()))
mLoadedPreferencesPlugins << plugin;
if (qobject_cast<WindowInterface *>(plugin->instance()))
mLoadedWindowPlugins << plugin;
}
// Set up the GUI
mGui->setupUi(this);
// Set the role of some of our menu items, so that macOS can move them into
// the application menu
mGui->actionQuit->setMenuRole(QAction::QuitRole);
mGui->actionPreferences->setMenuRole(QAction::PreferencesRole);
mGui->actionAbout->setMenuRole(QAction::AboutRole);
mGui->actionCheckForUpdates->setMenuRole(QAction::ApplicationSpecificRole);
// Title of our main window
// Note: we don't set it in our .ui file since this will require
// 'translating' it in other languages...
setWindowTitle(qAppName());
// Customise our docked windows action and handle it through a connection
// Note #1: the reason for having several shortcuts is because one or
// several of them are bound to be already used on the target
// system...
// Note #2: normally we would call setShortcuts() rather than setShortcut()
// and then manually creating several QShortcut objects, but it
// doesn't work (bug?)...
mGui->actionDockedWindows->setShortcut(QKeySequence(Qt::CTRL|Qt::Key_Space));
connect(mGui->actionDockedWindows, SIGNAL(triggered(bool)),
this, SLOT(showDockedWindows(const bool &)));
new QShortcut(QKeySequence(Qt::META|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
new QShortcut(QKeySequence(Qt::ALT|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
new QShortcut(QKeySequence(Qt::CTRL|Qt::META|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
new QShortcut(QKeySequence(Qt::CTRL|Qt::ALT|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
new QShortcut(QKeySequence(Qt::META|Qt::ALT|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
new QShortcut(QKeySequence(Qt::CTRL|Qt::META|Qt::ALT|Qt::Key_Space),
this, SLOT(toggleDockedWindows()));
// A connection to handle the status bar
connect(mGui->actionStatusBar, SIGNAL(toggled(bool)),
mGui->statusBar, SLOT(setVisible(bool)));
// Some connections to handle our various menu items
connect(mGui->actionQuit, SIGNAL(triggered(bool)),
this, SLOT(close()));
connect(mGui->actionResetAll, SIGNAL(triggered(bool)),
this, SLOT(resetAll()));
// Set the shortcuts of some actions
// Note: we do it here, so that we can use standard shortcuts (whenever
// possible)...
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
// Note: normally, we would be using QKeySequence::Quit, but we want to
// support both Alt+F4 and Ctrl+Q on Windows and Linux, and the
// default key sequence doesn't, so we set them ourselves...
mGui->actionQuit->setShortcuts(QList<QKeySequence>()
<< QKeySequence(Qt::ALT|Qt::Key_F4)
<< QKeySequence(Qt::CTRL|Qt::Key_Q));
#elif defined(Q_OS_MAC)
mGui->actionQuit->setShortcut(QKeySequence::Quit);
#else
#error Unsupported platform
#endif
#ifdef Q_OS_MAC
// A special shortcut to have OpenCOR minimised on macOS when pressing Cmd+M
// Note: indeed, when pressing Cmd+M on macOS, the active application is
// expected to minimise itself, but it doesn't using Qt only...
new QShortcut(QKeySequence(Qt::CTRL|Qt::Key_M),
this, SLOT(showMinimized()));
#endif
mGui->actionFullScreen->setShortcut(QKeySequence::FullScreen);
// Initialise the plugins themselves
foreach (Plugin *plugin, mLoadedPluginPlugins)
qobject_cast<PluginInterface *>(plugin->instance())->initializePlugin();
// Initialise the plugin further by doing things that can only be done by
// OpenCOR itself (e.g. set the central widget, create some menus)
foreach (Plugin *plugin, mPluginManager->loadedPlugins())
initializeGuiPlugin(plugin);
// Let our various plugins know that all of them have been initialised
// Note: this is important to do since the initialisation of a plugin is
// something that is done without knowing anything about other
// plugins. However, there may be things that require knowledge of
// what one or several other plugins are about, and this is something
// that can only be done once all the plugins have been initialised
// (e.g. the SimulationExperimentView plugin needs to know which
// solvers, if any, are available to it)...
foreach (Plugin *plugin, mLoadedPluginPlugins)
qobject_cast<PluginInterface *>(plugin->instance())->pluginsInitialized(mPluginManager->loadedPlugins());
// Keep track of the showing/hiding of the different window widgets
foreach (Plugin *plugin, mLoadedWindowPlugins) {
connect(qobject_cast<WindowInterface *>(plugin->instance())->windowWidget(), SIGNAL(visibilityChanged(bool)),
this, SLOT(updateDockWidgetsVisibility()));
}
// Show/hide and enable/disable the windows action depending on whether
// there are window widgets
showEnableAction(mGui->actionDockedWindows, mLoadedWindowPlugins.count());
// Retrieve the user settings from the previous session, if any
loadSettings();
// Initialise the checked state of our full screen action, since OpenCOR may
// (re)start in full screen mode
mGui->actionFullScreen->setChecked(isFullScreen());
// We are done initialising ourselves, so open/handle any file / OpenCOR URL
// we have been tracking until now
// Note: the way we open/handle those files / OpenCOR URLs ensures that we
// can still receive files / OpenCOR URLs to open/handle while we
// start opening/handling those that we have in stock, and this in the
// correct order...
GuiApplication *guiApplication = qobject_cast<GuiApplication *>(qApp);
while (guiApplication->hasFileNamesOrOpencorUrls())
openFileOrHandleUrl(guiApplication->firstFileNameOrOpencorUrl());
guiApplication->updateCanEmitFileOpenRequestSignal();
}
//==============================================================================
MainWindow::~MainWindow()
{
// Finalise our plugins
// Note: we only need to test for our default interface since we want to
// call the finalize method and this method is not overriden by any
// other interface...
foreach (Plugin *plugin, mLoadedPluginPlugins)
qobject_cast<PluginInterface *>(plugin->instance())->finalizePlugin();
// Delete our central widget
// Note: if we don't have one, then nothing will happen...
delete centralWidget();
// Delete some internal objects
delete mPluginManager;
delete mSettings;
// Delete the GUI
delete mGui;
}
//==============================================================================
void MainWindow::changeEvent(QEvent *pEvent)
{
// Default handling of the event
QMainWindow::changeEvent(pEvent);
// Do a few more things for some changes
if ( (pEvent->type() == QEvent::LocaleChange)
&& (mGui->actionSystem->isChecked())) {
// The system's locale has changed, so update OpenCOR's locale in case
// the user wants to use the system's locale
setLocale();
#ifdef Q_OS_MAC
} else if (pEvent->type() == QEvent::WindowStateChange) {
// The window state has changed, so update the checked state of our full
// screen action
// Note: useful on macOS since there is a special full screen button in
// the main window's title bar...
mGui->actionFullScreen->setChecked(isFullScreen());
#endif
}
}
//==============================================================================
void MainWindow::closeEvent(QCloseEvent *pEvent)
{
// Check with our Core plugin, if it has been loaded, that it's OK to close
bool canClose = true;
if (mPluginManager->corePlugin()) {
canClose = qobject_cast<CoreInterface *>(mPluginManager->corePlugin()->instance())->canClose();
// Note: if the Core plugin is loaded, then it means it supports the
// Core interface, so no need to check anything...
}
// Close ourselves, if possible
if (canClose) {
// Delete any Web inspector window (which may have been created through
// our use of QtWebKit)
foreach (QWindow *window, QGuiApplication::topLevelWindows()) {
if (!window->objectName().compare("QWebInspectorClassWindow"))
window->close();
}
// Keep track of our default settings
// Note: it must be done here, as opposed to the destructor, otherwise
// some settings (e.g. docked windows) won't be properly saved...
saveSettings();
// Accept the event
pEvent->accept();
} else {
// Ignore the event, i.e. don't close ourselves
pEvent->ignore();
}
}
//==============================================================================
void MainWindow::registerOpencorUrlScheme()
{
// Register our OpenCOR URL scheme
#if defined(Q_OS_WIN)
QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
QString applicationFileName = nativeCanonicalFileName(qApp->applicationFilePath());
settings.setValue("opencor/Default", QString("URL:%1 link").arg(qApp->applicationName()));
settings.setValue("opencor/Content Type", "x-scheme-handler/opencor");
settings.setValue("opencor/URL Protocol", "");
settings.setValue("opencor/DefaultIcon/Default", "\""+applicationFileName+"\",1");
settings.setValue("opencor/shell/Default", "open");
settings.setValue("opencor/shell/open/command/Default", "\""+applicationFileName+"\" \"%1\"");
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
#elif defined(Q_OS_LINUX)
if (!exec("which", QStringList() << "xdg-mime")) {
QString iconPath = nativeCanonicalFileName(QString("%1/.local/share/%2/%3/%3.png").arg(QDir::homePath(),
qApp->organizationName(),
qApp->applicationName()));
writeResourceToFile(iconPath, ":/app_icon");
writeFileContentsToFile(QString("%1/.local/share/applications/opencor.desktop").arg(QDir::homePath()),
QString("[Desktop Entry]\n"
"Type=Application\n"
"Name=%1\n"
"Exec=%2 %u\n"
"Icon=%3\n"
"Terminal=false\n"
"MimeType=x-scheme-handler/opencor\n").arg(qApp->applicationName(),
nativeCanonicalFileName(qApp->applicationFilePath()),
iconPath));
exec("xdg-mime", QStringList() << "default" << "opencor.desktop" << "x-scheme-handler/opencor");
}
#elif defined(Q_OS_MAC)
LSSetDefaultHandlerForURLScheme(CFSTR("opencor"),
CFBundleGetIdentifier(CFBundleGetMainBundle()));
#else
#error Unsupported platform
#endif
}
//==============================================================================
void MainWindow::initializeGuiPlugin(Plugin *pPlugin)
{
// Retrieve and apply the plugin's GUI settings, if any
GuiInterface *guiInterface = qobject_cast<GuiInterface *>(pPlugin->instance());
if (guiInterface) {
// Add the menus to our menu bar or merge them to existing menus, if
// needed
// Note: we must do that in reverse order since we are inserting menus,
// as opposed to appending some...
Gui::Menus guiMenus = guiInterface->guiMenus();
for (int i = guiMenus.count()-1; i >= 0; --i) {
// Insert the menu in the right place
QMenu *newMenu = guiMenus[i].menu();
QString newMenuName = newMenu->objectName();
QMenu *oldMenu = mMenus.value(newMenuName);
if (oldMenu && !guiMenus[i].action()) {
// A menu with the same name already exists, so add the contents
// of the new menu to the existing one
oldMenu->addSeparator();
oldMenu->addActions(newMenu->actions());
// Delete the new menu since we don't need it anymore
delete newMenu;
} else {
// No menu with the same name already exists (or the menu
// doesn't have a name), so add the new menu to our menu bar
switch (guiMenus[i].type()) {
case Gui::Menu::File:
// Not a relevant type, so do nothing
break;
case Gui::Menu::View:
mGui->menuBar->insertAction(mGui->menuView->menuAction(),
newMenu->menuAction());
break;
}
// Keep track of the new menu, but only if it has a name
if (!newMenuName.isEmpty())
mMenus.insert(newMenuName, newMenu);
}
}
// Add the actions/separators to our different menus
// Note: as for the menus above, we must do it in reverse order since we
// are inserting actions, as opposed to appending some...
Gui::MenuActions guiMenuActions = guiInterface->guiMenuActions();
for (int i = guiMenuActions.count()-1; i >= 0; --i) {
// Insert the action/separator to the right menu, if any
QMenu *menu = 0;
if (guiMenuActions[i].type() == Gui::MenuAction::File)
menu = mGui->menuFile;
else if (guiMenuActions[i].type() == Gui::MenuAction::Tools)
menu = mGui->menuTools;
if (menu) {
QAction *action = guiMenuActions[i].action();
if (action)
menu->insertAction(menu->actions().first(), action);
else
menu->insertSeparator(menu->actions().first());
}
}
// Make sure that our language menu item is first in our tools menu
mGui->menuTools->insertSeparator(mGui->menuTools->actions().first());
mGui->menuTools->insertAction(mGui->menuTools->actions().first(), mGui->menuLanguage->menuAction());
// Add some sub-menus before some menu items
foreach (const Gui::Menu &guiMenu, guiMenus) {
// Insert the menu before a menu item / separator
if (guiMenu.action()) {
switch (guiMenu.type()) {
case Gui::Menu::File:
mGui->menuFile->insertMenu(guiMenu.action(), guiMenu.menu());
break;
case Gui::Menu::View:
// Not a relevant type, so do nothing
break;
}
}
}
// Add some actions to some sub-menus and keep track of them
static QString pluginForFileNewMenu = QString();
foreach (const Gui::MenuAction &menuAction, guiInterface->guiMenuActions()) {
// Insert the action to the right menu
switch (menuAction.type()) {
case Gui::MenuAction::File:
// Not a relevant type, so do nothing
break;
case Gui::MenuAction::FileNew:
// Check whether the File|New menu has been created and if not,
// then create it
if (!mFileNewMenu) {
// The menu doesn't already exist, so create it
mFileNewMenu = new QMenu(this);
mFileNewMenu->menuAction()->setIcon(QIcon(":/oxygen/mimetypes/application-x-zerosize.png"));
// Add the New menu to our File menu and add a separator
// after it
mGui->menuFile->insertMenu(mGui->menuFile->actions().first(),
mFileNewMenu);
mGui->menuFile->insertSeparator(mGui->menuFile->actions()[1]);
pluginForFileNewMenu = pPlugin->name();
} else if (pluginForFileNewMenu.compare(pPlugin->name())) {
// The File|New menu already exists, so add a separator to
// it so that previous menu items (from a different plugin)
// don't get mixed up with the new one
mFileNewMenu->addSeparator();
pluginForFileNewMenu = pPlugin->name();
}
mFileNewMenu->addAction(menuAction.action());
break;
case Gui::MenuAction::Tools:
// Not a relevant type, so do nothing
break;
}
}
}
// Set our central widget, in case we are dealing with the Core plugin
if (pPlugin == mPluginManager->corePlugin()) {
// We are dealing with the Core plugin, so set our central widget
setCentralWidget(qobject_cast<CoreInterface *>(pPlugin->instance())->centralWidget());
// Note: if the Core plugin is loaded, then it means it supports the
// Core interface, so no need to check anything...
// Also keep track of GUI updates in our central widget
connect(static_cast<Core::CentralWidget *>(centralWidget()), SIGNAL(guiUpdated(Plugin *, const QString &)),
this, SLOT(updateGui(Plugin *, const QString &)));
}
// Add the plugin's window, in case we are dealing with a window plugin
WindowInterface *windowInterface = qobject_cast<WindowInterface *>(pPlugin->instance());
if (windowInterface) {
// Dock the window to its default dock area
addDockWidget(windowInterface->windowDefaultDockArea(), windowInterface->windowWidget());
// Add an action to our menu to show/hide the window
if (!pPlugin->name().compare("HelpWindow")) {
// Special case of the help window
mGui->menuHelp->insertAction(mGui->actionHomePage,
windowInterface->windowAction());
mGui->menuHelp->insertSeparator(mGui->actionHomePage);
} else {
// Update the View menu by adding the action to the
// View|Windows menu
updateViewWindowsMenu(windowInterface->windowAction());
}
// Connect the action to the window
connect(windowInterface->windowAction(), SIGNAL(triggered(bool)),
windowInterface->windowWidget(), SLOT(setVisible(bool)));
connect(windowInterface->windowWidget()->toggleViewAction(), SIGNAL(toggled(bool)),
windowInterface->windowAction(), SLOT(setChecked(bool)));
}
}
//==============================================================================
static const auto SettingsGeometry = QStringLiteral("Geometry");
static const auto SettingsState = QStringLiteral("State");
static const auto SettingsDockedWindowsVisible = QStringLiteral("DockedWindowsVisible");
static const auto SettingsDockedWindowsState = QStringLiteral("DockedWindowsState");
static const auto SettingsStatusBarVisible = QStringLiteral("StatusBarVisible");
//==============================================================================
void MainWindow::loadSettings()
{
// Retrieve and set the language to be used by OpenCOR
// Note: the setting is forced in order to account for locale-dependent
// initialisations (e.g. see CentralWidget::retranslateUi())...
setLocale(rawLocale(), true);
// Retrieve the geometry and state of the main window
if ( !restoreGeometry(mSettings->value(SettingsGeometry).toByteArray())
|| !restoreState(mSettings->value(SettingsState).toByteArray())) {
// The geometry and/or state of the main window couldn't be retrieved,
// so go with some default settings
// Default size and position of the main window
QRect desktopGeometry = qApp->desktop()->availableGeometry();
int horizSpace = desktopGeometry.width()/13;
int vertSpace = desktopGeometry.height()/13;
setGeometry(desktopGeometry.left()+horizSpace,
desktopGeometry.top()+vertSpace,
desktopGeometry.width()-2*horizSpace,
desktopGeometry.height()-2*vertSpace);
}
// Retrieve whether the docked windows are to be shown
showDockedWindows(mSettings->value(SettingsDockedWindowsVisible, true).toBool(), true);
// Retrieve the state of the docked windows
mDockedWindowsState = mSettings->value(SettingsDockedWindowsState, QByteArray()).toByteArray();
// Retrieve the settings of our various plugins
foreach (Plugin *plugin, mLoadedPluginPlugins) {
mSettings->beginGroup(SettingsPlugins);
mSettings->beginGroup(plugin->name());
qobject_cast<PluginInterface *>(plugin->instance())->loadSettings(mSettings);
mSettings->endGroup();
mSettings->endGroup();
}
// Let our core plugin know that all of the plugins have loaded their
// settings
// Note: this is similar to initializePlugin() vs. pluginsInitialized()...
if (mPluginManager->corePlugin()) {
qobject_cast<CoreInterface *>(mPluginManager->corePlugin()->instance())->settingsLoaded(mPluginManager->loadedPlugins());
// Note: if the Core plugin is loaded, then it means it supports the
// Core interface, so no need to check anything...
}
// Remove the File menu when on macOS, should no plugins be loaded
// Note: our File menu should only contain the Exit menu item, but on macOS
// that menu item gets automatically moved to the application menu...
#ifdef Q_OS_MAC
mGui->menuFile->menuAction()->setVisible(mPluginManager->loadedPlugins().count());
#endif
// Retrieve whether the status bar is to be shown
mGui->actionStatusBar->setChecked(mSettings->value(SettingsStatusBarVisible, true).toBool());
}
//==============================================================================
void MainWindow::saveSettings() const
{
// Keep track of the geometry and state of the main window
mSettings->setValue(SettingsGeometry, saveGeometry());
mSettings->setValue(SettingsState, saveState());
// Keep track of whether the docked windows are to be shown
mSettings->setValue(SettingsDockedWindowsVisible, mDockedWindowsVisible);
// Keep track of the state of the docked windows
mSettings->setValue(SettingsDockedWindowsState, mDockedWindowsState);
// Keep track of whether the status bar is to be shown
mSettings->setValue(SettingsStatusBarVisible,
mGui->statusBar->isVisible());
// Keep track of the settings of our various plugins
foreach (Plugin *plugin, mLoadedPluginPlugins) {
mSettings->beginGroup(SettingsPlugins);
mSettings->beginGroup(plugin->name());
qobject_cast<PluginInterface *>(plugin->instance())->saveSettings(mSettings);
mSettings->endGroup();
mSettings->endGroup();
}
}
//==============================================================================
void MainWindow::setLocale(const QString &pRawLocale, const bool &pForceSetting)
{
QString systemLocale = QLocale::system().name().left(2);
QString oldLocale = mRawLocale.isEmpty()?systemLocale:mRawLocale;
QString newLocale = pRawLocale.isEmpty()?systemLocale:pRawLocale;
// Keep track of the new locale, if needed
if (pRawLocale.compare(mRawLocale) || pForceSetting) {
mRawLocale = pRawLocale;
// Also keep a copy of the new raw locale in our settings (so that the
// new locale can be retrieved from plugins)
setRawLocale(mRawLocale);
}
// Check whether the new locale is different from the old one and if so,
// then retranslate everything
if (oldLocale.compare(newLocale) || pForceSetting) {
// Specify the language to be used by OpenCOR
QLocale::setDefault(QLocale(newLocale));
qApp->removeTranslator(&mQtBaseTranslator);
mQtBaseTranslator.load(QString(":/translations/qtbase_%1.qm").arg(newLocale));
qApp->installTranslator(&mQtBaseTranslator);
qApp->removeTranslator(&mQtHelpTranslator);
mQtHelpTranslator.load(QString(":/translations/qt_help_%1.qm").arg(newLocale));
qApp->installTranslator(&mQtHelpTranslator);
qApp->removeTranslator(&mQtXmlPatternsTranslator);
mQtXmlPatternsTranslator.load(QString(":/translations/qtxmlpatterns_%1.qm").arg(newLocale));
qApp->installTranslator(&mQtXmlPatternsTranslator);
qApp->removeTranslator(&mAppTranslator);
mAppTranslator.load(":/app_"+newLocale);
qApp->installTranslator(&mAppTranslator);
// Retranslate OpenCOR
mGui->retranslateUi(this);
// Retranslate some widgets that are not originally part of our user
// interface
if (mFileNewMenu)
I18nInterface::retranslateMenu(mFileNewMenu, tr("New"));
if (mViewWindowsMenu)
I18nInterface::retranslateMenu(mViewWindowsMenu, tr("Windows"));
// Update the translator of our various loaded plugins
// Note: we must update the translator of all our plugins before we can
// safely retranslate them since a plugin may require another
// plugin to work properly...
foreach (Plugin *plugin, mLoadedI18nPlugins)
qobject_cast<I18nInterface *>(plugin->instance())->updateTranslator(QString(":/%1_%2").arg(plugin->name(), newLocale));
// Retranslate our various plugins
foreach (Plugin *plugin, mLoadedI18nPlugins)
qobject_cast<I18nInterface *>(plugin->instance())->retranslateUi();
// Reorder our various View|Windows menu items, just in case
reorderViewWindowsMenu();
}
// Update the checked menu item
// Note: it has to be done every single time, since selecting a menu item
// will automatically toggle its checked status...
mGui->actionSystem->setChecked(pRawLocale.isEmpty());
mGui->actionEnglish->setChecked(!pRawLocale.compare(EnglishLocale));
mGui->actionFrench->setChecked(!pRawLocale.compare(FrenchLocale));
}
//==============================================================================
void MainWindow::reorderViewWindowsMenu()
{
// Reorder the View|Windows menu, should it exist
// Note: this is useful after having added a new menu item or after having
// changed the locale
if (mViewWindowsMenu) {
// Retrieve the title of the menu items and keep track of their actions
QStringList menuItemTitles;
QMap<QString, QAction *> menuItemActions;
foreach (QAction *menuItemAction, mViewWindowsMenu->actions()) {
// Remove any "&" present in the menu item title, as well as replace
// accentuated characters by non-accentuated ones, making the
// sorting sensible
QString menuItemTitle = menuItemAction->text().remove('&').normalized(QString::NormalizationForm_KD);
for (int i = menuItemTitle.length()-1; i >= 0; --i) {
if (menuItemTitle[i].category() == QChar::Mark_NonSpacing)
menuItemTitle.remove(i, 1);
}
// Keep track of the menu item title and the action to which it is
// associated
menuItemTitles << menuItemTitle;
menuItemActions.insert(menuItemTitle, menuItemAction);
}
// Sort the menu items
menuItemTitles.sort();
// Add the menu items actions in the new order
// Note: to use addAction will effectively 'move' the menu items to the
// end of the menu, so since we do it in the right order, we end
// up with the menu items being properly ordered...
foreach (const QString &menuItemTitle, menuItemTitles)
mViewWindowsMenu->addAction(menuItemActions.value(menuItemTitle));
}
}
//==============================================================================
void MainWindow::updateViewWindowsMenu(QAction *pAction)
{
// Check whether we need to insert a separator before the docked windows
// menu item
if (!mViewSeparator)
mViewSeparator = mGui->menuView->insertSeparator(mGui->actionDockedWindows);
// Check whether the View|Windows menu already exists and create it if not
if (!mViewWindowsMenu) {
// The View|Windows menu doesn't already exist, so create it
mViewWindowsMenu = new QMenu(this);
// Add the View|Windows menu to our View menu
mGui->menuView->insertMenu(mViewSeparator, mViewWindowsMenu);
}
// At this stage, the View|Windows menu exist, so add the given action to
// it
mViewWindowsMenu->addAction(pAction);
}
//==============================================================================
void MainWindow::showSelf()
{
// Note: to show ourselves, one would normally use activateWindow() (and
// possibly raise()), but depending on the operating system it may or
// not bring OpenCOR to the foreground, so instead we do what follows,
// depending on the operating system...
#if defined(Q_OS_WIN)
// Show ourselves the Windows way
// Retrieve OpenCOR's window Id
HWND mainWinId = reinterpret_cast<HWND>(winId());
// Bring OpenCOR to the foreground
DWORD foregroundThreadProcId = GetWindowThreadProcessId(GetForegroundWindow(), 0);
DWORD mainThreadProcId = GetWindowThreadProcessId(mainWinId, 0);
if (foregroundThreadProcId != mainThreadProcId) {
// OpenCOR's thread process Id is not that of the foreground window, so
// attach the foreground thread to OpenCOR's, set OpenCOR to the
// foreground, and detach the foreground thread from OpenCOR's
AttachThreadInput(foregroundThreadProcId, mainThreadProcId, true);
SetForegroundWindow(mainWinId);
SetFocus(mainWinId);
AttachThreadInput(foregroundThreadProcId, mainThreadProcId, false);
} else {
// OpenCOR's thread process Id is that of the foreground window, so
// just set OpenCOR to the foreground
SetForegroundWindow(mainWinId);
}
// Show/restore OpenCOR, depending on its current state
if (IsIconic(mainWinId))
ShowWindow(mainWinId, SW_RESTORE);
else
ShowWindow(mainWinId, SW_SHOW);
// Note: under Windows, to use activateWindow() will only highlight the
// application in the taskbar, since under Windows no application
// should be allowed to bring itself to the foreground when another
// application is already in the foreground. Fair enough, but it
// happens that, here, the user wants OpenCOR to be brought to the
// foreground, hence the above code to get the effect we are after...
#elif defined(Q_OS_LINUX) || defined(Q_OS_MAC)
// We are on Linux or macOS, so we can simply activate the window and raise
// ourselves
activateWindow();
raise();
#else
#error Unsupported platform
#endif
}
//==============================================================================
void MainWindow::handleArguments(const QStringList &pArguments)
{
// Handle the arguments that were passed to OpenCOR by handling them as a
// URL if they are an OpenCOR URL or by passing them to the Core plugin,
// should it be loaded
QStringList arguments = QStringList();
foreach (const QString &argument, pArguments) {
QUrl url = argument;
if (!url.scheme().compare("opencor"))