kateviewspace.cpp 27.3 KB
Newer Older
1
/* This file is part of the KDE project
2
3
4
   SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
   SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
   SPDX-FileCopyrightText: 2001, 2005 Anders Lund <anders.lund@lund.tdcadsl.dk>
5

6
   SPDX-License-Identifier: LGPL-2.0-only
7
8
*/
#include "kateviewspace.h"
9

10
#include "kateapp.h"
11
12
#include "katedebug.h"
#include "katedocmanager.h"
13
#include "katefileactions.h"
14
#include "katemainwindow.h"
15
#include "katesessionmanager.h"
Christoph Cullmann's avatar
Christoph Cullmann committed
16
#include "kateupdatedisabler.h"
17
#include "kateviewmanager.h"
Christoph Cullmann's avatar
Christoph Cullmann committed
18
#include <KActionCollection>
19

20
#include <KAcceleratorManager>
Dominik Haumann's avatar
Dominik Haumann committed
21
22
#include <KConfigGroup>
#include <KLocalizedString>
23

24
25
#include <QApplication>
#include <QClipboard>
Dominik Haumann's avatar
Dominik Haumann committed
26
#include <QHelpEvent>
27
#include <QMenu>
28
#include <QMessageBox>
Dominik Haumann's avatar
Dominik Haumann committed
29
30
#include <QStackedWidget>
#include <QToolButton>
Dominik Haumann's avatar
Dominik Haumann committed
31
#include <QToolTip>
32
#include <QWhatsThis>
33

34
35
// BEGIN KateViewSpace
KateViewSpace::KateViewSpace(KateViewManager *viewManager, QWidget *parent, const char *name)
Dominik Haumann's avatar
Dominik Haumann committed
36
37
    : QWidget(parent)
    , m_viewManager(viewManager)
38
    , m_isActiveSpace(false)
39
{
40
41
42
    setObjectName(QString::fromLatin1(name));
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(0);
Laurent Montel's avatar
Laurent Montel committed
43
    layout->setContentsMargins(0, 0, 0, 0);
44

45
    // BEGIN tab bar
46
47
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setSpacing(0);
Laurent Montel's avatar
Laurent Montel committed
48
    hLayout->setContentsMargins(0, 0, 0, 0);
49

50
    // add left <-> right history buttons
51
    m_historyBack = new QToolButton(this);
52
53
    m_historyBack->setToolTip(i18n("Go Back"));
    m_historyBack->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
54
55
56
57
    m_historyBack->setAutoRaise(true);
    KAcceleratorManager::setNoAccel(m_historyBack);
    m_historyBack->installEventFilter(this); // on click, active this view space
    hLayout->addWidget(m_historyBack);
58
59
60
61
    connect(m_historyBack, &QToolButton::clicked, this, [this] {
        goBack();
    });
    m_historyBack->setEnabled(false);
62
63

    m_historyForward = new QToolButton(this);
64
65
    m_historyForward->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
    m_historyForward->setToolTip(i18n("Go Forward"));
66
67
68
69
    m_historyForward->setAutoRaise(true);
    KAcceleratorManager::setNoAccel(m_historyForward);
    m_historyForward->installEventFilter(this); // on click, active this view space
    hLayout->addWidget(m_historyForward);
70
71
72
73
    connect(m_historyForward, &QToolButton::clicked, this, [this] {
        goForward();
    });
    m_historyForward->setEnabled(false);
74

75
76
77
    // add tab bar
    m_tabBar = new KateTabBar(this);
    connect(m_tabBar, &KateTabBar::currentChanged, this, &KateViewSpace::changeView);
78
    connect(m_tabBar, &KateTabBar::tabCloseRequested, this, &KateViewSpace::closeTabRequest, Qt::QueuedConnection);
79
    connect(m_tabBar, &KateTabBar::contextMenuRequest, this, &KateViewSpace::showContextMenu, Qt::QueuedConnection);
80
    connect(m_tabBar, &KateTabBar::newTabRequested, this, &KateViewSpace::createNewDocument);
81
    connect(m_tabBar, SIGNAL(activateViewSpaceRequested()), this, SLOT(makeActive()));
82
83
    hLayout->addWidget(m_tabBar);

84
85
86
    // add quick open
    m_quickOpen = new QToolButton(this);
    m_quickOpen->setAutoRaise(true);
87
    KAcceleratorManager::setNoAccel(m_quickOpen);
88
89
90
    m_quickOpen->installEventFilter(this); // on click, active this view space
    hLayout->addWidget(m_quickOpen);

91
    // forward tab bar quick open action to global quick open action
92
    QAction *bridge = new QAction(QIcon::fromTheme(QStringLiteral("tab-duplicate")), i18nc("indicator for more documents", "+%1", 100), this);
93
    m_quickOpen->setDefaultAction(bridge);
94
    QAction *quickOpen = m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_quick_open"));
95
96
97
    Q_ASSERT(quickOpen);
    bridge->setToolTip(quickOpen->toolTip());
    bridge->setWhatsThis(i18n("Click here to switch to the Quick Open view."));
Laurent Montel's avatar
Laurent Montel committed
98
    connect(bridge, &QAction::triggered, quickOpen, &QAction::trigger);
99

100
    // add vertical split view space
Christoph Cullmann's avatar
Christoph Cullmann committed
101
102
103
104
105
106
107
108
109
110
111
112
    m_split = new QToolButton(this);
    m_split->setAutoRaise(true);
    m_split->setPopupMode(QToolButton::InstantPopup);
    m_split->setIcon(QIcon::fromTheme(QStringLiteral("view-split-left-right")));
    m_split->addAction(m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_split_vert")));
    m_split->addAction(m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_split_horiz")));
    m_split->addAction(m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_close_current_space")));
    m_split->addAction(m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_close_others")));
    m_split->addAction(m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_hide_others")));
    m_split->setWhatsThis(i18n("Control view space splitting"));
    m_split->installEventFilter(this); // on click, active this view space
    hLayout->addWidget(m_split);
113
114

    layout->addLayout(hLayout);
115
    // END tab bar
116
117
118

    stack = new QStackedWidget(this);
    stack->setFocus();
Dominik Haumann's avatar
Dominik Haumann committed
119
    stack->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding));
120
121
122
123
124
    layout->addWidget(stack);

    m_group.clear();

    // connect signal to hide/show statusbar
Laurent Montel's avatar
Laurent Montel committed
125
126
    connect(m_viewManager->mainWindow(), &KateMainWindow::statusBarToggled, this, &KateViewSpace::statusBarToggled);
    connect(m_viewManager->mainWindow(), &KateMainWindow::tabBarToggled, this, &KateViewSpace::tabBarToggled);
127

Christoph Cullmann's avatar
Christoph Cullmann committed
128
    // init the bars...
129
    statusBarToggled();
Christoph Cullmann's avatar
Christoph Cullmann committed
130
    tabBarToggled();
131
132
}

133
134
bool KateViewSpace::eventFilter(QObject *obj, QEvent *event)
{
135
136
    QToolButton *button = qobject_cast<QToolButton *>(obj);

137
138
    // quick open button: show tool tip with shortcut
    if (button == m_quickOpen && event->type() == QEvent::ToolTip) {
139
        QHelpEvent *e = static_cast<QHelpEvent *>(event);
140
141
        QAction *quickOpen = m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_quick_open"));
        Q_ASSERT(quickOpen);
142
        QToolTip::showText(e->globalPos(), button->toolTip() + QStringLiteral(" (%1)").arg(quickOpen->shortcut().toString()), button);
143
144
145
146
147
148
149
        return true;
    }

    // quick open button: What's This
    if (button == m_quickOpen && event->type() == QEvent::WhatsThis) {
        QHelpEvent *e = static_cast<QHelpEvent *>(event);
        const int hiddenDocs = hiddenDocuments();
Alexander Lohnau's avatar
Alexander Lohnau committed
150
151
152
153
154
        QString helpText = (hiddenDocs == 0)
            ? i18n("Click here to switch to the Quick Open view.")
            : i18np("Currently, there is one more document open. To see all open documents, switch to the Quick Open view by clicking here.",
                    "Currently, there are %1 more documents open. To see all open documents, switch to the Quick Open view by clicking here.",
                    hiddenDocs);
155
156
        QWhatsThis::showText(e->globalPos(), helpText, m_quickOpen);
        return true;
Dominik Haumann's avatar
Dominik Haumann committed
157
    }
158
159

    // on mouse press on view space bar tool buttons: activate this space
160
    if (button && !isActiveSpace() && event->type() == QEvent::MouseButtonPress) {
161
        m_viewManager->setActiveSpace(this);
162
163
164
        if (currentView()) {
            m_viewManager->activateView(currentView()->document());
        }
165
166
    }
    return false;
167
168
}

169
void KateViewSpace::statusBarToggled()
170
{
171
    KateUpdateDisabler updatesDisabled(m_viewManager->mainWindow());
172
    for (auto view : qAsConst(m_docToView)) {
Christoph Cullmann's avatar
Christoph Cullmann committed
173
        view->setStatusBarEnabled(m_viewManager->mainWindow()->showStatusBar());
174
175
176
    }
}

Christoph Cullmann's avatar
Christoph Cullmann committed
177
178
void KateViewSpace::tabBarToggled()
{
179
    KateUpdateDisabler updatesDisabled(m_viewManager->mainWindow());
180
181
    m_historyBack->setVisible(m_viewManager->mainWindow()->showTabBar());
    m_historyForward->setVisible(m_viewManager->mainWindow()->showTabBar());
Christoph Cullmann's avatar
Christoph Cullmann committed
182
183
184
185
186
    m_tabBar->setVisible(m_viewManager->mainWindow()->showTabBar());
    m_split->setVisible(m_viewManager->mainWindow()->showTabBar());
    m_quickOpen->setVisible(m_viewManager->mainWindow()->showTabBar());
}

187
KTextEditor::View *KateViewSpace::createView(KTextEditor::Document *doc)
188
{
189
    // should only be called if a view does not yet exist
190
    Q_ASSERT(!m_docToView.contains(doc));
191

192
193
194
195
196
197
198
199
200
201
202
    /**
     * Create a fresh view
     */
    KTextEditor::View *v = doc->createView(stack, m_viewManager->mainWindow()->wrapper());

    // set status bar to right state
    v->setStatusBarEnabled(m_viewManager->mainWindow()->showStatusBar());

    // restore the config of this view if possible
    if (!m_group.isEmpty()) {
        QString fn = v->document()->url().toString();
203
        if (!fn.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
204
            QString vgroup = QStringLiteral("%1 %2").arg(m_group, fn);
205

Christoph Cullmann's avatar
Christoph Cullmann committed
206
            KateSession::Ptr as = KateApp::self()->sessionManager()->activeSession();
207
208
            if (as->config() && as->config()->hasGroup(vgroup)) {
                KConfigGroup cg(as->config(), vgroup);
209
                v->readSessionConfig(cg);
210
211
            }
        }
212
213
    }

214
215
216
217
218
    connect(v, &KTextEditor::View::cursorPositionChanged, this, [this](KTextEditor::View *view, const KTextEditor::Cursor &newPosition) {
        if (view && view->document())
            addJump(view->document()->url(), newPosition);
    });

219
    // register document, it is shown below through showView() then
Christoph Cullmann's avatar
Christoph Cullmann committed
220
    registerDocument(doc);
221

222
223
224
    // view shall still be not registered
    Q_ASSERT(!m_docToView.contains(doc));

225
226
    // insert View into stack
    stack->addWidget(v);
227
    m_docToView[doc] = v;
228
    showView(v);
229

230
    return v;
231
232
}

233
void KateViewSpace::removeView(KTextEditor::View *v)
234
{
235
236
237
    // remove view mappings
    Q_ASSERT(m_docToView.contains(v->document()));
    m_docToView.remove(v->document());
238

239
    // ...and now: remove from view space
240
    stack->removeWidget(v);
241
242
243
244
245
246
247
248
249

    // switch to most recently used rather than letting stack choose one
    // (last element could well be v->document() being removed here)
    for (auto it = m_registeredDocuments.rbegin(); it != m_registeredDocuments.rend(); ++it) {
        if (m_docToView.contains(*it)) {
            showView(*it);
            break;
        }
    }
250
251
}

252
253
bool KateViewSpace::showView(KTextEditor::Document *document)
{
254
255
256
    /**
     * nothing can be done if we have now view ready here
     */
257
    if (!m_docToView.contains(document)) {
258
259
260
        return false;
    }

261
262
263
264
265
266
267
268
269
270
271
272
    /**
     * update mru list order
     */
    const int index = m_registeredDocuments.lastIndexOf(document);
    // move view to end of list
    if (index < 0) {
        return false;
    }
    // move view to end of list
    m_registeredDocuments.removeAt(index);
    m_registeredDocuments.append(document);

273
274
275
    /**
     * show the wanted view
     */
276
277
278
279
    KTextEditor::View *kv = m_docToView[document];
    stack->setCurrentWidget(kv);
    kv->show();

280
281
282
283
284
    /**
     * we need to avoid that below's index changes will mess with current view
     */
    disconnect(m_tabBar, &KateTabBar::currentChanged, this, &KateViewSpace::changeView);

285
286
287
    /**
     * follow current view
     */
288
    m_tabBar->setCurrentDocument(document);
289

290
291
    // track tab changes again
    connect(m_tabBar, &KateTabBar::currentChanged, this, &KateViewSpace::changeView);
292
    return true;
293
294
}

295
void KateViewSpace::changeView(int idx)
296
{
297
298
299
300
    if (idx == -1) {
        return;
    }

301
    KTextEditor::Document *doc = m_tabBar->tabDocument(idx);
302
    Q_ASSERT(doc);
303

304
    // make sure we open the view in this view space
305
    if (!isActiveSpace()) {
306
307
        m_viewManager->setActiveSpace(this);
    }
Dominik Haumann's avatar
Dominik Haumann committed
308

309
310
    // tell the view manager to show the view
    m_viewManager->activateView(doc);
311
}
312

313
KTextEditor::View *KateViewSpace::currentView()
314
{
315
    // might be 0 if the stack contains no view
316
    return static_cast<KTextEditor::View *>(stack->currentWidget());
317
318
319
320
}

bool KateViewSpace::isActiveSpace()
{
321
    return m_isActiveSpace;
322
323
}

324
void KateViewSpace::setActive(bool active)
325
{
326
    m_isActiveSpace = active;
327
    m_tabBar->setActive(active);
328
329
}

330
331
void KateViewSpace::makeActive(bool focusCurrentView)
{
332
    if (!isActiveSpace()) {
333
334
335
336
337
338
339
340
        m_viewManager->setActiveSpace(this);
        if (focusCurrentView && currentView()) {
            m_viewManager->activateView(currentView()->document());
        }
    }
    Q_ASSERT(isActiveSpace());
}

341
void KateViewSpace::registerDocument(KTextEditor::Document *doc)
342
{
343
344
345
346
    /**
     * ignore request to register something that is already known
     */
    if (m_registeredDocuments.contains(doc)) {
347
348
349
        return;
    }

350
351
352
    /**
     * remember our new document
     */
353
    m_registeredDocuments.insert(0, doc);
354
355
356
357

    /**
     * ensure we cleanup after document is deleted, e.g. we remove the tab bar button
     */
Laurent Montel's avatar
Laurent Montel committed
358
    connect(doc, &QObject::destroyed, this, &KateViewSpace::documentDestroyed);
359

360
361
362
363
364
    /**
     * register document is used in places that don't like view creation
     * therefore we must ensure the currentChanged doesn't trigger that
     */
    disconnect(m_tabBar, &KateTabBar::currentChanged, this, &KateViewSpace::changeView);
365
366

    /**
367
     * create the tab for this document, if necessary
368
     */
369
    m_tabBar->setCurrentDocument(doc);
370

Christoph Cullmann's avatar
Christoph Cullmann committed
371
372
373
    /**
     * handle later document state changes
     */
374
375
376
377
378
379
380
    connect(doc, &KTextEditor::Document::documentNameChanged, this, &KateViewSpace::updateDocumentName);
    connect(doc, &KTextEditor::Document::documentUrlChanged, this, &KateViewSpace::updateDocumentUrl);
    connect(doc, &KTextEditor::Document::modifiedChanged, this, &KateViewSpace::updateDocumentState);

    /**
     * allow signals again, now that the tab is there
     */
381
    connect(m_tabBar, &KateTabBar::currentChanged, this, &KateViewSpace::changeView);
382
383
}

384
void KateViewSpace::documentDestroyed(QObject *doc)
385
{
386
387
388
389
    /**
     * WARNING: this pointer is half destroyed
     * only good enough to check pointer value e.g. for hashs
     */
390
    KTextEditor::Document *invalidDoc = static_cast<KTextEditor::Document *>(doc);
391
    Q_ASSERT(m_registeredDocuments.contains(invalidDoc));
392
    m_registeredDocuments.removeAll(invalidDoc);
393

394
395
396
397
    /**
     * we shall have no views for this document at this point in time!
     */
    Q_ASSERT(!m_docToView.contains(invalidDoc));
398

399
    // disconnect entirely
400
    disconnect(doc, nullptr, this, nullptr);
401

402
    /**
403
     * remove the tab for this document, if existing
404
     */
405
    m_tabBar->removeDocument(invalidDoc);
406
407
}

408
void KateViewSpace::updateDocumentName(KTextEditor::Document *doc)
409
{
410
    // update tab button if available, might not be the case for tab limit set!
411
    const int buttonId = m_tabBar->documentIdx(doc);
412
413
414
    if (buttonId >= 0) {
        m_tabBar->setTabText(buttonId, doc->documentName());
    }
415
416
}

Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
417
418
void KateViewSpace::updateDocumentUrl(KTextEditor::Document *doc)
{
419
    // update tab button if available, might not be the case for tab limit set!
420
    const int buttonId = m_tabBar->documentIdx(doc);
421
    if (buttonId >= 0) {
Christoph Cullmann's avatar
Christoph Cullmann committed
422
        m_tabBar->setTabToolTip(buttonId, doc->url().toDisplayString());
423
    }
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
424
425
}

426
427
428
429
void KateViewSpace::updateDocumentState(KTextEditor::Document *doc)
{
    QIcon icon;
    if (doc->isModified()) {
Laurent Montel's avatar
Laurent Montel committed
430
        icon = QIcon::fromTheme(QStringLiteral("document-save"));
431
432
    }

433
    // update tab button if available, might not be the case for tab limit set!
434
    const int buttonId = m_tabBar->documentIdx(doc);
435
436
437
    if (buttonId >= 0) {
        m_tabBar->setTabIcon(buttonId, icon);
    }
438
439
}

440
void KateViewSpace::closeTabRequest(int idx)
441
{
442
    auto *doc = m_tabBar->tabDocument(idx);
443
    Q_ASSERT(doc);
444
    m_viewManager->slotDocumentClose(doc);
445
446
}

447
448
449
void KateViewSpace::createNewDocument()
{
    // make sure we open the view in this view space
450
    if (!isActiveSpace()) {
451
452
453
454
455
456
457
458
459
460
        m_viewManager->setActiveSpace(this);
    }

    // create document
    KTextEditor::Document *doc = KateApp::self()->documentManager()->createDoc();

    // tell the view manager to show the document
    m_viewManager->activateView(doc);
}

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
void KateViewSpace::focusPrevTab()
{
    const int id = m_tabBar->prevTab();
    if (id >= 0) {
        changeView(id);
    }
}

void KateViewSpace::focusNextTab()
{
    const int id = m_tabBar->nextTab();
    if (id >= 0) {
        changeView(id);
    }
}

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
void KateViewSpace::addJump(const QUrl &url, KTextEditor::Cursor c)
{
    // we are in the middle of jumps somewhere?
    if (!m_locations.isEmpty() && currentLocation + 1 < m_locations.size()) {
        // erase all forward history
        m_locations.erase(m_locations.begin() + currentLocation + 1, m_locations.end());
    }

    // if same line, remove last entry
    if (!m_locations.isEmpty() && m_locations.back().url == url && m_locations.back().cursor.line() == c.line()) {
        m_locations.pop_back();
    }

    // limit size to 100, remove first 20
    if (m_locations.size() >= 100) {
        m_locations.erase(m_locations.begin(), m_locations.begin() + 20);
    }

    // this is our new forward

    m_locations.push_back({url, c});
    // set to last
    currentLocation = m_locations.size() - 1;
    // disable forward button as we are at the end now
    m_historyForward->setEnabled(false);
502
    Q_EMIT m_viewManager->historyForwardEnabled(false);
503
504
505
506

    // renable back
    if (currentLocation > 0) {
        m_historyBack->setEnabled(true);
507
        Q_EMIT m_viewManager->historyBackEnabled(true);
508
509
    }
}
510
511
512
513
514
515
516
int KateViewSpace::hiddenDocuments() const
{
    const int hiddenDocs = KateApp::self()->documents().count() - m_tabBar->count();
    Q_ASSERT(hiddenDocs >= 0);
    return hiddenDocs;
}

517
void KateViewSpace::showContextMenu(int idx, const QPoint &globalPos)
518
519
{
    // right now, show no context menu on empty tab bar space
520
    if (idx < 0) {
521
522
523
        return;
    }

524
    auto *doc = m_tabBar->tabDocument(idx);
525
526
    Q_ASSERT(doc);

527
528
    auto addActionFromCollection = [this](QMenu *menu, const char *action_name) {
        QAction *action = m_viewManager->mainWindow()->action(action_name);
529
530
531
        return menu->addAction(action->icon(), action->text());
    };

532
    QMenu menu(this);
533
534
    QAction *aCloseTab = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close")), i18n("&Close Document"));
    QAction *aCloseOthers = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18n("Close Other &Documents"));
535
    menu.addSeparator();
536
537
538
539
540
541
542
543
544
545
    QAction *aCopyPath = addActionFromCollection(&menu, "file_copy_filepath");
    QAction *aOpenFolder = addActionFromCollection(&menu, "file_open_containing_folder");
    QAction *aFileProperties = addActionFromCollection(&menu, "file_properties");
    menu.addSeparator();
    QAction *aRenameFile = addActionFromCollection(&menu, "file_rename");
    QAction *aDeleteFile = addActionFromCollection(&menu, "file_delete");
    menu.addSeparator();
    QMenu *mCompareWithActive = new QMenu(i18n("Compare with active document"), &menu);
    mCompareWithActive->setIcon(QIcon::fromTheme(QStringLiteral("kompare")));
    menu.addMenu(mCompareWithActive);
546

547
548
549
    if (KateApp::self()->documentManager()->documentList().count() < 2) {
        aCloseOthers->setEnabled(false);
    }
550

551
552
553
    if (doc->url().isEmpty()) {
        aCopyPath->setEnabled(false);
        aOpenFolder->setEnabled(false);
554
555
556
557
558
559
        aRenameFile->setEnabled(false);
        aDeleteFile->setEnabled(false);
        aFileProperties->setEnabled(false);
        mCompareWithActive->setEnabled(false);
    }

Alexander Lohnau's avatar
Alexander Lohnau committed
560
561
562
    auto activeDocument =
        KTextEditor::Editor::instance()->application()->activeMainWindow()->activeView()->document(); // used for mCompareWithActive which is used with another
                                                                                                      // tab which is not active
563
564
565
566
567
568
    // both documents must have urls and must not be the same to have the compare feature enabled
    if (activeDocument->url().isEmpty() || activeDocument == doc) {
        mCompareWithActive->setEnabled(false);
    }

    if (mCompareWithActive->isEnabled()) {
569
        for (auto &&diffTool : KateFileActions::supportedDiffTools()) {
570
571
572
            QAction *compareAction = mCompareWithActive->addAction(diffTool);
            compareAction->setData(diffTool);
        }
573
    }
574
575
576

    QAction *choice = menu.exec(globalPos);

577
578
579
580
    if (!choice) {
        return;
    }

581
    if (choice == aCloseTab) {
582
        closeTabRequest(idx);
583
584
    } else if (choice == aCloseOthers) {
        KateApp::self()->documentManager()->closeOtherDocuments(doc);
585
    } else if (choice == aCopyPath) {
586
        KateFileActions::copyFilePathToClipboard(doc);
587
    } else if (choice == aOpenFolder) {
588
589
590
591
592
593
594
595
596
597
        KateFileActions::openContainingFolder(doc);
    } else if (choice == aFileProperties) {
        KateFileActions::openFilePropertiesDialog(doc);
    } else if (choice == aRenameFile) {
        KateFileActions::renameDocumentFile(this, doc);
    } else if (choice == aDeleteFile) {
        KateFileActions::deleteDocumentFile(this, doc);
    } else if (choice->parent() == mCompareWithActive) {
        QString actionData = choice->data().toString(); // name of the executable of the diff program
        if (!KateFileActions::compareWithExternalProgram(activeDocument, doc, actionData)) {
Alexander Lohnau's avatar
Alexander Lohnau committed
598
599
600
601
            QMessageBox::information(this,
                                     i18n("Could not start program"),
                                     i18n("The selected program could not be started. Maybe it is not installed."),
                                     QMessageBox::StandardButton::Ok);
602
        }
603
604
605
    }
}

606
void KateViewSpace::saveConfig(KConfigBase *config, int myIndex, const QString &viewConfGrp)
607
{
608
    //   qCDebug(LOG_KATE)<<"KateViewSpace::saveConfig("<<myIndex<<", "<<viewConfGrp<<") - currentView: "<<currentView()<<")";
609
    QString groupname = QString(viewConfGrp + QStringLiteral("-ViewSpace %1")).arg(myIndex);
610

611
    // aggregate all views in view space (LRU ordered)
612
    QVector<KTextEditor::View *> views;
613
    QStringList lruList;
614
    for (KTextEditor::Document *doc : documentList()) {
615
        lruList << doc->url().toString();
616
617
618
619
620
        if (m_docToView.contains(doc)) {
            views.append(m_docToView[doc]);
        }
    }

621
    KConfigGroup group(config, groupname);
622
    group.writeEntry("Documents", lruList);
623
    group.writeEntry("Count", views.count());
624

625
626
627
    if (currentView()) {
        group.writeEntry("Active View", currentView()->document()->url().toString());
    }
628

629
630
    // Save file list, including cursor position in this instance.
    int idx = 0;
631
    for (QVector<KTextEditor::View *>::iterator it = views.begin(); it != views.end(); ++it) {
632
        if (!(*it)->document()->url().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
633
            group.writeEntry(QStringLiteral("View %1").arg(idx), (*it)->document()->url().toString());
634

635
            // view config, group: "ViewSpace <n> url"
Laurent Montel's avatar
Laurent Montel committed
636
            QString vgroup = QStringLiteral("%1 %2").arg(groupname, (*it)->document()->url().toString());
637
            KConfigGroup viewGroup(config, vgroup);
638
            (*it)->writeSessionConfig(viewGroup);
639
        }
640

641
642
        ++idx;
    }
643
644
}

645
void KateViewSpace::restoreConfig(KateViewManager *viewMan, const KConfigBase *config, const QString &groupname)
646
{
647
    KConfigGroup group(config, groupname);
648

Alexander Lohnau's avatar
Alexander Lohnau committed
649
650
    // workaround for the weird bug where the tabbar sometimes becomes invisible after opening a session via the session chooser dialog or the --start cmd
    // option
651
652
653
654
    // TODO: Debug the actual reason for the bug. See https://invent.kde.org/utilities/kate/-/merge_requests/189
    m_tabBar->hide();
    m_tabBar->show();

655
656
657
    // set back bar status to configured variant
    tabBarToggled();

658
659
660
    // restore Document lru list so that all tabs from the last session reappear
    const QStringList lruList = group.readEntry("Documents", QStringList());
    for (int i = 0; i < lruList.size(); ++i) {
661
662
        // ignore non-existing documents
        if (auto doc = KateApp::self()->documentManager()->findDocument(QUrl(lruList[i]))) {
663
            registerDocument(doc);
664
665
666
667
668
        }
    }

    // restore active view properties
    const QString fn = group.readEntry("Active View");
669
    if (!fn.isEmpty()) {
Christoph Cullmann's avatar
Christoph Cullmann committed
670
        KTextEditor::Document *doc = KateApp::self()->documentManager()->findDocument(QUrl(fn));
671

672
673
        if (doc) {
            // view config, group: "ViewSpace <n> url"
Laurent Montel's avatar
Laurent Montel committed
674
            QString vgroup = QStringLiteral("%1 %2").arg(groupname, fn);
675
            KConfigGroup configGroup(config, vgroup);
676

677
678
            auto view = viewMan->createView(doc, this);
            if (view) {
679
680
681
                // When a session is opened with a remote file being active, we need to wait
                // with applying saved session settings until the remote's temp file is initialised.
                if (!view->document()->url().isLocalFile()) {
682
                    QSharedPointer<QMetaObject::Connection> conn(new QMetaObject::Connection());
Christoph Cullmann's avatar
Christoph Cullmann committed
683
                    auto handler = [conn, view, configGroup](KTextEditor::Document *) {
684
                        disconnect(*conn);
685
                        view->readSessionConfig(configGroup);
686
687
                    };
                    *conn = connect(doc, &KTextEditor::Document::textChanged, view, handler);
688
689
690
                } else {
                    view->readSessionConfig(configGroup);
                }
691
                m_tabBar->setCurrentDocument(doc);
692
            }
693
        }
694
695
    }

696
697
    // avoid empty view space
    if (m_docToView.isEmpty()) {
698
        auto *doc = KateApp::self()->documentManager()->documentList().first();
699
700
        if (!fn.isEmpty()) {
            QUrl url(fn);
Christoph Cullmann's avatar
Christoph Cullmann committed
701
702
            KateApp::self()->documentManager()->documentInfo(doc)->doPostLoadOperations =
                !url.isLocalFile() && (KateApp::self()->hasCursorInArgs() || url.hasQuery());
703
        }
704
        viewMan->createView(doc, this);
705
    }
706

707
    m_group = groupname; // used for restroing view configs later
708
}
709
710
711
712
713
714
715
716
717
718
719
720

void KateViewSpace::goBack()
{
    if (m_locations.isEmpty() || currentLocation == 0) {
        return;
    }

    const auto &location = m_locations.at(currentLocation - 1);
    currentLocation--;

    if (currentLocation <= 0) {
        m_historyBack->setEnabled(false);
721
        Q_EMIT m_viewManager->historyBackEnabled(false);
722
723
724
725
726
727
728
729
    }

    if (auto v = m_viewManager->activeView()) {
        if (v->document() && v->document()->url() == location.url) {
            const QSignalBlocker blocker(m_viewManager->activeView());
            m_viewManager->activeView()->setCursorPosition(location.cursor);
            // enable forward
            m_historyForward->setEnabled(true);
730
            Q_EMIT m_viewManager->historyForwardEnabled(true);
731
732
733
734
735
736
737
            return;
        }
    }

    auto v = m_viewManager->openUrlWithView(location.url, QString());
    const QSignalBlocker blocker(v);
    v->setCursorPosition(location.cursor);
738
    // enable forward in viewspace + mainwindow
739
    m_historyForward->setEnabled(true);
740
    Q_EMIT m_viewManager->historyForwardEnabled(true);
741
742
743
744
745
746
747
748
749
750
}

bool KateViewSpace::isHistoryBackEnabled() const
{
    return m_historyBack->isEnabled();
}

bool KateViewSpace::isHistoryForwardEnabled() const
{
    return m_historyForward->isEnabled();
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
}

void KateViewSpace::goForward()
{
    if (m_locations.isEmpty()) {
        return;
    }
    if (currentLocation == m_locations.size() - 1) {
        return;
    }

    const auto &location = m_locations.at(currentLocation + 1);
    currentLocation++;

    if (currentLocation + 1 >= m_locations.size()) {
766
        Q_EMIT m_viewManager->historyForwardEnabled(false);
767
768
769
770
771
772
773
774
775
        m_historyForward->setEnabled(false);
    }

    if (!location.url.isValid() || !location.cursor.isValid()) {
        m_locations.remove(currentLocation);
        return;
    }

    m_historyBack->setEnabled(true);
776
    Q_EMIT m_viewManager->historyBackEnabled(true);
777
778
779
780
781
782
783
784
785
786
787
788
789
790

    if (auto v = m_viewManager->activeView()) {
        if (v->document() && v->document()->url() == location.url) {
            const QSignalBlocker blocker(m_viewManager->activeView());
            m_viewManager->activeView()->setCursorPosition(location.cursor);
            return;
        }
    }

    auto v = m_viewManager->openUrlWithView(location.url, QString());
    const QSignalBlocker blocker(v);
    v->setCursorPosition(location.cursor);
}

791
// END KateViewSpace