/
editingviewplugin.cpp
723 lines (528 loc) · 24.1 KB
/
editingviewplugin.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
/*******************************************************************************
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/>.
*******************************************************************************/
//==============================================================================
// Editing view plugin
//==============================================================================
#include "coreguiutils.h"
#include "editingviewinterface.h"
#include "editingviewplugin.h"
#include "editorwidget.h"
#include "filemanager.h"
//==============================================================================
#include <QApplication>
#include <QClipboard>
#include <QMainWindow>
#include <QMenu>
//==============================================================================
namespace OpenCOR {
namespace EditingView {
//==============================================================================
PLUGININFO_FUNC EditingViewPluginInfo()
{
Descriptions descriptions;
descriptions.insert("en", QString::fromUtf8("a plugin that provides core editing view facilities."));
descriptions.insert("fr", QString::fromUtf8("une extension qui fournit les fonctionalités de base d'une vue d'édition."));
return new PluginInfo(PluginInfo::Editing, false, false,
QStringList() << "EditorWidget",
descriptions);
}
//==============================================================================
EditingViewPlugin::EditingViewPlugin() :
mEditingViewInterface(0),
mEditor(0),
mFileName(QString())
{
}
//==============================================================================
// File handling interface
//==============================================================================
bool EditingViewPlugin::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 EditingViewPlugin::fileOpened(const QString &pFileName)
{
Q_UNUSED(pFileName);
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::filePermissionsChanged(const QString &pFileName)
{
// The given file has been un/locked, so update our GUI accordingly
updateGui(pFileName);
}
//==============================================================================
void EditingViewPlugin::fileModified(const QString &pFileName)
{
Q_UNUSED(pFileName);
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::fileReloaded(const QString &pFileName,
const bool &pFileChanged,
const bool &pFileJustSaved)
{
Q_UNUSED(pFileJustSaved);
// A file has been reloaded, so update our internals, if needed
// Note: we clearly still have an editor for the given file, but when
// reloading it, fileReloaded() will first be called for this plugin
// and then for the 'proper' editing plugins. In other words, we don't
// know at this stage whether a new editor will have been assigned to
// the given file. Indeed, upon reloading a file, a 'proper' editing
// plugin may decide to 'close' and 'reopen' the file, meaning that
// its original editor will be discarded and a new one assigned to
// it, hence we need to reset things...
if (pFileChanged && !pFileName.compare(mFileName)) {
mEditor = 0;
mFileName = QString();
}
}
//==============================================================================
void EditingViewPlugin::fileRenamed(const QString &pOldFileName,
const QString &pNewFileName)
{
// A file has been renamed, so update our internals, if needed
if (!pOldFileName.compare(mFileName))
mFileName = pNewFileName;
}
//==============================================================================
void EditingViewPlugin::fileClosed(const QString &pFileName)
{
// A file has been closed, so update our internals, if needed
if (!pFileName.compare(mFileName)) {
mEditor = 0;
mFileName = QString();
}
}
//==============================================================================
// GUI interface
//==============================================================================
void EditingViewPlugin::updateGui(Plugin *pViewPlugin, const QString &pFileName)
{
// Reset our previous editor and set up our new one, should the current
// plugin handle the editing view interface
mEditingViewInterface = pViewPlugin?qobject_cast<EditingViewInterface *>(pViewPlugin->instance()):0;
if (mEditingViewInterface) {
// Reset our previous editor's connections
if (mEditor) {
disconnect(mEditor, SIGNAL(textChanged()),
this, SLOT(updateUndoAndRedoActions()));
disconnect(mEditor, SIGNAL(copyAvailable(const bool &)),
this, SLOT(updateEditingActions()));
disconnect(mEditor, SIGNAL(canFindReplace(const bool &)),
this, SLOT(updateFindPreviousNextActions()));
disconnect(mEditor, SIGNAL(canSelectAll(const bool &)),
this, SLOT(updateSelectAllAction()));
}
// Retrieve our new editor widget
mEditor = mEditingViewInterface->editorWidget(pFileName);
mFileName = pFileName;
// Set our new editor's context menu, connections and background
if (mEditor) {
mEditor->setContextMenu(mEditMenu->actions());
connect(mEditor, SIGNAL(textChanged()),
this, SLOT(updateUndoAndRedoActions()));
connect(mEditor, SIGNAL(copyAvailable(const bool &)),
this, SLOT(updateEditingActions()));
connect(mEditor, SIGNAL(canFindReplace(const bool &)),
this, SLOT(updateFindPreviousNextActions()));
connect(mEditor, SIGNAL(canSelectAll(const bool &)),
this, SLOT(updateSelectAllAction()));
}
}
// Show/enable or hide/disable various actions, depending on whether the
// view plugin handles the editing view interface
Core::showEnableAction(mEditUndoAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditRedoAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditCutAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditCopyAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditPasteAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditDeleteAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditFindReplaceAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditFindNextAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditFindPreviousAction, mEditingViewInterface, mEditor);
Core::showEnableAction(mEditSelectAllAction, mEditingViewInterface, mEditor);
// Finish updating our GUI
updateGui(pFileName);
}
//==============================================================================
Gui::Menus EditingViewPlugin::guiMenus() const
{
// Return our menus
return Gui::Menus() << Gui::Menu(Gui::Menu::View, mEditMenu);
}
//==============================================================================
Gui::MenuActions EditingViewPlugin::guiMenuActions() const
{
// We don't handle this interface...
return Gui::MenuActions();
}
//==============================================================================
// I18n interface
//==============================================================================
void EditingViewPlugin::retranslateUi()
{
// Retranslate our Edit menu
retranslateMenu(mEditMenu, tr("Edit"));
// Unpopulate our Edit menu
// Note #1: this is required on macOS otherwise we get several console
// messages that read
// void QCocoaMenu::insertNative(QCocoaMenuItem *, QCocoaMenuItem *) Menu item is already in a menu, remove it from the other menu first before inserting
// Note #2: beforeAction is used to keep track of where our first action
// should really be. Indeed, on macOS, our Edit menu gets
// additional menu items (they are automatically added by macOS at
// the end of our Edit menu, should in the system locale), so we
// cannot just use mEditMenu->clear() to unpopulate our Edit menu
// since otherwise when repopulating it those extra menu items will
// end up before (rather than after) our menu items...
QAction *beforeAction = mEditMenu->insertSeparator(mEditUndoAction);
unpopulateEditMenu();
// Retranslate our different Edit actions
retranslateAction(mEditUndoAction, tr("Undo"),
tr("Undo the last action"));
retranslateAction(mEditRedoAction, tr("Redo"),
tr("Redo the last action"));
retranslateAction(mEditCutAction, tr("Cut"),
tr("Cut the selected text"));
retranslateAction(mEditCopyAction, tr("Copy"),
tr("Copy the selected text to the clipboard"));
retranslateAction(mEditPasteAction, tr("Paste"),
tr("Paste the contents of the clipboard"));
retranslateAction(mEditDeleteAction, tr("Delete"),
tr("Delete the selected text"));
retranslateAction(mEditFindReplaceAction, tr("Find/Replace..."),
tr("Search/replace a specific text"));
retranslateAction(mEditFindNextAction, tr("Find Next"),
tr("Search forwards for the same text"));
retranslateAction(mEditFindPreviousAction, tr("Find Previous"),
tr("Search backwards for the same text"));
retranslateAction(mEditSelectAllAction, tr("Select All"),
tr("Select all the text"));
// (Re)populate our Edit menu
populateEditMenu(beforeAction);
mEditMenu->removeAction(beforeAction);
}
//==============================================================================
// Plugin interface
//==============================================================================
bool EditingViewPlugin::definesPluginInterfaces()
{
// We define the editing view interface
return true;
}
//==============================================================================
bool EditingViewPlugin::pluginInterfacesOk(const QString &pFileName,
QObject *pInstance)
{
// Make sure that the given plugin instance uses the right version of the
// editing view interface, if it supports it
return !( qobject_cast<EditingViewInterface *>(pInstance)
&& (Plugin::interfaceVersion(pFileName, "editingViewInterfaceVersion") != editingViewInterfaceVersion()));
}
//==============================================================================
void EditingViewPlugin::initializePlugin()
{
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!Core::mainWindow())
return;
// Create our Edit menu
mEditMenu = Core::newMenu("Edit", Core::mainWindow());
// Create our different Edit actions, and add them to our Edit menu
mEditUndoAction = Core::newAction(QIcon(":/oxygen/actions/edit-undo.png"),
QKeySequence::Undo, Core::mainWindow());
mEditRedoAction = Core::newAction(QIcon(":/oxygen/actions/edit-redo.png"),
QKeySequence::Redo, Core::mainWindow());
mEditCutAction = Core::newAction(QIcon(":/oxygen/actions/edit-cut.png"),
QKeySequence::Cut, Core::mainWindow());
mEditCopyAction = Core::newAction(QIcon(":/oxygen/actions/edit-copy.png"),
QKeySequence::Copy, Core::mainWindow());
mEditPasteAction = Core::newAction(QIcon(":/oxygen/actions/edit-paste.png"),
QKeySequence::Paste, Core::mainWindow());
mEditDeleteAction = Core::newAction(QIcon(":/oxygen/actions/edit-delete.png"),
QKeySequence::Delete, Core::mainWindow());
mEditFindReplaceAction = Core::newAction(QIcon(":/oxygen/actions/edit-find.png"),
QKeySequence::Find, Core::mainWindow());
mEditFindNextAction = Core::newAction(QKeySequence::FindNext, Core::mainWindow());
mEditFindPreviousAction = Core::newAction(QKeySequence::FindPrevious, Core::mainWindow());
mEditSelectAllAction = Core::newAction(QKeySequence::SelectAll, Core::mainWindow());
populateEditMenu();
// Keep track of changes to the clipboard
connect(QApplication::clipboard(), SIGNAL(dataChanged()),
this, SLOT(clipboardDataChanged()));
// Some connections to handle our different editing actions
connect(mEditUndoAction, SIGNAL(triggered(bool)),
this, SLOT(doUndo()));
connect(mEditRedoAction, SIGNAL(triggered(bool)),
this, SLOT(doRedo()));
connect(mEditCutAction, SIGNAL(triggered(bool)),
this, SLOT(doCut()));
connect(mEditCopyAction, SIGNAL(triggered(bool)),
this, SLOT(doCopy()));
connect(mEditPasteAction, SIGNAL(triggered(bool)),
this, SLOT(doPaste()));
connect(mEditDeleteAction, SIGNAL(triggered(bool)),
this, SLOT(doDelete()));
connect(mEditFindReplaceAction, SIGNAL(triggered(bool)),
this, SLOT(doFindReplace()));
connect(mEditFindNextAction, SIGNAL(triggered(bool)),
this, SLOT(doFindNext()));
connect(mEditFindPreviousAction, SIGNAL(triggered(bool)),
this, SLOT(doFindPrevious()));
connect(mEditSelectAllAction, SIGNAL(triggered(bool)),
this, SLOT(doSelectAll()));
}
//==============================================================================
void EditingViewPlugin::finalizePlugin()
{
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::pluginsInitialized(const Plugins &pLoadedPlugins)
{
Q_UNUSED(pLoadedPlugins);
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::loadSettings(QSettings *pSettings)
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::saveSettings(QSettings *pSettings) const
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void EditingViewPlugin::handleUrl(const QUrl &pUrl)
{
Q_UNUSED(pUrl);
// We don't handle this interface...
}
//==============================================================================
// Plugin specific
//==============================================================================
void EditingViewPlugin::updateGui(const QString &pFileName)
{
// Update some actions and make our editor read-only or writable, if needed
if (mEditingViewInterface && !pFileName.compare(mFileName)) {
// Update some actions
updateUndoAndRedoActions();
updateEditingActions();
updateFindPreviousNextActions();
updateSelectAllAction();
// Make our editor widget read-only or writable
if (mEditor) {
mEditor->setReadOnly( !Core::FileManager::instance()->isReadableAndWritable(pFileName)
|| !mEditingViewInterface->isEditorWidgetUseable(pFileName));
}
}
}
//==============================================================================
void EditingViewPlugin::unpopulateEditMenu()
{
// Unpopulate our Edit menu
// Note: we do not want to use mEditMenu->clear() since we want to keep our
// tracker, so we can repopulate our Edit menu without any problem...
mEditMenu->removeAction(mEditUndoAction);
mEditMenu->removeAction(mEditRedoAction);
mEditMenu->removeAction(mEditCutAction);
mEditMenu->removeAction(mEditCopyAction);
mEditMenu->removeAction(mEditPasteAction);
mEditMenu->removeAction(mEditDeleteAction);
mEditMenu->removeAction(mEditFindReplaceAction);
mEditMenu->removeAction(mEditFindNextAction);
mEditMenu->removeAction(mEditFindPreviousAction);
mEditMenu->removeAction(mEditSelectAllAction);
}
//==============================================================================
void EditingViewPlugin::populateEditMenu(QAction *pBeforeAction)
{
// Populate our Edit menu
mEditMenu->insertAction(pBeforeAction, mEditUndoAction);
mEditMenu->insertAction(pBeforeAction, mEditRedoAction);
mEditMenu->insertSeparator(pBeforeAction);
mEditMenu->insertAction(pBeforeAction, mEditCutAction);
mEditMenu->insertAction(pBeforeAction, mEditCopyAction);
mEditMenu->insertAction(pBeforeAction, mEditPasteAction);
mEditMenu->insertAction(pBeforeAction, mEditDeleteAction);
mEditMenu->insertSeparator(pBeforeAction);
mEditMenu->insertAction(pBeforeAction, mEditFindReplaceAction);
mEditMenu->insertAction(pBeforeAction, mEditFindNextAction);
mEditMenu->insertAction(pBeforeAction, mEditFindPreviousAction);
mEditMenu->insertSeparator(pBeforeAction);
mEditMenu->insertAction(pBeforeAction, mEditSelectAllAction);
}
//==============================================================================
void EditingViewPlugin::clipboardDataChanged()
{
// Enable our paste action if the clipboard contains some text
if (mEditingViewInterface) {
mEditPasteAction->setEnabled( mEditor
&& Core::FileManager::instance()->isReadableAndWritable(mFileName)
&& !QApplication::clipboard()->text().isEmpty());
}
}
//==============================================================================
void EditingViewPlugin::updateUndoAndRedoActions()
{
// Update our undo/redo actions, and update the modified state of the
// current file
if (mEditingViewInterface) {
Core::FileManager *fileManagerInstance = Core::FileManager::instance();
bool editorAndFileReadableAndWritable = mEditor
&& fileManagerInstance->isReadableAndWritable(mFileName);
mEditUndoAction->setEnabled(editorAndFileReadableAndWritable && mEditor->isUndoAvailable());
mEditRedoAction->setEnabled(editorAndFileReadableAndWritable && mEditor->isRedoAvailable());
fileManagerInstance->setModified(mFileName,
mEditor && mEditingViewInterface->isEditorWidgetContentsModified(mFileName));
}
}
//==============================================================================
void EditingViewPlugin::updateEditingActions()
{
// Update our editing actions
if (mEditingViewInterface) {
bool editorAndHasSelectedText = mEditor
&& mEditor->hasSelectedText();
bool fileReadableOrWritable = Core::FileManager::instance()->isReadableAndWritable(mFileName);
mEditCutAction->setEnabled(editorAndHasSelectedText && fileReadableOrWritable);
mEditCopyAction->setEnabled(editorAndHasSelectedText);
clipboardDataChanged();
mEditDeleteAction->setEnabled(editorAndHasSelectedText && fileReadableOrWritable);
}
}
//==============================================================================
void EditingViewPlugin::updateFindPreviousNextActions()
{
// Update our find previous and next actions
if (mEditingViewInterface) {
bool canFindReplace = mEditor
&& mEditor->isFindPreviousNextAvailable();
mEditFindPreviousAction->setEnabled(canFindReplace);
mEditFindNextAction->setEnabled(canFindReplace);
}
}
//==============================================================================
void EditingViewPlugin::updateSelectAllAction()
{
// Update our select all action
if (mEditingViewInterface) {
mEditSelectAllAction->setEnabled( mEditor
&& mEditor->isSelectAllAvailable());
}
}
//==============================================================================
void EditingViewPlugin::doUndo()
{
// Undo the last action and update our undo/redo actions, should there be an
// editor
if (!mEditor)
return;
mEditor->undo();
updateUndoAndRedoActions();
}
//==============================================================================
void EditingViewPlugin::doRedo()
{
// Redo the last action and update our undo/redo actions, should there be an
// editor
if (!mEditor)
return;
mEditor->redo();
updateUndoAndRedoActions();
}
//==============================================================================
void EditingViewPlugin::doCut()
{
// Cut the text and update our undo/redo actions, should there be an editor
if (!mEditor)
return;
mEditor->cut();
updateUndoAndRedoActions();
}
//==============================================================================
void EditingViewPlugin::doCopy()
{
// Copy the text and update our undo/redo actions, should there be an editor
if (!mEditor)
return;
mEditor->copy();
}
//==============================================================================
void EditingViewPlugin::doPaste()
{
// Paste the text and update our undo/redo actions, should there be an
// editor
if (!mEditor)
return;
mEditor->paste();
updateUndoAndRedoActions();
}
//==============================================================================
void EditingViewPlugin::doDelete()
{
// Delete the text and update our undo/redo actions, should there be an
// editor
if (!mEditor)
return;
mEditor->del();
updateUndoAndRedoActions();
}
//==============================================================================
void EditingViewPlugin::doFindReplace()
{
// Show/select the find/replace widget in our editor
if (mEditor)
mEditor->setFindReplaceVisible(true);
}
//==============================================================================
void EditingViewPlugin::doFindNext()
{
// Find the next occurrence of the text in our editor
if (mEditor)
mEditor->findNext();
}
//==============================================================================
void EditingViewPlugin::doFindPrevious()
{
// Find the previous occurrence of the text in our editor
if (mEditor)
mEditor->findPrevious();
}
//==============================================================================
void EditingViewPlugin::doSelectAll()
{
// Select all the text and update our select all action, should there be an
// editor
if (!mEditor)
return;
mEditor->selectAll();
updateSelectAllAction();
}
//==============================================================================
} // namespace EditingView
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================