1 /*
2  *  SPDX-FileCopyrightText: 2012-2014 Andreas Cord-Landwehr <cordlandwehr@kde.org>
3  *
4  *  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5  */
6 
7 #ifndef PROJECT_H
8 #define PROJECT_H
9 
10 #include "libgraphtheory/editor.h"
11 #include "libgraphtheory/typenames.h"
12 
13 #include <QList>
14 #include <QString>
15 #include <QScopedPointer>
16 
17 class QUrl;
18 class ProjectPrivate;
19 
20 namespace KTextEditor
21 {
22     class Document;
23     class Editor;
24 }
25 
26 /**
27  * \class Project
28  * \brief A project represents the compilation of graphs, scripts and further meta information.
29  *
30  * Topics:
31  *  - \ref project_intro
32  *  - \ref project_usage
33  *
34  * \section project_intro Introduction
35  *
36  * A project consists of a zipped archive file that contains all of its files and, when opened,
37  * a working directory that contains (temporary) copies of all of these files. Only on writing back,
38  * the archive file gets updated.
39  *
40  * \section project_usage Using Projects
41  *
42  * A project can be created by creating an empty project or by using the overloaded constructor
43  * to open an existing project file.
44  */
45 class Project : public QObject
46 {
47     Q_OBJECT
48 
49 public:
50     /**
51      * Constructs an empty project.
52      */
53     explicit Project(GraphTheory::Editor *graphEditor);
54 
55     /**
56      * Opens the project @p url as well as all contained files.chive
57      */
58     explicit Project(const QUrl &url, GraphTheory::Editor *graphEditor);
59 
60     ~Project() override;
61 
62     /**
63      * @return the project's journal document
64      * Note that the pointer may be 0.
65      */
66     KTextEditor::Document * journalDocument() const;
67 
68   /*
69    * Handling of script documents.
70    */
71 public:
72     /**
73      * @return list of all scripts contained in this project
74      */
75     QList<KTextEditor::Document*> codeDocuments() const;
76 
77     /**
78      * Creates a code document based on a given file path
79      */
80     KTextEditor::Document* createCodeDocument(const QString& filePath);
81 
82     /**
83      * Returns a document of a given url (path)
84      */
85     KTextEditor::Document* openCodeDocument(const QUrl &url);
86 
87     /**
88      * Add code document @p document to project. This updates the document's url.
89      */
90     bool addCodeDocument(KTextEditor::Document *document);
91 
92     /**
93      * Import the script given in file @p url to the project. This creates a copy
94      * of the original document.
95      */
96     KTextEditor::Document * importCodeDocument(const QUrl &url);
97 
98     /**
99      * Remove the script file @p document from the project
100      */
101     void tryToRemoveCodeDocument(KTextEditor::Document *document);
102 
103     /**
104      * @return project dependent document name
105      */
106     QString documentName(KTextEditor::Document *document) const;
107 
108     /**
109      * Set a project dependent document name @p name for @p document , i.e., the name is only
110      * provided by the project but not by the text document file.
111      */
112     void setDocumentName(KTextEditor::Document *document, const QString &name);
113 
114 public Q_SLOTS:
115     /**
116      * Set the currently active graph document index to @p index.
117      * If the index does not exist, it will not be changed.
118      */
119     void setActiveCodeDocument(int index);
120 
121 Q_SIGNALS:
122     void codeDocumentAboutToBeAdded(KTextEditor::Document*,int);
123     void codeDocumentAdded();
124     void codeDocumentAboutToBeRemoved(int,int);
125     void codeDocumentRemoved();
126     void activeCodeDocumentChanged(int index);
127     void modifiedChanged();
128 
129   /*
130    * Handling of graph documents.
131    */
132 public:
133     /**
134      * @return list of all graph documents contained in this project
135      */
136     QList<GraphTheory::GraphDocumentPtr> graphDocuments() const;
137 
138     GraphTheory::GraphDocumentPtr activeGraphDocument() const;
139 
140     /**
141      * Add the graph document @p document to project. This updates the document's url.
142      */
143     bool addGraphDocument(GraphTheory::GraphDocumentPtr document);
144 
145     /**
146      * Import the graph document given in file @p documentUrl to the project. This creates a copy
147      * of the original graph document.
148      */
149     GraphTheory::GraphDocumentPtr importGraphDocument(const QUrl &documentUrl);
150 
151     /**
152      * Remove the graph file @p document from the project
153      */
154     void removeGraphDocument(GraphTheory::GraphDocumentPtr document);
155 
156 public Q_SLOTS:
157     /**
158      * Set the currently active graph document index to @p index.
159      * If the index does not exist, it will not be changed.
160      */
161     void setActiveGraphDocument(int index);
162 
163 Q_SIGNALS:
164     void graphDocumentAboutToBeAdded(GraphTheory::GraphDocumentPtr, int);
165     void graphDocumentAdded();
166     void graphDocumentAboutToBeRemoved(int,int);
167     void graphDocumentRemoved();
168     void activeGraphDocumentChanged(int index);
169     void activeGraphDocumentChanged(GraphTheory::GraphDocumentPtr document);
170 
171   /*
172    * General file related actions.
173    * None of these actions cause user interaction.
174    */
175 public:
176     /**
177      * Save project to path as given by projectUrl().
178      * @return @e true on success, i.e. the save has been done, otherwise
179      *         @e false
180      */
181     bool projectSave();
182 
183     /**
184      * Save project to path @p url. This also changes the projectUrl() path.
185      * @return @e true on success, i.e. the save has been done, otherwise
186      *         @e false
187      */
188     bool projectSaveAs(const QUrl &url);
189 
190     /**
191      * @return project file path
192      */
193     QUrl projectUrl() const;
194 
195     /**
196      * Set the project file to @p url. This is the path where the project is saved.
197      */
198     void setProjectUrl(const QUrl &url);
199 
200     /**
201      * @internal
202      * @return working directory that contains the extraced project archive while open
203      */
204     QString workingDir() const;
205 
206     /**
207      * @internal
208      * set modified state of project container to @c modified
209      * @note this does not change the modified state of any associated document
210      */
211     void setModified(bool modified = true);
212 
213     /**
214      * @return @e true if project (including any file of this project) was modified after last saving/loading, otherwise
215      *         @e false
216      */
217     bool isModified() const;
218 
219 private:
220     const QScopedPointer<ProjectPrivate> d;
221 };
222 
223 #endif
224