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