Salome HOME
Issue #1383 Preview button: store modified value before See preview
[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   /// Returns defailt selection mode in 3d viewer
278   int viewerSelectionMode() const { return myViewerSelMode; }
279
280   /// Highlights result objects in Object Browser according to
281   /// features found in the given list
282   void highlightResults(const QObjectPtrList& theObjects);
283
284   /// A constant string used for "Move to end" command definition
285   /// It is used for specific processing of Undo/Redo for this command.
286   static QString MOVE_TO_END_COMMAND;
287
288 signals:
289   /// Emitted when selection happens in Salome viewer
290   void salomeViewerSelection();
291
292   /// Emitted when error in application happens
293   void errorOccurred(const QString&);
294
295   //! the signal about the workshop actions states are updated.
296   void commandStatusUpdated();
297
298   //! the application is started
299   void applicationStarted();
300
301   //! Signal to update Undo history list
302   void updateUndoHistory(const QList<ActionInfo>&);
303
304   //! Signal to update Redo history list
305   void updateRedoHistory(const QList<ActionInfo>&);
306
307  public slots:
308    /// Update of commands status
309   void updateCommandStatus();
310
311   /// update history list (undo/redo commands)
312   void updateHistory();
313
314   /// Save current document
315   bool onSave();
316
317   /// Save current document to a file selected by user
318   bool onSaveAs();
319
320   /// Undo last command
321   void onUndo(int times = 1);
322
323   /// Redo previous command
324   void onRedo(int times = 1);
325
326   // Rebuild data tree
327   //void onRebuild();
328
329   /// Validates the operation to change the "Apply" button state.
330   /// \param thePreviousState the previous state of the widget
331   void onWidgetStateChanged(int thePreviousState);
332
333   /// Calls activate of the current widget again. It is possible that selection filters are
334   /// differs in the new state of paged container
335   void onValuesChanged();
336
337   /// Show property panel
338   void showPropertyPanel();
339
340   /// Hide property panel
341   void hidePropertyPanel();
342
343   /// Show object Browser
344   void showObjectBrowser();
345
346   /// Hide object Browser
347   void hideObjectBrowser();
348
349   /// Close document
350   void closeDocument();
351
352   /// Open document from file
353   void onOpen();
354
355   /// Create a new document
356   void onNew();
357
358 #ifndef HAVE_SALOME
359   /// Exit application
360   void onExit();
361
362   /// Open preferences dialog box
363   void onPreferences();
364 #endif
365
366   /// Activates/deactivates the trihedron in the viewer AIS context
367   void onTrihedronVisibilityChanged(bool theState);
368
369
370  protected:
371   /// Sets the granted operations for the parameter operation. Firstly, it finds the nested features
372   /// and set them into the operation. Secondly, it asks the module about ids of granted operations.
373   /// \param theOperation an operation
374    void setGrantedFeatures(ModuleBase_Operation* theOperation);
375
376   //! Find all referenced features. Return direct and indirect lists of referenced object
377   //! \param theList an objects to be checked
378   //! \param aDirectRefFeatures a list of direct reference features
379   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
380   void findReferences(const QObjectPtrList& theList,
381                       std::set<FeaturePtr>& aDirectRefFeatures,
382                       std::set<FeaturePtr>& aIndirectRefFeatures);
383
384   //! Shows a dialog box about references. Ask whether they should be also removed.
385   //! \param theList an objects to be checked
386   //! \param aDirectRefFeatures a list of direct reference features
387   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
388   //! \param theParent a parent widget for the question message box
389   //! \param doDeleteReferences if there are parameters between features, ask if they should be
390   //! replaced to their meaning without corresponded features remove
391   //! \return true if in message box answer is Yes
392   bool isDeleteFeatureWithReferences(const QObjectPtrList& theList,
393                                      const std::set<FeaturePtr>& aDirectRefFeatures,
394                                      const std::set<FeaturePtr>& aIndirectRefFeatures,
395                                      QWidget* theParent,
396                                      bool& doDeleteReferences);
397
398   //! \param theIgnoredFeatures a list of features to be ignored during delete
399   //! \param theList an objects to be checked
400   //! \param aDirectRefFeatures a list of direct reference features
401   //! \param aIndirectRefFeatures a list of features which depend on the feature through others
402   //! \param doDeleteReferences flag if referenced features should be removed also
403   bool deleteFeaturesInternal(const QObjectPtrList& theList,
404                               const std::set<FeaturePtr>& aDirectRefFeatures,
405                               const std::set<FeaturePtr>& aIndirectRefFeatures,
406                               const std::set<FeaturePtr>& theIgnoredFeatures,
407                               const bool doDeleteReferences = true);
408
409 private:
410   /// Display all results
411   //void displayAllResults();
412
413   /// Display results from document
414   /// \param theDoc a document
415   void displayDocumentResults(DocumentPtr theDoc);
416
417   /// Display results from a group
418   void displayGroupResults(DocumentPtr theDoc, std::string theGroup);
419
420  private slots:
421   /// SLOT, that is called after the operation is started. Update workshop state according to
422   /// the started operation, e.g. visualizes the property panel and connect to it.
423   /// \param theOpertion a started operation
424   void onOperationStarted(ModuleBase_Operation* theOperation);
425
426   /// SLOT, that is called after the operation is resumed. Update workshop state according to
427   /// the started operation, e.g. visualizes the property panel and connect to it.
428   /// \param theOpertion a resumed operation
429   virtual void onOperationResumed(ModuleBase_Operation* theOperation);
430
431   /// SLOT, that is called after the operation is stopped. Update workshop state, e.g.
432   /// hides the property panel and update the command status.
433   /// \param theOpertion a stopped operation
434   virtual void onOperationStopped(ModuleBase_Operation* theOperation);
435
436   /// SLOT, that is called after the operation is committed.
437   /// \param theOpertion a committed operation
438   virtual void onOperationCommitted(ModuleBase_Operation* theOperation);
439
440   /// SLOT, that is called after the operation is aborted.
441   /// \param theOpertion an aborted operation
442   void onOperationAborted(ModuleBase_Operation* theOperation);
443
444   /// Slot, which reacts to the context popup menu call
445   /// \param theId the data value of the clicked action
446   /// \param isChecked a state of toggle if the action is checkable
447   void onContextMenuCommand(const QString& theId, bool isChecked);
448
449   /// Set waiting cursor
450   void onStartWaiting();
451
452   /// Called by Ok button clicked in the property panel. Asks the error manager whether
453   /// the operation can be committed and do it if it returns true.
454   void onAcceptActionClicked();
455
456   /// Called by Preview button clicked in the property panel. Sends signal to model to
457   /// compute preview.
458   void onPreviewActionClicked();
459
460  private:
461    /// Init menu
462   void initMenu();
463
464   /// Register validators
465   void registerValidators() const;
466
467   /// Load module from external library
468   /// \param theModule name of the module
469   ModuleBase_IModule* loadModule(const QString& theModule);
470
471   /// Create module
472   bool createModule();
473
474   /// Create object browser widget
475   /// \param theParent a parent of widget
476   QDockWidget* createObjectBrowser(QWidget* theParent);
477
478   /// Create property panel widget
479   /// \param theParent a parent of widget
480   QDockWidget* createPropertyPanel(QWidget* theParent);
481
482   // Creates Dock widgets: Object browser and Property panel
483   void createDockWidgets();
484
485   //! Extends undo/redo toolbutton's with history menu
486   //! \param theObject - in the OpenParts it is a QToolButton by itself,
487   //! in salome mode - QAction that creates a button.
488   //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
489   //! \param theSlot - onUndo(int) or onRedo(int) SLOT
490   void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
491
492
493   /// Calls removeFeature of the document for each object in the list
494   //! \param theList an objects to be deleted
495   //! \param theIgnoredFeatures a list of features to be ignored during delete
496   //! \param theActionId an action command key to find context menu object types enabled for remove
497   bool removeFeatures(const QObjectPtrList& theList,
498                       const std::set<FeaturePtr>& theIgnoredFeatures,
499                       const QString& theActionId);
500
501   //! Creates list of actions (commands) by given history list from session
502   QList<ActionInfo> processHistoryList(const std::list<std::string>&) const;
503
504 private:
505 #ifndef HAVE_SALOME
506   AppElements_MainWindow* myMainWindow;
507 #endif
508
509   ModuleBase_IModule* myModule;
510   XGUI_ErrorMgr* myErrorMgr;
511   XGUI_ObjectsBrowser* myObjectBrowser;
512   XGUI_PropertyPanel* myPropertyPanel;
513   XGUI_SelectionMgr* mySelector;
514   XGUI_Displayer* myDisplayer;
515   XGUI_OperationMgr* myOperationMgr;  ///< manager to manipulate through the operations
516   XGUI_ActionsMgr* myActionsMgr;
517   XGUI_SalomeConnector* mySalomeConnector;
518   XGUI_ErrorDialog* myErrorDlg;
519   XGUI_ViewerProxy* myViewerProxy;
520   XGUI_ContextMenuMgr* myContextMenuMgr;
521   XGUI_ModuleConnector* myModuleConnector;
522   XGUI_WorkshopListener* myEventsListener;
523
524   QString myCurrentDir;
525
526   int myViewerSelMode;
527 };
528
529 #endif