kateprojectpluginview.h 9.66 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
 */

Yuri Chornoivan's avatar
Yuri Chornoivan committed
8
9
#ifndef _KATE_PROJECT_PLUGIN_VIEW_H_
#define _KATE_PROJECT_PLUGIN_VIEW_H_
Christoph Cullmann's avatar
Christoph Cullmann committed
10
11

#include "kateproject.h"
12
#include "kateprojectinfoview.h"
13
14
#include "kateprojectplugin.h"
#include "kateprojectview.h"
Christoph Cullmann's avatar
Christoph Cullmann committed
15

16
#include <QComboBox>
17
#include <QMenu>
18
#include <QPointer>
19
#include <QStackedWidget>
Christoph Cullmann's avatar
Christoph Cullmann committed
20
#include <QToolButton>
21

Christoph Cullmann's avatar
Christoph Cullmann committed
22
23
#include <KXMLGUIClient>

Waqar Ahmed's avatar
Waqar Ahmed committed
24
25
#include <memory>

26
27
class QAction;

Christoph Cullmann's avatar
Christoph Cullmann committed
28
class KateProjectPluginView : public QObject, public KXMLGUIClient
Christoph Cullmann's avatar
Christoph Cullmann committed
29
30
31
{
    Q_OBJECT

32
    Q_PROPERTY(QString projectFileName READ projectFileName NOTIFY projectFileNameChanged)
33
34
    Q_PROPERTY(QString projectName READ projectName)
    Q_PROPERTY(QString projectBaseDir READ projectBaseDir)
35
    Q_PROPERTY(QVariantMap projectMap READ projectMap NOTIFY projectMapChanged)
36
    Q_PROPERTY(QStringList projectFiles READ projectFiles)
37

38
39
40
    Q_PROPERTY(QString allProjectsCommonBaseDir READ allProjectsCommonBaseDir)
    Q_PROPERTY(QStringList allProjectsFiles READ allProjectsFiles)

41
42
public:
    KateProjectPluginView(KateProjectPlugin *plugin, KTextEditor::MainWindow *mainWindow);
Kevin Funk's avatar
Kevin Funk committed
43
    ~KateProjectPluginView() override;
Christoph Cullmann's avatar
Christoph Cullmann committed
44

45
46
47
48
    /**
     * content of current active project, as variant map
     * @return empty map if no project active, else content of project JSON
     */
49
    QVariantMap projectMap() const;
50

51
52
53
54
    /**
     * which project file is currently active?
     * @return empty string if none, else project file name
     */
55
    QString projectFileName() const;
56

57
    /**
58
59
     * Returns the name of the project
     */
60
    QString projectName() const;
61

62
    /**
63
64
     * Returns the base directory of the project
     */
65
    QString projectBaseDir() const;
66

67
68
69
70
    /**
     * files for the current active project?
     * @return empty list if none, else project files as stringlist
     */
71
72
    QStringList projectFiles() const;

73
74
75
76
77
78
79
80
81
82
83
84
85
    /**
     * Example: Two projects are loaded with baseDir1="/home/dev/project1" and
     * baseDir2="/home/dev/project2". Then "/home/dev/" is returned.
     * @see projectBaseDir().
     * Used for the Search&Replace plugin for option "Search in all open projects".
     */
    QString allProjectsCommonBaseDir() const;

    /**
     * @returns a flat list of files for all open projects (@see also projectFiles())
     */
    QStringList allProjectsFiles() const;

Christoph Cullmann's avatar
Christoph Cullmann committed
86
87
88
89
    /**
     * the main window we belong to
     * @return our main window
     */
90
91
    KTextEditor::MainWindow *mainWindow() const
    {
92
        return m_mainWindow;
Christoph Cullmann's avatar
Christoph Cullmann committed
93
    }
94

95
96
97
98
99
100
101
102
103
    /**
     * the plugin we belong to
     * @return our plugin
     */
    KateProjectPlugin *plugin() const
    {
        return m_plugin;
    }

104
105
106
107
108
109
110
111
112
113
114
115
    /**
     * @brief Shows diff in a fixed view, i.e., the view is recycled instead
     * of creating new view every time
     * @param contents diff contents
     */
    void showDiffInFixedView(const QByteArray &contents)
    {
        if (!m_fixedView.view) {
            m_fixedView.view = mainWindow()->openUrl(QUrl());
            m_fixedView.defaultMenu = m_fixedView.view->contextMenu();
        }

Waqar Ahmed's avatar
Waqar Ahmed committed
116
117
118
119
        m_fixedView.view->document()->setText(QString::fromUtf8(contents));
        m_fixedView.view->document()->setHighlightingMode(QStringLiteral("Diff"));
        /** We don't want save dialog on close */
        m_fixedView.view->document()->setModified(false);
120
        m_fixedView.view->setCursorPosition({0, 0});
Waqar Ahmed's avatar
Waqar Ahmed committed
121
        m_fixedView.restoreMenu();
122
123
        /** Activate this view */
        m_mainWindow->activateView(m_fixedView.view->document());
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    }

    /**
     * Same as above with call back for setting a context menu
     *
     * @param cb Callback on the view. This should always take KTextEditor::View*
     * as a parameter. This is mainly used to plug-in context-menu actions.
     */
    template<typename ViewCallback>
    void showDiffInFixedView(const QByteArray &contents, ViewCallback cb)
    {
        showDiffInFixedView(contents);
        cb(m_fixedView.view);
    }

139
public Q_SLOTS:
Christoph Cullmann's avatar
Christoph Cullmann committed
140
    /**
141
142
     * Create views for given project.
     * Either gives existing ones or creates new one
Christoph Cullmann's avatar
Christoph Cullmann committed
143
     * @param project project we want view for
144
     * @return views (normal + info view)
Christoph Cullmann's avatar
Christoph Cullmann committed
145
     */
146
    QPair<KateProjectView *, KateProjectInfoView *> viewForProject(KateProject *project);
147

148
149
150
151
152
153
    /**
     * Switch to project for given dir, if any there!
     * @param dir dir with the project
     */
    void switchToProject(const QDir &dir);

154
private Q_SLOTS:
155
156
157
158
159
    /**
     * Plugin config updated
     */
    void slotConfigUpdated();

160
161
162
163
    /**
     * New view got created, we need to update our connections
     * @param view new created view
     */
164
    void slotViewCreated(KTextEditor::View *view);
165
166
167
168
169

    /**
     * View got destroyed.
     * @param view deleted view
     */
170
    void slotViewDestroyed(QObject *view);
Christoph Cullmann's avatar
Christoph Cullmann committed
171

172
173
174
    /**
     * Activate the previous project.
     */
175
    void slotProjectPrev();
Christoph Cullmann's avatar
Christoph Cullmann committed
176

177
178
179
    /**
     * Activate the next project.
     */
180
    void slotProjectNext();
Christoph Cullmann's avatar
Christoph Cullmann committed
181
182
183
184
185

    /**
     * Reload current project, if any.
     * This will trigger a reload with force.
     */
186
    void slotProjectReload();
Christoph Cullmann's avatar
Christoph Cullmann committed
187

188
    /**
189
190
     * Getting project for others windows
     * and closing project documents.
191
     */
192
    void slotProjectAboutToClose();
Christoph Cullmann's avatar
Christoph Cullmann committed
193

194
195
196
197
    /**
     * Close current project.
     */
    void slotProjectClose(KateProject *project);
Christoph Cullmann's avatar
Christoph Cullmann committed
198

199
200
201
202
203
    /**
     * Lookup current word
     */
    void slotProjectIndex();

204
205
206
207
208
    /**
     * Goto current word
     */
    void slotGotoSymbol();

209
Q_SIGNALS:
Christoph Cullmann's avatar
Christoph Cullmann committed
210

211
212
213
214
    /**
     * Emitted if project is about to close.
     */
    void pluginProjectClose(KateProject *project);
Christoph Cullmann's avatar
Christoph Cullmann committed
215

216
    /**
217
     * Emitted if projectFileName changed.
218
     */
219
    void projectFileNameChanged();
220

221
222
223
    /**
     * Emitted if projectMap changed.
     */
224
    void projectMapChanged();
225

226
227
228
229
230
231
    /**
     * Emitted when a ctags lookup in requested
     * @param word lookup word
     */
    void projectLookupWord(const QString &word);

232
233
234
235
236
237
    /**
     * Emitted when a ctags goto sysmbol is requested
     * @param word lookup word
     */
    void gotoSymbol(const QString &word, int &results);

238
239
240
241
242
243
244
    /**
     * Signal for outgoing message, the host application will handle them!
     * Will only be handled inside the main windows of this plugin view.
     * @param message outgoing message we send to the host application
     */
    void message(const QVariantMap &message);

245
246
247
248
249
250
    /**
     * Signal for location changed. Location gets saved in history
     * of the current KateViewSpace
     * @param document url
     * @param c pos in document
     */
251
    void addPositionToHistory(const QUrl &url, KTextEditor::Cursor c);
252

253
private Q_SLOTS:
254
255
256
    /**
     * This slot is called whenever the active view changes in our main window.
     */
257
    void slotViewChanged();
258

259
260
261
262
    /**
     * Current project changed.
     * @param index index in toolbox
     */
263
    void slotCurrentChanged(int index);
264

265
266
267
    /**
     * Url changed, to auto-load projects
     */
268
    void slotDocumentUrlChanged(KTextEditor::Document *document);
269

270
271
272
273
274
    /**
     * Show context menu
     */
    void slotContextMenuAboutToShow();

275
276
277
278
279
    /**
     * Handle esc key and hide the toolview
     */
    void handleEsc(QEvent *e);

280
281
282
283
284
    /**
     * Update git status on toolview shown
     */
    void slotUpdateStatus(bool visible);

285
286
287
288
289
    /**
     * Open a folder / project
     */
    void openDirectoryOrProject();

290
291
292
293
294
    /**
     * Show projects To-Dos and Fix-mes
     */
    void showProjectTodos();

295
296
297
298
299
300
private:
    /**
     * find current selected or under cursor word
     */
    QString currentWord() const;

301
private:
Christoph Cullmann's avatar
Christoph Cullmann committed
302
303
304
305
    /**
     * our plugin
     */
    KateProjectPlugin *m_plugin;
306

Christoph Cullmann's avatar
Christoph Cullmann committed
307
308
309
310
    /**
     * the main window we belong to
     */
    KTextEditor::MainWindow *m_mainWindow;
311

Christoph Cullmann's avatar
Christoph Cullmann committed
312
313
314
315
    /**
     * our projects toolview
     */
    QWidget *m_toolView;
316

317
318
319
320
321
    /**
     * our projects info toolview
     */
    QWidget *m_toolInfoView;

Waqar Ahmed's avatar
Waqar Ahmed committed
322
323
324
325
326
    /**
     * our projects info toolview
     */
    std::unique_ptr<QWidget> m_gitToolView;

327
328
329
330
331
    /**
     * our cross-projects toolview
     */
    QWidget *m_toolMultiView;

332
    /**
333
     * combo box with all loaded projects inside
334
     */
335
    QComboBox *m_projectsCombo;
Christoph Cullmann's avatar
Christoph Cullmann committed
336

Waqar Ahmed's avatar
Waqar Ahmed committed
337
338
339
340
341
    /**
     * combo box with all loaded projects inside
     */
    QComboBox *m_projectsComboGit;

Christoph Cullmann's avatar
Christoph Cullmann committed
342
343
344
345
    /**
     * Reload button
     */
    QToolButton *m_reloadButton;
Christoph Cullmann's avatar
Christoph Cullmann committed
346

347
348
349
350
    /**
     * Closeing button
     */
    QToolButton *m_closeProjectButton;
351

352
353
354
355
356
    /**
     * Git status refresh button
     */
    QToolButton *m_gitStatusRefreshButton;

357
358
359
360
    /**
     * stacked widget will all currently created project views
     */
    QStackedWidget *m_stackedProjectViews;
361

362
363
364
365
366
    /**
     * stacked widget will all currently created project info views
     */
    QStackedWidget *m_stackedProjectInfoViews;

Waqar Ahmed's avatar
Waqar Ahmed committed
367
368
369
370
371
    /**
     * stacked widget will all currently created git views
     */
    QStackedWidget *m_stackedgitViews;

Christoph Cullmann's avatar
Christoph Cullmann committed
372
373
374
    /**
     * project => view
     */
375
    QMap<KateProject *, QPair<KateProjectView *, KateProjectInfoView *>> m_project2View;
376

377
378
379
380
381
    /**
     * remember current active view text editor view
     * might be 0
     */
    QPointer<KTextEditor::View> m_activeTextEditorView;
Christoph Cullmann's avatar
Christoph Cullmann committed
382

383
384
385
386
    /**
     * remember for which text views we might need to cleanup stuff
     */
    QSet<QObject *> m_textViews;
387
388
389
390
391

    /**
     * lookup action
     */
    QAction *m_lookupAction;
392
393
394
395
396
397

    /**
     * goto symbol action
     */
    QAction *m_gotoSymbolAction;
    QAction *m_gotoSymbolActionAppMenu;
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

    class FixedView
    {
    public:
        QPointer<KTextEditor::View> view;
        QPointer<QMenu> defaultMenu;

        void restoreMenu()
        {
            if (view && defaultMenu) {
                view->setContextMenu(defaultMenu);
            }
        }
    };

    /**
     * Fixed view for viewing diffs
     */
    FixedView m_fixedView;
Christoph Cullmann's avatar
Christoph Cullmann committed
417
418
419
};

#endif