Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / XGUI / XGUI_Workshop.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #ifndef XGUI_WORKSHOP_H
21 #define XGUI_WORKSHOP_H
22
23 #include "XGUI.h"
24 //#include "XGUI_Constants.h"
25 #include <ModuleBase_Definitions.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_Feature.h>
28
29 #include <ModuleBase_ActionInfo.h>
30
31 #include <QObject>
32 #include <QMap>
33 #include <QKeySequence>
34 #include <QIcon>
35
36 #ifndef HAVE_SALOME
37 class AppElements_MainWindow;
38 class AppElements_Command;
39 class AppElements_Workbench;
40 #endif
41
42 class XGUI_ActionsMgr;
43 class XGUI_ContextMenuMgr;
44 class XGUI_Displayer;
45 class XGUI_ErrorDialog;
46 class XGUI_ErrorMgr;
47 class XGUI_MenuMgr;
48 class XGUI_ModuleConnector;
49 class XGUI_ObjectsBrowser;
50 class XGUI_OperationMgr;
51 class XGUI_PropertyPanel;
52 class XGUI_SalomeConnector;
53 class XGUI_SalomeViewer;
54 class XGUI_SelectionMgr;
55 class XGUI_ViewerProxy;
56 class XGUI_WorkshopListener;
57
58 class ModuleBase_IModule;
59 class ModuleBase_IViewer;
60 class ModuleBase_Operation;
61
62 class QWidget;
63 class QDockWidget;
64 class QMainWindow;
65
66 class QAction;
67 class Config_DataModelReader;
68
69 /**\class XGUI_Workshop
70  * \ingroup GUI
71  * \brief Class which defines a configuration of the application (Workshop) and launches it.
72  */
73 class XGUI_EXPORT XGUI_Workshop : public QObject
74 {
75 Q_OBJECT
76  public:
77   /// Constructor
78   /// \param theConnector a Salome connector object.
79   /// Used only if the workshop is launched in Salome environment
80   XGUI_Workshop(XGUI_SalomeConnector* theConnector = 0);
81   virtual ~XGUI_Workshop();
82
83   //! Starting of the application
84   void startApplication();
85
86   /// Activates the module controls. Should be called after module creation
87   void activateModule();
88
89   /// Deactivates the module controls. Should be called after module creation
90   void deactivateModule();
91
92 #ifndef HAVE_SALOME
93   //! Returns main window (Desktop) of the application
94   AppElements_MainWindow* mainWindow() const
95   {
96     return myMainWindow;
97   }
98
99   //! Creates and adds a new workbench (menu group) with the given name and returns it
100   AppElements_Workbench* addWorkbench(const QString& theName);
101 #endif
102
103   //! Returns selection manager object
104   XGUI_SelectionMgr* selector() const
105   {
106     return mySelector;
107   }
108
109   //! Returns displayer
110   XGUI_Displayer* displayer() const
111   {
112     return myDisplayer;
113   }
114
115   //! ! Returns operation manager.
116   XGUI_OperationMgr* operationMgr() const
117   {
118     return myOperationMgr;
119   }
120
121   //! ! Returns error manager.
122   XGUI_ErrorMgr* errorMgr() const
123   {
124     return myErrorMgr;
125   }
126
127   //! ! Returns an actions manager
128   XGUI_ActionsMgr* actionsMgr() const
129   {
130     return myActionsMgr;
131   }
132
133   //! ! Returns an actions manager
134   XGUI_MenuMgr* menuMgr() const
135   {
136     return myMenuMgr;
137   }
138
139   //! Returns property panel widget
140   XGUI_PropertyPanel* propertyPanel() const
141   {
142     return myPropertyPanel;
143   }
144
145   //! Returns context menu manager object
146   XGUI_ContextMenuMgr* contextMenuMgr() const
147   {
148     return myContextMenuMgr;
149   }
150
151   //! Returns an object which provides interface to Salome Module (LightApp_Module)
152   XGUI_SalomeConnector* salomeConnector() const
153   {
154     return mySalomeConnector;
155   }
156
157   //! Provides an object which provides interface to Salome Viewer
158   ModuleBase_IViewer* salomeViewer() const;
159
160   //! Returns true if the application works as SALOME module
161   bool isSalomeMode() const
162   {
163     return mySalomeConnector != 0;
164   }
165
166   //! Returns Object browser
167   XGUI_ObjectsBrowser* objectBrowser() const
168   {
169     return myObjectBrowser;
170   }
171
172   /// This method is called by Salome module when selection is changed
173   void salomeViewerSelectionChanged();
174
175   /// Returns viewer which unifies access as to Salome viewer as to standalone viewer
176   XGUI_ViewerProxy* viewer() const
177   {
178     return myViewerProxy;
179   }
180
181   /// Returns the module connector
182   /// \returns the instance of connector
183   XGUI_ModuleConnector* moduleConnector() const
184   {
185     return myModuleConnector;
186   }
187
188   /// Returns a desktop
189   /// \return a desktop instance
190   QMainWindow* desktop() const;
191
192   //! Delete features
193   void deleteObjects();
194
195   //! Searches for selected features unused in other (not selected) features. If one or several
196   //! selected features are found, a warning message proposes to delete them. It contains
197   //! the list of features to be deleted.
198   void cleanHistory();
199
200   //! Returns true if the selected feature can be moved to the position after the current feature
201   //! \return boolean value
202   bool canMoveFeature();
203
204   //! Move selected features to be after the current feature
205   void moveObjects();
206
207   //! Returns true if the object can be shaded. If the object is a compsolid result, the method
208   //! checks subobjects of the result
209   //! \return boolean value
210   bool canBeShaded(const ObjectPtr& theObject) const;
211
212   //! Returns true if there is at least one selected body/construction/group result
213   //! \return boolean value
214   bool canChangeColor() const;
215
216   //! Change color of the results if it is possible
217   //! The operation is available for construction, body and group results
218   //! theObjects a list of selected objects
219   void changeColor(const QObjectPtrList& theObjects);
220
221   //! Returns true if there is at least one selected body/construction/group result
222   //! \return boolean value
223   bool canChangeDeflection() const;
224
225   //! Change deflection of the results if it is possible
226   //! The operation is available for construction, body and group results
227   //! theObjects a list of selected objects
228   void changeDeflection(const QObjectPtrList& theObjects);
229
230   //! Show the given features in 3d Viewer
231   void showObjects(const QObjectPtrList& theList, bool isVisible);
232
233   //! Show the given features in 3d Viewer
234   void showOnlyObjects(const QObjectPtrList& theList);
235
236   /// Set display mode for the given objects
237   /// \param theList a list of displayed objects
238   /// \param theMode a mode to set (see \ref XGUI_Displayer)
239   void setDisplayMode(const QObjectPtrList& theList, int theMode);
240
241   /// Set selection mode in viewer. If theMode=-1 then activate default mode
242   /// \param theMode the selection mode (according to TopAbs_ShapeEnum)
243   void setViewerSelectionMode(int theMode);
244
245   /// Activates current selection mode for the given list of objects
246   void activateObjectsSelection(const QObjectPtrList& theList);
247
248   /// Returns current module
249   ModuleBase_IModule* module() const
250   {
251     return myModule;
252   }
253
254   /// Returns current directory which contains data files
255   QString currentDataDir() const { return myCurrentDir; }
256
257   /// Returns current directory which contains data files
258   void setCurrentDataDir(const QString& theDir) { myCurrentDir = theDir; }
259
260   /**
261   * Save the current document into a directory
262   * \param theName - path to the directory
263   * \param theFileNames - returned file names created in this directory
264   */
265   void saveDocument(const QString& theName, std::list<std::string>& theFileNames);
266
267   /**
268    * If there is an active (uncommitted) operation shows a prompt to abort it
269    * and performs abortion if user agreed. Returns true if
270    * - operation aborted successfully
271    * - there is no active operation
272    */
273   bool abortAllOperations();
274
275   /// Updates workshop state according to the started operation, e.g. visualizes the property panel
276   /// and connect to it.
277   /// \param theOpertion a started operation
278   void operationStarted(ModuleBase_Operation* theOperation);
279
280
281   //! Delete features. Delete the referenced features. There can be a question with a list of
282   //! referenced objects.
283   //! \param theFeatures a list of objects to be deleted
284   bool deleteFeatures(const QObjectPtrList& theFeatures);
285
286   /// Deactivates the object, if it is active and the module returns that the activation
287   /// of selection for the object is not possible currently(the current operation uses it)
288   /// \param theObject an object
289   /// \param theUpdateViewer a boolean flag to update viewer immediately
290   void deactivateActiveObject(const ObjectPtr& theObject, const bool theUpdateViewer);
291
292   /// Returns true if the action of the feature is created to contain Accept/Cancel button
293   /// \param theFeature a feature
294   bool isFeatureOfNested(const FeaturePtr& theFeature);
295
296   //! Shows the message in the status bar
297   //! \param theMessage a message
298   void setStatusBarMessage(const QString& theMessage);
299
300   /// Has to be called in order to display objects with visibility status = true
301   void synchronizeViewer();
302
303   /// Has to be called in order to display objects from a specifed group
304   /// with visibility status = true
305   /// \param theDoc the document for objects synchronisation
306   /// \param theGroup the group name
307   /// \param theUpdateViewer update viewer flag
308   void synchronizeGroupInViewer(const DocumentPtr& theDoc,
309                                 const std::string& theGroup,
310                                 bool theUpdateViewer);
311
312   /// Update the property panel content by the XML description of the operation and set the panel
313   /// into the operation
314   /// \param theOperation an operation
315   void fillPropertyPanel(ModuleBase_Operation* theOperation);
316
317   /// Connects or disconnects to the value changed signal of the property panel widgets
318   /// \param isToConnect a boolean value whether connect or disconnect
319   void connectToPropertyPanel(const bool isToConnect);
320
321   /// Returns defailt selection mode in 3d viewer
322   QIntList viewerSelectionModes() const { return myViewerSelMode; }
323
324   /// Highlights result objects in Object Browser according to
325   /// features found in the given list
326   void highlightResults(const QObjectPtrList& theObjects);
327
328   /// Highlights feature objects in Object Browser according to
329   /// features found in the given list
330   void highlightFeature(const QObjectPtrList& theObjects);
331
332   /// Returns Data Model XML reader which contains information about
333   /// Data structure configuration
334   const Config_DataModelReader* dataModelXMLReader() const { return myDataModelXMLReader; }
335
336   /// A constant string used for "Move to end" command definition
337   /// It is used for specific processing of Undo/Redo for this command.
338   static QString MOVE_TO_END_COMMAND;
339
340   //! Closes all in the current session and load the directory
341   //! \param theDirectory a path to directory
342   void openDirectory(const QString& theDirectory);
343
344 signals:
345   /// Emitted when selection happens in Salome viewer
346   void salomeViewerSelection();
347
348   //! the signal about the workshop actions states are updated.
349   void commandStatusUpdated();
350
351   //! the application is started
352   void applicationStarted();
353
354   //! Signal to update Undo history list
355   void updateUndoHistory(const QList<ActionInfo>&);
356
357   //! Signal to update Redo history list
358   void updateRedoHistory(const QList<ActionInfo>&);
359
360  public slots:
361    /// Update of commands status
362   void updateCommandStatus();
363
364   /// update history list (undo/redo commands)
365   void updateHistory();
366
367   /// Save current document
368   bool onSave();
369
370   /// Save current document to a file selected by user
371   bool onSaveAs();
372
373   /// Undo last command
374   void onUndo(int times = 1);
375
376   /// Redo previous command
377   void onRedo(int times = 1);
378
379   // Rebuild data tree
380   //void onRebuild();
381
382   /// Validates the operation to change the "Apply" button state.
383   /// \param thePreviousState the previous state of the widget
384   void onWidgetStateChanged(int thePreviousState);
385
386   /// Calls activate of the current widget again. It is possible that selection filters are
387   /// differs in the new state of paged container
388   void onValuesChanged();
389
390   /// Listens the corresponded signal of model widget and updates Apply button state by feature
391   void onWidgetObjectUpdated();
392
393   /// Show property panel
394   void showPropertyPanel();
395
396   /// Hide property panel
397   void hidePropertyPanel();
398
399   /// Show object Browser
400   void showObjectBrowser();
401
402   /// Hide object Browser
403   void hideObjectBrowser();
404
405   /// Close document
406   void closeDocument();
407
408   /// Open document from file
409   void onOpen();
410
411   /// Create a new document
412   void onNew();
413
414 #ifndef HAVE_SALOME
415   /// Exit application
416   void onExit();
417
418   /// Open preferences dialog box
419   void onPreferences();
420 #endif
421
422   /// Activates/deactivates the trihedron in the viewer AIS context
423   void onTrihedronVisibilityChanged(bool theState);
424
425
426  protected:
427   /// Sets the granted operations for the parameter operation. Firstly, it finds the nested features
428   /// and set them into the operation. Secondly, it asks the module about ids of granted operations.
429   /// \param theOperation an operation
430    void setGrantedFeatures(ModuleBase_Operation* theOperation);
431
432 private:
433   /// Display all results
434   //void displayAllResults();
435
436   /// Display results from document
437   /// \param theDoc a document
438   void displayDocumentResults(DocumentPtr theDoc);
439
440   /// Display results from a group
441   void displayGroupResults(DocumentPtr theDoc, std::string theGroup);
442
443  private slots:
444   /// SLOT, that is called after the operation is started. Update workshop state according to
445   /// the started operation, e.g. visualizes the property panel and connect to it.
446   /// \param theOpertion a started operation
447   // void onOperationStarted(ModuleBase_Operation* theOperation);
448
449   /// SLOT, that is called after the operation is resumed. Update workshop state according to
450   /// the started operation, e.g. visualizes the property panel and connect to it.
451   /// \param theOpertion a resumed operation
452   virtual void onOperationResumed(ModuleBase_Operation* theOperation);
453
454   /// SLOT, that is called after the operation is stopped. Update workshop state, e.g.
455   /// hides the property panel and update the command status.
456   /// \param theOpertion a stopped operation
457   virtual void onOperationStopped(ModuleBase_Operation* theOperation);
458
459   /// SLOT, that is called after the operation is committed.
460   /// \param theOpertion a committed operation
461   virtual void onOperationCommitted(ModuleBase_Operation* theOperation);
462
463   /// SLOT, that is called after the operation is aborted.
464   /// \param theOpertion an aborted operation
465   void onOperationAborted(ModuleBase_Operation* theOperation);
466
467   /// Slot, which reacts to the context popup menu call
468   /// \param theId the data value of the clicked action
469   /// \param isChecked a state of toggle if the action is checkable
470   void onContextMenuCommand(const QString& theId, bool isChecked);
471
472   /// Set waiting cursor
473   void onStartWaiting();
474
475   /// Called by Ok button clicked in the property panel. Asks the error manager whether
476   /// the operation can be committed and do it if it returns true.
477   void onAcceptActionClicked();
478
479   /// Called by Preview button clicked in the property panel. Sends signal to model to
480   /// compute preview.
481   void onPreviewActionClicked();
482
483  private:
484    /// Init menu
485   void initMenu();
486
487   /// Register validators
488   void registerValidators() const;
489
490   /// Load module from external library
491   /// \param theModule name of the module
492   ModuleBase_IModule* loadModule(const QString& theModule);
493
494   /// Create module
495   bool createModule();
496
497   /// Create object browser widget
498   /// \param theParent a parent of widget
499   QDockWidget* createObjectBrowser(QWidget* theParent);
500
501   /// Create property panel widget
502   /// \param theParent a parent of widget
503   QDockWidget* createPropertyPanel(QWidget* theParent);
504
505   // Creates Dock widgets: Object browser and Property panel
506   void createDockWidgets();
507
508   //! Extends undo/redo toolbutton's with history menu
509   //! \param theObject - in the OpenParts it is a QToolButton by itself,
510   //! in salome mode - QAction that creates a button.
511   //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
512   //! \param theSlot - onUndo(int) or onRedo(int) SLOT
513   void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
514
515   //! Creates list of actions (commands) by given history list from session
516   QList<ActionInfo> processHistoryList(const std::list<std::string>&) const;
517
518 private:
519 #ifndef HAVE_SALOME
520   AppElements_MainWindow* myMainWindow;
521 #endif
522
523   ModuleBase_IModule* myModule;
524   XGUI_ErrorMgr* myErrorMgr;
525   XGUI_ObjectsBrowser* myObjectBrowser;
526   XGUI_PropertyPanel* myPropertyPanel;
527   XGUI_SelectionMgr* mySelector;
528   XGUI_Displayer* myDisplayer;
529   XGUI_OperationMgr* myOperationMgr;  ///< manager to manipulate through the operations
530   XGUI_ActionsMgr* myActionsMgr;
531   XGUI_MenuMgr* myMenuMgr; ///< manager to build menu/tool bar using order defined in XML
532   XGUI_SalomeConnector* mySalomeConnector;
533   XGUI_ErrorDialog* myErrorDlg;
534   XGUI_ViewerProxy* myViewerProxy;
535   XGUI_ContextMenuMgr* myContextMenuMgr;
536   XGUI_ModuleConnector* myModuleConnector;
537   XGUI_WorkshopListener* myEventsListener;
538
539   QString myCurrentDir;
540
541   QIntList myViewerSelMode;
542
543   Config_DataModelReader* myDataModelXMLReader;
544 };
545
546 #endif