-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy patheditors.cpp
702 lines (636 loc) · 22.7 KB
/
editors.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
#include "editors.h"
#include "txstabwidget.h"
#include "mostQtHeaders.h"
#include "utilsSystem.h"
#include "latexeditorview.h"
#include "minisplitter.h"
/*!
* This class manages all editors and their positioning in tabWidgets.
*
* It maintains the knowledge of the currentEditor() and the currentTabWidget().
* Also, it sends appropriate signals to the editors and tabWidgets and provides
* signals to hook to these changes.
*
* Currently this class serves two purposes:
*
* 1) It maintains an abstract order of editors. Editors can be grouped (currently
* implemented as tabs in tabWidgets). The groups are ordered and the editors
* within a group are ordered, thus providing a complete order of all editors.
* The main purpose of this class it to provide an interface to this order, which
* can be used in the main program without having to know details on the actual
* organization of groups.
*
* 2) It contains all tabWidgets and thus editors providing a simple horizontal or
* vertical layout of the tab groups (there are currently 2 groups).
* This purpose is secondary and mainly implemented for an easier transition from
* the single tabGroup to more complex editor layouts. It may be changed in the
* future and will probably move to a separate class. The public interface of the
* class should depend on this as little as possible.
*/
Editors::Editors(QWidget *parent) :
QWidget(parent), splitter(nullptr), currentGroupIndex(-1)
{
splitter = new MiniSplitter(Qt::Horizontal);
splitter->setChildrenCollapsible(false);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(splitter);
changes = new EditorChangeProxy(this);
connect(changes, SIGNAL(currentEditorChanged()), this, SIGNAL(currentEditorChanged()));
connect(changes, SIGNAL(listOfEditorsChanged()), this, SIGNAL(listOfEditorsChanged()));
connect(this, SIGNAL(editorsReordered()), this, SIGNAL(listOfEditorsChanged()));
}
/*!
* Adds the given tabWidget. This take ownership of the tabWidget.
*/
void Editors::addTabWidget(TxsTabWidget *w)
{
splitter->addWidget(w);
tabGroups.append(w);
if (currentGroupIndex < 0) {
currentGroupIndex = 0;
w->setActive(true);
} else {
if (w->isEmpty()) w->hide();
}
connect(w, SIGNAL(currentEditorChanged()), changes, SLOT(currentEditorChange()));
connect(w, SIGNAL(editorAboutToChangeByTabClick(LatexEditorView*,LatexEditorView*)), this, SLOT(onEditorChangeByTabClick(LatexEditorView*,LatexEditorView*)));
connect(w, SIGNAL(activationRequested()), SLOT(activateTabWidgetFromSender()));
connect(w, SIGNAL(closeEditorRequested(LatexEditorView*)), SLOT(requestCloseEditor(LatexEditorView*)));
connect(w, SIGNAL(tabBarContextMenuRequested(QPoint)), SLOT(tabBarContextMenu(QPoint)));
connect(w, SIGNAL(tabMoved(int,int)), SIGNAL(editorsReordered()));
}
void Editors::addEditor(LatexEditorView *edView, bool asCurrent)
{
insertEditor(edView, currentTabWidget(), -1, asCurrent);
}
void Editors::insertEditor(LatexEditorView *edView, int index, bool asCurrent)
{
if (index >= 0) {
for (int group = 0; group < tabGroups.length(); group++) {
if (tabGroups[group]->count() > index) {
insertEditor(edView, tabGroups[group], index, asCurrent);
return;
}
index -= tabGroups[group]->count();
}
// index out of range: append
}
// append to current tab bar
insertEditor(edView, currentTabWidget(), -1, asCurrent);
}
void Editors::moveEditor(LatexEditorView *edView, Editors::Position pos)
{
switch (pos) {
case AbsoluteFront:
moveToTabGroup(edView, tabGroups.first(), 0);
break;
case AbsoluteEnd: {
int lastNonHiddenIndex = tabGroups.size() - 1;
while (lastNonHiddenIndex > 0 && tabGroups[lastNonHiddenIndex]->isHidden()) {
lastNonHiddenIndex--;
}
if (lastNonHiddenIndex < 0) return;
moveToTabGroup(edView, tabGroups[lastNonHiddenIndex], -1);
break;
}
case GroupFront: {
TxsTabWidget *tw = tabWidgetFromEditor(edView);
moveToTabGroup(edView, tw, 0);
break;
}
case GroupEnd: {
TxsTabWidget *tw = tabWidgetFromEditor(edView);
moveToTabGroup(edView, tw, -1);
break;
}
}
}
/*!
* Insert the editor in the given tabWidget at pos.
* If tabWidget is 0, insert into the current tabWidget.
* If pos == -1, append.
* If asCurrent, make the editor the current editor.
*/
void Editors::insertEditor(LatexEditorView *edView, TxsTabWidget *tabWidget, int pos, bool asCurrent)
{
bool b = changes->block();
if (!tabWidget) return;
if (!tabWidget->isVisible()) {
tabWidget->setVisible(true);
// distribute available space
QList<int> sizes;
for (int i = 0; i < splitter->count(); i++) {
sizes << 10; // excess space is distributed equally
}
splitter->setSizes(sizes);
}
tabWidget->insertEditor(edView, pos, asCurrent);
connect(edView, SIGNAL(focusReceived()), this, SLOT(setCurrentEditorFromSender()));
if (asCurrent) {
setCurrentEditor(edView);
}
if (b) changes->release();
}
/*! \brief request to close the given editor.
*
* The widget cannot decide if the tab can really be closed, which can only be
* determined at top level with user interaction if there are unsaved changes to
* the editor. So we propagate the request up.
*/
void Editors::requestCloseEditor(LatexEditorView *edView)
{
LatexEditorView *originalCurrent = currentEditor();
if (edView == originalCurrent) {
// request to close the current editor
emit closeCurrentEditorRequested();
} else {
// request to close a non-current editor
setCurrentEditor(edView);
emit closeCurrentEditorRequested();
if (currentEditor() != edView) {
// closing (of the originally non-current) editor was successful.
// Therefore we activate the originally current editor again.
setCurrentEditor(originalCurrent);
}
}
}
/*!
* Remove the editor from the list of editors
*/
void Editors::removeEditor(LatexEditorView *edView)
{
disconnect(edView, SIGNAL(focusReceived()), this, SLOT(setCurrentEditorFromSender()));
removeEditor(edView, tabWidgetFromEditor(edView));
}
/*!
* Remove the editor from the given tabWidget and hides the widget if its empty and not the only one.
*/
void Editors::removeEditor(LatexEditorView *edView, TxsTabWidget *tabWidget)
{
if (!tabWidget) return;
bool b = changes->block();
bool isLastEditorInGroup = (tabWidget->indexOf(edView) == tabWidget->count() - 1);
if (edView == currentEditor()) {
if (isLastEditorInGroup) {
activatePreviousEditor();
} else {
activateNextEditor();
}
}
tabWidget->removeEditor(edView);
if (tabWidget->isEmpty() && tabWidget != tabGroups[0]) {
tabWidget->hide();
}
if (b) changes->release();
}
bool Editors::containsEditor(LatexEditorView *edView) const
{
return tabGroupIndexFromEditor(edView) >= 0;
}
TxsTabWidget *Editors::currentTabWidget() const
{
if (currentGroupIndex >= 0 && currentGroupIndex < tabGroups.length())
return tabGroups[currentGroupIndex];
return nullptr;
}
LatexEditorView *Editors::currentEditor() const
{
TxsTabWidget *tabs = currentTabWidget();
if (!tabs) return nullptr;
return qobject_cast<LatexEditorView *>(tabs->currentWidget());
}
/*!
* Sets the current editor. It is expected that (containsEditor(edView) == true).
* if setFocus, the focus will be changed to the new editor
*/
void Editors::setCurrentEditor(LatexEditorView *edView, bool setFocus)
{
if (currentEditor() == edView) {
return;
}
int group = tabGroupIndexFromEditor(edView);
if (group >= 0) {
bool b = changes->block();
setCurrentGroup(group);
tabGroups[group]->setCurrentEditor(edView);
if (setFocus) {
edView->setFocus();
}
if (b) changes->release();
return;
}
// catch calls in which editor is not a member tab.
// TODO: such calls are deprecated as bad practice. We should avoid them in the long run. For the moment the fallback to do nothing is ok.
qDebug() << "Warning (deprecated call): TxsTabWidget::setCurrentEditor: editor not member of TxsTabWidget";
}
QList<LatexEditorView *> Editors::editors()
{
QList<LatexEditorView *> editors;
foreach (TxsTabWidget *tabGroup, tabGroups) {
editors.append(tabGroup->editors());
}
return editors;
}
void Editors::setCurrentEditorFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
setCurrentEditor(act->data().value<LatexEditorView *>());
}
void Editors::setCurrentEditorFromSender()
{
LatexEditorView *edView = qobject_cast<LatexEditorView *>(sender());
REQUIRE(edView);
setCurrentEditor(edView);
}
void Editors::closeEditorFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *edView = act->data().value<LatexEditorView *>();
requestCloseEditor(edView);
}
void Editors::closeOtherEditorsFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *editorToKeep = act->data().value<LatexEditorView *>();
foreach (TxsTabWidget *tabWidget, tabGroups) {
foreach (LatexEditorView *edView, tabWidget->editors()) {
if (edView != editorToKeep) {
requestCloseEditor(edView);
}
}
}
}
void Editors::toggleReadOnlyFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *edView = act->data().value<LatexEditorView *>();
edView->editor->setReadOnly(!edView->editor->isReadOnly());
}
bool Editors::activateNextEditor()
{
if (currentGroupIndex < 0) return false;
bool b = changes->block();
if (!tabGroups[currentGroupIndex]->currentEditorViewIsLast()) {
tabGroups[currentGroupIndex]->gotoNextDocument();
} else {
// find the next non-empty group
int newIndex = currentGroupIndex;
for (int unused = 0; unused < tabGroups.length(); unused++) { // counter is only used as a stopping criterion
newIndex = (newIndex + 1) % tabGroups.length();
if (!tabGroups[newIndex]->isEmpty()) {
if (newIndex == currentGroupIndex && tabGroups[currentGroupIndex]->count() == 1) {
if (b) changes->release();
return false; // only one editor: we cannot change
}
setCurrentGroup(newIndex);
break;
}
}
tabGroups[currentGroupIndex]->gotoFirstDocument();
}
if (b) changes->release();
return true;
}
bool Editors::activatePreviousEditor()
{
if (currentGroupIndex < 0) return false;
bool b = changes->block();
if (!tabGroups[currentGroupIndex]->currentEditorViewIsFirst()) {
tabGroups[currentGroupIndex]->gotoPrevDocument();
} else {
// find the previous non-empty group
int newIndex = currentGroupIndex;
for (int unused = 0; unused < tabGroups.length(); unused++) { // counter is only used as a stopping criterion
newIndex = (newIndex == 0) ? (tabGroups.length() -1) : (newIndex - 1);
if (!tabGroups[newIndex]->isEmpty()) {
if (newIndex == currentGroupIndex && tabGroups[currentGroupIndex]->count() == 1) {
return false; // only one editor: we cannot change
}
setCurrentGroup(newIndex);
break;
}
}
tabGroups[currentGroupIndex]->gotoLastDocument();
}
if (b) changes->release();
return true;
}
/*!
* Activates the tabWidget that is the sender(). Returns true if the widget could be activated.
*/
bool Editors::activateTabWidgetFromSender()
{
TxsTabWidget *tabWidget = qobject_cast<TxsTabWidget *>(sender());
if (!tabWidget) return false;
LatexEditorView *edView = tabWidget->currentEditor();
if (!edView) return false;
setCurrentEditor(tabWidget->currentEditor());
return true;
}
/*!
* Context menu handler for tabbars.
*/
void Editors::tabBarContextMenu(const QPoint &point)
{
if (point.isNull()) return;
TxsTabWidget *tabGroup = qobject_cast<TxsTabWidget *>(sender());
if (!tabGroup) return;
QMenu menu;
QAction *act;
foreach (LatexEditorView *edView, editors()) {
act = menu.addAction(edView->displayNameForUI());
act->setData(QVariant::fromValue<LatexEditorView *>(edView));
connect(act, SIGNAL(triggered()), SLOT(setCurrentEditorFromAction()));
}
menu.addSeparator();
LatexEditorView *editorUnderCursor = tabGroup->editorAt(point);
if (editorUnderCursor) {
act = menu.addAction(tr("Move to other view"));
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
connect(act, SIGNAL(triggered()), SLOT(moveToOtherTabGroup()));
if (tabGroups[0] == tabWidgetFromEditor(editorUnderCursor)) {
act = menu.addAction(tr("Move all others to other view"));
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
connect(act, SIGNAL(triggered()), SLOT(moveAllOthersToOtherTabGroup()));
}
else {
act = menu.addAction(tr("Move all to other view"));
act->setData(QVariant::fromValue<TxsTabWidget *>(tabGroup));
connect(act, SIGNAL(triggered()), SLOT(moveAllToOtherTabGroup()));
}
}
act = menu.addAction((splitter->orientation() == Qt::Horizontal) ? tr("Split Vertically") : tr("Split Horizontally"));
connect(act, SIGNAL(triggered()), SLOT(changeSplitOrientation()));
menu.addSeparator();
if (editorUnderCursor) {
act = menu.addAction(tr("Copy file path"));
act->setData(QVariant::fromValue<LatexDocument *>(editorUnderCursor->getDocument()));
connect(act, SIGNAL(triggered()), SLOT(copyFilePath()));
// Open the containing folder of the currently opened document.
act = menu.addAction(msgGraphicalShellAction());
act->setData(QVariant::fromValue<LatexDocument *>(editorUnderCursor->getDocument()));
connect(act, SIGNAL(triggered()), SLOT(showInGraphicalShell_()));
menu.addSeparator();
QString text = tr("Set Read-Only");
if (editorUnderCursor->editor->isReadOnly())
text = tr("Unset Read-Only");
act = menu.addAction(text);
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
connect(act, SIGNAL(triggered()), SLOT(toggleReadOnlyFromAction()));
menu.addSeparator();
act = menu.addAction(tr("Close"));
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
connect(act, SIGNAL(triggered()), SLOT(closeEditorFromAction()));
act = menu.addAction(tr("Close All Other Documents"));
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
connect(act, SIGNAL(triggered()), SLOT(closeOtherEditorsFromAction()));
}
menu.exec(tabGroup->mapToGlobal(point));
}
void Editors::onEditorChangeByTabClick(LatexEditorView *from, LatexEditorView *to)
{
Q_UNUSED(from)
// the original event comes from a tab widget. from is the previously selected tab in that widget
// which has not been the current one one if the tab widget has not been the current
emit editorAboutToChangeByTabClick(currentEditor(), to);
}
/*!
* \brief copy file path of document to clipboard
*
* Called from the tab's context menu of the currently opened document
*/
void Editors::copyFilePath()
{
QAction *action = qobject_cast<QAction *>(sender());
if (!action) return;
LatexDocument *document = qvariant_cast<LatexDocument *>(action->data());
if (!document) return;
QClipboard* clipboard = QGuiApplication::clipboard();
if (!clipboard) return;
clipboard->setText(document->getFileInfo().absoluteFilePath());
}
/*!
* \brief open directory in external explorer
*
* Called from the tab's context menu of the currently opened document
*/
void Editors::showInGraphicalShell_()
{
QAction *action = qobject_cast<QAction *>(sender());
if (!action) return;
LatexDocument *document = qvariant_cast<LatexDocument *>(action->data());
if (!document) return;
showInGraphicalShell(this, document->getFileName());
}
/*!
* Called from and action with data() set to an editor. Moves the editor to the other tabGroup.
* This currently assumes a restriction to two tab groups.
*/
void Editors::moveToOtherTabGroup()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *edView = act->data().value<LatexEditorView *>();
if (!edView) return;
// NOTE: This code assumes exactly two tabGroups
int otherGroupIndex = (tabGroups[0] == tabWidgetFromEditor(edView)) ? 1 : 0;
moveToTabGroup(edView, tabGroups[otherGroupIndex], -1);
}
void Editors::moveAllToOtherTabGroup() {
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
TxsTabWidget *tabGroup = act->data().value<TxsTabWidget *>();
if (!tabGroup) return;
// NOTE: This code assumes exactly two tabGroups
int otherGroupIndex = (tabGroups[0] == tabGroup) ? 1 : 0;
foreach (LatexEditorView *edView, tabGroup->editors()){
moveToTabGroup(edView, tabGroups[otherGroupIndex], -1);
}
}
void Editors::moveAllOthersToOtherTabGroup() {
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *edViewCurrent = act->data().value<LatexEditorView *>();
if (!edViewCurrent) return;
TxsTabWidget *tabGroupCurrent = tabWidgetFromEditor(edViewCurrent);
// NOTE: This code assumes exactly two tabGroups
int otherGroupIndex = (tabGroups[0] == tabGroupCurrent) ? 1 : 0;
foreach (LatexEditorView *edView, tabGroupCurrent->editors()){
if (edView != edViewCurrent){
moveToTabGroup(edView, tabGroups[otherGroupIndex], -1);
}
}
}
/*!
* Move the editor to the given tabWidget at position targetIndex (if < 0, append).
*/
void Editors::moveToTabGroup(LatexEditorView *edView, int groupIndex, int targetIndex)
{
if (groupIndex < 0)
groupIndex = 0;
if (groupIndex > tabGroups.length() -1)
groupIndex = tabGroups.length() -1;
moveToTabGroup(edView, tabGroups[groupIndex], targetIndex);
}
/*!
* \brief move all editor to tabgroup 0 if there are no editors
* This basically "closes" tabgroup 1
*/
void Editors::moveAllToGroupZeroifEmpty()
{
if(tabGroups.length()>1 && tabGroups[0]->isEmpty()) {
TxsTabWidget *tabGroup = tabGroups[1];
if (!tabGroup) return;
// NOTE: This code assumes exactly two tabGroups
foreach (LatexEditorView *edView, tabGroup->editors())
moveToTabGroup(edView, tabGroups[0], -1);
}
}
bool Editors::getSplitVertical()
{
return splitter->orientation() == Qt::Vertical;
}
/*!
* Move the editor to the given tabWidget at position targetIndex (if < 0, append).
*/
void Editors::moveToTabGroup(LatexEditorView *edView, TxsTabWidget *target, int targetIndex)
{
if (!target || target->containsEditor(edView)) {
// only move within the tab
if (target == nullptr) target = tabWidgetFromEditor(edView);
if (target == nullptr) {
// the tab is REALLY not there, see crash in issue #899
insertEditor(edView,targetIndex);
return;
}
if (targetIndex < 0) targetIndex = qMax(0, target->count() - 1);
int currentIndex = target->indexOf(edView);
if (currentIndex != targetIndex) { // nothing todo otherwise
target->moveTab(target->indexOf(edView), targetIndex);
emit editorsReordered();
}
} else {
bool b = changes->block();
TxsTabWidget *source = tabWidgetFromEditor(edView);
removeEditor(edView, source);
insertEditor(edView, target, targetIndex, true);
if (b) changes->release();
}
}
void Editors::changeSplitOrientation()
{
splitter->setOrientation((splitter->orientation() == Qt::Horizontal) ? Qt::Vertical : Qt::Horizontal);
}
/*!
* Returns the number of the tabGroup to which the povided editor belongs or -1.
*/
int Editors::tabGroupIndexFromEditor(LatexEditorView *edView) const
{
for (int group = 0; group < tabGroups.length(); group++) {
int i = tabGroups[group]->indexOf(edView);
if (i >= 0) {
return group;
}
}
return -1;
}
/*!
* Returns a pointer to the tabWidget to which the provided editor belongs or 0.
*/
TxsTabWidget *Editors::tabWidgetFromEditor(LatexEditorView *edView) const
{
int group = tabGroupIndexFromEditor(edView);
if (group < 0) return nullptr;
return tabGroups[group];
}
void Editors::setCurrentGroup(int index)
{
if (index == currentGroupIndex) return;
if (currentGroupIndex >= 0) {
tabGroups[currentGroupIndex]->setActive(false);
}
currentGroupIndex = index;
tabGroups[currentGroupIndex]->setActive(true);
}
/*! \class EditorChangeProxy
*
* This class handles changes of the currentEditor
*
* There are two ways that can lead to emission of the signal currentEditorChanged():
*
* 1) First, other components can connect to the slot editorChange(). These signals are
* just propagated, except if the EditorChangeProxy is blocked.
*
* 2) Second, one can block() the propagation of changes and release() later on. At release(),
* the proxy checks if the currentEditor has changed from the one at blocking. If so, the
* currentEditorChanged() signal is emitted.
* Use the block/release mechanism to perform complex actions that consist of multiple
* editor changes (potentially even with inconsistent intermediate states) during which
* you don't want to emit currentEditorChanged().
* You have to make sure that you release the block if and only if you acquired it. Therefore,
* blocking should always use this pattern:
*
* bool b = changes->block();
* ...
* if (b) changes->release();
*
* If you have return statements in the intermediate code, be sure to place the release command
* also before every return.
*/
EditorChangeProxy::EditorChangeProxy(Editors *e) : editors(e), currentEditorAtBlock(nullptr), blocked(false) {}
//#define ecpDebug(x) qDebug(x)
#define ecpDebug(x)
bool EditorChangeProxy::block()
{
if (blocked) {
ecpDebug("already blocked");
return false;
}
blocked = true;
currentEditorAtBlock = editors->currentEditor();
listOfEditorsAtBlock = editors->editors();
ecpDebug("block activated");
return true;
}
void EditorChangeProxy::release()
{
ecpDebug("release");
if (blocked) {
blocked = false;
if (editors->currentEditor() != currentEditorAtBlock) {
ecpDebug("currentEditorChanged signaled at release");
emit currentEditorChanged();
}
if (editors->editors() != listOfEditorsAtBlock) {
ecpDebug("listOfEditorsChanged signaled at release");
emit listOfEditorsChanged();
}
} else {
// can only happen if the above mentioned blocking pattern was not used.
qDebug("WARNING: trying to realease an unblocked EditorChangeProxy. This hints at inconsistent code.");
}
}
void EditorChangeProxy::currentEditorChange()
{
if (!blocked) {
ecpDebug("currentEditorChanged passed");
emit currentEditorChanged();
} else {
ecpDebug("currentEditorChanged blocked");
}
}
void EditorChangeProxy::listOfEditorsChange()
{
if (!blocked) {
ecpDebug("listOfEditorsChanged passed");
emit listOfEditorsChanged();
} else {
ecpDebug("listOfEditorsChanged blocked");
}
}
#undef ecpDebug