Tulip  5.3.0
Large graphs analysis and drawing
TulipProject.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TULIPPROJECT_H
21 #define TULIPPROJECT_H
22 
23 #include <QDir>
24 #include <QString>
25 #include <QTemporaryDir>
26 
27 #include <tulip/tulipconf.h>
28 
29 #include <fstream>
30 
31 namespace tlp {
32 
33 class PluginProgress;
34 
35 /**
36  @ingroup Plugins
37 
38  @brief The TulipProject object handles the content of a Tulip project.
39 
40  All tulip projects contain a set of defined static meta-information:
41  @list
42  @li name (QString): the name of the project
43  @li description (QString): Comments about the project
44  @li author (QString): the author of the project
45  @li perspective (QString): the name of the perspective plugin associated to the project
46  @li date (QDate): the date of project's last modification
47  @li version (QString): the version of the Tulip project format
48  @endlist
49 
50  Alongside this information, one can store any kind of file into a Tulip project. Since a project
51  is meant to be associated to a specific perspective, the responisbility of those file
52  is left to the perspective.
53 
54  A TulipProject DOES NOT automatically save to disk. One will have to call the write() method to
55  serialize data.
56  @warning Precise implementation of the TulipProject object should NOT be known or used by the user
57  since it could be subject to changes.
58 
59  If something wrong happens when calling a method from TulipProject, this method will return either
60  false or a invalid result (see specific method documentation). The last error message can be
61  retrieved with the lastError() method.
62 
63  After opening and before saving a project, user will be able to list/delete files and directories
64  available in the project and open them using std filestreams or Qt's QIODevice.
65  Files can be opened using the stdFileStram and fileStream methods. They will always be opened in
66  Read/Write mode.
67 
68  Files in a tulip project are identified by their path. Those path ar similar to the path of a
69  standard file system and use the "/" character as a separator.
70  The root directory is identified by "/".
71 
72  @warning TulipProject path ALWAYS use the "/" character as a directory separator. This is
73  OS-independant.
74 
75  @note A file called graph.tlp located at the top-level directory will be identified by the
76  "/graph.tlp" path while the file "graph.tlp" located in the "data" directory will be identified by
77  "/data/graph.tlp".
78  */
79 class TLP_QT_SCOPE TulipProject : public QObject {
80  Q_OBJECT
81 
82  TulipProject() = delete;
83  explicit TulipProject(QTemporaryDir *);
84 
85 public:
86  ~TulipProject() override;
87 
88  /**
89  @brief Starts a new TulipProject from scratch
90 
91  This method builds up a new TulipProject file without taking any input.
92  @see openProject()
93  */
94  static TulipProject *newProject();
95 
96  /**
97  @brief Opens a previously saved tulip project file and returns the corresponding project
98 
99  This method will unpack a tulip project file into some directory and allow the user to
100  manipulate the files.
101  @see TulipProject::save()
102  @param file The file to open.
103  @param progress A progress handler.
104  @return a pointer to a TulipProject object.
105  */
106  static TulipProject *openProject(const QString &file, tlp::PluginProgress *progress = nullptr);
107 
108  /**
109  @brief Opens a previously saved tulip project file
110 
111  This method unpacks a tulip project file into some directory and allow the user to manipulate
112  the files.
113  @see TulipProject::save()
114  @param file The file to open.
115  @param progress A progress handler.
116  @return true if the file has been successfully opened
117  */
118  bool openProjectFile(const QString &file, tlp::PluginProgress *progress = nullptr);
119 
120  /*
121 
122  @brief Restores a project which has already been extracted into path
123 
124  @warning Using several TulipProject instances on the same directory may result in undefined
125  behavior. This method should only be used for crash handling purposes.
126  @param path The path where the archive was previously extracted
127  @return a pointer to a TulipProject object.
128 
129  static TulipProject *restoreProject(const QString &path); */
130 
131  /**
132  * @brief Removes all files in the project and unset project file if any
133  *
134  * @since Tulip 5.0
135  */
136  bool clearProject();
137 
138  /**
139  * @brief Sets the file where to save the project
140  * @param projectFile absolute path to a .tlpx file
141  *
142  * @since Tulip 5.0
143  */
144  void setProjectFile(const QString &projectFile);
145 
146  /**
147  @brief Writes files in the TulipProject into a packed archive.
148 
149  This method packs every file in the project into a single archive.
150  @note This method DOES NOT close the project. It only commits changes to the specified file. A
151  TulipProject is only closed when destroyed.
152  @param file Absolute path where files should be packed.
153  @param progress A progress handler
154  @return False if method failed
155  */
156  bool write(const QString &file, tlp::PluginProgress *progress = nullptr);
157 
158  /**
159  @brief Lists entries in a directory
160 
161  @see QDir documentation for a complete description of filtering arguments
162  @param path The path to scan. @see TulipProject
163  @return The list of files and directories present in the given directory
164  */
165  QStringList entryList(const QString &path, QDir::Filters filters = QDir::NoFilter,
166  QDir::SortFlags sort = QDir::NoSort);
167 
168  /**
169  @brief Lists entries in a directory
170 
171  @see QDir documentation for a complete description of filtering arguments
172  @param path The path to scan. @see TulipProject
173  @return The list of files and directories present in the given directory
174  */
175  QStringList entryList(const QString &path, const QStringList &nameFilters,
176  QDir::Filters filters = QDir::NoFilter,
177  QDir::SortFlags sort = QDir::NoSort);
178 
179  /**
180  @brief Checks if the specified file/folder exists
181 
182  @param path The path to check.
183  @return true if the path exists.
184  */
185  bool exists(const QString &path);
186 
187  /**
188  @brief Recursively creates the specified path.
189 
190  Created folders will be empty
191  @return true if path was successfully created.
192  */
193  bool mkpath(const QString &path);
194 
195  /**
196  @brief Checks if the given path is a directory.
197 
198  @param path The path to check. @see TulipProject
199  @return true/false wether the path is a directory.
200  */
201  bool isDir(const QString &path);
202 
203  /**
204  @brief Removes a file from the project.
205 
206  If the given path points to a directory, or if the file does not exist, this method will fail
207  and return false
208  @param path The path to delete. @see TulipProject
209  */
210  bool removeFile(const QString &path);
211 
212  /**
213  @brief Removes a directory from the project.
214 
215  If the given file points to a file, or if the directory does not exist, or if the directory is
216  not empty, this method will fail and return false.
217  @see removeAllDir to remove a non-empty directory.
218  @param path The path to delete. @see TulipProject
219  */
220  bool removeDir(const QString &path);
221 
222  /**
223  @brief Removes a directory and all its content from the project.
224 
225  If the given file points to a file, or if the directory does not exist, this method will fail
226  and return false.
227  @warning This will remove every file stored in the specified directory.
228  @param path The path to delete. @see TulipProject
229  */
230  bool removeAllDir(const QString &path);
231 
232  /**
233  @brief Copies a file from the local filesystem into the project
234 
235  @param source The absolute path of the file to copy
236  @param destination The project path where to copy the file
237  @return false if copy failed
238  */
239  bool copy(const QString &source, const QString &destination);
240 
241  /**
242  @brief Creates an empty file
243 
244  This method is similar to the UNIX's touch shell command. Except it won't renew the file's
245  creation date if the file already exists.
246  @param file the file to create
247  @return true if file creation was sucessful.
248  */
249  bool touch(const QString &path);
250 
251  /**
252  @brief Gets a STL file stream (default to R/W access mode) to the given path.
253 
254  @warning This method does not check if the given path is a directory or a file. User might get
255  an invalid filestream.
256  @warning It is up to the user to delete the std::fstream returned.
257  @param path The path to open. @see TulipProject
258  @return an opened filestream on the given path.
259  */
260  std::fstream *stdFileStream(const QString &path, std::ios_base::openmode = std::fstream::in |
261  std::fstream::out |
262  std::fstream::app);
263 
264  /**
265  @brief Gets a Qt I/O device (default to R/W access mode) to the given path.
266 
267  @warning This method does not check if the given path is a directory or a file. User might get
268  an invalid filestream.
269  @warning User SHOULD NOT cast the QIODevice returned by this method into any of its subclass
270  since the implementation might change in future versions.
271  @warning It is up to the user to delete the QIODevice returned.
272  @param path The path to open. @see TulipProject
273  @param mode The opening mode as described in the Qt documentation.
274  @return an opened Qt device on the given path.
275  */
276  QIODevice *fileStream(const QString &path, QIODevice::OpenMode mode = QIODevice::ReadWrite);
277 
278  /**
279  @brief Returns the archive file associated with this project.
280 
281  If the project has been opened from an existing file or if the write method has already been
282  called, this method will return the last file path specified.
283  In other cases, this method will return an empty string.
284  */
285  QString projectFile() const {
286  return _projectFile;
287  }
288 
289  /**
290  @brief This method returns the real absolute path corresponding to / in the TulipProject.
291 
292  This can be used to create a TulipProject directly from a path.
293  @warning Using several TulipProject instances at the same time on the same path may result in
294  undefined behavior.
295  */
296  QString absoluteRootPath() const;
297 
298  // Developer note: Every field in the TulipProject tagged as a Q_PROPERTY will automaticaly be
299  // serialized in the project.xml file
300  /**
301  @brief the name of the project
302  */
303  Q_PROPERTY(QString name READ name WRITE setName)
304  /**
305  * @see name
306  */
307  QString name() const;
308 
309  /**
310  @brief User-written description of the project
311  */
312  Q_PROPERTY(QString description READ description WRITE setDescription)
313  /**
314  * @see description
315  */
316  QString description() const;
317 
318  /**
319  @brief Name of the author
320  */
321  Q_PROPERTY(QString author READ author WRITE setAuthor)
322  /**
323  * @see author
324  */
325  QString author() const;
326 
327  /**
328  @brief Name of the perspective associated to the project.
329 
330  When the user open a project from Tulip, this porperty is first read to identify find kind of
331  perspective plugin should be launched to
332  open the project
333 
334  @warning If the perspective name associated to the project is invalid or correspond to a missing
335  plugin, tulip may not be able to open the file.
336  */
337  Q_PROPERTY(QString perspective READ perspective WRITE setPerspective)
338  /**
339  * @see perspective
340  */
341  QString perspective() const;
342 
343  /**
344  @brief The version of the Tulip project format with which the file was created.
345  Project from older format version will be always saved into the newest version available.
346  */
347  QString version() const;
348 
349  /**
350  @brief Returns the absolute filesystem path used to store the file
351  @warning Be cautious though since directly modifying project files without using TulipProject
352  methods could result in undefined behavior.
353  */
354  QString toAbsolutePath(const QString &relativePath);
355 
356 signals:
357  void projectFileChanged(const QString &projectFile);
358 
359 public slots:
360  /**
361  * @see name
362  */
363  void setName(const QString &);
364  /**
365  * @see description
366  */
367  void setDescription(const QString &);
368  /**
369  * @see author
370  */
371  void setAuthor(const QString &);
372  /**
373  * @see perspective
374  */
375  void setPerspective(const QString &);
376 
377 private:
378  bool writeMetaInfo();
379  bool readMetaInfo();
380 
381  // Core fileset
382  QTemporaryDir *_rootDir;
383  QString _projectFile;
384 
385  inline const QString rootDir() const {
386  return _rootDir->path();
387  }
388 
389  // Meta information
390  QString _author;
391  QString _name;
392  QString _description;
393  QString _perspective;
394 };
395 } // namespace tlp
396 #endif // TULIPPROJECT_H
QString projectFile() const
Returns the archive file associated with this project.
Definition: TulipProject.h:285
The TulipProject object handles the content of a Tulip project.
Definition: TulipProject.h:79
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...