Salome HOME
Merge branch 'Dev_1.5.0' into BR_REENTRANCE_OPERATION
[modules/shaper.git] / src / XGUI / XGUI_Workshop.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #ifndef XGUI_WORKSHOP_H
4 #define XGUI_WORKSHOP_H
5
6 #include "XGUI.h"
7 //#include "XGUI_Constants.h"
8 #include <ModuleBase_Definitions.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Feature.h>
11
12 #include <ModuleBase_ActionInfo.h>
13
14 #include <QObject>
15 #include <QMap>
16 #include <QKeySequence>
17 #include <QIcon>
18
19 #ifndef HAVE_SALOME
20 class AppElements_MainWindow;
21 class AppElements_Command;
22 class AppElements_Workbench;
23 #endif
24
25 class XGUI_ActionsMgr;
26 class XGUI_ContextMenuMgr;
27 class XGUI_Displayer;
28 class XGUI_ErrorDialog;
29 class XGUI_ErrorMgr;
30 class XGUI_ModuleConnector;
31 class XGUI_ObjectsBrowser;
32 class XGUI_OperationMgr;
33 class XGUI_PropertyPanel;
34 class XGUI_SalomeConnector;
35 class XGUI_SalomeViewer;
36 class XGUI_SelectionMgr;
37 class XGUI_ViewerProxy;
38 class XGUI_WorkshopListener;
39
40 class ModuleBase_IModule;
41 class ModuleBase_IViewer;
42 class ModuleBase_Operation;
43
44 class QWidget;
45 class QDockWidget;
46 class QMainWindow;
47
48 class QAction;
49
50 /**\class XGUI_Workshop
51  * \ingroup GUI
52  * \brief Class which defines a configuration of the application (Workshop) and launches it.
53  */
54 class XGUI_EXPORT XGUI_Workshop : public QObject
55 {
56 Q_OBJECT
57  public:
58   /// Constructor
59   /// \param theConnector a Salome connector object. 
60   /// Used only if the workshop is launched in Salome environment
61   XGUI_Workshop(XGUI_SalomeConnector* theConnector = 0);
62   virtual ~XGUI_Workshop();
63
64   //! Starting of the application
65   void startApplication();
66
67   /// Activates the module controls. Should be called after module creation
68   void activateModule();
69
70   /// Deactivates the module controls. Should be called after module creation
71   void deactivateModule();
72
73 #ifndef HAVE_SALOME
74   //! Returns main window (Desktop) of the application
75   AppElements_MainWindow* mainWindow() const
76   {
77     return myMainWindow;
78   }
79
80   //! Creates and adds a new workbench (menu group) with the given name and returns it
81   AppElements_Workbench* addWorkbench(const QString& theName);
82 #endif
83
84   //! Returns selection manager object
85   XGUI_SelectionMgr* selector() const
86   {
87     return mySelector;
88   }
89
90   //! Returns displayer
91   XGUI_Displayer* displayer() const
92   {
93     return myDisplayer;
94   }
95
96   //! ! Returns operation manager.
97   XGUI_OperationMgr* operationMgr() const
98   {
99     return myOperationMgr;
100   }
101
102   //! ! Returns error manager.
103   XGUI_ErrorMgr* errorMgr() const
104   {
105     return myErrorMgr;
106   }
107
108   //! ! Returns an actions manager
109   XGUI_ActionsMgr* actionsMgr() const
110   {
111     return myActionsMgr;
112   }
113
114   //! Returns property panel widget
115   XGUI_PropertyPanel* propertyPanel() const
116   {
117     return myPropertyPanel;
118   }
119
120   //! Returns context menu manager object
121   XGUI_ContextMenuMgr* contextMenuMgr() const
122   {
123     return myContextMenuMgr;
124   }
125
126   //! Returns an object which provides interface to Salome Module (LightApp_Module)
127   XGUI_SalomeConnector* salomeConnector() const
128   {
129     return mySalomeConnector;
130   }
131
132   //! Provides an object which provides interface to Salome Viewer
133   ModuleBase_IViewer* salomeViewer() const;
134
135   //! Returns true if the application works as SALOME module
136   bool isSalomeMode() const
137   {
138     return mySalomeConnector != 0;
139   }
140
141   //! Returns Object browser
142   XGUI_ObjectsBrowser* objectBrowser() const
143   {
144     return myObjectBrowser;
145   }
146
147   /// This method is called by Salome module when selection is changed
148   void salomeViewerSelectionChanged();
149
150   /// Returns viewer which unifies access as to Salome viewer as to standalone viewer
151   XGUI_ViewerProxy* viewer() const
152   {
153     return myViewerProxy;
154   }
155
156   /// Returns the module connector
157   /// \returns the instance of connector
158   XGUI_ModuleConnector* moduleConnector() const
159   {
160     return myModuleConnector;
161   }
162
163   /// Returns a desktop
164   /// \return a desktop instance
165   QMainWindow* desktop() const;
166
167   //! Delete features
168   void deleteObjects();
169
170   //! Returns true if the selected feature can be moved to the position after the current feature
171   //! \return boolean value
172   bool canMoveFeature();
173
174   //! Move selected features to be after the current feature
175   void moveObjects();
176
177   //! Returns true if the object can be shaded. If the object is a compsolid result, the method
178   //! checks subobjects of the result
179   //! \return boolean value
180   bool canBeShaded(const ObjectPtr& theObject) const;
181
182   //! Returns true if there is at least one selected body/construction/group result
183   //! \return boolean value
184   bool canChangeColor() const;
185
186   //! Change color of the features if it is possible
187   //! The operation is available for construction, body and group results
188   //! theObjects a list of selected objects
189   void changeColor(const QObjectPtrList& theObjects);
190
191   //! Show the given features in 3d Viewer
192   void showObjects(const QObjectPtrList& theList, bool isVisible);
193
194   //! Show the given features in 3d Viewer
195   void showOnlyObjects(const QObjectPtrList& theList);
196
197   /// Set display mode for the given objects
198   /// \param theList a list of displayed objects
199   /// \param theMode a mode to set (see \ref XGUI_Displayer)
200   void setDisplayMode(const QObjectPtrList& theList, int theMode);
201
202   /// Returns current module
203   ModuleBase_IModule* module() const
204   {
205     return myModule;
206   }
207
208   /// Returns current directory which contains data files
209   QString currentDataDir() const { return myCurrentDir; }
210
211   /// Returns current directory which contains data files
212   void setCurrentDataDir(const QString& theDir) { myCurrentDir = theDir; }
213
214   /**
215   * Save the current document into a directory
216   * \param theName - path to the directory
217   * \param theFileNames - returned file names created in this directory
218   */
219   void saveDocument(const QString& theName, std::list<std::string>& theFileNames);
220
221   /**
222    * If there is an active (uncommitted) operation shows a prompt to abort it
223    * and performs abortion if user agreed. Returns true if
224    * - operation aborted successfully
225    * - there is no active operation
226    */
227   bool abortAllOperations();
228
229   //! Delete features. Delete the referenced features. There can be a question with a list of referenced
230   //! objects.
231   //! \param theList an objects to be deleted
232   //! \param theIgnoredFeatures a list of features to be ignored during delete
233   //! \param theParent a parent widget for the question message box
234   //! \param theAskAboutDeleteReferences if true, the message box with a list of references to the
235   //! objects features appear. If the user chose do not continue, the deletion is not performed
236   //! \return the success of the delete 
237   bool deleteFeatures(const QObjectPtrList& theList,
238                       const std::set<FeaturePtr>& theIgnoredFeatures = std::set<FeaturePtr>(),
239                       QWidget* theParent = 0,
240                       const bool theAskAboutDeleteReferences = false);
241
242   /// Deactivates the object, if it is active and the module returns that the activation
243   /// of selection for the object is not possible currently(the current operation uses it)
244   /// \param theObject an object
245   /// \param theUpdateViewer a boolean flag to update viewer immediately
246   void deactivateActiveObject(const ObjectPtr& theObject, const bool theUpdateViewer);
247
248   /// Returns true if the action of the feature is created to contain Accept/Cancel button
249   /// \param theFeature a feature
250   bool isFeatureOfNested(const FeaturePtr& theFeature);
251
252   /// Has to be called in order to display objects with visibility status = true
253   void synchronizeViewer();
254
255   /// Has to be called in order to display objects from a cpecifed group with visibility status = true
256   /// \param theDoc the document for objects synchronisation
257   /// \param theGroup the group name
258   /// \param theUpdateViewer update viewer flag
259   void synchronizeGroupInViewer(const DocumentPtr& theDoc, const std::string& theGroup, bool theUpdateViewer);
260
261 signals:
262   /// Emitted when selection happens in Salome viewer
263   void salomeViewerSelection();
264
265   /// Emitted when error in application happens
266   void errorOccurred(const QString&);
267
268   //! the signal about the workshop actions states are updated.
269   void commandStatusUpdated();
270
271   //! the application is started
272   void applicationStarted();
273
274   //! Signal to update Undo history list
275   void updateUndoHistory(const QList<ActionInfo>&);
276
277   //! Signal to update Redo history list
278   void updateRedoHistory(const QList<ActionInfo>&);
279
280  public slots:
281    /// Update of commands status
282   void updateCommandStatus();
283
284   /// update history list (undo/redo commands)
285   void updateHistory();
286
287   /// Save current document
288   bool onSave();
289
290   /// Save current document to a file selected by user
291   bool onSaveAs();
292
293   /// Undo last command
294   void onUndo(int times = 1);
295
296   /// Redo previous command
297   void onRedo(int times = 1);
298
299   /// Rebuild data tree
300   void onRebuild();
301
302   /// Validates the operation to change the "Apply" button state.
303   /// \param thePreviousState the previous state of the widget
304   void onWidgetStateChanged(int thePreviousState);
305
306   /// Show property panel
307   void showPropertyPanel();
308
309   /// Hide property panel
310   void hidePropertyPanel();
311
312   /// Show object Browser
313   void showObjectBrowser();
314
315   /// Hide object Browser
316   void hideObjectBrowser();
317
318   /// Close document
319   void closeDocument();
320
321   /// Open document from file
322   void onOpen();
323
324   /// Create a new document
325   void onNew();
326
327 #ifndef HAVE_SALOME
328   /// Exit application
329   void onExit();
330
331   /// Open preferences dialog box
332   void onPreferences();
333 #endif
334
335  protected:
336   /// Sets the granted operations for the parameter operation. Firstly, it finds the nested features
337   /// and set them into the operation. Secondly, it asks the module about ids of granted operations.
338   /// \param theOperation an operation
339    void setGrantedFeatures(ModuleBase_Operation* theOperation);
340
341   /// Update the property panel content by the XML description of the operation and set the panel
342   /// into the operation
343   /// \param theOperation an operation
344   void setPropertyPanel(ModuleBase_Operation* theOperation);
345
346   /// Connects or disconnects to the value changed signal of the property panel widgets
347   /// \param isToConnect a boolean value whether connect or disconnect
348   void connectToPropertyPanel(const bool isToConnect);
349
350 private:
351   /// Display all results
352   //void displayAllResults();
353
354   /// Display results from document
355   /// \param theDoc a document
356   void displayDocumentResults(DocumentPtr theDoc);
357
358   /// Display results from a group
359   void displayGroupResults(DocumentPtr theDoc, std::string theGroup);
360
361  private slots:
362   /// SLOT, that is called after the operation is started. Update workshop state according to
363   /// the started operation, e.g. visualizes the property panel and connect to it.
364   /// \param theOpertion a started operation
365   void onOperationStarted(ModuleBase_Operation* theOperation);
366
367   /// SLOT, that is called after the operation is resumed. Update workshop state according to
368   /// the started operation, e.g. visualizes the property panel and connect to it.
369   /// \param theOpertion a resumed operation
370   virtual void onOperationResumed(ModuleBase_Operation* theOperation);
371
372   /// SLOT, that is called after the operation is stopped. Update workshop state, e.g.
373   /// hides the property panel and update the command status.
374   /// \param theOpertion a stopped operation
375   virtual void onOperationStopped(ModuleBase_Operation* theOperation);
376
377   /// SLOT, that is called after the operation is committed.
378   /// \param theOpertion a committed operation
379   virtual void onOperationCommitted(ModuleBase_Operation* theOperation);
380
381   /// SLOT, that is called after the operation is aborted.
382   /// \param theOpertion an aborted operation
383   void onOperationAborted(ModuleBase_Operation* theOperation);
384
385   /// Slot, which reacts to the context popup menu call
386   /// \param theId the data value of the clicked action
387   /// \param isChecked a state of toggle if the action is checkable
388   void onContextMenuCommand(const QString& theId, bool isChecked);
389
390   /// Set waiting cursor
391   void onStartWaiting();
392
393   /// Called by Ok button clicked in the property panel. Asks the error manager whether
394   /// the operation can be committed and do it if it returns true.
395   void onAcceptActionClicked();
396
397   /// Listens the corresponded signal from operation manager and send it with the Ok
398   /// action to operation manager.
399   /// \param theEnabled an enabled state for the action
400   //void onValidationStateChanged(bool theEnabled);
401
402   //connect(myOperationMgr,  SIGNAL(validationStateChanged(bool)),
403   //        aOkAct,          SLOT(setEnabled(bool)));
404
405
406  private:
407    /// Init menu
408   void initMenu();
409
410   /// Register validators
411   void registerValidators() const;
412
413   /// Load module from external library
414   /// \param theModule name of the module
415   ModuleBase_IModule* loadModule(const QString& theModule);
416
417   /// Create module
418   bool createModule();
419
420   /// Create object browser widget
421   /// \param theParent a parent of widget
422   QDockWidget* createObjectBrowser(QWidget* theParent);
423
424   /// Create property panel widget
425   /// \param theParent a parent of widget
426   QDockWidget* createPropertyPanel(QWidget* theParent);
427
428   // Creates Dock widgets: Object browser and Property panel
429   void createDockWidgets();
430
431   //! Extends undo/redo toolbutton's with history menu
432   //! \param theObject - in the OpenParts it is a QToolButton by itself,
433   //! in salome mode - QAction that creates a button.
434   //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
435   //! \param theSlot - onUndo(int) or onRedo(int) SLOT
436   void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
437
438   //! Creates list of actions (commands) by given history list from session
439   QList<ActionInfo> processHistoryList(const std::list<std::string>&) const;
440
441 private:
442 #ifndef HAVE_SALOME
443   AppElements_MainWindow* myMainWindow;
444 #endif
445
446   ModuleBase_IModule* myModule;
447   XGUI_ErrorMgr* myErrorMgr;
448   XGUI_ObjectsBrowser* myObjectBrowser;
449   XGUI_PropertyPanel* myPropertyPanel;
450   XGUI_SelectionMgr* mySelector;
451   XGUI_Displayer* myDisplayer;
452   XGUI_OperationMgr* myOperationMgr;  ///< manager to manipulate through the operations
453   XGUI_ActionsMgr* myActionsMgr;
454   XGUI_SalomeConnector* mySalomeConnector;
455   XGUI_ErrorDialog* myErrorDlg;
456   XGUI_ViewerProxy* myViewerProxy;
457   XGUI_ContextMenuMgr* myContextMenuMgr;
458   XGUI_ModuleConnector* myModuleConnector;
459   XGUI_WorkshopListener* myEventsListener;
460
461   QString myCurrentDir;
462 };
463
464 #endif