File indexing completed on 2024-04-21 07:44:53

0001 /*
0002     SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
0003     SPDX-FileCopyrightText: 2009 Erlend Hamberg <ehamberg@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_GLOBAL_H
0009 #define KATE_GLOBAL_H
0010 
0011 #include <ktexteditor_export.h>
0012 
0013 #include <ktexteditor/editor.h>
0014 #include <ktexteditor/view.h>
0015 
0016 #include <KAboutData>
0017 #include <KSharedConfig>
0018 
0019 #include <KTextEditor/Application>
0020 #include <KTextEditor/MainWindow>
0021 
0022 #include <QList>
0023 #include <QPointer>
0024 
0025 #include <array>
0026 #include <memory>
0027 
0028 class QStringListModel;
0029 class QTextToSpeech;
0030 
0031 class KateCmd;
0032 class KateModeManager;
0033 class KateGlobalConfig;
0034 class KateDocumentConfig;
0035 class KateViewConfig;
0036 class KateRendererConfig;
0037 namespace KTextEditor
0038 {
0039 class DocumentPrivate;
0040 }
0041 namespace KTextEditor
0042 {
0043 class ViewPrivate;
0044 class Command;
0045 }
0046 class KateScriptManager;
0047 class KDirWatch;
0048 class KateHlManager;
0049 class KateSpellCheckManager;
0050 class KateWordCompletionModel;
0051 class KateAbstractInputModeFactory;
0052 class KateKeywordCompletionModel;
0053 class KateVariableExpansionManager;
0054 
0055 namespace KTextEditor
0056 {
0057 /**
0058  * KTextEditor::EditorPrivate
0059  * One instance of this class is hold alive during
0060  * a kate part session, as long as any factory, document
0061  * or view stay around, here is the place to put things
0062  * which are needed and shared by all this objects ;)
0063  */
0064 class KTEXTEDITOR_EXPORT EditorPrivate final : public KTextEditor::Editor
0065 {
0066     Q_OBJECT
0067 
0068     friend class KTextEditor::Editor;
0069 
0070     // unit testing support
0071 public:
0072     /**
0073      * Calling this function internally sets a flag such that unitTestMode()
0074      * returns \p true.
0075      */
0076     static void enableUnitTestMode();
0077 
0078     /**
0079      * Returns \p true, if the unit test mode was enabled through a call of
0080      * enableUnitTestMode(), otherwise \p false.
0081      */
0082     static bool unitTestMode();
0083 
0084     // for setDefaultEncoding
0085     friend class KateDocumentConfig;
0086 
0087 private:
0088     /**
0089      * Default constructor, private, as singleton
0090      * @param staticInstance pointer to fill with content of this
0091      */
0092     KTEXTEDITOR_NO_EXPORT
0093     explicit EditorPrivate(QPointer<KTextEditor::EditorPrivate> &staticInstance);
0094 
0095 public:
0096     /**
0097      * Destructor
0098      */
0099     ~EditorPrivate() override;
0100 
0101     /**
0102      * Create a new document object
0103      * @param parent parent object
0104      * @return created KTextEditor::Document
0105      */
0106     KTextEditor::Document *createDocument(QObject *parent) override;
0107 
0108     /**
0109      * Returns a list of all documents of this editor.
0110      * @return list of all existing documents
0111      */
0112     QList<KTextEditor::Document *> documents() override
0113     {
0114         return m_documents;
0115     }
0116 
0117     /**
0118      * Set the global application object.
0119      * This will allow the editor component to access
0120      * the hosting application.
0121      * @param application application object
0122      */
0123     void setApplication(KTextEditor::Application *application) override
0124     {
0125         // switch back to dummy application?
0126         m_application = application ? application : &m_dummyApplication;
0127     }
0128 
0129     /**
0130      * Current hosting application, if any set.
0131      * @return current application object or nullptr
0132      */
0133     KTextEditor::Application *application() const override
0134     {
0135         return m_application;
0136     }
0137 
0138     /**
0139      * General Information about this editor
0140      */
0141 public:
0142     /**
0143      * return the about data
0144      * @return about data of this editor part
0145      */
0146     const KAboutData &aboutData() const override
0147     {
0148         return m_aboutData;
0149     }
0150 
0151     /**
0152      * Configuration management
0153      */
0154 public:
0155     /**
0156      * Shows a config dialog for the part, changes will be applied
0157      * to the editor, but not saved anywhere automagically, call
0158      * writeConfig to save them
0159      */
0160     void configDialog(QWidget *parent) override;
0161 
0162     /**
0163      * Number of available config pages
0164      * If the editor returns a number < 1, it doesn't support this
0165      * and the embedding app should use the configDialog () instead
0166      * @return number of config pages
0167      */
0168     int configPages() const override;
0169 
0170     /**
0171      * returns config page with the given number,
0172      * config pages from 0 to configPages()-1 are available
0173      * if configPages() > 0
0174      */
0175     KTextEditor::ConfigPage *configPage(int number, QWidget *parent) override;
0176 
0177     /**
0178      * Kate Part Internal stuff ;)
0179      */
0180 public:
0181     /**
0182      * singleton accessor
0183      * @return instance of the factory
0184      */
0185     static KTextEditor::EditorPrivate *self();
0186 
0187     /**
0188      * register document at the factory
0189      * this allows us to loop over all docs for example on config changes
0190      * @param doc document to register
0191      */
0192     void registerDocument(KTextEditor::DocumentPrivate *doc);
0193 
0194     /**
0195      * unregister document at the factory
0196      * @param doc document to register
0197      */
0198     void deregisterDocument(KTextEditor::DocumentPrivate *doc);
0199 
0200     /**
0201      * register view at the factory
0202      * this allows us to loop over all views for example on config changes
0203      * @param view view to register
0204      */
0205     void registerView(KTextEditor::ViewPrivate *view);
0206 
0207     /**
0208      * unregister view at the factory
0209      * @param view view to unregister
0210      */
0211     void deregisterView(KTextEditor::ViewPrivate *view);
0212 
0213     /**
0214      * return a list of all registered views
0215      * @return all known views
0216      */
0217     const std::vector<KTextEditor::ViewPrivate *> &views()
0218     {
0219         return m_views;
0220     }
0221 
0222     /**
0223      * global dirwatch
0224      * @return dirwatch instance
0225      */
0226     KDirWatch *dirWatch()
0227     {
0228         return m_dirWatch;
0229     }
0230 
0231     /**
0232      * The global configuration of katepart, e.g. katepartrc
0233      * @return global shared access to katepartrc config
0234      */
0235     static KSharedConfigPtr config();
0236 
0237     /**
0238      * global mode manager
0239      * used to manage the modes centrally
0240      * @return mode manager
0241      */
0242     KateModeManager *modeManager()
0243     {
0244         return m_modeManager;
0245     }
0246 
0247     /**
0248      * fallback document config
0249      * @return default config for all documents
0250      */
0251     KateDocumentConfig *documentConfig()
0252     {
0253         return m_documentConfig;
0254     }
0255 
0256     /**
0257      * fallback view config
0258      * @return default config for all views
0259      */
0260     KateViewConfig *viewConfig()
0261     {
0262         return m_viewConfig;
0263     }
0264 
0265     /**
0266      * fallback renderer config
0267      * @return default config for all renderers
0268      */
0269     KateRendererConfig *rendererConfig()
0270     {
0271         return m_rendererConfig;
0272     }
0273 
0274     /**
0275      * Global script collection
0276      */
0277     KateScriptManager *scriptManager()
0278     {
0279         return m_scriptManager;
0280     }
0281 
0282     /**
0283      * hl manager
0284      * @return hl manager
0285      */
0286     KateHlManager *hlManager()
0287     {
0288         return m_hlManager;
0289     }
0290 
0291     /**
0292      * command manager
0293      * @return command manager
0294      */
0295     KateCmd *cmdManager()
0296     {
0297         return m_cmdManager;
0298     }
0299 
0300     /**
0301      * spell check manager
0302      * @return spell check manager
0303      */
0304     KateSpellCheckManager *spellCheckManager()
0305     {
0306         return m_spellCheckManager;
0307     }
0308 
0309     /**
0310      * global instance of the simple word completion mode
0311      * @return global instance of the simple word completion mode
0312      */
0313     KateWordCompletionModel *wordCompletionModel()
0314     {
0315         return m_wordCompletionModel;
0316     }
0317 
0318     /**
0319      * Global instance of the language-aware keyword completion model
0320      * @return global instance of the keyword completion model
0321      */
0322     KateKeywordCompletionModel *keywordCompletionModel()
0323     {
0324         return m_keywordCompletionModel;
0325     }
0326 
0327     /**
0328      * query for command
0329      * @param cmd name of command to query for
0330      * @return found command or 0
0331      */
0332     KTextEditor::Command *queryCommand(const QString &cmd) const override;
0333 
0334     /**
0335      * Get a list of all registered commands.
0336      * \return list of all commands
0337      */
0338     QList<KTextEditor::Command *> commands() const override;
0339 
0340     /**
0341      * Get a list of available commandline strings.
0342      * \return commandline strings
0343      */
0344     QStringList commandList() const override;
0345 
0346     /**
0347      * Copy text to clipboard an remember it in the history
0348      * @param text text to copy to clipboard, does nothing if empty!
0349      * @param fileName fileName of the text to copy, used for highlighting
0350      */
0351     void copyToClipboard(const QString &text, const QString &fileName);
0352 
0353     /**
0354      * A clipboard entry stores the copied text and the filename of
0355      * the copied text.
0356      */
0357     struct ClipboardEntry {
0358         /**
0359          * The copied text
0360          */
0361         QString text;
0362         /**
0363          * The file name of the file containing the copied text,
0364          * used for syntax highlighting
0365          */
0366         QString fileName;
0367     };
0368 
0369     friend inline bool operator==(const ClipboardEntry &lhs, const ClipboardEntry &rhs)
0370     {
0371         return lhs.text == rhs.text && lhs.fileName == rhs.fileName;
0372     }
0373 
0374     /**
0375      * Clipboard history, filled with text we ever copied
0376      * to clipboard via copyToClipboard.
0377      */
0378     const QList<ClipboardEntry> &clipboardHistory() const
0379     {
0380         return m_clipboardHistory;
0381     }
0382 
0383     /**
0384      * Dummy main window to be null safe.
0385      * @return dummy main window
0386      */
0387     KTextEditor::MainWindow *dummyMainWindow()
0388     {
0389         return &m_dummyMainWindow;
0390     }
0391 
0392     /**
0393      * @return list of available input mode factories
0394      */
0395     const std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> &inputModeFactories()
0396     {
0397         return m_inputModeFactories;
0398     }
0399 
0400     /**
0401      * Search pattern history shared among simple/power search instances.
0402      */
0403     QStringListModel *searchHistoryModel();
0404 
0405     /**
0406      * Replace pattern history shared among simple/power search instances.
0407      */
0408     QStringListModel *replaceHistoryModel();
0409 
0410     /**
0411      * Call this function to store the history models to the application config.
0412      */
0413     void saveSearchReplaceHistoryModels();
0414 
0415     /**
0416      * Returns the variable expansion manager.
0417      */
0418     KateVariableExpansionManager *variableExpansionManager();
0419 
0420     /**
0421      * Trigger delayed emission of config changed.
0422      */
0423     void triggerConfigChanged();
0424 
0425     void copyToMulticursorClipboard(const QStringList &texts);
0426 
0427     QStringList multicursorClipboard() const;
0428 
0429     /**
0430      * text to speech engine to be use by the view actions, constructed on demand.
0431      * @param view the view that want to use it
0432      */
0433     QTextToSpeech *speechEngine(KTextEditor::ViewPrivate *view);
0434 
0435 private Q_SLOTS:
0436     /**
0437      * Emit configChanged if needed.
0438      * Used to bundle emissions.
0439      */
0440     void emitConfigChanged();
0441 
0442     /**
0443      * Was the view that started the current speak output destroyed?
0444      */
0445     void speechEngineUserDestoyed();
0446 
0447     /**
0448      * Speech error occured.
0449      * @param view view to signal error to
0450      * @param errorString error to show
0451      */
0452     void speechError(KTextEditor::ViewPrivate *view, const QString &errorString);
0453 
0454 Q_SIGNALS:
0455     /**
0456      * Emitted if the history of clipboard changes via copyToClipboard
0457      */
0458     void clipboardHistoryChanged();
0459 
0460 protected:
0461     bool eventFilter(QObject *, QEvent *) override;
0462 
0463 private Q_SLOTS:
0464     void updateColorPalette();
0465 
0466 private:
0467     /**
0468      * about data (authors and more)
0469      */
0470     KAboutData m_aboutData;
0471 
0472     /**
0473      * registered docs, map from general to specialized pointer
0474      */
0475     QList<KTextEditor::Document *> m_documents;
0476 
0477     /**
0478      * registered views
0479      */
0480     std::vector<KTextEditor::ViewPrivate *> m_views;
0481 
0482     /**
0483      * global dirwatch object
0484      */
0485     KDirWatch *m_dirWatch;
0486 
0487     /**
0488      * mode manager
0489      */
0490     KateModeManager *m_modeManager;
0491 
0492     /**
0493      * global config
0494      */
0495     KateGlobalConfig *m_globalConfig;
0496 
0497     /**
0498      * fallback document config
0499      */
0500     KateDocumentConfig *m_documentConfig;
0501 
0502     /**
0503      * fallback view config
0504      */
0505     KateViewConfig *m_viewConfig;
0506 
0507     /**
0508      * fallback renderer config
0509      */
0510     KateRendererConfig *m_rendererConfig;
0511 
0512     /**
0513      * internal commands
0514      */
0515     std::array<KTextEditor::Command *, 5> m_cmds;
0516 
0517     /**
0518      * script manager
0519      */
0520     KateScriptManager *m_scriptManager;
0521 
0522     /**
0523      * hl manager
0524      */
0525     KateHlManager *m_hlManager;
0526 
0527     /**
0528      * command manager
0529      */
0530     KateCmd *m_cmdManager;
0531 
0532     /**
0533      * variable expansion manager
0534      */
0535     KateVariableExpansionManager *m_variableExpansionManager;
0536 
0537     /**
0538      * spell check manager
0539      */
0540     KateSpellCheckManager *m_spellCheckManager;
0541 
0542     /**
0543      * global instance of the simple word completion mode
0544      */
0545     KateWordCompletionModel *m_wordCompletionModel;
0546 
0547     /**
0548      * global instance of the language-specific keyword completion model
0549      */
0550     KateKeywordCompletionModel *m_keywordCompletionModel;
0551 
0552     /**
0553      * clipboard history
0554      */
0555     QList<ClipboardEntry> m_clipboardHistory;
0556 
0557     /**
0558      * Dummy application object to be null safe
0559      */
0560     KTextEditor::Application m_dummyApplication;
0561 
0562     /**
0563      * access to application
0564      */
0565     QPointer<KTextEditor::Application> m_application;
0566 
0567     /**
0568      * Dummy main window to be null safe
0569      */
0570     KTextEditor::MainWindow m_dummyMainWindow;
0571 
0572     /**
0573      * input modes factories
0574      * for all input modes in the KTextEditor::View::InputMode we have here an entry
0575      */
0576     std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> m_inputModeFactories;
0577 
0578     /**
0579      * simple list that stores text copied
0580      * from all cursors selection
0581      * It's main purpose is providing multi-paste
0582      * support.
0583      */
0584     QStringList m_multicursorClipboard;
0585 
0586     /**
0587      * Shared history models for search & replace.
0588      */
0589     QStringListModel *m_searchHistoryModel;
0590     QStringListModel *m_replaceHistoryModel;
0591 
0592     /**
0593      * We collapse configChanged signals to avoid that e.g.
0594      * Document/View/Renderer/... updates cause X emitted signals in a row.
0595      * This bool tells if we still shall emit a signal in the delayed connected
0596      * slot.
0597      */
0598     bool m_configWasChanged = false;
0599 
0600     /**
0601      * text to speech engine to be use by the view actions, constructed on demand
0602      */
0603     QTextToSpeech *m_speechEngine = nullptr;
0604 
0605     /**
0606      * view that triggered last speech action
0607      * used to show error messages and to stop output on view destruction
0608      */
0609     QPointer<KTextEditor::ViewPrivate> m_speechEngineLastUser;
0610 };
0611 
0612 }
0613 
0614 #endif