kateprojectpluginview.cpp 21.9 KB
Newer Older
Christoph Cullmann's avatar
Christoph Cullmann committed
1
2
/*  This file is part of the Kate project.
 *
3
 *  SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
Christoph Cullmann's avatar
Christoph Cullmann committed
4
 *
5
 *  SPDX-License-Identifier: LGPL-2.0-or-later
Christoph Cullmann's avatar
Christoph Cullmann committed
6
7
8
 */

#include "kateprojectpluginview.h"
9
#include "fileutil.h"
Waqar Ahmed's avatar
Waqar Ahmed committed
10
#include "gitwidget.h"
11
#include "kateprojectinfoviewindex.h"
Christoph Cullmann's avatar
Christoph Cullmann committed
12

Christoph Cullmann's avatar
Christoph Cullmann committed
13
#include <ktexteditor/application.h>
14
#include <ktexteditor/codecompletioninterface.h>
15
16
17
#include <ktexteditor/document.h>
#include <ktexteditor/editor.h>
#include <ktexteditor/view.h>
Christoph Cullmann's avatar
Christoph Cullmann committed
18

19
20
21
#include <KAboutData>
#include <KActionCollection>
#include <KActionMenu>
Christoph Cullmann's avatar
Christoph Cullmann committed
22
#include <KLocalizedString>
23
24
#include <KPluginFactory>
#include <KStringHandler>
Christoph Cullmann's avatar
Christoph Cullmann committed
25
#include <KXMLGUIFactory>
Christoph Cullmann's avatar
Christoph Cullmann committed
26

27
#include <QAction>
Christoph Cullmann's avatar
Christoph Cullmann committed
28
#include <QDialog>
Christoph Cullmann's avatar
Christoph Cullmann committed
29
#include <QHBoxLayout>
30
#include <QKeyEvent>
31
#include <QMenu>
32
#include <QTimer>
33
#include <QVBoxLayout>
Christoph Cullmann's avatar
Christoph Cullmann committed
34

35
36
37
38
39
40
41
42
K_PLUGIN_FACTORY_WITH_JSON(KateProjectPluginFactory, "kateprojectplugin.json", registerPlugin<KateProjectPlugin>();)

KateProjectPluginView::KateProjectPluginView(KateProjectPlugin *plugin, KTextEditor::MainWindow *mainWin)
    : QObject(mainWin)
    , m_plugin(plugin)
    , m_mainWindow(mainWin)
    , m_toolView(nullptr)
    , m_toolInfoView(nullptr)
43
    , m_toolMultiView(nullptr)
44
    , m_lookupAction(nullptr)
45
46
    , m_gotoSymbolAction(nullptr)
    , m_gotoSymbolActionAppMenu(nullptr)
Christoph Cullmann's avatar
Christoph Cullmann committed
47
{
48
49
50
    KXMLGUIClient::setComponentName(QStringLiteral("kateproject"), i18n("Kate Project Manager"));
    setXMLFile(QStringLiteral("ui.rc"));

51
52
53
    /**
     * create toolviews
     */
Alexander Lohnau's avatar
Alexander Lohnau committed
54
55
56
57
58
    m_toolView = m_mainWindow->createToolView(m_plugin,
                                              QStringLiteral("kateproject"),
                                              KTextEditor::MainWindow::Left,
                                              QIcon::fromTheme(QStringLiteral("project-open")),
                                              i18n("Projects"));
Waqar Ahmed's avatar
Waqar Ahmed committed
59
60
61
62
63
    m_gitToolView.reset(m_mainWindow->createToolView(m_plugin,
                                                     QStringLiteral("kateprojectgit"),
                                                     KTextEditor::MainWindow::Left,
                                                     QIcon(QStringLiteral(":/icons/icons/sc-apps-git.svg")),
                                                     i18n("Git")));
Alexander Lohnau's avatar
Alexander Lohnau committed
64
65
66
67
68
    m_toolInfoView = m_mainWindow->createToolView(m_plugin,
                                                  QStringLiteral("kateprojectinfo"),
                                                  KTextEditor::MainWindow::Bottom,
                                                  QIcon::fromTheme(QStringLiteral("view-choose")),
                                                  i18n("Current Project"));
69
70
71
72
73
74
75
76
77

    /**
     * create the combo + buttons for the toolViews + stacked widgets
     */
    m_projectsCombo = new QComboBox(m_toolView);
    m_projectsCombo->setFrame(false);
    m_reloadButton = new QToolButton(m_toolView);
    m_reloadButton->setAutoRaise(true);
    m_reloadButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
78
79
80
    m_closeProjectButton = new QToolButton(m_toolView);
    m_closeProjectButton->setAutoRaise(true);
    m_closeProjectButton->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));
81
82
83
84
    QHBoxLayout *layout = new QHBoxLayout();
    layout->setSpacing(0);
    layout->addWidget(m_projectsCombo);
    layout->addWidget(m_reloadButton);
85
    layout->addWidget(m_closeProjectButton);
86
87
88
    m_toolView->layout()->addItem(layout);
    m_toolView->layout()->setSpacing(0);

Waqar Ahmed's avatar
Waqar Ahmed committed
89
90
    m_projectsComboGit = new QComboBox(m_gitToolView.get());
    m_projectsComboGit->setFrame(false);
91
92
93
94
95
96
97
98
99
    m_gitStatusRefreshButton = new QToolButton(m_gitToolView.get());
    m_gitStatusRefreshButton->setAutoRaise(true);
    m_gitStatusRefreshButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
    layout = new QHBoxLayout();
    layout->setSpacing(0);
    layout->addWidget(m_projectsComboGit);
    layout->addWidget(m_gitStatusRefreshButton);
    m_gitToolView->layout()->addItem(layout);
    m_gitToolView->layout()->setSpacing(0);
Waqar Ahmed's avatar
Waqar Ahmed committed
100

101
102
    m_stackedProjectViews = new QStackedWidget(m_toolView);
    m_stackedProjectInfoViews = new QStackedWidget(m_toolInfoView);
Waqar Ahmed's avatar
Waqar Ahmed committed
103
104
105
106
107
108
109
110
111
    m_stackedgitViews = new QStackedWidget(m_gitToolView.get());

    connect(m_projectsCombo,
            static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
            m_projectsComboGit,
            static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged));
    connect(m_projectsComboGit, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this](int index) {
        m_projectsCombo->setCurrentIndex(index);
    });
112
113
    connect(m_projectsCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &KateProjectPluginView::slotCurrentChanged);
    connect(m_reloadButton, &QToolButton::clicked, this, &KateProjectPluginView::slotProjectReload);
114

115
    connect(m_closeProjectButton, &QToolButton::clicked, this, &KateProjectPluginView::slotProjectAboutToClose);
116
117
    connect(m_plugin, &KateProjectPlugin::pluginViewProjectClosing, this, &KateProjectPluginView::slotProjectClose);
    
118
119
120
121
122
    connect(m_gitStatusRefreshButton, &QToolButton::clicked, this, [this] {
        if (auto widget = m_stackedgitViews->currentWidget()) {
            qobject_cast<GitWidget *>(widget)->getStatus();
        }
    });
123
124
125
126
    /**
     * create views for all already existing projects
     * will create toolviews on demand!
     */
127
    const auto projectList = m_plugin->projects();
128
    for (KateProject *project : projectList) {
Dominik Haumann's avatar
Dominik Haumann committed
129
        viewForProject(project);
130
    }
131
132
133
    // If the list of projects is empty we do not want to restore the tool view from the last session, BUG: 432296
    if (projectList.isEmpty()) {
        // We have to call this in the next iteration of the event loop, after the session is restored
134
        QTimer::singleShot(0, this, [this]() {
135
            m_mainWindow->hideToolView(m_toolView);
Christoph Cullmann's avatar
Christoph Cullmann committed
136
            m_mainWindow->hideToolView(m_gitToolView.get());
137
            m_mainWindow->hideToolView(m_toolInfoView);
Christoph Cullmann's avatar
Christoph Cullmann committed
138
139
140
            if (m_toolMultiView) {
                m_mainWindow->hideToolView(m_toolMultiView);
            }
141
142
        });
    }
143
144
145
146

    /**
     * connect to important signals, e.g. for auto project view creation
     */
147
    connect(m_plugin, &KateProjectPlugin::projectCreated, this, &KateProjectPluginView::viewForProject);
148
    connect(m_plugin, &KateProjectPlugin::configUpdated, this, &KateProjectPluginView::slotConfigUpdated);
149
150
    connect(m_mainWindow, &KTextEditor::MainWindow::viewChanged, this, &KateProjectPluginView::slotViewChanged);
    connect(m_mainWindow, &KTextEditor::MainWindow::viewCreated, this, &KateProjectPluginView::slotViewCreated);
151
152
153
154

    /**
     * connect for all already existing views
     */
155
    const auto views = m_mainWindow->views();
156
    for (KTextEditor::View *view : views) {
Dominik Haumann's avatar
Dominik Haumann committed
157
        slotViewCreated(view);
158
    }
159
160
161
162
163
164
165
166
167

    /**
     * trigger once view change, to highlight right document
     */
    slotViewChanged();

    /**
     * back + forward
     */
Dominik Haumann's avatar
Dominik Haumann committed
168
169
170
171
    auto a = actionCollection()->addAction(KStandardAction::Back, QStringLiteral("projects_prev_project"), this, SLOT(slotProjectPrev()));
    actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Left));
    a = actionCollection()->addAction(KStandardAction::Forward, QStringLiteral("projects_next_project"), this, SLOT(slotProjectNext()));
    actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Right));
172
173
    a = actionCollection()->addAction(QStringLiteral("projects_goto_index"), this, SLOT(slotProjectIndex()));
    a->setText(i18n("Lookup"));
174
    actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::ALT | Qt::Key_1));
175
    m_gotoSymbolActionAppMenu = a = actionCollection()->addAction(KStandardAction::Goto, QStringLiteral("projects_goto_symbol"), this, SLOT(slotGotoSymbol()));
176
177
178

    // popup menu
    auto popup = new KActionMenu(i18n("Project"), this);
Laurent Montel's avatar
Laurent Montel committed
179
    actionCollection()->addAction(QStringLiteral("popup_project"), popup);
180

Laurent Montel's avatar
Laurent Montel committed
181
    m_lookupAction = popup->menu()->addAction(i18n("Lookup: %1", QString()), this, &KateProjectPluginView::slotProjectIndex);
182
    m_gotoSymbolAction = popup->menu()->addAction(i18n("Goto: %1", QString()), this, &KateProjectPluginView::slotGotoSymbol);
183

184
    connect(popup->menu(), &QMenu::aboutToShow, this, &KateProjectPluginView::slotContextMenuAboutToShow);
185

186
187
    connect(m_mainWindow, &KTextEditor::MainWindow::unhandledShortcutOverride, this, &KateProjectPluginView::handleEsc);

188
189
190
    // update git status on toolview visibility change
    connect(m_gitToolView.get(), SIGNAL(toolVisibleChanged(bool)), this, SLOT(slotUpdateStatus(bool)));

191
192
193
194
    /**
     * add us to gui
     */
    m_mainWindow->guiFactory()->addClient(this);
195
196
197
198
199

    /**
     * align to current config
     */
    slotConfigUpdated();
Christoph Cullmann's avatar
Christoph Cullmann committed
200
201
202
203
}

KateProjectPluginView::~KateProjectPluginView()
{
204
205
206
    /**
     * cleanup for all views
     */
207
    for (QObject *view : qAsConst(m_textViews)) {
208
209
210
211
212
213
214
215
216
217
218
219
220
        KTextEditor::CodeCompletionInterface *cci = qobject_cast<KTextEditor::CodeCompletionInterface *>(view);
        if (cci) {
            cci->unregisterCompletionModel(m_plugin->completion());
        }
    }

    /**
     * cu toolviews
     */
    delete m_toolView;
    m_toolView = nullptr;
    delete m_toolInfoView;
    m_toolInfoView = nullptr;
221
222
    delete m_toolMultiView;
    m_toolMultiView = nullptr;
223
224
225
226
227

    /**
     * cu gui client
     */
    m_mainWindow->guiFactory()->removeClient(this);
Christoph Cullmann's avatar
Christoph Cullmann committed
228
229
}

230
231
232
233
234
235
void KateProjectPluginView::slotConfigUpdated()
{
    if (!m_plugin->multiProjectGoto()) {
        delete m_toolMultiView;
        m_toolMultiView = nullptr;
    } else if (!m_toolMultiView) {
Alexander Lohnau's avatar
Alexander Lohnau committed
236
237
238
239
240
        m_toolMultiView = m_mainWindow->createToolView(m_plugin,
                                                       QStringLiteral("kateprojectmulti"),
                                                       KTextEditor::MainWindow::Bottom,
                                                       QIcon::fromTheme(QStringLiteral("view-choose")),
                                                       i18n("Projects Index"));
241
242
243
244
245
246
247
248
249
        auto gotoindex = new KateProjectInfoViewIndex(this, nullptr, m_toolMultiView);
        m_toolMultiView->layout()->addWidget(gotoindex);
    }

    // update action state
    m_gotoSymbolActionAppMenu->setEnabled(m_toolMultiView);
    m_gotoSymbolAction->setEnabled(m_toolMultiView);
}

250
QPair<KateProjectView *, KateProjectInfoView *> KateProjectPluginView::viewForProject(KateProject *project)
Christoph Cullmann's avatar
Christoph Cullmann committed
251
{
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    /**
     * needs valid project
     */
    Q_ASSERT(project);

    /**
     * existing view?
     */
    if (m_project2View.contains(project)) {
        return m_project2View.value(project);
    }

    /**
     * create new views
     */
267
    KateProjectView *view = new KateProjectView(this, project, m_mainWindow);
268
    KateProjectInfoView *infoView = new KateProjectInfoView(this, project);
Waqar Ahmed's avatar
Waqar Ahmed committed
269
    GitWidget *gitView = new GitWidget(project, m_mainWindow, this);
270
271
272
273
274
275
276

    /**
     * attach to toolboxes
     * first the views, then the combo, that triggers signals
     */
    m_stackedProjectViews->addWidget(view);
    m_stackedProjectInfoViews->addWidget(infoView);
Waqar Ahmed's avatar
Waqar Ahmed committed
277
    m_stackedgitViews->addWidget(gitView);
278
    m_projectsCombo->addItem(QIcon::fromTheme(QStringLiteral("project-open")), project->name(), project->fileName());
Waqar Ahmed's avatar
Waqar Ahmed committed
279
    m_projectsComboGit->addItem(QIcon::fromTheme(QStringLiteral("project-open")), project->name(), project->fileName());
280
281
282
283

    /**
     * remember and return it
     */
284
    return (m_project2View[project] = QPair<KateProjectView *, KateProjectInfoView *>(view, infoView));
Christoph Cullmann's avatar
Christoph Cullmann committed
285
286
}

287
QString KateProjectPluginView::projectFileName() const
288
{
289
290
291
292
293
294
    QWidget *active = m_stackedProjectViews->currentWidget();
    if (!active) {
        return QString();
    }

    return static_cast<KateProjectView *>(active)->project()->fileName();
295
296
}

297
QString KateProjectPluginView::projectName() const
298
{
299
300
301
302
    QWidget *active = m_stackedProjectViews->currentWidget();
    if (!active) {
        return QString();
    }
303

304
    return static_cast<KateProjectView *>(active)->project()->name();
305
306
}

307
QString KateProjectPluginView::projectBaseDir() const
308
{
309
310
311
312
313
314
    QWidget *active = m_stackedProjectViews->currentWidget();
    if (!active) {
        return QString();
    }

    return static_cast<KateProjectView *>(active)->project()->baseDir();
315
316
}

317
QVariantMap KateProjectPluginView::projectMap() const
318
{
319
320
321
322
323
324
    QWidget *active = m_stackedProjectViews->currentWidget();
    if (!active) {
        return QVariantMap();
    }

    return static_cast<KateProjectView *>(active)->project()->projectMap();
325
326
}

327
QStringList KateProjectPluginView::projectFiles() const
328
{
329
330
331
332
333
334
    KateProjectView *active = static_cast<KateProjectView *>(m_stackedProjectViews->currentWidget());
    if (!active) {
        return QStringList();
    }

    return active->project()->files();
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
QString KateProjectPluginView::allProjectsCommonBaseDir() const
{
    auto projects = m_plugin->projects();

    if (projects.empty()) {
        return QString();
    }

    if (projects.size() == 1) {
        return projects[0]->baseDir();
    }

    QString commonParent1 = FileUtil::commonParent(projects[0]->baseDir(), projects[1]->baseDir());

    for (int i = 2; i < projects.size(); i++) {
        commonParent1 = FileUtil::commonParent(commonParent1, projects[i]->baseDir());
    }

    return commonParent1;
}

QStringList KateProjectPluginView::allProjectsFiles() const
{
    QStringList fileList;

362
363
    const auto projectList = m_plugin->projects();
    for (auto project : projectList) {
364
365
366
367
368
369
        fileList.append(project->files());
    }

    return fileList;
}

370
void KateProjectPluginView::slotViewChanged()
371
{
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
    /**
     * get active view
     */
    KTextEditor::View *activeView = m_mainWindow->activeView();

    /**
     * update pointer, maybe disconnect before
     */
    if (m_activeTextEditorView) {
        m_activeTextEditorView->document()->disconnect(this);
    }
    m_activeTextEditorView = activeView;

    /**
     * no current active view, return
     */
    if (!m_activeTextEditorView) {
        return;
    }

    /**
     * connect to url changed, for auto load
     */
395
    connect(m_activeTextEditorView->document(), &KTextEditor::Document::documentUrlChanged, this, &KateProjectPluginView::slotDocumentUrlChanged);
396
397
398
399
400

    /**
     * trigger slot once
     */
    slotDocumentUrlChanged(m_activeTextEditorView->document());
401
402
}

403
void KateProjectPluginView::slotCurrentChanged(int index)
404
{
405
    // trigger change of stacked widgets
406
407
    m_stackedProjectViews->setCurrentIndex(index);
    m_stackedProjectInfoViews->setCurrentIndex(index);
Waqar Ahmed's avatar
Waqar Ahmed committed
408
409
410
411
412
413
    m_stackedgitViews->setCurrentIndex(index);

    {
        const QSignalBlocker blocker(m_projectsComboGit);
        m_projectsComboGit->setCurrentIndex(index);
    }
414

415
    // update focus proxy + open currently selected document
416
    if (QWidget *current = m_stackedProjectViews->currentWidget()) {
417
        m_stackedProjectViews->setFocusProxy(current);
418
419
420
        static_cast<KateProjectView *>(current)->openSelectedDocument();
    }

421
422
423
424
425
    // update focus proxy
    if (QWidget *current = m_stackedProjectInfoViews->currentWidget()) {
        m_stackedProjectInfoViews->setFocusProxy(current);
    }

Waqar Ahmed's avatar
Waqar Ahmed committed
426
427
428
429
430
431
    // update git focus proxy + update status
    if (QWidget *current = m_stackedgitViews->currentWidget()) {
        m_stackedgitViews->setFocusProxy(current);
        static_cast<GitWidget *>(current)->getStatus();
    }

432
    // project file name might have changed
Christoph Cullmann's avatar
Christoph Cullmann committed
433
434
    Q_EMIT projectFileNameChanged();
    Q_EMIT projectMapChanged();
435
436
}

437
void KateProjectPluginView::slotDocumentUrlChanged(KTextEditor::Document *document)
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
    /**
     * abort if empty url or no local path
     */
    if (document->url().isEmpty() || !document->url().isLocalFile()) {
        return;
    }

    /**
     * search matching project
     */
    KateProject *project = m_plugin->projectForUrl(document->url());
    if (!project) {
        return;
    }

    /**
     * select the file FIRST
     */
    m_project2View.value(project).first->selectFile(document->url().toLocalFile());

    /**
     * get active project view and switch it, if it is for a different project
     * do this AFTER file selection
     */
    KateProjectView *active = static_cast<KateProjectView *>(m_stackedProjectViews->currentWidget());
    if (active != m_project2View.value(project).first) {
        int index = m_projectsCombo->findData(project->fileName());
        if (index >= 0) {
            m_projectsCombo->setCurrentIndex(index);
        }
    }
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
void KateProjectPluginView::switchToProject(const QDir &dir)
{
    /**
     * search matching project
     */
    KateProject *project = m_plugin->projectForDir(dir);
    if (!project) {
        return;
    }

    /**
     * get active project view and switch it, if it is for a different project
     * do this AFTER file selection
     */
    KateProjectView *active = static_cast<KateProjectView *>(m_stackedProjectViews->currentWidget());
    if (active != m_project2View.value(project).first) {
        int index = m_projectsCombo->findData(project->fileName());
        if (index >= 0) {
            m_projectsCombo->setCurrentIndex(index);
        }
    }
}

495
void KateProjectPluginView::slotViewCreated(KTextEditor::View *view)
496
{
497
498
499
    /**
     * connect to destroyed
     */
500
    connect(view, &KTextEditor::View::destroyed, this, &KateProjectPluginView::slotViewDestroyed);
501
502
503
504
505
506
507
508
509
510
511
512
513

    /**
     * add completion model if possible
     */
    KTextEditor::CodeCompletionInterface *cci = qobject_cast<KTextEditor::CodeCompletionInterface *>(view);
    if (cci) {
        cci->registerCompletionModel(m_plugin->completion());
    }

    /**
     * remember for this view we need to cleanup!
     */
    m_textViews.insert(view);
514
515
}

516
void KateProjectPluginView::slotViewDestroyed(QObject *view)
517
{
518
519
520
521
    /**
     * remove remembered views for which we need to cleanup on exit!
     */
    m_textViews.remove(view);
522
523
}

524
void KateProjectPluginView::slotProjectPrev()
525
{
526
527
528
529
530
531
532
533
534
    if (!m_projectsCombo->count()) {
        return;
    }

    if (m_projectsCombo->currentIndex() == 0) {
        m_projectsCombo->setCurrentIndex(m_projectsCombo->count() - 1);
    } else {
        m_projectsCombo->setCurrentIndex(m_projectsCombo->currentIndex() - 1);
    }
535
536
}

537
void KateProjectPluginView::slotProjectNext()
538
{
539
540
541
542
543
544
545
546
547
    if (!m_projectsCombo->count()) {
        return;
    }

    if (m_projectsCombo->currentIndex() + 1 == m_projectsCombo->count()) {
        m_projectsCombo->setCurrentIndex(0);
    } else {
        m_projectsCombo->setCurrentIndex(m_projectsCombo->currentIndex() + 1);
    }
548
549
}

550
void KateProjectPluginView::slotProjectReload()
Christoph Cullmann's avatar
Christoph Cullmann committed
551
{
552
553
554
555
556
557
    /**
     * force reload if any active project
     */
    if (QWidget *current = m_stackedProjectViews->currentWidget()) {
        static_cast<KateProjectView *>(current)->project()->reload(true);
    }
558
559
560
561
562
563
    /**
     * Refresh git status
     */
    if (auto widget = m_stackedgitViews->currentWidget()) {
        qobject_cast<GitWidget *>(widget)->getStatus();
    }
Christoph Cullmann's avatar
Christoph Cullmann committed
564
565
}

566
void KateProjectPluginView::slotProjectAboutToClose()
567
{
568
569
    if (QWidget *current = m_stackedProjectViews->currentWidget())
    {
570
        m_plugin->closeProject(static_cast<KateProjectView *>(current)->project());
571
    }
572
573
}

574
575
void KateProjectPluginView::slotProjectClose(KateProject *project)
{
576
    const int index = m_plugin->projects().indexOf(project);
577
    m_project2View.erase(m_project2View.find(project));
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
    
    QWidget* stackedProjectViewsWidget = m_stackedProjectViews->widget(index);
    m_stackedProjectViews->removeWidget(stackedProjectViewsWidget);
    delete stackedProjectViewsWidget;
    
    QWidget* stackedProjectInfoViewsWidget = m_stackedProjectInfoViews->widget(index);
    m_stackedProjectInfoViews->removeWidget(stackedProjectInfoViewsWidget);
    delete stackedProjectInfoViewsWidget;
    
    QWidget* stackedgitViewsWidget = m_stackedgitViews->widget(index);
    m_stackedgitViews->removeWidget(stackedgitViewsWidget);
    delete stackedgitViewsWidget;
    
    m_projectsCombo->removeItem(index);
    m_projectsComboGit->removeItem(index);
593
594
}

595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
QString KateProjectPluginView::currentWord() const
{
    KTextEditor::View *kv = m_activeTextEditorView;
    if (!kv) {
        return QString();
    }

    if (kv->selection() && kv->selectionRange().onSingleLine()) {
        return kv->selectionText();
    }

    return kv->document()->wordAt(kv->cursorPosition());
}

void KateProjectPluginView::slotProjectIndex()
{
    const QString word = currentWord();
    if (!word.isEmpty()) {
614
        auto tabView = qobject_cast<QTabWidget *>(m_stackedProjectInfoViews->currentWidget());
615
        if (tabView) {
616
            if (auto codeIndex = tabView->findChild<KateProjectInfoViewIndex *>()) {
617
618
                tabView->setCurrentWidget(codeIndex);
            }
619
620
        }
        m_mainWindow->showToolView(m_toolInfoView);
Christoph Cullmann's avatar
Christoph Cullmann committed
621
        Q_EMIT projectLookupWord(word);
622
623
624
    }
}

625
626
627
628
629
630
631
632
633
void KateProjectPluginView::slotGotoSymbol()
{
    if (!m_toolMultiView) {
        return;
    }

    const QString word = currentWord();
    if (!word.isEmpty()) {
        int results = 0;
Christoph Cullmann's avatar
Christoph Cullmann committed
634
        Q_EMIT gotoSymbol(word, results);
635
636
637
638
639
640
        if (results > 1) {
            m_mainWindow->showToolView(m_toolMultiView);
        }
    }
}

641
642
643
644
645
646
647
648
649
void KateProjectPluginView::slotContextMenuAboutToShow()
{
    const QString word = currentWord();
    if (word.isEmpty()) {
        return;
    }

    const QString squeezed = KStringHandler::csqueeze(word, 30);
    m_lookupAction->setText(i18n("Lookup: %1", squeezed));
650
    m_gotoSymbolAction->setText(i18n("Goto: %1", squeezed));
651
652
}

653
654
655
656
657
658
659
660
void KateProjectPluginView::handleEsc(QEvent *e)
{
    if (!m_mainWindow) {
        return;
    }

    QKeyEvent *k = static_cast<QKeyEvent *>(e);
    if (k->key() == Qt::Key_Escape && k->modifiers() == Qt::NoModifier) {
661
662
        const auto infoView = qobject_cast<const KateProjectInfoView *>(m_stackedProjectInfoViews->currentWidget());
        if (m_toolInfoView->isVisible() && (!infoView || !infoView->ignoreEsc())) {
663
664
665
666
667
            m_mainWindow->hideToolView(m_toolInfoView);
        }
    }
}

668
669
670
671
672
673
674
675
676
677
678
void KateProjectPluginView::slotUpdateStatus(bool visible)
{
    if (!visible) {
        return;
    }

    if (auto widget = m_stackedgitViews->currentWidget()) {
        static_cast<GitWidget *>(widget)->getStatus();
    }
}

679
#include "kateprojectpluginview.moc"