File indexing completed on 2024-04-28 04:36:30

0001 /*
0002     SPDX-FileCopyrightText: 2007 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_IDOCUMENTCONTROLLER_H
0008 #define KDEVPLATFORM_IDOCUMENTCONTROLLER_H
0009 
0010 #include <QObject>
0011 #include <QList>
0012 #include <QUrl>
0013 
0014 #include <KTextEditor/Range>
0015 
0016 #include "interfacesexport.h"
0017 
0018 #include "idocument.h"
0019 
0020 namespace KTextEditor {
0021     class View;
0022 }
0023 
0024 namespace KDevelop {
0025 
0026 class ICore;
0027 
0028 class KDEVPLATFORMINTERFACES_EXPORT IDocumentFactory {
0029 public:
0030     virtual ~IDocumentFactory() {}
0031     virtual IDocument* create(const QUrl&, ICore* ) = 0;
0032 };
0033 
0034 /**
0035  *
0036  * Allows to access the open documents and also open new ones
0037  *
0038  * @class IDocumentController
0039  */
0040 class KDEVPLATFORMINTERFACES_EXPORT IDocumentController: public QObject {
0041     Q_OBJECT
0042 public:
0043     enum DocumentActivation
0044     {
0045         DefaultMode = 0,            /**Activate document and create a view if no other flags passed.*/
0046         DoNotActivate = 1,          /**Don't activate the Document.*/
0047         DoNotCreateView = 2,        /**Don't create and show the view for the Document.*/
0048         DoNotFocus = 4,             /**Don't change the keyboard focus.*/
0049         DoNotAddToRecentOpen = 8,   /**Don't add the document to the File/Open Recent menu.*/
0050     };
0051     Q_DECLARE_FLAGS(DocumentActivationParams, DocumentActivation)
0052 
0053     explicit IDocumentController(QObject *parent);
0054 
0055     /**
0056      * Finds the first document object corresponding to a given url.
0057      *
0058      * @param url The Url of the document.
0059      * @return The corresponding document, or null if not found.
0060      */
0061     virtual KDevelop::IDocument* documentForUrl( const QUrl & url ) const = 0;
0062 
0063     /// @return The list of all open documents
0064     virtual QList<KDevelop::IDocument*> openDocuments() const = 0;
0065 
0066     /**
0067      * Returns the currently active or focused document.
0068      *
0069      * @return The active document.
0070      */
0071     virtual KDevelop::IDocument* activeDocument() const = 0;
0072 
0073     virtual void activateDocument( KDevelop::IDocument * document, const KTextEditor::Range& range = KTextEditor::Range::invalid() ) = 0;
0074 
0075     virtual void registerDocumentForMimetype( const QString&, KDevelop::IDocumentFactory* ) = 0;
0076 
0077     virtual bool saveAllDocuments(KDevelop::IDocument::DocumentSaveMode mode = KDevelop::IDocument::Default) = 0;
0078     virtual bool saveSomeDocuments(const QList<IDocument*>& list, KDevelop::IDocument::DocumentSaveMode mode = KDevelop::IDocument::Default) = 0;
0079     virtual bool saveAllDocumentsForWindow(KParts::MainWindow* mw, IDocument::DocumentSaveMode mode, bool currentAreaOnly = false) = 0;
0080 
0081     /// Opens a text document containing the @p data text.
0082     virtual KDevelop::IDocument* openDocumentFromText( const QString& data ) = 0;
0083 
0084     virtual IDocumentFactory* factory(const QString& mime) const = 0;
0085 
0086     /**
0087      * @returns the KTextEditor::View of the current document, in case it is a text document
0088      */
0089     virtual KTextEditor::View* activeTextDocumentView() const = 0;
0090 
0091 public Q_SLOTS:
0092     /**
0093      * Opens a new or existing document.
0094      *
0095      * @param url The full Url of the document to open.
0096      * @param cursor The location information, if applicable.
0097      * @param activationParams Indicates whether to fully activate the document.
0098      * @param encoding the encoding for the document, the name must be accepted by QTextCodec,
0099      *                 if an empty encoding name is given, the document should fallback to its
0100      *                 own default encoding, e.g. the system encoding or the global user settings
0101      */
0102     KDevelop::IDocument* openDocument( const QUrl &url,
0103             const KTextEditor::Cursor& cursor,
0104             DocumentActivationParams activationParams = {},
0105             const QString& encoding = {});
0106 
0107     /**
0108      * Opens a new or existing document.
0109      *
0110      * @param url The full Url of the document to open.
0111      * @param range The range of text to select, if applicable.
0112      * @param activationParams Indicates whether to fully activate the document
0113      * @param encoding the encoding for the document, the name must be accepted by QTextCodec,
0114      *                 if an empty encoding name is given, the document should fallback to its
0115      *                 own default encoding, e.g. the system encoding or the global user settings
0116      * @param buddy Optional buddy document. If 0, the registered IBuddyDocumentFinder
0117      *              for the URL's mimetype will be queried to find a fitting buddy.
0118      *              If a buddy was found (or passed) @p url will be opened next
0119      *              to its buddy.
0120      *
0121      * @return The opened document
0122      */
0123     virtual KDevelop::IDocument* openDocument( const QUrl &url,
0124             const KTextEditor::Range& range = KTextEditor::Range::invalid(),
0125             DocumentActivationParams activationParams = {},
0126             const QString& encoding = {},
0127             IDocument* buddy = nullptr) = 0;
0128 
0129     /**
0130      * Opens a document from the IDocument instance.
0131      *
0132      * @param doc The IDocument to add
0133      * @param range The location information, if applicable.
0134      * @param activationParams Indicates whether to fully activate the document.
0135      * @param buddy Optional buddy document. If 0, the registered IBuddyDocumentFinder
0136      *              for the Documents mimetype will be queried to find a fitting buddy.
0137      *              If a buddy was found (or passed) @p url will be opened next
0138      *              to its buddy.
0139      */
0140     virtual bool openDocument(IDocument* doc,
0141             const KTextEditor::Range& range = KTextEditor::Range::invalid(),
0142             DocumentActivationParams activationParams = {},
0143             IDocument* buddy = nullptr) = 0;
0144 
0145     /**
0146      * Opens a new or existing document.
0147      *
0148      * @param url The full Url of the document to open.
0149      * @param prefName The name of the preferred KPart to open that document
0150      */
0151     virtual KDevelop::IDocument* openDocument( const QUrl &url, const QString& prefName ) = 0;
0152 
0153     virtual bool closeAllDocuments() = 0;
0154 
0155 Q_SIGNALS:
0156     /// Emitted when the document has been activated.
0157     void documentActivated( KDevelop::IDocument* document );
0158 
0159     /**
0160      * Emitted whenever the active cursor jumps from one document+cursor to another,
0161      * as e.g. caused by a call to openDocument(..).
0162      *
0163      * This is also emitted when a document is only activated. Then previousDocument is zero.
0164      */
0165     void documentJumpPerformed( KDevelop::IDocument* newDocument, KTextEditor::Cursor newCursor,
0166                                 KDevelop::IDocument* previousDocument, KTextEditor::Cursor previousCursor);
0167     
0168     /// Emitted when a document has been saved.
0169     void documentSaved( KDevelop::IDocument* document );
0170 
0171     /**
0172      * Emitted when a document has been opened.
0173      *
0174      * NOTE: The document may not be loaded from disk/network at this point.
0175      * NOTE: No views exist for the document at the time this signal is emitted.
0176      */
0177     void documentOpened( KDevelop::IDocument* document );
0178 
0179     /**
0180      * Emitted when a document has been loaded.
0181      *
0182      * NOTE: No views exist for the document at the time this signal is emitted.
0183      */
0184     void documentLoaded( KDevelop::IDocument* document );
0185 
0186     /**
0187      * Emitted when a text document has been loaded, and the text document created.
0188      *
0189      * NOTE: no views exist for the document at the time this signal is emitted.
0190      */
0191     void textDocumentCreated( KDevelop::IDocument* document );
0192 
0193     /// Emitted when a document has been closed.
0194     void documentClosed( KDevelop::IDocument* document );
0195 
0196     /**
0197      * This is emitted when the document state(the relationship
0198      * between the file in the editor and the file stored on disk) changes.
0199      */
0200     void documentStateChanged( KDevelop::IDocument* document );
0201 
0202     /// This is emitted when the document content changed.
0203     void documentContentChanged( KDevelop::IDocument* document );
0204 
0205     /**
0206      * Emitted when a document has been loaded, but before documentLoaded(..) is emitted.
0207      * 
0208      * This allows parts of kdevplatform to prepare data-structures that can be used by other parts
0209      * during documentLoaded(..).
0210      */
0211     void documentLoadedPrepare( KDevelop::IDocument* document );
0212 
0213     /// Emitted when a document url has changed.
0214     void documentUrlChanged(KDevelop::IDocument* document, const QUrl& previousUrl);
0215 
0216     friend class IDocument;
0217 };
0218 
0219 Q_DECLARE_OPERATORS_FOR_FLAGS(IDocumentController::DocumentActivationParams)
0220 } // namespace KDevelop
0221 
0222 #endif