/
centralwidget.cpp
2185 lines (1586 loc) · 76.6 KB
/
centralwidget.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/>.
*******************************************************************************/
//==============================================================================
// Central widget
//==============================================================================
#include "centralwidget.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "filehandlinginterface.h"
#include "filemanager.h"
#include "guiinterface.h"
#include "interfaces.h"
#include "tabbarwidget.h"
#include "usermessagewidget.h"
#include "viewinterface.h"
#include "viewwidget.h"
//==============================================================================
#include <QApplication>
#include <QDesktopWidget>
#include <QDialogButtonBox>
#include <QDragEnterEvent>
#include <QFile>
#include <QFileInfo>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QMimeData>
#include <QPushButton>
#include <QRect>
#include <QSettings>
#include <QSizePolicy>
#include <QStackedWidget>
#include <QStatusBar>
#include <QUrl>
#include <QVariant>
#include <QVBoxLayout>
//==============================================================================
namespace OpenCOR {
namespace Core {
//==============================================================================
CentralWidgetMode::CentralWidgetMode(CentralWidget *pOwner) :
mEnabled(false),
mViewPlugins(Plugins())
{
// Initialise a few internal objects
mViewTabs = pOwner->newTabBarWidget(QTabBar::RoundedEast);
}
//==============================================================================
CentralWidgetMode::~CentralWidgetMode()
{
// Delete some internal objects
delete mViewTabs;
}
//==============================================================================
bool CentralWidgetMode::isEnabled() const
{
// Return whether the mode is enabled
return mEnabled;
}
//==============================================================================
void CentralWidgetMode::setEnabled(const bool &pEnabled)
{
// Set whether a mode is enabled
mEnabled = pEnabled;
}
//==============================================================================
TabBarWidget * CentralWidgetMode::viewTabs() const
{
// Return our view tabs
return mViewTabs;
}
//==============================================================================
Plugins CentralWidgetMode::viewPlugins() const
{
// Return our view plugins
return mViewPlugins;
}
//==============================================================================
void CentralWidgetMode::addViewPlugin(Plugin *pViewPlugin)
{
// Add the given view plugin to our list
mViewPlugins << pViewPlugin;
}
//==============================================================================
CentralWidget::CentralWidget(QWidget *pParent) :
Widget(pParent),
mState(Starting),
mSettings(0),
mLoadedFileHandlingPlugins(Plugins()),
mLoadedFileTypePlugins(Plugins()),
mLoadedGuiPlugins(Plugins()),
mLoadedViewPlugins(Plugins()),
mModeTabIndexModes(QMap<int, ViewInterface::Mode>()),
mModeModeTabIndexes(QMap<ViewInterface::Mode, int>()),
mFileModeTabIndexes(QMap<QString, int>()),
mFileModeViewTabIndexes(QMap<QString, QMap<int, int>>()),
mFileNames(QStringList()),
mModes(QMap<ViewInterface::Mode, CentralWidgetMode *>()),
mRemoteLocalFileNames(QMap<QString, QString>()),
mViews(QMap<QString, QWidget *>())
{
// Create and set our horizontal layout
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setContentsMargins(QMargins());
layout->setSpacing(0);
setLayout(layout);
// Allow for things to be dropped on us
setAcceptDrops(true);
// Create our modes tab bar with no tabs by default
mModeTabs = newTabBarWidget(QTabBar::RoundedWest);
// Create our modes
#ifdef ENABLE_SAMPLE_PLUGINS
mModes.insert(ViewInterface::SampleMode, new CentralWidgetMode(this));
#endif
#ifdef ENABLE_TEST_PLUGINS
mModes.insert(ViewInterface::TestMode, new CentralWidgetMode(this));
#endif
mModes.insert(ViewInterface::EditingMode, new CentralWidgetMode(this));
mModes.insert(ViewInterface::SimulationMode, new CentralWidgetMode(this));
mModes.insert(ViewInterface::AnalysisMode, new CentralWidgetMode(this));
// Note: these will be deleted in CentralWidget's destructor...
// Create our files tab bar widget
mFileTabs = newTabBarWidget(QTabBar::RoundedNorth, true);
// Create our contents widget
mContents = new QStackedWidget(this);
mContents->setFrameShape(QFrame::StyledPanel);
// Create our logo view which simply displays OpenCOR's logo
mLogoView = new QWidget(this);
mLogoView->setStyleSheet("QWidget {"
" background-color: white;"
" background-image: url(:/Core/logo.png);"
" background-position: center;"
" background-repeat: no-repeat;"
"}");
mContents->addWidget(mLogoView);
// Create our no view message widget which will display a customised message
// to let the user know that there view doesn't support the type of the
// current file
mNoViewMsg = new UserMessageWidget(":/oxygen/actions/help-about.png", this);
mNoViewMsg->setVisible(false);
// Create and set up our central widget
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *centralWidgetVBoxLayout = new QVBoxLayout(centralWidget);
centralWidgetVBoxLayout->setContentsMargins(QMargins());
centralWidgetVBoxLayout->setSpacing(0);
centralWidget->setLayout(centralWidgetVBoxLayout);
centralWidgetVBoxLayout->addWidget(mFileTabs);
centralWidgetVBoxLayout->addWidget(mContents);
// Add the widgets to our layout
layout->addWidget(mModeTabs);
layout->addWidget(centralWidget);
foreach (CentralWidgetMode *mode, mModes)
layout->addWidget(mode->viewTabs());
// A connection to handle the case where a file was created or duplicated
FileManager *fileManagerInstance = FileManager::instance();
connect(fileManagerInstance, SIGNAL(fileCreated(const QString &, const QString &)),
this, SLOT(fileCreated(const QString &, const QString &)));
connect(fileManagerInstance, SIGNAL(fileDuplicated(const QString &)),
this, SLOT(fileDuplicated(const QString &)));
// Some connections to handle changes to a file
connect(fileManagerInstance, SIGNAL(filePermissionsChanged(const QString &)),
this, SLOT(filePermissionsChanged(const QString &)));
connect(fileManagerInstance, SIGNAL(fileModified(const QString &)),
this, SLOT(fileModified(const QString &)));
// Some connections to handle our files tab bar
connect(mFileTabs, SIGNAL(currentChanged(int)),
this, SLOT(updateGui()));
connect(mFileTabs, SIGNAL(tabMoved(int, int)),
this, SLOT(moveFile(const int &, const int &)));
connect(mFileTabs, SIGNAL(tabCloseRequested(int)),
this, SLOT(closeFile(const int &)));
// A connection to handle our modes tab bar
connect(mModeTabs, SIGNAL(currentChanged(int)),
this, SLOT(updateGui()));
connect(mModeTabs, SIGNAL(currentChanged(int)),
this, SLOT(updateFileTabIcons()));
// Some connections to handle our mode views tab bar
foreach (CentralWidgetMode *mode, mModes) {
connect(mode->viewTabs(), SIGNAL(currentChanged(int)),
this, SLOT(updateGui()));
connect(mode->viewTabs(), SIGNAL(currentChanged(int)),
this, SLOT(updateFileTabIcons()));
}
// Create our remote file dialog
//---GRY--- THE ORIGINAL PLAN WAS TO HAVE A REGULAR EXPRESSION TO VALIDATE A
// URL, BUT IT LOOKS LIKE THERE MIGHT BE AN ISSUE WITH
// QRegularExpressionValidator, SO WE SIMPLY ALLOW FREE TEXT FOR NOW
// (SEE https://bugreports.qt.io/browse/QTBUG-38034)
mRemoteFileDialog = new Dialog(this);
QGridLayout *dialogLayout = new QGridLayout(mRemoteFileDialog);
mRemoteFileDialog->setLayout(dialogLayout);
mRemoteFileDialogUrlLabel = new QLabel(mRemoteFileDialog);
mRemoteFileDialogUrlValue = new QLineEdit(mRemoteFileDialog);
mRemoteFileDialogUrlValue->setMinimumWidth(qApp->desktop()->availableGeometry().width()/5);
dialogLayout->addWidget(mRemoteFileDialogUrlLabel, 0, 0);
dialogLayout->addWidget(mRemoteFileDialogUrlValue, 0, 1);
mRemoteFileDialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Open|QDialogButtonBox::Cancel, this);
dialogLayout->addWidget(mRemoteFileDialogButtonBox, 1, 0, 1, 2);
dialogLayout->setSizeConstraint(QLayout::SetFixedSize);
openRemoteFileChanged();
// Some connections to handle our remote file dialog
connect(mRemoteFileDialogUrlValue, SIGNAL(textChanged(const QString &)),
this, SLOT(openRemoteFileChanged()));
connect(mRemoteFileDialogButtonBox, SIGNAL(accepted()),
this, SLOT(doOpenRemoteFile()));
connect(mRemoteFileDialogButtonBox, SIGNAL(rejected()),
this, SLOT(cancelOpenRemoteFile()));
}
//==============================================================================
CentralWidget::~CentralWidget()
{
// We are shutting down, so we must let updateGui() know about it otherwise
// we may get a segmentation fault (should there be a need to switch from
// one view to another)
mState = Stopping;
// Close all the files
// Note: we force the closing of all the files since canClose() will have
// been called before to decide what needs to be done with modified
// files...
closeAllFiles(true);
// Delete our various modes
foreach (CentralWidgetMode *mode, mModes)
delete mode;
}
//==============================================================================
static const auto SettingsFileNamesOrUrls = QStringLiteral("FileNamesOrUrls");
static const auto SettingsCurrentFileNameOrUrl = QStringLiteral("CurrentFileNameOrUrl");
static const auto SettingsFileIsRemote = QStringLiteral("FileIsRemote%1");
static const auto SettingsFileMode = QStringLiteral("FileMode%1");
static const auto SettingsFileModeView = QStringLiteral("FileModeView%1%2");
//==============================================================================
void CentralWidget::loadSettings(QSettings *pSettings)
{
// Keep track of our settings
mSettings = pSettings;
// Some connections to handle an external change in the state of a file
// Note: we do it here because we want other plugins to get a chance to
// handle our file manager's signals before us. Indeed, in the case of
// the CellML file manager for example, we will want the CellML file
// manager to reload a CellML file before our different views get
// asked (by us, the central widget) to reload the file (i.e. update
// their GUIs)...
FileManager *fileManagerInstance = FileManager::instance();
connect(fileManagerInstance, SIGNAL(fileChanged(const QString &, const bool &, const bool &)),
this, SLOT(fileChanged(const QString &, const bool &, const bool &)));
connect(fileManagerInstance, SIGNAL(fileDeleted(const QString &)),
this, SLOT(fileDeleted(const QString &)));
// Some connections to handle an internal change in the state of a file
// Note: we do it here for the same reason as above (see the note for the
// connections to handle an external change in the state of a file)...
connect(fileManagerInstance, SIGNAL(fileModified(const QString &)),
this, SLOT(updateModifiedSettings()));
connect(fileManagerInstance, SIGNAL(fileReloaded(const QString &, const bool &)),
this, SLOT(fileReloaded(const QString &, const bool &)));
connect(fileManagerInstance, SIGNAL(fileRenamed(const QString &, const QString &)),
this, SLOT(fileRenamed(const QString &, const QString &)));
connect(fileManagerInstance, SIGNAL(fileSaved(const QString &)),
this, SLOT(fileSaved(const QString &)));
// Let the user know of a few default things about ourselves by emitting a
// few signals
emit canSave(false);
emit canSaveAs(false);
emit canSaveAll(false);
emit atLeastOneFile(false);
emit atLeastTwoFiles(false);
// Retrieve and open the files that were previously opened
QStringList fileNamesOrUrls = pSettings->value(SettingsFileNamesOrUrls).toStringList();
foreach (const QString &fileNameOrUrl, fileNamesOrUrls) {
if (pSettings->value(SettingsFileIsRemote.arg(fileNameOrUrl)).toBool())
openRemoteFile(fileNameOrUrl, false);
else
openFile(fileNameOrUrl);
}
// Retrieve the selected modes and views of our different files
foreach (const QString &fileName, mFileNames) {
QString fileNameOrUrl = fileManagerInstance->isRemote(fileName)?
fileManagerInstance->url(fileName):
fileName;
ViewInterface::Mode fileMode = ViewInterface::modeFromString(pSettings->value(SettingsFileMode.arg(fileNameOrUrl)).toString());
if (fileMode != ViewInterface::UnknownMode)
mFileModeTabIndexes.insert(fileName, mModeModeTabIndexes.value(fileMode));
QMap<int, int> modeViewTabIndexes = QMap<int, int>();
for (int i = 0, iMax = mModeTabs->count(); i < iMax; ++i) {
fileMode = mModeTabIndexModes.value(i);
QString viewPluginName = pSettings->value(SettingsFileModeView.arg(fileNameOrUrl, ViewInterface::modeAsString(fileMode))).toString();
Plugins viewPlugins = mModes.value(fileMode)->viewPlugins();
for (int j = 0, jMax = viewPlugins.count(); j < jMax; ++j) {
if (!viewPluginName.compare(viewPlugins[j]->name())) {
modeViewTabIndexes.insert(i, j);
break;
}
}
}
mFileModeViewTabIndexes.insert(fileName, modeViewTabIndexes);
}
// Select the previously selected file, if it still exists, by pretending to
// open it (which, in turn, will select the file)
QString crtFileNameOrUrl = pSettings->value(SettingsCurrentFileNameOrUrl).toString();
QString crtFileName = pSettings->value(SettingsFileIsRemote.arg(crtFileNameOrUrl)).toBool()?
mRemoteLocalFileNames.value(crtFileNameOrUrl):
crtFileNameOrUrl;
if (mFileNames.contains(crtFileName)) {
openFile(crtFileName);
} else {
// The previously selected file doesn't exist anymore, so select the
// first file (otherwise the last file will be selected)
mFileTabs->setCurrentIndex(0);
}
// Retrieve the seleted modes and views, in case there are no files
if (mFileNames.isEmpty()) {
ViewInterface::Mode fileMode = ViewInterface::modeFromString(pSettings->value(SettingsFileMode.arg(QString())).toString());
if (fileMode != ViewInterface::UnknownMode)
mModeTabs->setCurrentIndex(mModeModeTabIndexes.value(fileMode));
for (int i = 0, iMax = mModeTabs->count(); i < iMax; ++i) {
fileMode = mModeTabIndexModes.value(i);
CentralWidgetMode *mode = mModes.value(fileMode);
QString viewPluginName = pSettings->value(SettingsFileModeView.arg(QString(), ViewInterface::modeAsString(fileMode))).toString();
Plugins viewPlugins = mode->viewPlugins();
for (int j = 0, jMax = viewPlugins.count(); j < jMax; ++j) {
if (!viewPluginName.compare(viewPlugins[j]->name())) {
mode->viewTabs()->setCurrentIndex(j);
break;
}
}
}
}
}
//==============================================================================
void CentralWidget::saveSettings(QSettings *pSettings) const
{
// Remove all the settings related to previously opened files
static const QString SettingsFileIsRemoteHeader = SettingsFileIsRemote.arg(QString());
static const QString SettingsFileModeHeader = SettingsFileMode.arg(QString());
static const QString SettingsFileModeViewHeader = SettingsFileModeView.arg(QString(), QString());
foreach (const QString &key, pSettings->allKeys()) {
if ( key.startsWith(SettingsFileIsRemoteHeader)
|| key.startsWith(SettingsFileModeHeader)
|| key.startsWith(SettingsFileModeViewHeader)) {
pSettings->remove(key);
}
}
// Keep track of the files that are opened, skipping new files
FileManager *fileManagerInstance = FileManager::instance();
QStringList fileNames = QStringList();
QStringList fileNamesOrUrls = QStringList();
foreach (const QString &fileName, mFileNames) {
if (!fileManagerInstance->isNew(fileName)) {
// The file is not new, so keep track of it, as well as of whether
// it's a remote file
bool fileIsRemote = fileManagerInstance->isRemote(fileName);
if (fileIsRemote)
fileNamesOrUrls << fileManagerInstance->url(fileName);
else
fileNamesOrUrls << fileName;
fileNames << fileName;
pSettings->setValue(SettingsFileIsRemote.arg(fileNamesOrUrls.last()), fileIsRemote);
}
}
pSettings->setValue(SettingsFileNamesOrUrls, fileNamesOrUrls);
// Keep track of the selected modes and views of our different files
foreach (const QString &fileName, fileNames) {
QString fileNameOrUrl = fileManagerInstance->isRemote(fileName)?
fileManagerInstance->url(fileName):
fileName;
pSettings->setValue(SettingsFileMode.arg(fileNameOrUrl),
ViewInterface::modeAsString(mModeTabIndexModes.value(mFileModeTabIndexes.value(fileName))));
QMap<int, int> modeViewTabIndexes = mFileModeViewTabIndexes.value(fileName);
for (int i = 0, iMax = mModeTabs->count(); i < iMax; ++i) {
ViewInterface::Mode fileMode = mModeTabIndexModes.value(i);
pSettings->setValue(SettingsFileModeView.arg(fileNameOrUrl, ViewInterface::modeAsString(fileMode)),
mModes.value(fileMode)->viewPlugins()[modeViewTabIndexes.value(i)]->name());
}
}
// Keep track of the currently selected file
// Note: we don't rely on mFileTabs->currentIndex() since it may refer to a
// new file, which we will have been skipped above...
QString crtFileNameOrUrl = QString();
if (fileNames.count()) {
QString crtFileName = mFileNames[mFileTabs->currentIndex()];
if (fileNames.contains(crtFileName)) {
crtFileNameOrUrl = fileManagerInstance->isRemote(crtFileName)?
fileManagerInstance->url(crtFileName):
crtFileName;
}
}
pSettings->setValue(SettingsCurrentFileNameOrUrl, crtFileNameOrUrl);
// Keep track of the selected modes and views, should there be no files the
// next time we use OpenCOR
pSettings->setValue(SettingsFileMode.arg(QString()),
ViewInterface::modeAsString(mModeTabIndexModes.value(mModeTabs->currentIndex())));
for (int i = 0, iMax = mModeTabs->count(); i < iMax; ++i) {
ViewInterface::Mode fileMode = mModeTabIndexModes.value(i);
CentralWidgetMode *mode = mModes.value(fileMode);
pSettings->setValue(SettingsFileModeView.arg(QString(), ViewInterface::modeAsString(fileMode)),
mode->viewPlugins()[mode->viewTabs()->currentIndex()]->name());
}
}
//==============================================================================
void CentralWidget::settingsLoaded(const Plugins &pLoadedPlugins)
{
// Determine which loaded plugins support a given interface
foreach (Plugin *plugin, pLoadedPlugins) {
if (qobject_cast<FileHandlingInterface *>(plugin->instance()))
mLoadedFileHandlingPlugins << plugin;
if (qobject_cast<FileTypeInterface *>(plugin->instance()))
mLoadedFileTypePlugins << plugin;
if (qobject_cast<GuiInterface *>(plugin->instance()))
mLoadedGuiPlugins << plugin;
if (qobject_cast<ViewInterface *>(plugin->instance()))
mLoadedViewPlugins << plugin;
}
// Update our state now that our plugins are fully ready
mState = Idling;
// Update the GUI
updateGui();
// Let our plugins know that our files have been opened
// Note: the below is normally done as part of openFile(), but
// mLoadedFileHandlingPlugins is not yet set when openFile() gets
// called as part of OpenCOR's loading its settings, so we do it here
// instead...
foreach (Plugin *plugin, mLoadedFileHandlingPlugins) {
foreach (const QString &fileName, mFileNames)
qobject_cast<FileHandlingInterface *>(plugin->instance())->fileOpened(fileName);
}
}
//==============================================================================
void CentralWidget::retranslateUi()
{
// Retranslate our modes tab bar
#ifdef ENABLE_SAMPLE_PLUGINS
mModeTabs->setTabText(mModeModeTabIndexes.value(ViewInterface::SampleMode, -1),
tr("Sample"));
#endif
#ifdef ENABLE_TEST_PLUGINS
mModeTabs->setTabText(mModeModeTabIndexes.value(ViewInterface::TestMode, -1),
tr("Test"));
#endif
mModeTabs->setTabText(mModeModeTabIndexes.value(ViewInterface::EditingMode, -1),
tr("Editing"));
mModeTabs->setTabText(mModeModeTabIndexes.value(ViewInterface::SimulationMode, -1),
tr("Simulation"));
mModeTabs->setTabText(mModeModeTabIndexes.value(ViewInterface::AnalysisMode, -1),
tr("Analysis"));
// Retranslate our mode views tab bar
foreach (CentralWidgetMode *mode, mModes) {
TabBarWidget *viewTabs = mode->viewTabs();
for (int i = 0, iMax = viewTabs->count(); i < iMax; ++i)
viewTabs->setTabText(i, qobject_cast<ViewInterface *>(mode->viewPlugins()[i]->instance())->viewName());
}
// Retranslate our modified settings, if needed
updateModifiedSettings();
// Retranslate our no view widget message
updateNoViewMsg();
// Retranslate our remote file dialog
mRemoteFileDialog->setWindowTitle(tr("Open Remote File"));
mRemoteFileDialogUrlLabel->setText(tr("URL:"));
}
//==============================================================================
QString CentralWidget::currentFileName() const
{
// Return the current file name, if any
int fileTabIndex = mFileTabs->currentIndex();
if (fileTabIndex != -1)
return mFileNames[fileTabIndex];
else
return QString();
}
//==============================================================================
void CentralWidget::updateFileTab(const int &pIndex, const bool &pIconOnly)
{
// Update the text, tool tip and icon to be used for the given file tab
static const QIcon NoIcon = QIcon();
static const QIcon InternetIcon = QIcon(":/oxygen/categories/applications-internet.png");
static const QIcon LockedIcon = QIcon(":/oxygen/status/object-locked.png");
FileManager *fileManagerInstance = FileManager::instance();
QString fileName = mFileNames[pIndex];
bool fileIsRemote = fileManagerInstance->isRemote(fileName);
QIcon tabIcon = QIcon();
if (!pIconOnly) {
bool fileIsNew = fileManagerInstance->isNew(fileName);
QString url = fileManagerInstance->url(fileName);
QString tabText = fileIsNew?
tr("File")+" #"+QString::number(fileManagerInstance->newIndex(fileName)):
fileIsRemote?
QUrl(url).fileName():
QFileInfo(fileName).fileName();
mFileTabs->setTabText(pIndex, tabText+(fileManagerInstance->isNewOrModified(fileName)?
"*":
QString()));
mFileTabs->setTabToolTip(pIndex, fileIsNew?
tabText:
fileIsRemote?
url:
fileName);
tabIcon = qobject_cast<ViewInterface *>(viewPlugin(pIndex)->instance())->fileTabIcon(mFileNames[pIndex]);
}
if (tabIcon.isNull()) {
mFileTabs->setTabIcon(pIndex, fileIsRemote?
InternetIcon:
QFile::exists(fileName)?
fileManagerInstance->isReadableAndWritable(fileName)?
NoIcon:
LockedIcon:
NoIcon);
// Note: we really want to call isReadableAndWritable() rather than
// isLocked() since no icon should be shown only if the file can
// be both readable and writable (see
// CorePlugin::filePermissionsChanged())...
} else {
mFileTabs->setTabIcon(pIndex, tabIcon);
}
}
//==============================================================================
void CentralWidget::openFile(const QString &pFileName, const File::Type &pType,
const QString &pUrl)
{
// Make sure that modes are available and that the file exists
if (!mModeTabs->count() || !QFile::exists(pFileName)) {
// Let the user know about us not being able to open the file, but only
// if we are not starting OpenCOR, i.e. only if our main window is
// visible
if (mainWindow()->isVisible()) {
warningMessageBox(pUrl.isEmpty()?
tr("Open File"):
tr("Open Remote File"),
tr("<strong>%1</strong> could not be opened.").arg(pFileName));
}
return;
}
// Check whether the file is already opened and, if so, select it and leave
QString nativeFileName = nativeCanonicalFileName(pFileName);
for (int i = 0, iMax = mFileNames.count(); i < iMax; ++i) {
if (!mFileNames[i].compare(nativeFileName)) {
mFileTabs->setCurrentIndex(i);
return;
}
}
// Register the file with our file manager
FileManager::instance()->manage(nativeFileName, pType, pUrl);
// Keep track of the mapping between the remote file and its local version,
// if needed
if (!pUrl.isEmpty())
mRemoteLocalFileNames.insert(pUrl, nativeFileName);
// Create a new tab, insert it just after the current tab, set the full name
// of the file as the tool tip for the new tab, and make the new tab the
// current one
// Note #1: mFileNames is, for example, used to retrieve the name of a file,
// which we want to close (see CentralWidget::closeFile()), so we
// must make sure that the order of its contents matches that of
// the tabs...
// Note #2: rather than using mFileNames, we could have used a tab's tool
// tip, but this makes it a bit tricky to handle with regards to
// connections and therefore with regards to some events triggering
// updateGui() to be called when the tool tip has not yet been
// assigned...
int fileTabIndex = mFileTabs->currentIndex()+1;
mFileNames.insert(fileTabIndex, nativeFileName);
mFileTabs->insertTab(fileTabIndex, QString());
updateFileTab(fileTabIndex);
mFileTabs->setCurrentIndex(fileTabIndex);
// Everything went fine, so let our plugins know that our file has been
// opened
// Note: this requires using mLoadedFileHandlingPlugins, but it will not be
// set when we come here following OpenCOR's loading of settings,
// hence we do something similar to what is done in
// settingsLoaded()...
foreach (Plugin *plugin, mLoadedFileHandlingPlugins)
qobject_cast<FileHandlingInterface *>(plugin->instance())->fileOpened(nativeFileName);
}
//==============================================================================
void CentralWidget::openFiles(const QStringList &pFileNames)
{
// Open the various files
for (int i = 0, iMax = pFileNames.count(); i < iMax; ++i)
openFile(pFileNames[i]);
}
//==============================================================================
void CentralWidget::openFile()
{
// Ask for the file(s) to be opened
QStringList fileNames = getOpenFileNames(tr("Open File"),
filters(fileTypeInterfaces()));
// Open the file(s)
openFiles(fileNames);
}
//==============================================================================
void CentralWidget::openRemoteFile(const QString &pUrl,
const bool &pShowWarning)
{
// Make sure that pUrl really refers to a remote file
bool isLocalFile;
QString fileNameOrUrl;
checkFileNameOrUrl(pUrl, isLocalFile, fileNameOrUrl);
if (isLocalFile) {
// It looks like the user tried to open a local file using a URL, e.g.
// file:///home/me/mymodel.cellml
// rather than a local file name, e.g.
// /home/me/mymodel.cellml
// so open the file as a local file and leave
openFile(fileNameOrUrl);
return;
}
// Check whether the remote file is already opened and if so select it,
// otherwise retrieve its contents
QString fileName = mRemoteLocalFileNames.value(fileNameOrUrl);
if (fileName.isEmpty()) {
// The remote file isn't already opened, so download its contents
QByteArray fileContents;
QString errorMessage;
if (readFileContentsFromUrlWithBusyWidget(fileNameOrUrl, fileContents, &errorMessage)) {
// We were able to retrieve the contents of the remote file, so ask
// our file manager to create a new remote file
FileManager *fileManagerInstance = FileManager::instance();
#ifdef QT_DEBUG
FileManager::Status createStatus =
#endif
fileManagerInstance->create(fileNameOrUrl, fileContents);
#ifdef QT_DEBUG
// Make sure that the file has indeed been created
if (createStatus != FileManager::Created)
qFatal("FATAL ERROR | %s:%d: the remote file was not created.", __FILE__, __LINE__);
#endif
} else {
// We were not able to retrieve the contents of the remote file, so
// let the user know about it
if (pShowWarning) {
warningMessageBox(tr("Open Remote File"),
tr("<strong>%1</strong> could not be opened (%2).").arg(fileNameOrUrl, formatMessage(errorMessage)));
}
}
} else {
openFile(fileName, File::Remote, fileNameOrUrl);
}
}
//==============================================================================
void CentralWidget::openRemoteFileChanged()
{
// Enable/disable the open button depending on whether we have some text for
// the remote file
mRemoteFileDialogButtonBox->button(QDialogButtonBox::Open)->setEnabled(!mRemoteFileDialogUrlValue->text().isEmpty());
}
//==============================================================================
void CentralWidget::doOpenRemoteFile()
{
// Open the remote file
mRemoteFileDialog->accept();
openRemoteFile(mRemoteFileDialogUrlValue->text());
}
//==============================================================================
void CentralWidget::cancelOpenRemoteFile()
{
// Cancel the opening of a remote file
mRemoteFileDialog->reject();
}
//==============================================================================
void CentralWidget::openRemoteFile()
{
// Ask for the URL of the remote file that is to be opened
mRemoteFileDialogUrlValue->setText(QString());
mSettings->beginGroup("RemoteFileDialog");
mRemoteFileDialog->exec(mSettings);
mSettings->endGroup();
}
//==============================================================================
void CentralWidget::reloadFile(const int &pIndex, const bool &pForce)
{
// Ask our file manager to reload the file, but only if it isn't new and if
// the user wants (in case the file has been modified)
int realIndex = (pIndex == -1)?mFileTabs->currentIndex():pIndex;
if (realIndex != -1) {
FileManager *fileManagerInstance = FileManager::instance();
QString fileName = mFileNames[realIndex];
if (!fileManagerInstance->isNew(fileName)) {
bool doReloadFile = true;
if (!pForce && fileManagerInstance->isModified(fileName)) {
// The current file is modified, so ask the user whether s/he
// still wants to reload it
doReloadFile = questionMessageBox(tr("File Modified"),
tr("<strong>%1</strong> has been modified. Do you still want to reload it?").arg(fileName)) == QMessageBox::Yes;
}
// Reload the file, if needed, and consider it as non-modified
// anymore (in case it was before)
// Note: by making the file non-modified anymore, we clearly assume
// that all the view plugins do their job properly and update
// their GUI...
if (doReloadFile) {
// Actually redownload the file, if it is a remote one, making
// sure that our busy widget is show during that process (since
// it may take some time)
// Note: our busy widget will get hidden in updateGui() (which
// will be called from fileReloaded())...
if (fileManagerInstance->isRemote(fileName)) {
QString url = fileManagerInstance->url(fileName);
QString fileContents;
QString errorMessage;
bool res = readFileContentsFromUrlWithBusyWidget(url, fileContents, &errorMessage);
if (res) {
writeFileContentsToFile(fileName, fileContents);
fileManagerInstance->reload(fileName, true);
} else {
warningMessageBox(tr("Reload Remote File"),
tr("<strong>%1</strong> could not be reloaded (%2).").arg(url, formatMessage(errorMessage)));
}
} else {
fileManagerInstance->reload(fileName, true);
fileManagerInstance->setModified(fileName, false);
}
}
}
}
}
//==============================================================================
void CentralWidget::duplicateFile()
{
// Make sure that the file is neither new nor modified
QString fileName = currentFileName();
FileManager *fileManagerInstance = FileManager::instance();