1 | /* |
2 | SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org> |
3 | SPDX-FileCopyrightText: 2009 Erlend Hamberg <ehamberg@gmail.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef KATE_GLOBAL_H |
9 | #define KATE_GLOBAL_H |
10 | |
11 | #include <ktexteditor_export.h> |
12 | |
13 | #include <ktexteditor/editor.h> |
14 | #include <ktexteditor/view.h> |
15 | |
16 | #include <KAboutData> |
17 | #include <KSharedConfig> |
18 | |
19 | #include <KTextEditor/Application> |
20 | #include <KTextEditor/MainWindow> |
21 | |
22 | #include <QList> |
23 | #include <QPointer> |
24 | |
25 | #include <array> |
26 | #include <memory> |
27 | |
28 | class QStringListModel; |
29 | class QTextToSpeech; |
30 | |
31 | class KateCmd; |
32 | class KateModeManager; |
33 | class KateGlobalConfig; |
34 | class KateDocumentConfig; |
35 | class KateViewConfig; |
36 | class KateRendererConfig; |
37 | namespace KTextEditor |
38 | { |
39 | class DocumentPrivate; |
40 | } |
41 | namespace KTextEditor |
42 | { |
43 | class ViewPrivate; |
44 | class Command; |
45 | } |
46 | class KateScriptManager; |
47 | class KDirWatch; |
48 | class KateHlManager; |
49 | class KateSpellCheckManager; |
50 | class KateWordCompletionModel; |
51 | class KateAbstractInputModeFactory; |
52 | class KateKeywordCompletionModel; |
53 | class KateVariableExpansionManager; |
54 | |
55 | namespace KTextEditor |
56 | { |
57 | /** |
58 | * KTextEditor::EditorPrivate |
59 | * One instance of this class is hold alive during |
60 | * a kate part session, as long as any factory, document |
61 | * or view stay around, here is the place to put things |
62 | * which are needed and shared by all this objects ;) |
63 | */ |
64 | class KTEXTEDITOR_EXPORT EditorPrivate final : public KTextEditor::Editor |
65 | { |
66 | Q_OBJECT |
67 | |
68 | friend class KTextEditor::Editor; |
69 | |
70 | public: |
71 | // for setDefaultEncoding |
72 | friend class KateDocumentConfig; |
73 | |
74 | private: |
75 | /** |
76 | * Default constructor, private, as singleton |
77 | * @param staticInstance pointer to fill with content of this |
78 | */ |
79 | KTEXTEDITOR_NO_EXPORT |
80 | explicit EditorPrivate(QPointer<KTextEditor::EditorPrivate> &staticInstance); |
81 | |
82 | public: |
83 | /** |
84 | * Destructor |
85 | */ |
86 | ~EditorPrivate() override; |
87 | |
88 | /** |
89 | * Create a new document object |
90 | * @param parent parent object |
91 | * @return created KTextEditor::Document |
92 | */ |
93 | KTextEditor::Document *createDocument(QObject *parent) override; |
94 | |
95 | /** |
96 | * Returns a list of all documents of this editor. |
97 | * @return list of all existing documents |
98 | */ |
99 | QList<KTextEditor::Document *> documents() override |
100 | { |
101 | return m_documents; |
102 | } |
103 | |
104 | /** |
105 | * Set the global application object. |
106 | * This will allow the editor component to access |
107 | * the hosting application. |
108 | * @param application application object |
109 | */ |
110 | void setApplication(KTextEditor::Application *application) override |
111 | { |
112 | // switch back to dummy application? |
113 | m_application = application ? application : &m_dummyApplication; |
114 | } |
115 | |
116 | /** |
117 | * Current hosting application, if any set. |
118 | * @return current application object or nullptr |
119 | */ |
120 | KTextEditor::Application *application() const override |
121 | { |
122 | return m_application; |
123 | } |
124 | |
125 | /** |
126 | * General Information about this editor |
127 | */ |
128 | public: |
129 | /** |
130 | * return the about data |
131 | * @return about data of this editor part |
132 | */ |
133 | const KAboutData &aboutData() const override |
134 | { |
135 | return m_aboutData; |
136 | } |
137 | |
138 | /** |
139 | * Configuration management |
140 | */ |
141 | public: |
142 | /** |
143 | * Shows a config dialog for the part, changes will be applied |
144 | * to the editor, but not saved anywhere automagically, call |
145 | * writeConfig to save them |
146 | */ |
147 | void configDialog(QWidget *parent) override; |
148 | |
149 | /** |
150 | * Number of available config pages |
151 | * If the editor returns a number < 1, it doesn't support this |
152 | * and the embedding app should use the configDialog () instead |
153 | * @return number of config pages |
154 | */ |
155 | int configPages() const override; |
156 | |
157 | /** |
158 | * returns config page with the given number, |
159 | * config pages from 0 to configPages()-1 are available |
160 | * if configPages() > 0 |
161 | */ |
162 | KTextEditor::ConfigPage *configPage(int number, QWidget *parent) override; |
163 | |
164 | /** |
165 | * Kate Part Internal stuff ;) |
166 | */ |
167 | public: |
168 | /** |
169 | * singleton accessor |
170 | * @return instance of the factory |
171 | */ |
172 | static KTextEditor::EditorPrivate *self(); |
173 | |
174 | /** |
175 | * register document at the factory |
176 | * this allows us to loop over all docs for example on config changes |
177 | * @param doc document to register |
178 | */ |
179 | void registerDocument(KTextEditor::DocumentPrivate *doc); |
180 | |
181 | /** |
182 | * unregister document at the factory |
183 | * @param doc document to register |
184 | */ |
185 | void deregisterDocument(KTextEditor::DocumentPrivate *doc); |
186 | |
187 | /** |
188 | * register view at the factory |
189 | * this allows us to loop over all views for example on config changes |
190 | * @param view view to register |
191 | */ |
192 | void registerView(KTextEditor::ViewPrivate *view); |
193 | |
194 | /** |
195 | * unregister view at the factory |
196 | * @param view view to unregister |
197 | */ |
198 | void deregisterView(KTextEditor::ViewPrivate *view); |
199 | |
200 | /** |
201 | * return a list of all registered views |
202 | * @return all known views |
203 | */ |
204 | const std::vector<KTextEditor::ViewPrivate *> &views() |
205 | { |
206 | return m_views; |
207 | } |
208 | |
209 | /** |
210 | * global dirwatch |
211 | * @return dirwatch instance |
212 | */ |
213 | KDirWatch *dirWatch() |
214 | { |
215 | return m_dirWatch; |
216 | } |
217 | |
218 | /** |
219 | * The global configuration of katepart, e.g. katepartrc |
220 | * @return global shared access to katepartrc config |
221 | */ |
222 | static KSharedConfigPtr config(); |
223 | |
224 | /** |
225 | * global mode manager |
226 | * used to manage the modes centrally |
227 | * @return mode manager |
228 | */ |
229 | KateModeManager *modeManager() |
230 | { |
231 | return m_modeManager; |
232 | } |
233 | |
234 | /** |
235 | * fallback document config |
236 | * @return default config for all documents |
237 | */ |
238 | KateDocumentConfig *documentConfig() |
239 | { |
240 | return m_documentConfig; |
241 | } |
242 | |
243 | /** |
244 | * fallback view config |
245 | * @return default config for all views |
246 | */ |
247 | KateViewConfig *viewConfig() |
248 | { |
249 | return m_viewConfig; |
250 | } |
251 | |
252 | /** |
253 | * fallback renderer config |
254 | * @return default config for all renderers |
255 | */ |
256 | KateRendererConfig *rendererConfig() |
257 | { |
258 | return m_rendererConfig; |
259 | } |
260 | |
261 | /** |
262 | * Global script collection |
263 | */ |
264 | KateScriptManager *scriptManager() |
265 | { |
266 | return m_scriptManager; |
267 | } |
268 | |
269 | /** |
270 | * hl manager |
271 | * @return hl manager |
272 | */ |
273 | KateHlManager *hlManager() |
274 | { |
275 | return m_hlManager; |
276 | } |
277 | |
278 | /** |
279 | * command manager |
280 | * @return command manager |
281 | */ |
282 | KateCmd *cmdManager() |
283 | { |
284 | return m_cmdManager; |
285 | } |
286 | |
287 | /** |
288 | * spell check manager |
289 | * @return spell check manager |
290 | */ |
291 | KateSpellCheckManager *spellCheckManager() |
292 | { |
293 | return m_spellCheckManager; |
294 | } |
295 | |
296 | /** |
297 | * global instance of the simple word completion mode |
298 | * @return global instance of the simple word completion mode |
299 | */ |
300 | KateWordCompletionModel *wordCompletionModel() |
301 | { |
302 | return m_wordCompletionModel; |
303 | } |
304 | |
305 | /** |
306 | * Global instance of the language-aware keyword completion model |
307 | * @return global instance of the keyword completion model |
308 | */ |
309 | KateKeywordCompletionModel *keywordCompletionModel() |
310 | { |
311 | return m_keywordCompletionModel; |
312 | } |
313 | |
314 | /** |
315 | * query for command |
316 | * @param cmd name of command to query for |
317 | * @return found command or 0 |
318 | */ |
319 | KTextEditor::Command *queryCommand(const QString &cmd) const override; |
320 | |
321 | /** |
322 | * Get a list of all registered commands. |
323 | * \return list of all commands |
324 | */ |
325 | QList<KTextEditor::Command *> commands() const override; |
326 | |
327 | /** |
328 | * Get a list of available commandline strings. |
329 | * \return commandline strings |
330 | */ |
331 | QStringList commandList() const override; |
332 | |
333 | /** |
334 | * Copy text to clipboard an remember it in the history |
335 | * @param text text to copy to clipboard, does nothing if empty! |
336 | * @param fileName fileName of the text to copy, used for highlighting |
337 | */ |
338 | void copyToClipboard(const QString &text, const QString &fileName); |
339 | |
340 | /** |
341 | * A clipboard entry stores the copied text and the filename of |
342 | * the copied text. |
343 | */ |
344 | struct ClipboardEntry { |
345 | /** |
346 | * The copied text |
347 | */ |
348 | QString text; |
349 | /** |
350 | * The file name of the file containing the copied text, |
351 | * used for syntax highlighting |
352 | */ |
353 | QString fileName; |
354 | }; |
355 | |
356 | friend inline bool operator==(const ClipboardEntry &lhs, const ClipboardEntry &rhs) |
357 | { |
358 | return lhs.text == rhs.text && lhs.fileName == rhs.fileName; |
359 | } |
360 | |
361 | /** |
362 | * Clipboard history, filled with text we ever copied |
363 | * to clipboard via copyToClipboard. |
364 | */ |
365 | const QList<ClipboardEntry> &clipboardHistory() const |
366 | { |
367 | return m_clipboardHistory; |
368 | } |
369 | |
370 | /** |
371 | * Dummy main window to be null safe. |
372 | * @return dummy main window |
373 | */ |
374 | KTextEditor::MainWindow *dummyMainWindow() |
375 | { |
376 | return &m_dummyMainWindow; |
377 | } |
378 | |
379 | /** |
380 | * @return list of available input mode factories |
381 | */ |
382 | const std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> &inputModeFactories() |
383 | { |
384 | return m_inputModeFactories; |
385 | } |
386 | |
387 | /** |
388 | * Search pattern history shared among simple/power search instances. |
389 | */ |
390 | QStringListModel *searchHistoryModel(); |
391 | |
392 | /** |
393 | * Replace pattern history shared among simple/power search instances. |
394 | */ |
395 | QStringListModel *replaceHistoryModel(); |
396 | |
397 | /** |
398 | * Call this function to store the history models to the application config. |
399 | */ |
400 | void saveSearchReplaceHistoryModels(); |
401 | |
402 | /** |
403 | * Returns the variable expansion manager. |
404 | */ |
405 | KateVariableExpansionManager *variableExpansionManager(); |
406 | |
407 | /** |
408 | * Trigger delayed emission of config changed. |
409 | */ |
410 | void triggerConfigChanged(); |
411 | |
412 | /** |
413 | * text to speech engine to be use by the view actions, constructed on demand. |
414 | * @param view the view that want to use it |
415 | */ |
416 | QTextToSpeech *speechEngine(KTextEditor::ViewPrivate *view); |
417 | |
418 | private Q_SLOTS: |
419 | /** |
420 | * Emit configChanged if needed. |
421 | * Used to bundle emissions. |
422 | */ |
423 | void emitConfigChanged(); |
424 | |
425 | /** |
426 | * Was the view that started the current speak output destroyed? |
427 | */ |
428 | void speechEngineUserDestoyed(); |
429 | |
430 | /** |
431 | * Speech error occured. |
432 | * @param view view to signal error to |
433 | * @param errorString error to show |
434 | */ |
435 | void speechError(KTextEditor::ViewPrivate *view, const QString &errorString); |
436 | |
437 | Q_SIGNALS: |
438 | /** |
439 | * Emitted if the history of clipboard changes via copyToClipboard |
440 | */ |
441 | void clipboardHistoryChanged(); |
442 | |
443 | protected: |
444 | bool eventFilter(QObject *, QEvent *) override; |
445 | |
446 | private Q_SLOTS: |
447 | void updateColorPalette(); |
448 | |
449 | private: |
450 | /** |
451 | * about data (authors and more) |
452 | */ |
453 | KAboutData m_aboutData; |
454 | |
455 | /** |
456 | * registered docs, map from general to specialized pointer |
457 | */ |
458 | QList<KTextEditor::Document *> m_documents; |
459 | |
460 | /** |
461 | * registered views |
462 | */ |
463 | std::vector<KTextEditor::ViewPrivate *> m_views; |
464 | |
465 | /** |
466 | * global dirwatch object |
467 | */ |
468 | KDirWatch *m_dirWatch; |
469 | |
470 | /** |
471 | * mode manager |
472 | */ |
473 | KateModeManager *m_modeManager; |
474 | |
475 | /** |
476 | * global config |
477 | */ |
478 | KateGlobalConfig *m_globalConfig; |
479 | |
480 | /** |
481 | * fallback document config |
482 | */ |
483 | KateDocumentConfig *m_documentConfig; |
484 | |
485 | /** |
486 | * fallback view config |
487 | */ |
488 | KateViewConfig *m_viewConfig; |
489 | |
490 | /** |
491 | * fallback renderer config |
492 | */ |
493 | KateRendererConfig *m_rendererConfig; |
494 | |
495 | /** |
496 | * internal commands |
497 | */ |
498 | std::array<KTextEditor::Command *, 6> m_cmds; |
499 | |
500 | /** |
501 | * script manager |
502 | */ |
503 | KateScriptManager *m_scriptManager; |
504 | |
505 | /** |
506 | * hl manager |
507 | */ |
508 | KateHlManager *m_hlManager; |
509 | |
510 | /** |
511 | * command manager |
512 | */ |
513 | KateCmd *m_cmdManager; |
514 | |
515 | /** |
516 | * variable expansion manager |
517 | */ |
518 | KateVariableExpansionManager *m_variableExpansionManager; |
519 | |
520 | /** |
521 | * spell check manager |
522 | */ |
523 | KateSpellCheckManager *m_spellCheckManager; |
524 | |
525 | /** |
526 | * global instance of the simple word completion mode |
527 | */ |
528 | KateWordCompletionModel *m_wordCompletionModel; |
529 | |
530 | /** |
531 | * global instance of the language-specific keyword completion model |
532 | */ |
533 | KateKeywordCompletionModel *m_keywordCompletionModel; |
534 | |
535 | /** |
536 | * clipboard history |
537 | */ |
538 | QList<ClipboardEntry> m_clipboardHistory; |
539 | |
540 | /** |
541 | * Dummy application object to be null safe |
542 | */ |
543 | KTextEditor::Application m_dummyApplication; |
544 | |
545 | /** |
546 | * access to application |
547 | */ |
548 | QPointer<KTextEditor::Application> m_application; |
549 | |
550 | /** |
551 | * Dummy main window to be null safe |
552 | */ |
553 | KTextEditor::MainWindow m_dummyMainWindow; |
554 | |
555 | /** |
556 | * input modes factories |
557 | * for all input modes in the KTextEditor::View::InputMode we have here an entry |
558 | */ |
559 | std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> m_inputModeFactories; |
560 | |
561 | /** |
562 | * Shared history models for search & replace. |
563 | */ |
564 | QStringListModel *m_searchHistoryModel; |
565 | QStringListModel *m_replaceHistoryModel; |
566 | |
567 | /** |
568 | * We collapse configChanged signals to avoid that e.g. |
569 | * Document/View/Renderer/... updates cause X emitted signals in a row. |
570 | * This bool tells if we still shall emit a signal in the delayed connected |
571 | * slot. |
572 | */ |
573 | bool m_configWasChanged = false; |
574 | |
575 | /** |
576 | * text to speech engine to be use by the view actions, constructed on demand |
577 | */ |
578 | QTextToSpeech *m_speechEngine = nullptr; |
579 | |
580 | /** |
581 | * view that triggered last speech action |
582 | * used to show error messages and to stop output on view destruction |
583 | */ |
584 | QPointer<KTextEditor::ViewPrivate> m_speechEngineLastUser; |
585 | }; |
586 | |
587 | } |
588 | |
589 | #endif |
590 | |