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

0001 /*
0002     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0003     SPDX-FileCopyrightText: 2007 Alexander Dymo <adymo@kdevelop.org>
0004     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KDEVPLATFORM_IDOCUMENT_H
0010 #define KDEVPLATFORM_IDOCUMENT_H
0011 
0012 #include <QObject>
0013 #include <QUrl>
0014 #include <QScopedPointer>
0015 
0016 #include "interfacesexport.h"
0017 
0018 namespace KParts { class Part; class MainWindow; }
0019 namespace KTextEditor {
0020 class Cursor;
0021 class Document;
0022 class Range;
0023 class View;
0024 }
0025 namespace Sublime{ class View; }
0026 
0027 class QMimeType;
0028 class QWidget;
0029 
0030 namespace KDevelop {
0031 class ICore;
0032 class IDocumentPrivate;
0033 
0034 /**
0035  * A single document being edited by the IDE.
0036  *
0037  * The base class for tracking a document.  It contains the URL,
0038  * the part, and any associated metadata for the document.
0039  *
0040  * The advantages are:
0041  * - an easier key for use in maps and the like
0042  * - a value which does not change when the filename changes
0043  * - clearer distinction in the code between an open document and a url
0044  *   (which may or may not be open)
0045  */
0046 class KDEVPLATFORMINTERFACES_EXPORT IDocument {
0047 public:
0048     virtual ~IDocument();
0049 
0050     /**Document state.*/
0051     enum DocumentState
0052     {
0053         Clean,             /**< Document is not touched.*/
0054         Modified,          /**< Document is modified inside the IDE.*/
0055         Dirty,             /**< Document is modified by an external process.*/
0056         DirtyAndModified   /**< Document is modified inside the IDE and at the same time by an external process.*/
0057     };
0058 
0059     enum DocumentSaveMode
0060     {
0061         Default = 0x0 /**< standard save mode, gives a warning message if the file was modified outside the editor */,
0062         Silent = 0x1 /**< silent save mode, doesn't warn the user if the file was modified outside the editor */,
0063         Discard = 0x2 /**< discard mode, don't save any unchanged data */
0064     };
0065 
0066     /**
0067      * Returns the URL of this document.
0068      */
0069     virtual QUrl url() const = 0;
0070 
0071     /**
0072      * Returns the mimetype of the document.
0073      */
0074     virtual QMimeType mimeType() const = 0;
0075 
0076     /**
0077      * Returns the part for given @p view if this document is a KPart document or 0 otherwise.
0078      */
0079     virtual KParts::Part* partForView(QWidget *view) const = 0;
0080 
0081     /**
0082      * Set a 'pretty' name for this document. That name will be used when displaying the document in the UI,
0083      * instead of the filename and/or path.
0084      * @param name The pretty name to use. Give an empty name to reset.
0085      * */
0086     virtual void setPrettyName(const QString& name);
0087     
0088     /**
0089      * returns the pretty name of this document that was set through setPrettyName(...).
0090      * If no pretty name was set, an empty string is returned.
0091      * */
0092     virtual QString prettyName() const;
0093     
0094     /**
0095      * Returns the text editor, if this is a text document or 0 otherwise.
0096      */
0097     virtual KTextEditor::Document* textDocument() const = 0;
0098 
0099     /**
0100      * Saves the document.
0101      * @return true if the document was saved, false otherwise
0102      */
0103     virtual bool save(DocumentSaveMode mode = Default) = 0;
0104 
0105     /**
0106      * Reloads the document.
0107      */
0108     virtual void reload() = 0;
0109 
0110     /**
0111      * Requests that the document be closed.
0112      *
0113      * \returns whether the document was successfully closed.
0114      */
0115     virtual bool close(DocumentSaveMode mode = Default) = 0;
0116 
0117     /**
0118      * Enquires whether this document is currently active in the currently active mainwindow.
0119      */
0120     virtual bool isActive() const = 0;
0121 
0122     /**
0123     * Checks the state of this document.
0124     * @return The document state.
0125     */
0126     virtual DocumentState state() const = 0;
0127 
0128     /**
0129      * Access the current text cursor position, if possible.
0130      *
0131      * \returns the current text cursor position, or an invalid cursor otherwise.
0132      */
0133     virtual KTextEditor::Cursor cursorPosition() const = 0;
0134 
0135     /**
0136      * Set the current text cursor position, if possible.
0137      *
0138      * \param cursor new cursor position.
0139      */
0140     virtual void setCursorPosition(const KTextEditor::Cursor &cursor) = 0;
0141 
0142     /**
0143      * Retrieve the current text selection, if one exists.
0144      *
0145      * \returns the current text selection
0146      */
0147     virtual KTextEditor::Range textSelection() const;
0148 
0149     /**
0150      * Set the current text selection, if possible.
0151      *
0152      * \param range new cursor position.
0153      */
0154     virtual void setTextSelection(const KTextEditor::Range &range) = 0;
0155 
0156     /**
0157      * @returns the text in a given range
0158      */
0159     virtual QString text(const KTextEditor::Range &range) const;
0160 
0161     /**
0162      * Retrieve the current text line, if one exists.
0163      *
0164      * @returns the current text line
0165      */
0166     virtual QString textLine() const;
0167 
0168     /**
0169      * Retrieve the current text word, if one exists.
0170      *
0171      * @returns the current text word
0172      */
0173     virtual QString textWord() const;
0174 
0175     /**
0176      * Performs document activation actions if any.
0177      * This needs to call notifyActivated()
0178      */
0179     virtual void activate(Sublime::View *activeView, KParts::MainWindow *mainWindow) = 0;
0180 
0181     /**
0182      * @returns the active text view in case it's a text document and it has one.
0183      */
0184     virtual KTextEditor::View* activeTextView() const;
0185 
0186 protected:
0187     ICore* core();
0188     explicit IDocument( ICore* );
0189     void notifySaved();
0190     void notifyStateChanged();
0191     void notifyActivated();
0192     void notifyContentChanged();
0193     void notifyTextDocumentCreated();
0194     void notifyUrlChanged(const QUrl& previousUrl);
0195     void notifyLoaded();
0196 
0197 private:
0198     const QScopedPointer<class IDocumentPrivate> d_ptr;
0199     Q_DECLARE_PRIVATE(IDocument)
0200     friend class IDocumentPrivate;
0201 };
0202 
0203 }
0204 
0205 Q_DECLARE_INTERFACE(KDevelop::IDocument, "org.kdevelop.IDocument")
0206 
0207 #endif
0208