Tulip  5.4.0
Large graphs analysis and drawing
Perspective.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 _PERSPECTIVE_H
21 #define _PERSPECTIVE_H
22 
23 #include <tulip/PluginContext.h>
24 #include <tulip/Plugin.h>
25 #include <tulip/TlpQtTools.h>
26 
27 #include <QString>
28 #include <QVariantMap>
29 #include <QSet>
30 #include <QMenu>
31 
32 class QMainWindow;
33 class QTcpSocket;
34 
35 namespace tlp {
36 
37 class PluginProgress;
38 class TulipProject;
39 
40 static const std::string PERSPECTIVE_CATEGORY = "Perspective";
41 
42 /**
43  * @ingroup Plugins
44  * @brief A context data structure for tlp::Perspective instances.
45  *
46  * @see tlp::Perspective
47  */
48 class TLP_QT_SCOPE PerspectiveContext : public tlp::PluginContext {
49 public:
50  PerspectiveContext() : mainWindow(nullptr), project(nullptr), tulipPort(0) {}
51  QMainWindow *mainWindow;
52  TulipProject *project;
53  QString externalFile;
54  QVariantMap parameters;
55  quint64 tulipPort;
56  unsigned int id;
57 };
58 
59 /**
60  * @ingroup Plugins
61  * @brief A Perspective is a Tulip plugin that completely re-defines the user interface.
62  *
63  * A Perspective aims at using the multiple features available in Tulip to create a complete,
64  * coherent workflow dedicated to a particular use-case.
65  * Perspectives are chosen by the user when first running the Tulip agent. The main perspective is
66  * called "Tulip". This perspective unveils all the Tulip features and aims at being a protyping and
67  * researching platform.
68  *
69  * A Perspective always acts in its own process and communicate with the Tulip agent via TCP
70  * sockets.
71  * Thus, it is the Perspective's responsibility to offer the possibility to display graphs, run
72  * plugins, etc. A lot of helper classes can be found into the tulip-gui API like pre-made widgets,
73  * Qt models, etc.
74  *
75  * Perspective's data is stored into a TulipProject. A TulipProject is an archive capable of
76  * containing heterogeneous data (like multiple graphs, textures, extra files, etc). When a
77  * TulipProject gets saved by a perspective, it is linked to it in its meta-information.
78  * Creating a Perspective primarily into implementing the start() method that builds the GUI. This
79  * method will be called by the overleying system when a new perspective is launched. The rest of
80  * the logic implementation is left to the developer.
81  */
82 class TLP_QT_SCOPE Perspective : public QObject, public tlp::Plugin {
83  Q_OBJECT
84 
85  static tlp::Perspective *_instance;
86  QSet<QString> _reservedProperties;
87  QTcpSocket *_agentSocket;
88  unsigned int _perspectiveId;
89  bool _maximised;
90  void sendAgentMessage(const QString &);
91  void notifyProjectLocation(const QString &path);
92 
93 protected:
94  /**
95  * @brief The project associated to this perspective. This project can be empty or contain data
96  * depending on how the Perspective was launched:
97  * To launch a perspective, the tulip_perspective executable is called with the following
98  * arguments:
99  * @code
100  * tulip_perspective [--perspective=Name] [file_path]
101  * @endcode
102  * @list
103  * @li The --perspective argument forces Tulip to start the "Name" perspective. Even if the
104  * project states otherwise. If this argument is not specified, Tulip will look in the project's
105  * meta-data to dertermine the perspective to launch.
106  * @li file_path is the absolute path of the project archive to associate with the perspective. If
107  * file_path is not given, the --perspective argument must be declared in order for Tulip to know
108  * what perspective to launch. In this case, _project will point to an empty (but valid!) project.
109  * @endlist
110  */
111  TulipProject *_project;
112 
113  /**
114  * @brief The main window on which the perspective should build the GUI.
115  *
116  * It is not the Perspective's responsibility to destroy the main window when the application gets
117  * closed. Generally speaking, destroying the _mainWindow pointer could lead to undefined
118  * behavior.
119  */
120  QMainWindow *_mainWindow;
121 
122  /**
123  * If the user provided a file to the tulip_perspective but this file is not recognized as a valid
124  * TulipProject, the full path of the file will be stored into the _externalFile member.
125  * @note Remember that perspectives should always store their data into TulipProject in order to
126  * keep a consistent workflow.
127  */
128  QString _externalFile;
129 
130  /**
131  * @brief Contains extra parameters that have not been parsed by the overleying system. Those are
132  * considered to be Perspective-specific and are forwarded into this variable.
133  */
134  QVariantMap _parameters;
135  bool checkSocketConnected();
136 
137 public:
138  enum ProgressOption {
139  NoProgressOption = 0x0,
140  IsPreviewable = 0x1,
141  IsCancellable = 0x2,
142  IsStoppable = 0x4
143  };
144  Q_DECLARE_FLAGS(ProgressOptions, ProgressOption)
145 
146  std::string category() const override {
147  return PERSPECTIVE_CATEGORY;
148  }
149  std::string icon() const override {
150  return ":/tulip/gui/icons/32/plugin_controller.png";
151  }
152 
153  /**
154  * @brief Called at the beginning of the tulip_perspective to set the Perspective singleton.
155  * @see instance()
156  */
157  static void setInstance(tlp::Perspective *);
158 
159  /**
160  * Since a Perspective has its own process to work in, there can be only one perspective instance
161  * by process.
162  * In order to ease the development process, Perspective are available as a singleton in order for
163  * child widgets and plugins to be able to access to the features of a Perspective.
164  * This architecture was added for developers who want to create an application containing several
165  * plugins grouped into Perspective.
166  * @return The perspective singleton
167  */
168  static tlp::Perspective *instance();
169 
170  /**
171  * @return A typed instance of the perspective singleton.
172  */
173  template <typename T>
174  static T *typedInstance() {
175  return dynamic_cast<T *>(instance());
176  }
177 
178  /**
179  * @brief Constructs a perspective object
180  * @warning There should not be any logic implemented into the Perspective's constructor. See the
181  * start method instead.
182  */
184  ~Perspective() override;
185 
186  /**
187  * @brief Build the main window GUI and starts the workflow.
188  * When this method is called, it means that the Perspective is responsible of the application
189  * workflow until the application is closed by the user.
190  * @param progress A progress handler
191  */
192  virtual void start(tlp::PluginProgress *progress) = 0;
193 
194  /**
195  * @brief Creates a progress handler and returns it.
196  * This method allows lower-level widgets to create top-level progress bars directly from the
197  * Perspective.
198  * @return
199  */
200  virtual PluginProgress *
201  progress(ProgressOptions options = ProgressOptions(IsPreviewable | IsStoppable | IsCancellable));
202 
203  /**
204  * @brief usage Displays a usage message when called from the tulip_perspective executable
205  */
206  virtual void usage(std::string &usage_str) const {
207  usage_str = "No options for this perspective.";
208  }
209 
210  /**
211  * @return The Perspective's main window.
212  */
213  QMainWindow *mainWindow() const;
214 
215  /**
216  * @brief Checks if the name corresponds to a reserved properties.
217  * Perspectives are allowed to reserve graph properties. A reserved graph properties is a core
218  * property that cannot be deleted by the user and cannot be renamed.
219  * @return true if the perspective is registered.
220  */
221  bool isReservedPropertyName(QString name);
222 
223  /**
224  * @brief Sets a new property name as registered
225  */
226  void registerReservedProperty(QString);
227 
228  /**
229  * @brief Tells the perspective that the graph visualizations should be redrawn.
230  * @param center if true, visualization should also be centered (eg. the layout has been changed)
231  */
232  virtual void redrawPanels(bool center = false) = 0;
233 
234  /**
235  * @brief Tells the perspective that the visualizations for a given graph should be centered.
236  * @note By default, this method does nothing.
237  */
238  virtual void centerPanelsForGraph(tlp::Graph *);
239 
240  void resetTitle() {
241  emit resetWindowTitle();
242  }
243 
244  /**
245  * @brief a static function to ease the display of status messages
246  */
247  static void showStatusMessage(const QString &msg) {
248  instance()->displayStatusMessage(msg);
249  }
250 
251  /**
252  * @brief a static function to ease the display of status messages
253  */
254  static void showStatusMessage(const std::string &msg) {
255  showStatusMessage(tlp::tlpStringToQString(msg));
256  }
257 
258  /**
259  * @brief a static function to enable the redirection of the statusTip
260  * or toolTip of menu actions
261  */
262  static void redirectStatusTipOfMenu(QMenu *menu);
263 
264  /**
265  * @brief a static function to log a message
266  * see qInstallMessageHandler
267  */
268  static void showLogMessage(QtMsgType type, const QMessageLogContext &context,
269  const QString &msg) {
270  instance()->logMessage(type, context, msg);
271  }
272 
273  /**
274  * @brief a static function to display the log messages
275  */
276  static void showLogMessages() {
277  instance()->displayLogMessages();
278  }
279 
280 public slots:
281  /**
282  * @brief Called when the user wants to close the application.
283  * @return Returning false prevents the window from being closed but the Perspective will have to
284  * implement its own way of closing the application.
285  */
286  virtual bool terminated() {
287  return true;
288  }
289 
290 signals:
291  void resetWindowTitle();
292 
293 protected slots:
294  /**
295  * @brief Send a message to the Tulip agent to make him display the Plugins Center page.
296  */
297  void showPluginsCenter();
298 
299  /**
300  * @brief Call this slot to switch to full screen or windowed mode
301  * @param f is true, switch to full screen mode. If false, switch to windowed mode
302  */
303  void showFullScreen(bool f);
304 
305  /**
306  * @brief Send a message to the Tulip agent to make him display the Projects page.
307  */
308  void showProjectsPage();
309 
310  /**
311  * @brief Send a message to the Tulip agent to make him display the "About us" page.
312  */
313  void showAboutPage();
314 
315  /**
316  * @brief Send a message to the Tulip agent to make him display a message in the system
317  * notification area.
318  * @param s The message to display.
319  */
320  void showTrayMessage(const QString &s);
321 
322  /**
323  * @brief Send a message to the Tulip agent to make him display an error message that will be
324  * shown in the system notification as well as on the welcome page.
325  * @param title The message's title.
326  * @param s The message to display.
327  */
328  void showErrorMessage(const QString &title, const QString &s);
329 
330  /**
331  * @brief Send a message to the Tulip agent to make him open a new Tulip Project.
332  * @param path the absolute path of the project file.
333  */
334  virtual void openProjectFile(const QString &path);
335 
336  /**
337  * @brief Send a message to the Tulip agent to make him open a new Perspective without a project.
338  * @param name The name of the Perspective to create.
339  */
340  void createPerspective(const QString &name);
341 
342  /**
343  * @brief Show the statusTip (or the toolTip) of an action
344  * @param action a QAction
345  */
346  void showStatusTipOf(QAction *action);
347 
348  /**
349  * @brief a virtual function to display a status message
350  */
351  virtual void displayStatusMessage(const QString &s);
352 
353  /**
354  * @brief a virtual function to clear the last status message
355  */
356  virtual void clearStatusMessage();
357 
358  /**
359  * @brief a virtual function to display the whole logs
360  */
361  virtual void displayLogMessages() {}
362 
363  /**
364  * @brief a virtual function to log a message
365  */
366  virtual void logMessage(QtMsgType, const QMessageLogContext &, const QString &) {}
367 };
368 Q_DECLARE_OPERATORS_FOR_FLAGS(Perspective::ProgressOptions)
369 } // namespace tlp
370 
371 #endif //_PERSPECTIVE_H
QVariantMap _parameters
Contains extra parameters that have not been parsed by the overleying system. Those are considered to...
Definition: Perspective.h:134
virtual void usage(std::string &usage_str) const
usage Displays a usage message when called from the tulip_perspective executable
Definition: Perspective.h:206
QMainWindow * _mainWindow
The main window on which the perspective should build the GUI.
Definition: Perspective.h:120
virtual void logMessage(QtMsgType, const QMessageLogContext &, const QString &)
a virtual function to log a message
Definition: Perspective.h:366
virtual void displayLogMessages()
a virtual function to display the whole logs
Definition: Perspective.h:361
Definition: TlpTools.h:48
QString _externalFile
Definition: Perspective.h:128
TulipProject * _project
The project associated to this perspective. This project can be empty or contain data depending on ho...
Definition: Perspective.h:111
static void showLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
a static function to log a message see qInstallMessageHandler
Definition: Perspective.h:268
virtual void displayStatusMessage(const QString &s)
a virtual function to display a status message
virtual bool terminated()
Called when the user wants to close the application.
Definition: Perspective.h:286
Contains runtime parameters for a plugin.
Definition: PluginContext.h:42
static void showLogMessages()
a static function to display the log messages
Definition: Perspective.h:276
static T * typedInstance()
Definition: Perspective.h:174
A context data structure for tlp::Perspective instances.
Definition: Perspective.h:48
Top-level interface for plug-ins.
Definition: Plugin.h:85
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...
A Perspective is a Tulip plugin that completely re-defines the user interface.
Definition: Perspective.h:82
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.
Definition: TlpQtTools.h:55
std::string icon() const override
The icon (preferably a thumbnail) of the plugin.
Definition: Perspective.h:149
static void showStatusMessage(const std::string &msg)
a static function to ease the display of status messages
Definition: Perspective.h:254
static void showStatusMessage(const QString &msg)
a static function to ease the display of status messages
Definition: Perspective.h:247