Salome HOME
00245d70e0f0f26370957a44fe4c4e45c60671c3
[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
51 /**\class XGUI_Workshop
52  * \ingroup GUI
53  * \brief Class which defines a configuration of the application (Workshop) and launches it.
54  */
55 class XGUI_EXPORT XGUI_Workshop : public QObject
56 {
57 Q_OBJECT
58  public:
59   /// Constructor
60   /// \param theConnector a Salome connector object. 
61   /// Used only if the workshop is launched in Salome environment
62   XGUI_Workshop(XGUI_SalomeConnector* theConnector = 0);
63   virtual ~XGUI_Workshop();
64
65   //! Starting of the application
66   void startApplication();
67
68   /// Activates the module controls. Should be called after module creation
69   void activateModule();
70
71   /// Deactivates the module controls. Should be called after module creation
72   void deactivateModule();
73
74 #ifndef HAVE_SALOME
75   //! Returns main window (Desktop) of the application
76   AppElements_MainWindow* mainWindow() const
77   {
78     return myMainWindow;
79   }
80
81   //! Creates and adds a new workbench (menu group) with the given name and returns it
82   AppElements_Workbench* addWorkbench(const QString& theName);
83 #endif
84
85   //! Returns selection manager object
86   XGUI_SelectionMgr* selector() const
87   {
88     return mySelector;
89   }
90
91   //! Returns displayer
92   XGUI_Displayer* displayer() const
93   {
94     return myDisplayer;
95   }
96
97   //! ! Returns operation manager.
98   XGUI_OperationMgr* operationMgr() const
99   {
100     return myOperationMgr;
101   }
102
103   //! ! Returns error manager.
104   XGUI_ErrorMgr* errorMgr() const
105   {
106     return myErrorMgr;
107   }
108
109   //! ! Returns an actions manager
110   XGUI_ActionsMgr* actionsMgr() const
111   {
112     return myActionsMgr;
113   }
114
115   //! Returns property panel widget
116   XGUI_PropertyPanel* propertyPanel() const
117   {
118     return myPropertyPanel;
119   }
120
121   //! Returns context menu manager object
122   XGUI_ContextMenuMgr* contextMenuMgr() const
123   {
124     return myContextMenuMgr;
125   }
126
127   //! Returns an object which provides interface to Salome Module (LightApp_Module)
128   XGUI_SalomeConnector* salomeConnector() const
129   {
130     return mySalomeConnector;
131   }
132
133   //! Provides an object which provides interface to Salome Viewer
134   ModuleBase_IViewer* salomeViewer() const;
135
136   //! Returns true if the application works as SALOME module
137   bool isSalomeMode() const
138   {
139     return mySalomeConnector != 0;
140   }
141
142   //! Returns Object browser
143   XGUI_ObjectsBrowser* objectBrowser() const
144   {
145     return myObjectBrowser;
146   }
147
148   /// This method is called by Salome module when selection is changed
149   void salomeViewerSelectionChanged();
150
151   /// Returns viewer which unifies access as to Salome viewer as to standalone viewer
152   XGUI_ViewerProxy* viewer() const
153   {
154     return myViewerProxy;
155   }
156
157   /// Returns the module connector
158   /// \returns the instance of connector
159   XGUI_ModuleConnector* moduleConnector() const
160   {
161     return myModuleConnector;
162   }
163
164   /// Returns a desktop
165   /// \return a desktop instance
166   QMainWindow* desktop() const;
167
168   //! Delete features
169   void deleteObjects();
170
171   //! Searches for selected features unused in other (not selected) features. If one or several
172   //! selected features are found, a warning message proposes to delete them. It contains 
173   //! the list of features to be deleted.
174   void cleanHistory();
175
176   //! Returns true if the selected feature can be moved to the position after the current feature
177   //! \return boolean value
178   bool canMoveFeature();
179
180   //! Move selected features to be after the current feature
181   void moveObjects();
182
183   //! Returns true if the object can be shaded. If the object is a compsolid result, the method
184   //! checks subobjects of the result
185   //! \return boolean value
186   bool canBeShaded(const ObjectPtr& theObject) const;
187
188   //! Returns true if there is at least one selected body/construction/group result
189   //! \return boolean value
190   bool canChangeColor() const;
191
192   //! Change color of the features if it is possible
193   //! The operation is available for construction, body and group results
194   //! theObjects a list of selected objects
195   void changeColor(const QObjectPtrList& theObjects);
196
197   //! Show the given features in 3d Viewer
198   void showObjects(const QObjectPtrList& theList, bool isVisible);
199
200   //! Show the given features in 3d Viewer
201   void showOnlyObjects(const QObjectPtrList& theList);
202
203   /// Set display mode for the given objects
204   /// \param theList a list of displayed objects
205   /// \param theMode a mode to set (see \ref XGUI_Displayer)
206   void setDisplayMode(const QObjectPtrList& theList, int theMode);
207
208   /// Returns current module
209   ModuleBase_IModule* module() const
210   {
211     return myModule;
212   }
213
214   /// Returns current directory which contains data files
215   QString currentDataDir() const { return myCurrentDir; }
216
217   /// Returns current directory which contains data files
218   void setCurrentDataDir(const QString& theDir) { myCurrentDir = theDir; }
219
220   /**
221   * Save the current document into a directory
222   * \param theName - path to the directory
223   * \param theFileNames - returned file names created in this directory
224   */
225   void saveDocument(const QString& theName, std::list<std::string>& theFileNames);
226
227   /**
228    * If there is an active (uncommitted) operation shows a prompt to abort it
229    * and performs abortion if user agreed. Returns true if
230    * - operation aborted successfully
231    * - there is no active operation
232    */
233   bool abortAllOperations();
234
235   //! Delete features. Delete the referenced features. There can be a question with a list of
236   //! referenced objects.
237   //! \param theList an objects to be deleted
238   //! \param theIgnoredFeatures a list of features to be ignored during delete
239   bool deleteFeatures(const QObjectPtrList& theFeatures,
240                       const std::set<FeaturePtr>& theIgnoredFeatures = std::set<FeaturePtr>());
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   /// A constant string used for "Move to end" command definition
262   /// It is used for specific processing of Undo/Redo for this command.
263   static QString MOVE_TO_END_COMMAND;
264
265 signals:
266   /// Emitted when selection happens in Salome viewer
267   void salomeViewerSelection();
268
269   /// Emitted when error in application happens
270   void errorOccurred(const QString&);
271
272   //! the signal about the workshop actions states are updated.
273   void commandStatusUpdated();
274
275   //! the application is started
276   void applicationStarted();
277
278   //! Signal to update Undo history list
279   void updateUndoHistory(const QList<ActionInfo>&);
280
281   //! Signal to update Redo history list
282   void updateRedoHistory(const QList<ActionInfo>&);
283
284  public slots:
285    /// Update of commands status
286   void updateCommandStatus();
287
288   /// update history list (undo/redo commands)
289   void updateHistory();
290
291   /// Save current document
292   bool onSave();
293
294   /// Save current document to a file selected by user
295   bool onSaveAs();
296
297   /// Undo last command
298   void onUndo(int times = 1);
299
300   /// Redo previous command
301   void onRedo(int times = 1);
302
303   // Rebuild data tree
304   //void onRebuild();
305
306   /// Validates the operation to change the "Apply" button state.
307   /// \param thePreviousState the previous state of the widget
308   void onWidgetStateChanged(int thePreviousState);
309
310   /// Calls activate of the current widget again. It is possible that selection filters are
311   /// differs in the new state of paged container
312   void onValuesChanged();
313
314   /// Show property panel
315   void showPropertyPanel();
316
317   /// Hide property panel
318   void hidePropertyPanel();
319
320   /// Show object Browser
321   void showObjectBrowser();
322
323   /// Hide object Browser
324   void hideObjectBrowser();
325
326   /// Close document
327   void closeDocument();
328
329   /// Open document from file
330   void onOpen();
331
332   /// Create a new document
333   void onNew();
334
335 #ifndef HAVE_SALOME
336   /// Exit application
337   void onExit();
338
339   /// Open preferences dialog box
340   void onPreferences();
341 #endif
342
343   /// Activates/deactivates the trihedron in the viewer AIS context
344   void onTrihedronVisibilityChanged(bool theState);
345
346  protected:
347   /// Sets the granted operations for the parameter operation. Firstly, it finds the nested features
348   /// and set them into the operation. Secondly, it asks the module about ids of granted operations.
349   /// \param theOperation an operation
350    void setGrantedFeatures(ModuleBase_Operation* theOperation);
351
352   /// Update the property panel content by the XML description of the operation and set the panel
353   /// into the operation
354   /// \param theOperation an operation
355   void setPropertyPanel(ModuleBase_Operation* theOperation);
356
357   /// Connects or disconnects to the value changed signal of the property panel widgets
358   /// \param isToConnect a boolean value whether connect or disconnect
359   void connectToPropertyPanel(const bool isToConnect);
360
361   //! Find all referenced features. Return direct and indirect lists of referenced object
362   //! \param theList an objects to be checked
363   //! \param aDirectRefFeatures a list of direct reference features
364   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
365   void findReferences(const QObjectPtrList& theList,
366                       std::set<FeaturePtr>& aDirectRefFeatures,
367                       std::set<FeaturePtr>& aIndirectRefFeatures);
368
369   //! Shows a dialog box about references. Ask whether they should be also removed.
370   //! \param theList an objects to be checked
371   //! \param aDirectRefFeatures a list of direct reference features
372   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
373   //! \param theParent a parent widget for the question message box
374   //! \param doDeleteReferences if there are parameters between features, ask if they should be
375   //! replaced to their meaning without corresponded features remove
376   //! \return true if in message box answer is Yes
377   bool isDeleteFeatureWithReferences(const QObjectPtrList& theList,
378                                      const std::set<FeaturePtr>& aDirectRefFeatures,
379                                      const std::set<FeaturePtr>& aIndirectRefFeatures,
380                                      QWidget* theParent,
381                                      bool& doDeleteReferences);
382
383   //! \param theIgnoredFeatures a list of features to be ignored during delete
384   //! \param theList an objects to be checked
385   //! \param aDirectRefFeatures a list of direct reference features
386   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
387   //! \param doDeleteReferences flag if referenced features should be removed also
388   bool deleteFeaturesInternal(const QObjectPtrList& theList,
389                               const std::set<FeaturePtr>& aDirectRefFeatures,
390                               const std::set<FeaturePtr>& aIndirectRefFeatures,
391                               const std::set<FeaturePtr>& theIgnoredFeatures,
392                               const bool doDeleteReferences = true);
393
394 private:
395   /// Display all results
396   //void displayAllResults();
397
398   /// Display results from document
399   /// \param theDoc a document
400   void displayDocumentResults(DocumentPtr theDoc);
401
402   /// Display results from a group
403   void displayGroupResults(DocumentPtr theDoc, std::string theGroup);
404
405  private slots:
406   /// SLOT, that is called after the operation is started. Update workshop state according to
407   /// the started operation, e.g. visualizes the property panel and connect to it.
408   /// \param theOpertion a started operation
409   void onOperationStarted(ModuleBase_Operation* theOperation);
410
411   /// SLOT, that is called after the operation is resumed. Update workshop state according to
412   /// the started operation, e.g. visualizes the property panel and connect to it.
413   /// \param theOpertion a resumed operation
414   virtual void onOperationResumed(ModuleBase_Operation* theOperation);
415
416   /// SLOT, that is called after the operation is stopped. Update workshop state, e.g.
417   /// hides the property panel and update the command status.
418   /// \param theOpertion a stopped operation
419   virtual void onOperationStopped(ModuleBase_Operation* theOperation);
420
421   /// SLOT, that is called after the operation is committed.
422   /// \param theOpertion a committed operation
423   virtual void onOperationCommitted(ModuleBase_Operation* theOperation);
424
425   /// SLOT, that is called after the operation is aborted.
426   /// \param theOpertion an aborted operation
427   void onOperationAborted(ModuleBase_Operation* theOperation);
428
429   /// Slot, which reacts to the context popup menu call
430   /// \param theId the data value of the clicked action
431   /// \param isChecked a state of toggle if the action is checkable
432   void onContextMenuCommand(const QString& theId, bool isChecked);
433
434   /// Set waiting cursor
435   void onStartWaiting();
436
437   /// Called by Ok button clicked in the property panel. Asks the error manager whether
438   /// the operation can be committed and do it if it returns true.
439   void onAcceptActionClicked();
440
441  private:
442    /// Init menu
443   void initMenu();
444
445   /// Register validators
446   void registerValidators() const;
447
448   /// Load module from external library
449   /// \param theModule name of the module
450   ModuleBase_IModule* loadModule(const QString& theModule);
451
452   /// Create module
453   bool createModule();
454
455   /// Create object browser widget
456   /// \param theParent a parent of widget
457   QDockWidget* createObjectBrowser(QWidget* theParent);
458
459   /// Create property panel widget
460   /// \param theParent a parent of widget
461   QDockWidget* createPropertyPanel(QWidget* theParent);
462
463   // Creates Dock widgets: Object browser and Property panel
464   void createDockWidgets();
465
466   //! Extends undo/redo toolbutton's with history menu
467   //! \param theObject - in the OpenParts it is a QToolButton by itself,
468   //! in salome mode - QAction that creates a button.
469   //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
470   //! \param theSlot - onUndo(int) or onRedo(int) SLOT
471   void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
472
473
474   /// Calls removeFeature of the document for each object in the list
475   //! \param theList an objects to be deleted
476   //! \param theIgnoredFeatures a list of features to be ignored during delete
477   //! \param theActionId an action command key to find context menu object types enabled for remove
478   bool removeFeatures(const QObjectPtrList& theList,
479                       const std::set<FeaturePtr>& theIgnoredFeatures,
480                       const QString& theActionId);
481
482   //! Creates list of actions (commands) by given history list from session
483   QList<ActionInfo> processHistoryList(const std::list<std::string>&) const;
484
485 private:
486 #ifndef HAVE_SALOME
487   AppElements_MainWindow* myMainWindow;
488 #endif
489
490   ModuleBase_IModule* myModule;
491   XGUI_ErrorMgr* myErrorMgr;
492   XGUI_ObjectsBrowser* myObjectBrowser;
493   XGUI_PropertyPanel* myPropertyPanel;
494   XGUI_SelectionMgr* mySelector;
495   XGUI_Displayer* myDisplayer;
496   XGUI_OperationMgr* myOperationMgr;  ///< manager to manipulate through the operations
497   XGUI_ActionsMgr* myActionsMgr;
498   XGUI_SalomeConnector* mySalomeConnector;
499   XGUI_ErrorDialog* myErrorDlg;
500   XGUI_ViewerProxy* myViewerProxy;
501   XGUI_ContextMenuMgr* myContextMenuMgr;
502   XGUI_ModuleConnector* myModuleConnector;
503   XGUI_WorkshopListener* myEventsListener;
504
505   QString myCurrentDir;
506 };
507
508 #endif