/
coreplugin.cpp
845 lines (624 loc) · 30 KB
/
coreplugin.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
/*******************************************************************************
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/>.
*******************************************************************************/
//==============================================================================
// Core plugin
//==============================================================================
#include "centralwidget.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "coreplugin.h"
#include "datastoreinterface.h"
#include "filemanager.h"
#include "filetypeinterface.h"
#include "interfaces.h"
#include "organisationwidget.h"
#include "plugin.h"
#include "solverinterface.h"
//==============================================================================
#include <QAction>
#include <QApplication>
#include <QEvent>
#include <QFile>
#include <QMainWindow>
#include <QMenu>
#include <QPalette>
#include <QSettings>
//==============================================================================
namespace OpenCOR {
namespace Core {
//==============================================================================
PLUGININFO_FUNC CorePluginInfo()
{
Descriptions descriptions;
descriptions.insert("en", QString::fromUtf8("the core plugin."));
descriptions.insert("fr", QString::fromUtf8("l'extension de base."));
return new PluginInfo(PluginInfo::Miscellaneous, false, false,
QStringList(),
descriptions);
}
//==============================================================================
CorePlugin::CorePlugin() :
mRecentFileNamesOrUrls(QStringList())
{
}
//==============================================================================
// Core interface
//==============================================================================
QWidget * CorePlugin::centralWidget()
{
// Return our central widget
return mCentralWidget;
}
//==============================================================================
void CorePlugin::settingsLoaded(const Plugins &pLoadedPlugins)
{
// Let our central widget know that all the other plugins have now loaded
// their settings
mCentralWidget->settingsLoaded(pLoadedPlugins);
}
//==============================================================================
void CorePlugin::handleArguments(const QStringList &pArguments)
{
// All the arguments are files to open, so open them as if they were remote
// files since if they are not then CentralWidget::openRemoteFile() will
// open them as normal files
foreach (const QString &argument, pArguments)
mCentralWidget->openRemoteFile(argument);
}
//==============================================================================
bool CorePlugin::canClose()
{
// To determine whether we can close, we must ask our central widget
return mCentralWidget->canClose();
}
//==============================================================================
// File handling interface
//==============================================================================
bool CorePlugin::saveFile(const QString &pOldFileName,
const QString &pNewFileName,
bool &pNeedFeedback)
{
Q_UNUSED(pOldFileName);
Q_UNUSED(pNewFileName);
Q_UNUSED(pNeedFeedback);
// We don't handle this interface...
return false;
}
//==============================================================================
void CorePlugin::fileOpened(const QString &pFileName)
{
// Remove the file from our list of recent files and update our Reopen
// sub-menu
FileManager *fileManagerInstance = FileManager::instance();
if (fileManagerInstance->isRemote(pFileName)?
mRecentFileNamesOrUrls.removeOne(fileManagerInstance->url(pFileName)):
mRecentFileNamesOrUrls.removeOne(pFileName)) {
updateFileReopenMenu();
}
}
//==============================================================================
void CorePlugin::filePermissionsChanged(const QString &pFileName)
{
// Update the checked state of our Locked menu, if needed
if (!pFileName.compare(mCentralWidget->currentFileName()))
updateNewModifiedSensitiveActions();
}
//==============================================================================
void CorePlugin::fileModified(const QString &pFileName)
{
// Update our new/modified sensitive actions, if needed
if (!pFileName.compare(mCentralWidget->currentFileName()))
updateNewModifiedSensitiveActions();
}
//==============================================================================
void CorePlugin::fileReloaded(const QString &pFileName)
{
Q_UNUSED(pFileName);
// We don't handle this interface...
}
//==============================================================================
void CorePlugin::fileRenamed(const QString &pOldFileName,
const QString &pNewFileName)
{
// Remove the new file from our list of recent files, should it be there
// Note: it's fine if the new file isn't in our list since nothing will be
// done in that case (thus avoiding us having to test for its
// presence)...
if (mRecentFileNamesOrUrls.removeOne(pNewFileName))
updateFileReopenMenu();
// A file has been created or saved under a new name, so we want the old
// file name to be added to our list of recent files, i.e. as if it had been
// closed
fileClosed(pOldFileName);
}
//==============================================================================
void CorePlugin::fileClosed(const QString &pFileName)
{
// Add, if isn't new and it still exists (i.e. we are not here because the
// file has been deleted), the file to our list of recent files (making sure
// that we don't end up with more than 10 recent file names) and update our
// Reopen sub-menu
// Note: the most recent file is to be shown first...
FileManager *fileManagerInstance = FileManager::instance();
if ( !fileManagerInstance->isNew(pFileName)
&& QFile::exists(pFileName)) {
if (fileManagerInstance->isRemote(pFileName))
mRecentFileNamesOrUrls.prepend(fileManagerInstance->url(pFileName));
else
mRecentFileNamesOrUrls.prepend(pFileName);
while (mRecentFileNamesOrUrls.count() > 10)
mRecentFileNamesOrUrls.removeLast();
updateFileReopenMenu();
}
}
//==============================================================================
// GUI interface
//==============================================================================
void CorePlugin::updateGui(Plugin *pViewPlugin, const QString &pFileName)
{
Q_UNUSED(pViewPlugin);
Q_UNUSED(pFileName);
// Update our new/modified sensitive actions
updateNewModifiedSensitiveActions();
}
//==============================================================================
Gui::Menus CorePlugin::guiMenus() const
{
// Return our menus
return Gui::Menus() << Gui::Menu(Gui::Menu::File, mFileOpenReloadSeparator, mFileReopenSubMenu);
}
//==============================================================================
Gui::MenuActions CorePlugin::guiMenuActions() const
{
// Return our menu actions
return Gui::MenuActions() << Gui::MenuAction(Gui::MenuAction::FileNew, mFileNewFileAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileOpenAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileOpenRemoteAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileOpenReloadSeparator)
<< Gui::MenuAction(Gui::MenuAction::File, mFileReloadAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()))
<< Gui::MenuAction(Gui::MenuAction::File, mFileDuplicateAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()))
<< Gui::MenuAction(Gui::MenuAction::File, mFileLockedAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()))
<< Gui::MenuAction(Gui::MenuAction::File, mFileSaveAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileSaveAsAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileSaveAllAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()))
<< Gui::MenuAction(Gui::MenuAction::File, mFilePreviousAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileNextAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()))
<< Gui::MenuAction(Gui::MenuAction::File, mFileCloseAction)
<< Gui::MenuAction(Gui::MenuAction::File, mFileCloseAllAction)
<< Gui::MenuAction(Gui::MenuAction::File, Core::newSeparator(Core::mainWindow()));
}
//==============================================================================
// I18n interface
//==============================================================================
void CorePlugin::retranslateUi()
{
// Retranslate our different File actions
retranslateAction(mFileNewFileAction, tr("File"),
tr("Create a new file"));
retranslateAction(mFileOpenAction, tr("Open..."),
tr("Open a file"));
retranslateAction(mFileOpenRemoteAction, tr("Open Remote..."),
tr("Open a remote file"));
retranslateAction(mFileReloadAction, tr("Reload"),
tr("Reload the current file"));
retranslateAction(mFileDuplicateAction, tr("Duplicate"),
tr("Duplicate the current file"));
retranslateAction(mFileLockedAction, tr("Locked"),
tr("Toggle the locked state of the current file"));
retranslateAction(mFileSaveAction, tr("Save"),
tr("Save the current file"));
retranslateAction(mFileSaveAsAction, tr("Save As..."),
tr("Save the current file under a different name"));
retranslateAction(mFileSaveAllAction, tr("Save All"),
tr("Save all the files"));
retranslateAction(mFilePreviousAction, tr("Previous"),
tr("Select the previous file"));
retranslateAction(mFileNextAction, tr("Next"),
tr("Select the next file"));
retranslateAction(mFileCloseAction, tr("Close"),
tr("Close the current file"));
retranslateAction(mFileCloseAllAction, tr("Close All"),
tr("Close all the files"));
// Retranslate our File sub-menu and its action
retranslateMenu(mFileReopenSubMenu, tr("Reopen"));
retranslateAction(mFileReopenMostRecentFileAction, tr("Most Recent"),
tr("Reopen the most recently closed file"));
retranslateAction(mFileClearReopenSubMenuAction, tr("Clear Menu"),
tr("Clear the menu"));
// Retranslate our central widget
mCentralWidget->retranslateUi();
}
//==============================================================================
// Plugin interface
//==============================================================================
bool CorePlugin::definesPluginInterfaces()
{
// We don't handle this interface...
return false;
}
//==============================================================================
bool CorePlugin::pluginInterfacesOk(const QString &pFileName,
QObject *pInstance)
{
Q_UNUSED(pFileName);
Q_UNUSED(pInstance);
// We don't handle this interface...
return false;
}
//==============================================================================
void CorePlugin::initializePlugin()
{
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!mainWindow())
return;
// Create our central widget
mCentralWidget = new CentralWidget(mainWindow());
mCentralWidget->setObjectName("CentralWidget");
// Create our different File actions
mFileNewFileAction = newAction(QIcon(":/oxygen/actions/document-new.png"),
QKeySequence::New, mainWindow());
mFileOpenAction = newAction(QIcon(":/oxygen/actions/document-open.png"),
QKeySequence::Open, mainWindow());
mFileOpenRemoteAction = newAction(QIcon(":/oxygen/actions/document-open-remote.png"),
QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_O),
mainWindow());
mFileReloadAction = newAction(mainWindow());
mFileDuplicateAction = newAction(QKeySequence(Qt::CTRL|Qt::Key_D),
mainWindow());
mFileLockedAction = newAction(true, QKeySequence(Qt::CTRL|Qt::Key_L),
mainWindow());
mFileSaveAction = newAction(QIcon(":/oxygen/actions/document-save.png"),
QKeySequence::Save, mainWindow());
mFileSaveAsAction = newAction(QIcon(":/oxygen/actions/document-save-as.png"),
QKeySequence::SaveAs, mainWindow());
mFileSaveAllAction = newAction(QIcon(":/oxygen/actions/document-save-all.png"),
mainWindow());
// Note: for mFilePreviousAction and mFileNextAction, we would normally use
// QKeySequence::PreviousChild and QKeySequence::NextChild,
// respectively, but for Qt this means using Ctrl+Shift+BackTab and
// Ctrl+Tab, respectively, on Windows/Linux, and Ctrl+{ and Ctrl+},
// respectively, on macOS. On Windows, Ctrl+Shift+BackTab just doesn't
// work, on macOS those key sequences are not the most natural ones.
// So, instead, it would be more natural to use Ctrl+Shift+Tab and
// Ctr+Tab, respectively, on Windows/Linux, and Meta+Shift+Tab and
// Meta+Tab, respectively, on macOS. The original plan was therefore
// to use QKeySequence::PreviousChild and QKeySequence::NextChild, as
// well as our preferred key sequences, but Qt ended up 'allowing'
// only using one of them. So, in the end, we only use our preferred
// key sequences...
mFilePreviousAction = newAction(QIcon(":/oxygen/actions/go-previous.png"),
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_Tab),
#elif defined(Q_OS_MAC)
QKeySequence(Qt::META|Qt::SHIFT|Qt::Key_Tab),
#else
#error Unsupported platform
#endif
mainWindow());
mFileNextAction = newAction(QIcon(":/oxygen/actions/go-next.png"),
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
QKeySequence(Qt::CTRL|Qt::Key_Tab),
#elif defined(Q_OS_MAC)
QKeySequence(Qt::META|Qt::Key_Tab),
#else
#error Unsupported platform
#endif
mainWindow());
mFileCloseAction = newAction(QIcon(":/oxygen/actions/document-close.png"),
#if defined(Q_OS_WIN)
QList<QKeySequence>() << QKeySequence::Close << QKeySequence(Qt::CTRL|Qt::Key_W),
#elif defined(Q_OS_LINUX) || defined(Q_OS_MAC)
QKeySequence::Close,
#else
#error Unsupported platform
#endif
mainWindow());
mFileCloseAllAction = newAction(mainWindow());
// Create the separator before which we will insert our Reopen sub-menu
mFileOpenReloadSeparator = newSeparator(mainWindow());
// Create our Reopen sub-menu
mFileReopenSubMenu = newMenu(QIcon(":/oxygen/actions/document-open-recent.png"),
mainWindow());
mFileReopenMostRecentFileAction = newAction(QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_T),
mainWindow());
mFileReopenSubMenuSeparator1 = newSeparator(mainWindow());
mFileReopenSubMenuSeparator2 = newSeparator(mainWindow());
mFileClearReopenSubMenuAction = newAction(mainWindow());
mFileReopenSubMenu->addAction(mFileReopenMostRecentFileAction);
mFileReopenSubMenu->addAction(mFileReopenSubMenuSeparator1);
mFileReopenSubMenu->addAction(mFileReopenSubMenuSeparator2);
mFileReopenSubMenu->addAction(mFileClearReopenSubMenuAction);
// Some connections to handle our different File actions
connect(mFileNewFileAction, SIGNAL(triggered(bool)),
this, SLOT(newFile()));
connect(mFileOpenAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(openFile()));
connect(mFileOpenRemoteAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(openRemoteFile()));
connect(mFileReloadAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(reloadFile()));
connect(mFileDuplicateAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(duplicateFile()));
connect(mFileLockedAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(toggleLockedFile()));
connect(mFileSaveAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(saveFile()));
connect(mFileSaveAsAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(saveFileAs()));
connect(mFileSaveAllAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(saveAllFiles()));
connect(mFilePreviousAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(previousFile()));
connect(mFileNextAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(nextFile()));
connect(mFileCloseAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(closeFile()));
connect(mFileCloseAllAction, SIGNAL(triggered(bool)),
mCentralWidget, SLOT(closeAllFiles()));
// Some connections to update the enabled state of our various actions
connect(mCentralWidget, SIGNAL(atLeastOneView(const bool &)),
mFileOpenAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastOneView(const bool &)),
mFileOpenRemoteAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastOneView(const bool &)),
this, SLOT(updateFileReopenMenu(const bool &)));
connect(mCentralWidget, SIGNAL(canSave(const bool &)),
mFileSaveAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(canSaveAs(const bool &)),
mFileSaveAsAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(canSaveAll(const bool &)),
mFileSaveAllAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastTwoFiles(const bool &)),
mFilePreviousAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastTwoFiles(const bool &)),
mFileNextAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastOneFile(const bool &)),
mFileCloseAction, SLOT(setEnabled(bool)));
connect(mCentralWidget, SIGNAL(atLeastOneFile(const bool &)),
mFileCloseAllAction, SLOT(setEnabled(bool)));
// A connection related to our Reopen sub-menu
connect(mFileReopenMostRecentFileAction, SIGNAL(triggered(bool)),
this, SLOT(reopenMostRecentFile()));
connect(mFileClearReopenSubMenuAction, SIGNAL(triggered(bool)),
this, SLOT(clearReopenSubMenu()));
}
//==============================================================================
void CorePlugin::finalizePlugin()
{
// We don't handle this interface...
}
//==============================================================================
void CorePlugin::pluginsInitialized(const Plugins &pLoadedPlugins)
{
// Retrieve the different file type, solver and data store interfaces that
// are available to us
FileTypeInterfaces fileTypeInterfaces = FileTypeInterfaces();
SolverInterfaces solverInterfaces = SolverInterfaces();
DataStoreInterfaces dataStoreInterfaces = DataStoreInterfaces();
foreach (Plugin *plugin, pLoadedPlugins) {
// Look for a file type
FileTypeInterface *fileTypeInterface = qobject_cast<FileTypeInterface *>(plugin->instance());
if (fileTypeInterface)
fileTypeInterfaces << fileTypeInterface;
// Look for a solver
SolverInterface *solverInterface = qobject_cast<SolverInterface *>(plugin->instance());
if (solverInterface)
solverInterfaces << solverInterface;
// Look for a data store
DataStoreInterface *dataStoreInterface = qobject_cast<DataStoreInterface *>(plugin->instance());
if (dataStoreInterface)
dataStoreInterfaces << dataStoreInterface;
}
// Keep track of our various interfaces
static InterfacesData data(fileTypeInterfaces, solverInterfaces,
dataStoreInterfaces);
Core::globalInstance(InterfacesDataSignature, &data);
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!mainWindow())
return;
// Check, based on the loaded plugins, which views, if any, our central
// widget should support
foreach (Plugin *plugin, pLoadedPlugins) {
ViewInterface *viewInterface = qobject_cast<ViewInterface *>(plugin->instance());
if (viewInterface) {
// The plugin implements our View interface, so add it to our
// central widget
mCentralWidget->addView(plugin);
}
}
}
//==============================================================================
static const auto SettingsRecentFiles = QStringLiteral("RecentFiles");
//==============================================================================
void CorePlugin::loadSettings(QSettings *pSettings)
{
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!Core::mainWindow())
return;
// Retrieve the recent files
// Note: it's important to retrieve the recent files before retrieving our
// central widget settings since mRecentFileNamesOrUrls gets updated
// as a result of opening/closing a file...
mRecentFileNamesOrUrls = pSettings->value(SettingsRecentFiles).toStringList();
// Update our Reopen sub-menu
updateFileReopenMenu();
// Retrieve the central widget settings
pSettings->beginGroup(mCentralWidget->objectName());
mCentralWidget->loadSettings(pSettings);
pSettings->endGroup();
}
//==============================================================================
void CorePlugin::saveSettings(QSettings *pSettings) const
{
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!Core::mainWindow())
return;
// Keep track of the recent files
pSettings->setValue(SettingsRecentFiles, mRecentFileNamesOrUrls);
// Keep track of the central widget settings
pSettings->beginGroup(mCentralWidget->objectName());
mCentralWidget->saveSettings(pSettings);
pSettings->endGroup();
}
//==============================================================================
void CorePlugin::handleUrl(const QUrl &pUrl)
{
// Handle the action that was passed to us
QString actionName = pUrl.authority();
if (!actionName.compare("Core.selectMode", Qt::CaseInsensitive)) {
// We want to select a given mode
mCentralWidget->selectMode(urlArguments(pUrl));
} else if (!actionName.compare("Core.selectView", Qt::CaseInsensitive)) {
// We want to select a given view
mCentralWidget->selectView(urlArguments(pUrl));
}
}
//==============================================================================
// Plugin specific
//==============================================================================
void CorePlugin::newFile()
{
// Ask our file manager to create a new file
FileManager *fileManagerInstance = FileManager::instance();
#ifdef QT_DEBUG
FileManager::Status createStatus =
#endif
fileManagerInstance->create();
#ifdef QT_DEBUG
// Make sure that the file has indeed been created
if (createStatus != FileManager::Created)
qFatal("FATAL ERROR | %s:%d: the new file was not created.", __FILE__, __LINE__);
#endif
}
//==============================================================================
void CorePlugin::updateFileReopenMenu(const bool &pEnabled)
{
// Update the contents of our Reopen sub-menu by first cleaning it
foreach (QAction *action, mFileReopenSubMenu->actions()) {
if ( (action != mFileReopenMostRecentFileAction)
&& (action != mFileReopenSubMenuSeparator1)
&& (action != mFileReopenSubMenuSeparator2)) {
mFileReopenSubMenu->removeAction(action);
delete action;
} else if (action == mFileReopenSubMenuSeparator2) {
break;
}
}
// Add the recent files to our Reopen sub-menu
foreach (const QString &recentFile, mRecentFileNamesOrUrls) {
QAction *action = newAction(mainWindow());
action->setEnabled(pEnabled);
action->setText(recentFile);
connect(action, SIGNAL(triggered(bool)),
this, SLOT(reopenRecentFile()));
mFileReopenSubMenu->insertAction(mFileReopenSubMenuSeparator2, action);
}
// Enable/disable our reopen sub-menu actions depending on whether we have
// recent file names
bool hasRecentFileNamesOrUrls = !mRecentFileNamesOrUrls.isEmpty();
mFileReopenMostRecentFileAction->setEnabled(hasRecentFileNamesOrUrls);
mFileClearReopenSubMenuAction->setEnabled(hasRecentFileNamesOrUrls);
Core::showEnableAction(mFileReopenSubMenuSeparator2, hasRecentFileNamesOrUrls);
}
//==============================================================================
void CorePlugin::updateNewModifiedSensitiveActions()
{
// Update our actions keeping in mind the fact that a file may be modified
// and even possibly new
QString fileName = mCentralWidget->currentFileName();
if (!fileName.isEmpty()) {
FileManager *fileManagerInstance = FileManager::instance();
bool fileIsNew = fileManagerInstance->isNew(fileName);
bool fileIsNewRemoteOrModified = fileIsNew
|| fileManagerInstance->isRemote(fileName)
|| fileManagerInstance->isModified(fileName);
mFileReloadAction->setEnabled(!fileIsNew);
mFileDuplicateAction->setEnabled(!fileIsNewRemoteOrModified);
mFileLockedAction->setEnabled( !fileIsNewRemoteOrModified
&& fileManagerInstance->isReadable(fileName));
} else {
mFileReloadAction->setEnabled(false);
mFileDuplicateAction->setEnabled(false);
mFileLockedAction->setEnabled(false);
}
mFileLockedAction->setChecked(!FileManager::instance()->isReadableAndWritable(fileName));
// Note: we really want to call isReadableAndWritable() rather than
// isLocked() since from the GUI perspective a file should only be
// considered unlocked if it can be both readable and writable
// (see CentralWidget::updateFileTab())...
}
//==============================================================================
void CorePlugin::reopenFile(const QString &pFileName)
{
// Remove the file from our list of recent files and update our Reopen
// sub-menu, if needed
if (mRecentFileNamesOrUrls.removeOne(pFileName))
updateFileReopenMenu();
// Check that the recent file still exists
bool isLocalFile;
QString fileNameOrUrl;
checkFileNameOrUrl(pFileName, isLocalFile, fileNameOrUrl);
if (isLocalFile) {
if (QFile::exists(fileNameOrUrl)) {
// Open the recent file
mCentralWidget->openFile(fileNameOrUrl);
} else {
// The file doesn't exist anymore, so let the user know about it
warningMessageBox(tr("Reopen File"),
tr("<strong>%1</strong> does not exist anymore.").arg(fileNameOrUrl));
}
} else {
// Open the recent remote file
mCentralWidget->openRemoteFile(fileNameOrUrl);
}
}
//==============================================================================
void CorePlugin::reopenRecentFile()
{
// Reopen a recent file
reopenFile(qobject_cast<QAction *>(sender())->text());
}
//==============================================================================
void CorePlugin::reopenMostRecentFile()
{
// Reopen the most recently closed file
// Note: we don't want to get a reference to mRecentFileNamesOrUrls' first
// item, hence we construct a new string from it. Indeed, reopenFile()
// is going to remove that item from mRecentFileNamesOrUrls, so if we
// were to use the reference, it would eventually become an empty
// string...
reopenFile(QString(mRecentFileNamesOrUrls.first()));
}
//==============================================================================
void CorePlugin::clearReopenSubMenu()
{
// Indirectly clear our Reopen sub-menu
mRecentFileNamesOrUrls.clear();
updateFileReopenMenu();
}
//==============================================================================
} // namespace Core
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================