Salome HOME
187182e6686dc30e3a4f2f2ad3aeafe217d5d520
[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   /// Set selection mode in viewer. If theMode=-1 then activate default mode
209   /// \param theMode the selection mode (according to TopAbs_ShapeEnum)
210   void setViewerSelectionMode(int theMode);
211
212   /// Activates current selection mode for the given list of objects
213   void activateObjectsSelection(const QObjectPtrList& theList);
214
215   /// Returns current module
216   ModuleBase_IModule* module() const
217   {
218     return myModule;
219   }
220
221   /// Returns current directory which contains data files
222   QString currentDataDir() const { return myCurrentDir; }
223
224   /// Returns current directory which contains data files
225   void setCurrentDataDir(const QString& theDir) { myCurrentDir = theDir; }
226
227   /**
228   * Save the current document into a directory
229   * \param theName - path to the directory
230   * \param theFileNames - returned file names created in this directory
231   */
232   void saveDocument(const QString& theName, std::list<std::string>& theFileNames);
233
234   /**
235    * If there is an active (uncommitted) operation shows a prompt to abort it
236    * and performs abortion if user agreed. Returns true if
237    * - operation aborted successfully
238    * - there is no active operation
239    */
240   bool abortAllOperations();
241
242   //! Delete features. Delete the referenced features. There can be a question with a list of
243   //! referenced objects.
244   //! \param theList an objects to be deleted
245   //! \param theIgnoredFeatures a list of features to be ignored during delete
246   bool deleteFeatures(const QObjectPtrList& theFeatures,
247                       const std::set<FeaturePtr>& theIgnoredFeatures = std::set<FeaturePtr>());
248
249   /// Deactivates the object, if it is active and the module returns that the activation
250   /// of selection for the object is not possible currently(the current operation uses it)
251   /// \param theObject an object
252   /// \param theUpdateViewer a boolean flag to update viewer immediately
253   void deactivateActiveObject(const ObjectPtr& theObject, const bool theUpdateViewer);
254
255   /// Returns true if the action of the feature is created to contain Accept/Cancel button
256   /// \param theFeature a feature
257   bool isFeatureOfNested(const FeaturePtr& theFeature);
258
259   /// Has to be called in order to display objects with visibility status = true
260   void synchronizeViewer();
261
262   /// Has to be called in order to display objects from a cpecifed group with visibility status = true
263   /// \param theDoc the document for objects synchronisation
264   /// \param theGroup the group name
265   /// \param theUpdateViewer update viewer flag
266   void synchronizeGroupInViewer(const DocumentPtr& theDoc, const std::string& theGroup, bool theUpdateViewer);
267
268   /// Update the property panel content by the XML description of the operation and set the panel
269   /// into the operation
270   /// \param theOperation an operation
271   void setPropertyPanel(ModuleBase_Operation* theOperation);
272
273   /// Connects or disconnects to the value changed signal of the property panel widgets
274   /// \param isToConnect a boolean value whether connect or disconnect
275   void connectToPropertyPanel(const bool isToConnect);
276
277   /// A constant string used for "Move to end" command definition
278   /// It is used for specific processing of Undo/Redo for this command.
279   static QString MOVE_TO_END_COMMAND;
280
281 signals:
282   /// Emitted when selection happens in Salome viewer
283   void salomeViewerSelection();
284
285   /// Emitted when error in application happens
286   void errorOccurred(const QString&);
287
288   //! the signal about the workshop actions states are updated.
289   void commandStatusUpdated();
290
291   //! the application is started
292   void applicationStarted();
293
294   //! Signal to update Undo history list
295   void updateUndoHistory(const QList<ActionInfo>&);
296
297   //! Signal to update Redo history list
298   void updateRedoHistory(const QList<ActionInfo>&);
299
300  public slots:
301    /// Update of commands status
302   void updateCommandStatus();
303
304   /// update history list (undo/redo commands)
305   void updateHistory();
306
307   /// Save current document
308   bool onSave();
309
310   /// Save current document to a file selected by user
311   bool onSaveAs();
312
313   /// Undo last command
314   void onUndo(int times = 1);
315
316   /// Redo previous command
317   void onRedo(int times = 1);
318
319   // Rebuild data tree
320   //void onRebuild();
321
322   /// Validates the operation to change the "Apply" button state.
323   /// \param thePreviousState the previous state of the widget
324   void onWidgetStateChanged(int thePreviousState);
325
326   /// Calls activate of the current widget again. It is possible that selection filters are
327   /// differs in the new state of paged container
328   void onValuesChanged();
329
330   /// Show property panel
331   void showPropertyPanel();
332
333   /// Hide property panel
334   void hidePropertyPanel();
335
336   /// Show object Browser
337   void showObjectBrowser();
338
339   /// Hide object Browser
340   void hideObjectBrowser();
341
342   /// Close document
343   void closeDocument();
344
345   /// Open document from file
346   void onOpen();
347
348   /// Create a new document
349   void onNew();
350
351 #ifndef HAVE_SALOME
352   /// Exit application
353   void onExit();
354
355   /// Open preferences dialog box
356   void onPreferences();
357 #endif
358
359   /// Activates/deactivates the trihedron in the viewer AIS context
360   void onTrihedronVisibilityChanged(bool theState);
361
362   /// Returns defailt selection mode in 3d viewer
363   int viewerSelectionMode() const { return myViewerSelMode; }
364
365
366  protected:
367   /// Sets the granted operations for the parameter operation. Firstly, it finds the nested features
368   /// and set them into the operation. Secondly, it asks the module about ids of granted operations.
369   /// \param theOperation an operation
370    void setGrantedFeatures(ModuleBase_Operation* theOperation);
371
372   //! Find all referenced features. Return direct and indirect lists of referenced object
373   //! \param theList an objects to be checked
374   //! \param aDirectRefFeatures a list of direct reference features
375   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
376   void findReferences(const QObjectPtrList& theList,
377                       std::set<FeaturePtr>& aDirectRefFeatures,
378                       std::set<FeaturePtr>& aIndirectRefFeatures);
379
380   //! Shows a dialog box about references. Ask whether they should be also removed.
381   //! \param theList an objects to be checked
382   //! \param aDirectRefFeatures a list of direct reference features
383   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
384   //! \param theParent a parent widget for the question message box
385   //! \param doDeleteReferences if there are parameters between features, ask if they should be
386   //! replaced to their meaning without corresponded features remove
387   //! \return true if in message box answer is Yes
388   bool isDeleteFeatureWithReferences(const QObjectPtrList& theList,
389                                      const std::set<FeaturePtr>& aDirectRefFeatures,
390                                      const std::set<FeaturePtr>& aIndirectRefFeatures,
391                                      QWidget* theParent,
392                                      bool& doDeleteReferences);
393
394   //! \param theIgnoredFeatures a list of features to be ignored during delete
395   //! \param theList an objects to be checked
396   //! \param aDirectRefFeatures a list of direct reference features
397   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
398   //! \param doDeleteReferences flag if referenced features should be removed also
399   bool deleteFeaturesInternal(const QObjectPtrList& theList,
400                               const std::set<FeaturePtr>& aDirectRefFeatures,
401                               const std::set<FeaturePtr>& aIndirectRefFeatures,
402                               const std::set<FeaturePtr>& theIgnoredFeatures,
403                               const bool doDeleteReferences = true);
404
405 private:
406   /// Display all results
407   //void displayAllResults();
408
409   /// Display results from document
410   /// \param theDoc a document
411   void displayDocumentResults(DocumentPtr theDoc);
412
413   /// Display results from a group
414   void displayGroupResults(DocumentPtr theDoc, std::string theGroup);
415
416  private slots:
417   /// SLOT, that is called after the operation is started. Update workshop state according to
418   /// the started operation, e.g. visualizes the property panel and connect to it.
419   /// \param theOpertion a started operation
420   void onOperationStarted(ModuleBase_Operation* theOperation);
421
422   /// SLOT, that is called after the operation is resumed. Update workshop state according to
423   /// the started operation, e.g. visualizes the property panel and connect to it.
424   /// \param theOpertion a resumed operation
425   virtual void onOperationResumed(ModuleBase_Operation* theOperation);
426
427   /// SLOT, that is called after the operation is stopped. Update workshop state, e.g.
428   /// hides the property panel and update the command status.
429   /// \param theOpertion a stopped operation
430   virtual void onOperationStopped(ModuleBase_Operation* theOperation);
431
432   /// SLOT, that is called after the operation is committed.
433   /// \param theOpertion a committed operation
434   virtual void onOperationCommitted(ModuleBase_Operation* theOperation);
435
436   /// SLOT, that is called after the operation is aborted.
437   /// \param theOpertion an aborted operation
438   void onOperationAborted(ModuleBase_Operation* theOperation);
439
440   /// Slot, which reacts to the context popup menu call
441   /// \param theId the data value of the clicked action
442   /// \param isChecked a state of toggle if the action is checkable
443   void onContextMenuCommand(const QString& theId, bool isChecked);
444
445   /// Set waiting cursor
446   void onStartWaiting();
447
448   /// Called by Ok button clicked in the property panel. Asks the error manager whether
449   /// the operation can be committed and do it if it returns true.
450   void onAcceptActionClicked();
451
452  private:
453    /// Init menu
454   void initMenu();
455
456   /// Register validators
457   void registerValidators() const;
458
459   /// Load module from external library
460   /// \param theModule name of the module
461   ModuleBase_IModule* loadModule(const QString& theModule);
462
463   /// Create module
464   bool createModule();
465
466   /// Create object browser widget
467   /// \param theParent a parent of widget
468   QDockWidget* createObjectBrowser(QWidget* theParent);
469
470   /// Create property panel widget
471   /// \param theParent a parent of widget
472   QDockWidget* createPropertyPanel(QWidget* theParent);
473
474   // Creates Dock widgets: Object browser and Property panel
475   void createDockWidgets();
476
477   //! Extends undo/redo toolbutton's with history menu
478   //! \param theObject - in the OpenParts it is a QToolButton by itself,
479   //! in salome mode - QAction that creates a button.
480   //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
481   //! \param theSlot - onUndo(int) or onRedo(int) SLOT
482   void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
483
484
485   /// Calls removeFeature of the document for each object in the list
486   //! \param theList an objects to be deleted
487   //! \param theIgnoredFeatures a list of features to be ignored during delete
488   //! \param theActionId an action command key to find context menu object types enabled for remove
489   bool removeFeatures(const QObjectPtrList& theList,
490                       const std::set<FeaturePtr>& theIgnoredFeatures,
491                       const QString& theActionId);
492
493   //! Creates list of actions (commands) by given history list from session
494   QList<ActionInfo> processHistoryList(const std::list<std::string>&) const;
495
496 private:
497 #ifndef HAVE_SALOME
498   AppElements_MainWindow* myMainWindow;
499 #endif
500
501   ModuleBase_IModule* myModule;
502   XGUI_ErrorMgr* myErrorMgr;
503   XGUI_ObjectsBrowser* myObjectBrowser;
504   XGUI_PropertyPanel* myPropertyPanel;
505   XGUI_SelectionMgr* mySelector;
506   XGUI_Displayer* myDisplayer;
507   XGUI_OperationMgr* myOperationMgr;  ///< manager to manipulate through the operations
508   XGUI_ActionsMgr* myActionsMgr;
509   XGUI_SalomeConnector* mySalomeConnector;
510   XGUI_ErrorDialog* myErrorDlg;
511   XGUI_ViewerProxy* myViewerProxy;
512   XGUI_ContextMenuMgr* myContextMenuMgr;
513   XGUI_ModuleConnector* myModuleConnector;
514   XGUI_WorkshopListener* myEventsListener;
515
516   QString myCurrentDir;
517
518   int myViewerSelMode;
519 };
520
521 #endif