1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef _U2_PROJECT_MODEL_H_
23 #define _U2_PROJECT_MODEL_H_
24 
25 #include <QAction>
26 
27 #include "DocumentModel.h"
28 #include "StateLockableDataModel.h"
29 
30 #define CURRENT_PROJECT_VERSION "1.0"
31 
32 namespace U2 {
33 
34 class GObjectViewState;
35 class Document;
36 class Project;
37 class Task;
38 class U2OpStatus;
39 
40 #define PROJECT_FILE_PURE_EXT QString("uprj")
41 #define PROJECTFILE_EXT QString("." + PROJECT_FILE_PURE_EXT)
42 #define DIALOG_FILTER_PROJECT_EXT QString(" (*" + PROJECTFILE_EXT + ")")
43 
44 // data loader hints
45 #define ProjectLoaderHint_CloseActiveProject "close-active-project"
46 #define ProjectLoaderHint_ForceFormatOptions "force-format-options"
47 #define ProjectLoaderHint_LoadWithoutView "load-without-view"
48 #define ProjectLoaderHint_LoadUnloadedDocument "load-unloaded-document"
49 #define ProjectLoaderHint_UseImporters "use-importers"
50 #define ProjectLoaderHint_MultipleFilesMode_URLDocument "multiple-files-mode-url-document"
51 #define ProjectLoaderHint_MultipleFilesMode_URLsDocumentConsistOf "multiple-files-mode-urls-document-consist-of"
52 #define ProjectLoaderHint_MultipleFilesMode_SaveDocumentFlag "multiple-files-mode-save-document-flag"
53 #define ProjectLoaderHint_MultipleFilesMode_Flag "multiple-files-mode-flag"
54 #define ProjectLoaderHint_MultipleFilesMode_RealDocumentFormat "multiple-files-mode-real-document-format"
55 #define ProjectLoaderHint_MergeMode_DifferentAlphabets "merge-sequences-different-alphabets"
56 #define ProjectLoaderHint_DontCheckForExistence "dont-check-for-existence"
57 #define ProjectLoaderHint_OpenBySystemIfFormatDetectionFailed "open-by-system-if-format-detection-failed"
58 #define ProjectLoaderHint_DoNotAddToRecentDocuments "do-not-add-to-recent-documents"
59 
60 /**
61  * When this hint is provided the 'openWithProjectTask' skips document format detection.
62  * Effective only for a single document mode today.
63  */
64 #define ProjectLoaderHint_DocumentFormat "document-format"
65 
66 /// Service responsible for project loading / unloading
67 class U2CORE_EXPORT ProjectLoader : public QObject {
68 public:
69     /**
70      * Runs openWithProjectTask for the recent files/projects list item.
71      * If a project or a document with the given URL does not exist asks about removing it from the recent files list.
72      */
73     virtual void runOpenRecentFileOrProjectTask(const GUrl &url) = 0;
74 
75     /**
76         Opens files and adds them to the current project. If project does not exists - creates anonymous one
77         If the file is project file - loads it.
78     */
79     virtual Task *openWithProjectTask(const QList<GUrl> &urls, const QVariantMap &hints = QVariantMap()) = 0;
80 
81     /** Creates new project. If URL is empty the project created is anonymous */
82     virtual Task *createNewProjectTask(const GUrl &url = GUrl()) = 0;
83 
84     /**
85      * Returns a task to load a project or a document.
86      * The method may return nullptr if something went wrong: for example the project/document is already loaded.
87      */
88     virtual Task *createProjectLoadingTask(const GUrl &url, const QVariantMap &hints = QVariantMap()) = 0;
89 
90     /** Creates new project instance */
91     virtual Project *createProject(const QString &name, const QString &url, QList<Document *> &documents, QList<GObjectViewState *> &states) = 0;
92 
93     virtual QAction *getAddExistingDocumentAction() = 0;
94 
95     // This function can return NULL, don't forget to check the result.
96     Task *openWithProjectTask(const GUrl &url, const QVariantMap &hints = QVariantMap()) {
97         QList<GUrl> urls;
98         urls << url;
99         return openWithProjectTask(urls, hints);
100     };
101 };
102 
103 /// Project model
104 class U2CORE_EXPORT Project : public StateLockableTreeItem {
105     Q_OBJECT
106     Q_PROPERTY(QString name WRITE setProjectName READ getProjectName)
107     Q_PROPERTY(QString url WRITE setProjectURL READ getProjectURL)
108     Q_PROPERTY(QList<Document *> docs READ getDocuments)
109 
110 public:
111     virtual ~Project();
112 
113     virtual const QString &getProjectName() const = 0;
114 
115     virtual void setProjectName(const QString &name) = 0;
116 
117     virtual const QString &getProjectURL() const = 0;
118 
119     virtual void setProjectURL(const QString &) = 0;
120 
121     virtual const QList<Document *> &getDocuments() const = 0;
122 
123     Q_INVOKABLE virtual void addDocument(Document *d) = 0;
124 
125     Q_INVOKABLE virtual void removeDocument(Document *d, bool autodelete = true) = 0;
126 
127     Q_INVOKABLE virtual Document *findDocumentByURL(const QString &url) const = 0;
128 
129     Q_INVOKABLE virtual Document *findDocumentByURL(const GUrl &url) const = 0;
130 
131     virtual bool lockResources(int sizeMB, const QString &url, QString &error) = 0;
132 
133     virtual const QList<GObjectViewState *> &getGObjectViewStates() const = 0;
134 
135     virtual void addGObjectViewState(GObjectViewState *s) = 0;
136 
137     virtual void removeGObjectViewState(GObjectViewState *s) = 0;
138 
139     virtual void makeClean() = 0;
140 
141     virtual quint64 getObjectIdCounter() const = 0;
142 
143     virtual void setObjectIdCounter(quint64 c) = 0;
144 
145     static void setupToEngine(QScriptEngine *engine);
146 
147     virtual void removeRelations(const QString &docUrl) = 0;
148 
149     virtual void updateDocInRelations(const QString &oldDocUrl, const QString &newDocUrl) = 0;
150 
151 private:
152     static QScriptValue toScriptValue(QScriptEngine *engine, Project *const &in);
153     static void fromScriptValue(const QScriptValue &object, Project *&out);
154 signals:
155     void si_projectURLChanged(const QString &oldURL);
156 
157     void si_projectRenamed(Project *p);
158 
159     void si_documentAdded(Document *d);
160 
161     void si_documentRemoved(Document *d);
162 
163     void si_objectViewStateAdded(GObjectViewState *);
164 
165     void si_objectViewStateRemoved(GObjectViewState *);
166 };
167 
168 }  // namespace U2
169 Q_DECLARE_METATYPE(U2::Project *)
170 Q_DECLARE_METATYPE(QList<U2::Document *>)
171 
172 #endif
173