]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
Avoid blinking on the Redo functionality
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 //#include "XGUI_Constants.h"
4 #include "XGUI_Tools.h"
5 #include "XGUI_Workshop.h"
6 #include "XGUI_SelectionMgr.h"
7 #include "XGUI_Selection.h"
8 #include "XGUI_ObjectsBrowser.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_OperationMgr.h"
11 #include "XGUI_SalomeConnector.h"
12 #include "XGUI_ActionsMgr.h"
13 #include "XGUI_ErrorDialog.h"
14 #include "XGUI_ViewerProxy.h"
15 #include "XGUI_PropertyPanel.h"
16 #include "XGUI_ContextMenuMgr.h"
17 #include "XGUI_ModuleConnector.h"
18 #include <XGUI_QtEvents.h>
19 #include <XGUI_HistoryMenu.h>
20
21 #include <AppElements_Workbench.h>
22 #include <AppElements_Viewer.h>
23 #include <AppElements_Command.h>
24 #include <AppElements_MainMenu.h>
25 #include <AppElements_MainWindow.h>
26 #include <AppElements_MenuGroupPanel.h>
27 #include <AppElements_Button.h>
28
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_Preferences.h>
31
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Feature.h>
35 #include <ModelAPI_Data.h>
36 #include <ModelAPI_AttributeDocRef.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_ResultGroup.h>
40 #include <ModelAPI_ResultConstruction.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_AttributeIntArray.h>
43
44 //#include <PartSetPlugin_Part.h>
45
46 #include <Events_Loop.h>
47 #include <Events_Error.h>
48 #include <Events_LongOp.h>
49
50 #include <ModuleBase_Operation.h>
51 #include <ModuleBase_Operation.h>
52 #include <ModuleBase_OperationDescription.h>
53 #include <ModuleBase_SelectionValidator.h>
54 #include <ModuleBase_WidgetFactory.h>
55 #include <ModuleBase_Tools.h>
56 #include <ModuleBase_IViewer.h>
57 #include<ModuleBase_FilterFactory.h>
58 #include <ModuleBase_PageBase.h>
59
60 #include <Config_Common.h>
61 #include <Config_FeatureMessage.h>
62 #include <Config_PointerMessage.h>
63 #include <Config_ModuleReader.h>
64 #include <Config_PropManager.h>
65 #include <Config_SelectionFilterMessage.h>
66
67 #include <QApplication>
68 #include <QFileDialog>
69 #include <QMessageBox>
70 #include <QMdiSubWindow>
71 #include <QPushButton>
72 #include <QDockWidget>
73 #include <QLayout>
74 #include <QThread>
75 #include <QObject>
76 #include <QMenu>
77 #include <QToolButton>
78 #include <QAction>
79 #include <QDialog>
80 #include <QDialogButtonBox>
81 #include <QHBoxLayout>
82 #include <QtxColorButton.h>
83
84 #ifdef _DEBUG
85 #include <QDebug>
86 #include <iostream>
87 #endif
88
89 #ifdef WIN32
90 #include <windows.h>
91 #else
92 #include <dlfcn.h>
93 #endif
94
95 //#define DEBUG_FEATURE_CREATED
96 //#define DEBUG_FEATURE_REDISPLAY
97
98 QString objectInfo(ObjectPtr theObj)
99 {
100   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
101   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
102   QString aFeatureStr = "feature";
103   if(aRes.get()) {
104     aFeatureStr.append("(Result)");
105     aFeature = ModelAPI_Feature::feature(aRes);
106   }
107   if (aFeature.get()) {
108     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
109     if (aFeature->data().get() && aFeature->data()->isValid())
110       aFeatureStr.append(QString("(name=%1)").arg(aFeature->data()->name().c_str()).toStdString().c_str());
111   }
112   return aFeatureStr;
113 }
114
115
116 QMap<QString, QString> XGUI_Workshop::myIcons;
117
118
119 QIcon XGUI_Workshop::featureIcon(const FeaturePtr& theFeature)
120 {
121   QIcon anIcon;
122
123   std::string aKind = theFeature->getKind();
124   QString aId(aKind.c_str());
125   if (!myIcons.contains(aId))
126     return anIcon;
127
128   QString anIconString = myIcons[aId];
129
130   ModelAPI_ExecState aState = theFeature->data()->execState();
131   switch(aState) {
132     case ModelAPI_StateDone:
133     case ModelAPI_StateNothing: {
134       anIcon = QIcon(anIconString);
135     }
136     break;
137     case ModelAPI_StateMustBeUpdated: {
138       anIcon = ModuleBase_Tools::lighter(anIconString);
139     }
140     break;
141     case ModelAPI_StateExecFailed: {
142       anIcon = ModuleBase_Tools::composite(":pictures/exec_state_failed.png", anIconString);
143     }
144     break;
145     case ModelAPI_StateInvalidArgument: {
146       anIcon = ModuleBase_Tools::composite(":pictures/exec_state_invalid_parameters.png",
147                                            anIconString);
148     }
149     break;
150     default: break;  
151   }
152   return anIcon;  
153 }
154
155 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
156     : QObject(),
157       myCurrentDir(QString()),
158       myModule(NULL),
159       mySalomeConnector(theConnector),
160       myPropertyPanel(0),
161       myObjectBrowser(0),
162       myDisplayer(0),
163       myUpdatePrefs(false),
164       myPartActivating(false)
165 {
166   myMainWindow = mySalomeConnector ? 0 : new AppElements_MainWindow();
167
168   myDisplayer = new XGUI_Displayer(this);
169
170   mySelector = new XGUI_SelectionMgr(this);
171   //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
172
173   myOperationMgr = new XGUI_OperationMgr(this);
174   myActionsMgr = new XGUI_ActionsMgr(this);
175   myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
176   myContextMenuMgr = new XGUI_ContextMenuMgr(this);
177   connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
178           SLOT(onContextMenuCommand(const QString&, bool)));
179
180   myViewerProxy = new XGUI_ViewerProxy(this);
181   connect(myViewerProxy, SIGNAL(selectionChanged()),
182           myActionsMgr,  SLOT(updateOnViewSelection()));
183
184   myModuleConnector = new XGUI_ModuleConnector(this);
185
186   connect(myOperationMgr, SIGNAL(operationStarted(ModuleBase_Operation*)), 
187           SLOT(onOperationStarted(ModuleBase_Operation*)));
188   connect(myOperationMgr, SIGNAL(operationResumed(ModuleBase_Operation*)),
189           SLOT(onOperationResumed(ModuleBase_Operation*)));
190   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
191           SLOT(onOperationStopped(ModuleBase_Operation*)));
192   connect(myOperationMgr, SIGNAL(operationCommitted(ModuleBase_Operation*)), 
193           SLOT(onOperationCommitted(ModuleBase_Operation*)));
194   connect(myOperationMgr, SIGNAL(operationAborted(ModuleBase_Operation*)), 
195           SLOT(onOperationAborted(ModuleBase_Operation*)));
196   connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
197   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
198 }
199
200 //******************************************************
201 XGUI_Workshop::~XGUI_Workshop(void)
202 {
203   delete myDisplayer;
204 }
205
206 //******************************************************
207 void XGUI_Workshop::startApplication()
208 {
209   initMenu();
210
211   Config_PropManager::registerProp("Plugins", "default_path", "Default Path",
212                                    Config_Prop::Directory, "");
213
214   //Initialize event listening
215   Events_Loop* aLoop = Events_Loop::loop();
216   aLoop->registerListener(this, Events_Error::errorID());  //!< Listening application errors.
217   aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
218   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
219   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
220   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
221   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
222   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
223   aLoop->registerListener(this, Events_LongOp::eventID());
224   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
225   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
226   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOSHOW));
227   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
228   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
229
230   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
231   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
232
233   registerValidators();
234
235   // Calling of  loadCustomProps before activating module is required
236   // by Config_PropManger to restore user-defined path to plugins
237   ModuleBase_Preferences::loadCustomProps();
238   activateModule();
239   if (myMainWindow) {
240     myMainWindow->show();
241     updateCommandStatus();
242   }
243   
244   onNew();
245
246   emit applicationStarted();
247 }
248
249 //******************************************************
250 void XGUI_Workshop::initMenu()
251 {
252   myContextMenuMgr->createActions();
253
254   if (isSalomeMode()) {
255     // Create only Undo, Redo commands
256     QAction* aAction = salomeConnector()->addDesktopCommand("UNDO_CMD", tr("Undo"),
257                                                          tr("Undo last command"),
258                                                          QIcon(":pictures/undo.png"),
259                                                          QKeySequence::Undo, false, "MEN_DESK_EDIT");
260     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
261     addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
262
263     aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
264                                                 QIcon(":pictures/redo.png"), QKeySequence::Redo,
265                                                 false, "MEN_DESK_EDIT");
266     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
267     addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
268
269     salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
270     aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
271                                                 QIcon(":pictures/rebuild.png"), QKeySequence(),
272                                                 false, "MEN_DESK_EDIT");
273     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRebuild()));
274     salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
275
276     aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Export NewGeom..."), tr("Export the current document into a NewGeom file"),
277                                                 QIcon(), QKeySequence(),
278                                                 false, "MEN_DESK_FILE");
279     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSaveAs()));
280
281     aAction = salomeConnector()->addDesktopCommand("OPEN_CMD", tr("Import NewGeom..."), tr("Import a NewGeom file"),
282                                                 QIcon(), QKeySequence(),
283                                                 false, "MEN_DESK_FILE");
284     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
285     salomeConnector()->addDesktopMenuSeparator("MEN_DESK_FILE");
286
287     return;
288   }
289   // File commands group
290   AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
291
292   AppElements_Command* aCommand;
293
294   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
295                                 QIcon(":pictures/save.png"), QKeySequence::Save);
296   aCommand->connectTo(this, SLOT(onSave()));
297   //aCommand->disable();
298
299   QString aUndoId = "UNDO_CMD";
300   aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
301                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
302   aCommand->connectTo(this, SLOT(onUndo()));
303   AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
304   addHistoryMenu(aUndoButton,
305                  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
306                  SLOT(onUndo(int)));
307
308   QString aRedoId = "REDO_CMD";
309   aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
310                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
311   aCommand->connectTo(this, SLOT(onRedo()));
312   AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
313   addHistoryMenu(aRedoButton,
314                  SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
315                  SLOT(onRedo(int)));
316
317   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
318     QIcon(":pictures/rebuild.png"), QKeySequence());
319   aCommand->connectTo(this, SLOT(onRebuild()));
320
321   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
322                                 QIcon(":pictures/save.png"), QKeySequence());
323   aCommand->connectTo(this, SLOT(onSaveAs()));
324   //aCommand->disable();
325
326   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
327                                 QIcon(":pictures/open.png"), QKeySequence::Open);
328   aCommand->connectTo(this, SLOT(onOpen()));
329
330   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
331   //                              QIcon(":pictures/new.png"), QKeySequence::New);
332   //aCommand->connectTo(this, SLOT(onNew()));
333
334   aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
335                                 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
336   aCommand->connectTo(this, SLOT(onPreferences()));
337
338   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
339                                 QIcon(":pictures/close.png"), QKeySequence::Close);
340   aCommand->connectTo(this, SLOT(onExit()));
341   //FIXME: SBH's test action. Can be used for some GUI tests.
342 //  #ifdef _DEBUG
343 //    aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
344 //                                  QIcon(":pictures/close.png"), QKeySequence(), true);
345 //    aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
346 //  #endif
347 }
348
349 //******************************************************
350 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
351 {
352   AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
353   return aMenuBar->addWorkbench(theName);
354 }
355
356 //******************************************************
357 void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessage)
358 {
359   if (QApplication::instance()->thread() != QThread::currentThread()) {
360     #ifdef _DEBUG
361     std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
362     #endif
363     SessionPtr aMgr = ModelAPI_Session::get();
364     PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
365     QApplication::postEvent(this, aPostponeEvent);
366     return;
367   }
368
369   //A message to start feature creation received.
370   if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
371     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
372        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
373     if (!aFeatureMsg->isInternal()) {
374       addFeature(aFeatureMsg);
375     }
376   }
377   // Process creation of Part
378   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
379     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
380         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
381     onFeatureCreatedMsg(aUpdMsg);
382     if (myUpdatePrefs) {
383       if (mySalomeConnector)
384         mySalomeConnector->createPreferences();
385       myUpdatePrefs = false;
386     }
387   }
388   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
389     myUpdatePrefs = true;
390   }
391   // Redisplay feature
392   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
393     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
394         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
395     onFeatureRedisplayMsg(aUpdMsg);
396   }
397   //Update property panel on corresponding message. If there is no current operation (no
398   //property panel), or received message has different feature to the current - do nothing.
399   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
400     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
401         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
402     onFeatureUpdatedMsg(anUpdateMsg);
403   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
404     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDelMsg =
405         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
406     onObjectDeletedMsg(aDelMsg);
407   } else if (theMessage->eventID() == Events_LongOp::eventID()) {
408     if (Events_LongOp::isPerformed()) {
409       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
410     } else {
411       QApplication::restoreOverrideCursor();
412     }
413   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW)) {
414     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
415         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
416     const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
417     QObjectPtrList aList;
418     std::set<ObjectPtr>::const_iterator aIt;
419     for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
420       aList.append(*aIt);
421     showObjects(aList, true);
422   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE)) {
423     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
424         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
425     const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
426     QObjectPtrList aList;
427     std::set<ObjectPtr>::const_iterator aIt;
428     for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
429       aList.append(*aIt);
430     showObjects(aList, false);
431   }
432   //An operation passed by message. Start it, process and commit.
433   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
434     std::shared_ptr<Config_PointerMessage> aPartSetMsg =
435         std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
436     //myPropertyPanel->cleanContent();
437     ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
438
439     if (myOperationMgr->startOperation(anOperation)) {
440       myPropertyPanel->updateContentWidget(anOperation->feature());
441       if (!anOperation->getDescription()->hasXmlRepresentation()) {
442         if (anOperation->commit())
443           updateCommandStatus();
444       }
445     }
446   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
447     myActionsMgr->update();
448     // Find and Activate active part
449     if (myPartActivating)
450       return;
451     SessionPtr aMgr = ModelAPI_Session::get();
452     DocumentPtr aActiveDoc = aMgr->activeDocument();
453     DocumentPtr aDoc = aMgr->moduleDocument();
454     if (aActiveDoc == aDoc) {
455       activatePart(ResultPartPtr()); 
456       return;
457     }
458     std::string aGrpName = ModelAPI_ResultPart::group();
459     for (int i = 0; i < aDoc->size(aGrpName); i++) {
460       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
461       if (aPart->partDoc() == aActiveDoc) {
462         activatePart(aPart); // Activate a part which corresponds to active Doc
463         return;
464       }
465     }
466     // If not found then activate global document
467     activatePart(ResultPartPtr()); 
468
469   }
470   else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
471     std::shared_ptr<Config_SelectionFilterMessage> aMsg = 
472       std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
473     if (aMsg) {
474       ModuleBase_FilterFactory* aFactory = moduleConnector()->selectionFilters();
475       if (!aMsg->attributeId().empty()) {
476         aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
477                                aMsg->parameters());
478       }
479     }
480   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
481     // the viewer's update context will not happens until viewer updated is emitted
482     myDisplayer->enableUpdateViewer(false);
483   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
484     // the viewer's update context is unblocked, the viewer's update works
485     myDisplayer->enableUpdateViewer(true);
486   } else {
487     //Show error dialog if error message received.
488     std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
489     if (anAppError) {
490       emit errorOccurred(QString::fromLatin1(anAppError->description()));
491     }
492     return;
493   }
494   if (!isSalomeMode()) {
495     SessionPtr aMgr = ModelAPI_Session::get();
496     if (aMgr->isModified() != myMainWindow->isModifiedState())
497       myMainWindow->setModifiedState(aMgr->isModified());
498   }
499 }
500
501 //******************************************************
502 QMainWindow* XGUI_Workshop::desktop() const
503 {
504   return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
505 }
506
507 //******************************************************
508 void XGUI_Workshop::onStartWaiting()
509 {
510   if (Events_LongOp::isPerformed()) {
511     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
512   }
513 }
514
515 //******************************************************
516 void XGUI_Workshop::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
517 {
518   std::set<ObjectPtr> aFeatures = theMsg->objects();
519   if (myOperationMgr->hasOperation()) {
520     FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
521     std::set<ObjectPtr>::const_iterator aIt;
522     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
523       ObjectPtr aNewFeature = (*aIt);
524       if (aNewFeature == aCurrentFeature) {
525         myPropertyPanel->updateContentWidget(aCurrentFeature);
526         break;
527       }
528     }
529   }
530   myOperationMgr->onValidateOperation();
531   if (myObjectBrowser)
532     myObjectBrowser->processEvent(theMsg);
533 }
534
535 //******************************************************
536 void XGUI_Workshop::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
537 {
538   std::set<ObjectPtr> aObjects = theMsg->objects();
539   std::set<ObjectPtr>::const_iterator aIt;
540
541 #ifdef DEBUG_FEATURE_REDISPLAY
542   QStringList anInfo;
543   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
544     anInfo.append(objectInfo((*aIt)));
545   }
546   QString anInfoStr = anInfo.join(", ");
547   qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
548 #endif
549
550   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
551     ObjectPtr aObj = (*aIt);
552
553     // Hide the object if it is invalid or concealed one
554     bool aHide = !aObj->data() || !aObj->data()->isValid();
555     if (!aHide) { // check that this is not hidden result
556       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
557       aHide = aRes && aRes->isConcealed();
558     }
559     if (aHide)
560       myDisplayer->erase(aObj, false);
561     else {
562       // Redisplay the visible object or the object of the current operation
563       bool isVisibleObject = myDisplayer->isVisible(aObj);
564       #ifdef DEBUG_FEATURE_REDISPLAY
565       //QString anObjInfo = objectInfo((aObj));
566       //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
567       #endif
568
569       if (isVisibleObject)  { // redisplay visible object
570         displayObject(aObj);  // In order to update presentation
571         if (myOperationMgr->hasOperation()) {
572           ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
573           if (!aOperation->isEditOperation() &&
574               aOperation->hasObject(aObj) && myDisplayer->isActive(aObj))
575             myDisplayer->deactivate(aObj);
576         }
577       } else { // display object if the current operation has it
578         ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
579         if (aOperation && aOperation->hasObject(aObj)) {
580           ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
581           if (displayObject(aObj)) {
582             // Deactivate object of current operation from selection
583             if (myDisplayer->isActive(aObj))
584               myDisplayer->deactivate(aObj);
585           }
586         }
587       }
588     }
589   }
590   myDisplayer->updateViewer();
591 }
592
593 //******************************************************
594 void XGUI_Workshop::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
595 {
596   std::set<ObjectPtr> aObjects = theMsg->objects();
597   std::set<ObjectPtr>::const_iterator aIt;
598 #ifdef DEBUG_FEATURE_CREATED
599   QStringList anInfo;
600   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
601     anInfo.append(objectInfo((*aIt)));
602   }
603   QString anInfoStr = anInfo.join(", ");
604   qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
605 #endif
606
607   //bool aHasPart = false;
608   bool isDisplayed = false;
609   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
610     ObjectPtr anObject = *aIt;
611     // the validity of the data should be checked here in order to avoid display of the objects,
612     // which were created, then deleted, but flush for the creation event happens after that
613     if (!anObject->data() || !anObject->data()->isValid())
614       continue;
615     //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
616     //if (aPart) {
617       //aHasPart = true;
618       // If a feature is created from the aplication's python console  
619       // it doesn't stored in the operation mgr and doesn't displayed
620     //} else {
621     isDisplayed = displayObject(*aIt);
622     //}
623   }
624   if (myObjectBrowser)
625     myObjectBrowser->processEvent(theMsg);
626   if (isDisplayed)
627     myDisplayer->updateViewer();
628   //if (aHasPart) { // TODO: Avoid activate last part on loading of document
629   //  activateLastPart();
630   //}
631 }
632
633 //******************************************************
634 void XGUI_Workshop::onObjectDeletedMsg(const std::shared_ptr<ModelAPI_ObjectDeletedMessage>& theMsg)
635 {
636   if (myObjectBrowser)
637     myObjectBrowser->processEvent(theMsg);
638   //std::set<ObjectPtr> aFeatures = theMsg->objects();
639 }
640
641 //******************************************************
642 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
643 {
644   setNestedFeatures(theOperation);
645
646   if (theOperation->getDescription()->hasXmlRepresentation()) {  //!< No need for property panel
647     connectWithOperation(theOperation);
648     setPropertyPanel(theOperation);
649   }
650   updateCommandStatus();
651
652   myModule->operationStarted(theOperation);
653 }
654
655 //******************************************************
656 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
657 {
658   setNestedFeatures(theOperation);
659
660   if (theOperation->getDescription()->hasXmlRepresentation()) {  //!< No need for property panel
661     // connectWithOperation(theOperation); already connected
662     setPropertyPanel(theOperation);
663   }
664   updateCommandStatus();
665
666   myModule->operationResumed(theOperation);
667 }
668
669
670 //******************************************************
671 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
672 {
673   ModuleBase_ISelection* aSel = mySelector->selection();
674   QObjectPtrList aObj = aSel->selectedPresentations();
675   //!< No need for property panel
676   updateCommandStatus();
677   hidePropertyPanel();
678   myPropertyPanel->cleanContent();
679
680   // Activate objects created by current operation 
681   // in order to clean selection modes
682   QIntList aModes;
683   myDisplayer->activateObjects(aModes);
684   myModule->operationStopped(theOperation);
685 }
686
687
688 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
689 {
690   myModule->operationCommitted(theOperation);
691 }
692
693 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
694 {
695   myModule->operationAborted(theOperation);
696 }
697
698 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
699 {
700   if (this->isSalomeMode()) 
701     theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
702   else 
703     theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
704 }
705
706 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
707 {
708   showPropertyPanel();
709   QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
710   ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
711                                                                 myModuleConnector);
712
713   myPropertyPanel->cleanContent();
714   aFactory.createWidget(myPropertyPanel->contentWidget());
715
716   QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
717   foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
718     bool isStoreValue = !theOperation->isEditOperation() &&
719                         !aWidget->getDefaultValue().empty() &&
720                         !aWidget->isComputedDefault();
721     aWidget->setFeature(theOperation->feature(), isStoreValue);
722     aWidget->enableFocusProcessing();
723   }
724   
725   myPropertyPanel->setModelWidgets(aWidgets);
726   theOperation->setPropertyPanel(myPropertyPanel);
727
728   myModule->propertyPanelDefined(theOperation);
729
730   myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
731 }
732
733 bool XGUI_Workshop::event(QEvent * theEvent)
734 {
735   PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
736   if (aPostponedEv) {
737     std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
738     processEvent(aEventPtr);
739     return true;
740   }
741   return false;
742 }
743
744 /*
745  *
746  */
747 void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
748 {
749   if (!theMessage) {
750 #ifdef _DEBUG
751     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
752 #endif
753     return;
754   }
755   ActionInfo aFeatureInfo;
756   aFeatureInfo.initFrom(theMessage);
757   // Remember features icons
758   myIcons[QString::fromStdString(theMessage->id())] = aFeatureInfo.iconFile;
759
760   QString aWchName = QString::fromStdString(theMessage->workbenchId());
761   QStringList aNestedFeatures =
762       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
763   QString aDocKind = QString::fromStdString(theMessage->documentKind());
764   if (isSalomeMode()) {
765     QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo);
766     salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures);
767     salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind);
768
769     myActionsMgr->addCommand(aAction);
770     myModule->actionCreated(aAction);
771   } else {
772     //Find or create Workbench
773     AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
774     AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
775     if (!aPage) {
776       aPage = addWorkbench(aWchName);
777     }
778     //Find or create Group
779     QString aGroupName = QString::fromStdString(theMessage->groupId());
780     AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
781     if (!aGroup) {
782       aGroup = aPage->addGroup(aGroupName);
783     }
784     // Check if hotkey sequence is already defined:
785     QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut);
786     if(aHotKey != aFeatureInfo.shortcut) {
787       aFeatureInfo.shortcut = aHotKey;
788     }
789     // Create feature...
790     AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
791                                                        aDocKind,
792                                                        aNestedFeatures);
793     // Enrich created button with accept/abort buttons if necessary
794     AppElements_Button* aButton = aCommand->button();
795     if (aButton->isColumnButton()) {
796       QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
797       QList<QAction*> anActList;
798       if (aNestedActions.contains("accept")) {
799         QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, aButton);
800         connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
801         anActList << anAction;
802       }
803       if (aNestedActions.contains("abort")) {
804         QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, aButton);
805         connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
806         anActList << anAction;
807       }
808       aButton->setAdditionalButtons(anActList);
809     }
810     myActionsMgr->addCommand(aCommand);
811     myModule->actionCreated(aCommand);
812   }
813 }
814
815 /*
816  * Makes a signal/slot connections between Property Panel
817  * and given operation. The given operation becomes a
818  * current operation and previous operation if exists
819  */
820 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
821 {
822   QAction* aCommand = 0;
823   if (isSalomeMode()) {
824     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
825   } else {
826     AppElements_MainMenu* aMenu = myMainWindow->menuObject();
827     FeaturePtr aFeature = theOperation->feature();
828     if(aFeature)
829       aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
830   }
831   //Abort operation on uncheck the command
832   if (aCommand) {
833     connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
834   }
835 }
836
837 /*
838  * Saves document with given name.
839  */
840 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
841 {
842   QApplication::restoreOverrideCursor();
843   SessionPtr aMgr = ModelAPI_Session::get();
844   aMgr->save(theName.toLatin1().constData(), theFileNames);
845   QApplication::restoreOverrideCursor();
846 }
847
848 bool XGUI_Workshop::isActiveOperationAborted()
849 {
850   return myOperationMgr->abortAllOperations();
851 }
852
853 //******************************************************
854 void XGUI_Workshop::onExit()
855 {
856   SessionPtr aMgr = ModelAPI_Session::get();
857   if (aMgr->isModified()) {
858     int anAnswer = QMessageBox::question(
859         myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
860         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
861     if (anAnswer == QMessageBox::Save) {
862       bool saved = onSave();
863       if (!saved) {
864         return;
865       }
866     } else if (anAnswer == QMessageBox::Cancel) {
867       return;
868     }
869   }
870   qApp->exit();
871 }
872
873 //******************************************************
874 void XGUI_Workshop::onNew()
875 {
876   QApplication::setOverrideCursor(Qt::WaitCursor);
877   if (objectBrowser() == 0) {
878     createDockWidgets();
879     mySelector->connectViewers();
880   }
881   myViewerProxy->connectToViewer();
882   showObjectBrowser();
883   if (!isSalomeMode()) {
884     myMainWindow->showPythonConsole();
885     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
886     aWnd->showMaximized();
887     updateCommandStatus();
888   }
889   myContextMenuMgr->connectViewer();
890   QApplication::restoreOverrideCursor();
891 }
892
893 //******************************************************
894 void XGUI_Workshop::onOpen()
895 {
896   if(!isActiveOperationAborted())
897     return;
898   //save current file before close if modified
899   SessionPtr aSession = ModelAPI_Session::get();
900   if (aSession->isModified()) {
901     //TODO(sbh): re-launch the app?
902     int anAnswer = QMessageBox::question(
903         myMainWindow, tr("Save current file"),
904         tr("The document is modified, save before opening another?"),
905         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
906     if (anAnswer == QMessageBox::Save) {
907       onSave();
908     } else if (anAnswer == QMessageBox::Cancel) {
909       return;
910     }
911     aSession->closeAll();
912     myCurrentDir = "";
913   }
914
915   //show file dialog, check if readable and open
916   myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
917   if (myCurrentDir.isEmpty())
918     return;
919   QFileInfo aFileInfo(myCurrentDir);
920   if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
921     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
922     myCurrentDir = "";
923     return;
924   }
925   QApplication::setOverrideCursor(Qt::WaitCursor);
926   aSession->load(myCurrentDir.toLatin1().constData());
927   myObjectBrowser->rebuildDataTree();
928   displayAllResults();
929   updateCommandStatus();
930   QApplication::restoreOverrideCursor();
931 }
932
933 //******************************************************
934 bool XGUI_Workshop::onSave()
935 {
936   if(!isActiveOperationAborted())
937     return false;
938   if (myCurrentDir.isEmpty()) {
939     return onSaveAs();
940   }
941   std::list<std::string> aFiles;
942   saveDocument(myCurrentDir, aFiles);
943   updateCommandStatus();
944   if (!isSalomeMode())
945     myMainWindow->setModifiedState(false);
946   return true;
947 }
948
949 //******************************************************
950 bool XGUI_Workshop::onSaveAs()
951 {
952   if(!isActiveOperationAborted())
953     return false;
954   QFileDialog dialog(mainWindow());
955   dialog.setWindowTitle(tr("Select directory to save files..."));
956   dialog.setFileMode(QFileDialog::Directory);
957   dialog.setFilter(tr("Folders (*)"));
958   dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
959   dialog.setViewMode(QFileDialog::Detail);
960
961   if (!dialog.exec()) {
962     return false;
963   }
964   QString aTempDir = dialog.selectedFiles().first();
965   QDir aDir(aTempDir);
966   if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
967     int answer = QMessageBox::question(
968         myMainWindow,
969         //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
970         tr("Save"),
971         tr("The folder already contains some files, save anyway?"),
972         QMessageBox::Save | QMessageBox::Cancel);
973     if (answer == QMessageBox::Cancel) {
974       return false;
975     }
976   }
977   myCurrentDir = aTempDir;
978   if (!isSalomeMode()) {
979     myMainWindow->setCurrentDir(myCurrentDir, false);
980     myMainWindow->setModifiedState(false);
981   }
982   return onSave();
983 }
984
985 //******************************************************
986 void XGUI_Workshop::onUndo(int theTimes)
987 {
988   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
989   SessionPtr aMgr = ModelAPI_Session::get();
990   if (aMgr->isOperation())
991     operationMgr()->onAbortOperation();
992   for (int i = 0; i < theTimes; ++i) {
993     aMgr->undo();
994   }
995   updateCommandStatus();
996 }
997
998 //******************************************************
999 void XGUI_Workshop::onRedo(int theTimes)
1000 {
1001   // the viewer update should be blocked in order to avoid the features blinking. For the created
1002   // feature a results are created, the flush of the created signal caused the viewer redisplay for
1003   // each created result. After a redisplay signal is flushed. So, the viewer update is blocked until
1004   // redo of all possible objects happens
1005   bool isUpdateEnabled = myDisplayer->enableUpdateViewer(false);
1006
1007   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
1008   SessionPtr aMgr = ModelAPI_Session::get();
1009   if (aMgr->isOperation())
1010     operationMgr()->onAbortOperation();
1011   for (int i = 0; i < theTimes; ++i) {
1012     aMgr->redo();
1013   }
1014   updateCommandStatus();
1015
1016   // unblock the viewer update functionality and make update on purpose
1017   myDisplayer->enableUpdateViewer(isUpdateEnabled);
1018   myDisplayer->updateViewer();
1019 }
1020
1021 //******************************************************
1022 void XGUI_Workshop::onRebuild()
1023 {
1024   SessionPtr aMgr = ModelAPI_Session::get();
1025   bool aWasOperation = aMgr->isOperation(); // keep this value
1026   if (!aWasOperation) {
1027     aMgr->startOperation("Rebuild");
1028   }
1029   static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
1030   Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
1031     new Events_Message(aRebuildEvent, this)));
1032   if (!aWasOperation) {
1033     aMgr->finishOperation();
1034   }
1035 }
1036
1037 //******************************************************
1038 void XGUI_Workshop::onPreferences()
1039 {
1040   ModuleBase_Prefs aModif;
1041   ModuleBase_Preferences::editPreferences(aModif);
1042   if (aModif.size() > 0) {
1043     QString aSection;
1044     foreach (ModuleBase_Pref aPref, aModif)
1045     {
1046       aSection = aPref.first;
1047       if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
1048         if (!isSalomeMode())
1049           myMainWindow->viewer()->updateFromResources();
1050       } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
1051         if (!isSalomeMode())
1052           myMainWindow->menuObject()->updateFromResources();
1053       }
1054     }
1055   }
1056 }
1057
1058 //******************************************************
1059 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
1060 {
1061   QString libName = QString::fromStdString(library(theModule.toStdString()));
1062   if (libName.isEmpty()) {
1063     qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
1064     return 0;
1065   }
1066
1067   QString err;
1068   CREATE_FUNC crtInst = 0;
1069
1070 #ifdef WIN32
1071   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
1072   if (!modLib) {
1073     LPVOID lpMsgBuf;
1074     ::FormatMessage(
1075         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1076         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1077     QString aMsg((char*) &lpMsgBuf);
1078     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
1079     ::LocalFree(lpMsgBuf);
1080   } else {
1081     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
1082     if (!crtInst) {
1083       LPVOID lpMsgBuf;
1084       ::FormatMessage(
1085           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
1086               | FORMAT_MESSAGE_IGNORE_INSERTS,
1087           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1088       QString aMsg((char*) &lpMsgBuf);
1089       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
1090       ::LocalFree(lpMsgBuf);
1091     }
1092   }
1093 #else
1094   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
1095   if ( !modLib ) {
1096     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
1097   } else {
1098     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
1099     if ( !crtInst ) {
1100       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
1101     }
1102   }
1103 #endif
1104
1105   ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
1106
1107   if (!err.isEmpty()) {
1108     if (mainWindow()) {
1109       Events_Error::send(err.toStdString());
1110     } else {
1111       qWarning(qPrintable(err));
1112     }
1113   }
1114   return aModule;
1115 }
1116
1117 //******************************************************
1118 bool XGUI_Workshop::activateModule()
1119 {
1120   Config_ModuleReader aModuleReader;
1121   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
1122   myModule = loadModule(moduleName);
1123   if (!myModule)
1124     return false;
1125
1126   connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
1127     myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
1128   connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
1129     myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
1130
1131   myModule->createFeatures();
1132   myActionsMgr->update();
1133   return true;
1134 }
1135
1136 //******************************************************
1137 void XGUI_Workshop::updateCommandStatus()
1138 {
1139   QList<QAction*> aCommands;
1140   if (isSalomeMode()) {  // update commands in SALOME mode
1141     aCommands = salomeConnector()->commandList();
1142   } else {
1143     AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
1144     foreach (AppElements_Command* aCmd, aMenuBar->features())
1145       aCommands.append(aCmd);
1146   }
1147   SessionPtr aMgr = ModelAPI_Session::get();
1148   if (aMgr->hasModuleDocument()) {
1149     QAction *aUndoCmd, *aRedoCmd;
1150     foreach(QAction* aCmd, aCommands) {
1151       QString aId = aCmd->data().toString();
1152       if (aId == "UNDO_CMD")
1153         aUndoCmd = aCmd;
1154       else if (aId == "REDO_CMD")
1155         aRedoCmd = aCmd;
1156       else
1157         // Enable all commands
1158         aCmd->setEnabled(true);
1159     }
1160
1161     aUndoCmd->setEnabled(myModule->canUndo());
1162     aRedoCmd->setEnabled(myModule->canRedo());
1163     updateHistory();
1164   } else {
1165     foreach(QAction* aCmd, aCommands) {
1166       QString aId = aCmd->data().toString();
1167       if (aId == "NEW_CMD")
1168         aCmd->setEnabled(true);
1169       else if (aId == "EXIT_CMD")
1170         aCmd->setEnabled(true);
1171       else
1172         aCmd->setEnabled(false);
1173     }
1174   }
1175   myActionsMgr->update();
1176   emit commandStatusUpdated();
1177 }
1178
1179 void XGUI_Workshop::updateHistory()
1180 {
1181   std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
1182   QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
1183   emit updateUndoHistory(aUndoRes);
1184
1185   std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
1186   QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
1187   emit updateRedoHistory(aRedoRes);
1188 }
1189
1190 //******************************************************
1191 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
1192 {
1193   QDockWidget* aObjDock = new QDockWidget(theParent);
1194   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1195   aObjDock->setWindowTitle(tr("Object browser"));
1196   aObjDock->setStyleSheet(
1197       "::title { position: relative; padding-left: 5px; text-align: left center }");
1198   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
1199   connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
1200           SLOT(changeCurrentDocument(ObjectPtr)));
1201   aObjDock->setWidget(myObjectBrowser);
1202
1203   myContextMenuMgr->connectObjectBrowser();
1204   return aObjDock;
1205 }
1206
1207 //******************************************************
1208 /*
1209  * Creates dock widgets, places them in corresponding area
1210  * and tabifies if necessary.
1211  */
1212 void XGUI_Workshop::createDockWidgets()
1213 {
1214   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1215   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
1216   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
1217   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
1218   myPropertyPanel->setupActions(myActionsMgr);
1219   myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1220   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
1221   hidePropertyPanel();  ///<! Invisible by default
1222   hideObjectBrowser();
1223   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
1224   myPropertyPanel->installEventFilter(myOperationMgr);
1225
1226   QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
1227   connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
1228   QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
1229   connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
1230   connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
1231   connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
1232           myOperationMgr,  SLOT(onKeyReleased(QKeyEvent*)));
1233   connect(myOperationMgr,  SIGNAL(validationStateChanged(bool)),
1234           aOkAct,          SLOT(setEnabled(bool)));
1235   QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
1236   connect(myOperationMgr,  SIGNAL(nestedStateChanged(bool)),
1237           aAcceptAllAct,   SLOT(setEnabled(bool)));
1238
1239 }
1240
1241 //******************************************************
1242 void XGUI_Workshop::showPropertyPanel()
1243 {
1244   QAction* aViewAct = myPropertyPanel->toggleViewAction();
1245   ///<! Restore ability to close panel from the window's menu
1246   aViewAct->setEnabled(true);
1247   myPropertyPanel->show();
1248   myPropertyPanel->raise();
1249 }
1250
1251 //******************************************************
1252 void XGUI_Workshop::hidePropertyPanel()
1253 {
1254   QAction* aViewAct = myPropertyPanel->toggleViewAction();
1255   ///<! Do not allow to show empty property panel
1256   aViewAct->setEnabled(false);
1257   myPropertyPanel->hide();
1258 }
1259
1260 //******************************************************
1261 void XGUI_Workshop::showObjectBrowser()
1262 {
1263   myObjectBrowser->parentWidget()->show();
1264 }
1265
1266 //******************************************************
1267 void XGUI_Workshop::hideObjectBrowser()
1268 {
1269   myObjectBrowser->parentWidget()->hide();
1270 }
1271
1272 //******************************************************
1273 void XGUI_Workshop::onFeatureTriggered()
1274 {
1275   QAction* aCmd = dynamic_cast<QAction*>(sender());
1276   if (aCmd) {
1277     QString aId = salomeConnector()->commandId(aCmd);
1278     if (!aId.isNull())
1279       myModule->launchOperation(aId);
1280   }
1281 }
1282
1283 //******************************************************
1284 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1285 {
1286   SessionPtr aMgr = ModelAPI_Session::get();
1287   if (theObj) {
1288     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1289     if (aPart) {
1290       DocumentPtr aPartDoc = aPart->partDoc();
1291       if (aPartDoc) {
1292         aMgr->setActiveDocument(aPartDoc);
1293         return;
1294       }
1295     }
1296   }
1297   aMgr->setActiveDocument(aMgr->moduleDocument());
1298 }
1299
1300 //******************************************************
1301 void XGUI_Workshop::salomeViewerSelectionChanged()
1302 {
1303   emit salomeViewerSelection();
1304 }
1305
1306 //**************************************************************
1307 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
1308 {
1309   return mySalomeConnector->viewer();
1310 }
1311
1312 //**************************************************************
1313 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1314 {
1315   QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1316   if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1317     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1318     activatePart(aPart);
1319   } else if (theId == "DEACTIVATE_PART_CMD")
1320     activatePart(ResultPartPtr());
1321   else if (theId == "DELETE_CMD")
1322     deleteObjects();
1323   else if (theId == "COLOR_CMD")
1324     changeColor(aObjects);
1325   else if (theId == "SHOW_CMD")
1326     showObjects(aObjects, true);
1327   else if (theId == "HIDE_CMD")
1328     showObjects(aObjects, false);
1329   else if (theId == "SHOW_ONLY_CMD")
1330     showOnlyObjects(aObjects);
1331   else if (theId == "SHADING_CMD")
1332     setDisplayMode(aObjects, XGUI_Displayer::Shading);
1333   else if (theId == "WIREFRAME_CMD")
1334     setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1335   else if (theId == "HIDEALL_CMD")
1336     myDisplayer->eraseAll();
1337   else if (theId == "EDIT_CMD") {
1338     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
1339     if (aFeature)
1340       myModule->editFeature(aFeature);
1341   }
1342 }
1343
1344 //**************************************************************
1345 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1346 {
1347   if (!myPartActivating) {
1348     myPartActivating = true;
1349     if (theFeature)
1350       theFeature->activate();
1351     changeCurrentDocument(theFeature);
1352     myObjectBrowser->activatePart(theFeature);
1353     myPartActivating = false;
1354   }
1355   updateCommandStatus();
1356 }
1357
1358 //**************************************************************
1359 //void XGUI_Workshop::activateLastPart()
1360 //{
1361 //  SessionPtr aMgr = ModelAPI_Session::get();
1362 //  DocumentPtr aDoc = aMgr->moduleDocument();
1363 //  std::string aGrpName = ModelAPI_ResultPart::group();
1364 //  ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1365 //  ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1366 //  if (aPart) {
1367 //    activatePart(aPart);
1368 //  }
1369 //}
1370
1371 //**************************************************************
1372 void XGUI_Workshop::deleteObjects()
1373 {
1374   ModuleBase_IModule* aModule = module();
1375   // 1. allow the module to delete objects, do nothing if it has succeed
1376   if (aModule->deleteObjects())
1377     return;
1378
1379   if (!isActiveOperationAborted())
1380     return;
1381   QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1382   // 1. start operation
1383   QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1384   aDescription += tr(" %1");
1385   QStringList aObjectNames;
1386   foreach (ObjectPtr aObj, anObjects) {
1387     if (!aObj->data().get())
1388       continue;
1389     aObjectNames << QString::fromStdString(aObj->data()->name());
1390   }
1391   aDescription = aDescription.arg(aObjectNames.join(", "));
1392
1393   SessionPtr aMgr = ModelAPI_Session::get();
1394   aMgr->startOperation(aDescription.toStdString());
1395   // 2. close the documents of the removed parts if the result part is in a list of selected objects
1396   foreach (ObjectPtr aObj, anObjects)
1397   {
1398     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1399     if (aPart) {
1400       DocumentPtr aDoc = aObj->document();
1401       if (aDoc == aMgr->activeDocument()) {
1402         aDoc->close();
1403       }
1404     }
1405   }
1406   // 3. delete objects
1407   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1408   std::set<FeaturePtr> anIgnoredFeatures;
1409   if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1410     myDisplayer->updateViewer();
1411     aMgr->finishOperation();
1412     updateCommandStatus();
1413   }
1414   else {
1415     aMgr->abortOperation();
1416   }
1417 }
1418
1419 //**************************************************************
1420 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1421                                    std::set<FeaturePtr> theIgnoredFeatures,
1422                                    QWidget* theParent,
1423                                    const bool theAskAboutDeleteReferences)
1424 {
1425   // 1. find all referenced features
1426   std::set<FeaturePtr> aRefFeatures;
1427   foreach (ObjectPtr aObj, theList) {
1428     FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1429     if (aFeature.get() != NULL) {
1430       aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
1431     }
1432   }
1433   // 2. warn about the references remove, break the delete operation if the user chose it
1434   if (theAskAboutDeleteReferences && !aRefFeatures.empty()) {
1435     QStringList aRefNames;
1436     std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1437                                          aLast = aRefFeatures.end();
1438     for (; anIt != aLast; anIt++) {
1439       aRefNames.append((*anIt)->name().c_str());
1440     }
1441     QString aNames = aRefNames.join(", ");
1442
1443     QMessageBox::StandardButton aRes = QMessageBox::warning(
1444         theParent, tr("Delete features"),
1445         QString(tr("Selected features are used in the following features: %1.\
1446 These features will be deleted also. Would you like to continue?")).arg(aNames),
1447         QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1448     if (aRes != QMessageBox::Yes)
1449       return false;
1450   }
1451
1452   // 3. remove referenced features
1453   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1454                                        aLast = aRefFeatures.end();
1455   for (; anIt != aLast; anIt++) {
1456     FeaturePtr aFeature = (*anIt);
1457     DocumentPtr aDoc = aFeature->document();
1458     if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1459       aDoc->removeFeature(aFeature);
1460   }
1461
1462   // 4. remove the parameter features
1463   foreach (ObjectPtr aObj, theList) {
1464     FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1465     if (aFeature) {
1466       DocumentPtr aDoc = aObj->document();
1467       if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1468         aDoc->removeFeature(aFeature);
1469     }
1470   }
1471   return true;
1472 }
1473
1474 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1475 {
1476   bool isFoundResultType = false;
1477   foreach(ObjectPtr anObj, theObjects)
1478   {
1479     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1480     if (aResult.get() == NULL)
1481       continue;
1482
1483     isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1484     if (isFoundResultType)
1485       break;
1486   }
1487   return isFoundResultType;
1488 }
1489
1490 //**************************************************************
1491 bool XGUI_Workshop::canChangeColor() const
1492 {
1493   QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1494
1495   std::set<std::string> aTypes;
1496   aTypes.insert(ModelAPI_ResultGroup::group());
1497   aTypes.insert(ModelAPI_ResultConstruction::group());
1498   aTypes.insert(ModelAPI_ResultBody::group());
1499   return hasResults(aObjects, aTypes);
1500 }
1501
1502 //**************************************************************
1503 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1504 {
1505   std::vector<int> aColor;
1506   foreach(ObjectPtr anObject, theObjects) {
1507
1508     AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1509     aColor.resize(3);
1510     anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1511     if (!aColor.empty())
1512       break;
1513   }
1514   if (aColor.size() != 3)
1515     return;
1516
1517   // 2. show the dialog to change the value
1518   QDialog* aDlg = new QDialog();
1519   QVBoxLayout* aLay = new QVBoxLayout(aDlg);
1520
1521   QtxColorButton* aColorBtn = new QtxColorButton(aDlg);
1522   aColorBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1523
1524   aLay->addWidget(aColorBtn);
1525   aColorBtn->setColor(QColor(aColor[0], aColor[1], aColor[2]));
1526
1527   QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
1528                                                     Qt::Horizontal, aDlg);
1529   connect(aButtons, SIGNAL(accepted()), aDlg, SLOT(accept()));
1530   connect(aButtons, SIGNAL(rejected()), aDlg, SLOT(reject()));
1531   aLay->addWidget(aButtons);
1532
1533   aDlg->move(QCursor::pos());
1534   bool isDone = aDlg->exec() == QDialog::Accepted;
1535   if (!isDone)
1536     return;
1537
1538   QColor aColorResult = aColorBtn->color();
1539   int aRedResult = aColorResult.red(),
1540       aGreenResult = aColorResult.green(),
1541       aBlueResult = aColorResult.blue();
1542
1543   if (aRedResult == aColor[0] && aGreenResult == aColor[1] && aBlueResult == aColor[2])
1544     return;
1545
1546   // 3. abort the previous operation and start a new one
1547   SessionPtr aMgr = ModelAPI_Session::get();
1548   bool aWasOperation = aMgr->isOperation(); // keep this value
1549   if (!aWasOperation) {
1550     QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1551     aMgr->startOperation(aDescription.toStdString());
1552   }
1553
1554   // 4. set the value to all results
1555   AttributeIntArrayPtr aColorAttr;
1556   foreach(ObjectPtr anObj, theObjects) {
1557     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1558     if (aResult.get() != NULL) {
1559       aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1560       if (aColorAttr.get() != NULL) {
1561         if (!aColorAttr->size()) {
1562           aColorAttr->setSize(3);
1563         }
1564         aColorAttr->setValue(0, aRedResult);
1565         aColorAttr->setValue(1, aGreenResult);
1566         aColorAttr->setValue(2, aBlueResult);
1567       }
1568     }
1569   }
1570   if (!aWasOperation)
1571     aMgr->finishOperation();
1572   updateCommandStatus();
1573 }
1574
1575 //**************************************************************
1576 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1577 {
1578   foreach (ObjectPtr aObj, theList)
1579   {
1580     if (isVisible) {
1581       displayObject(aObj);
1582     } else {
1583       myDisplayer->erase(aObj, false);
1584     }
1585   }
1586   myDisplayer->updateViewer();
1587 }
1588
1589 //**************************************************************
1590 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1591 {
1592   myDisplayer->showOnly(theList);
1593 }
1594
1595
1596 //**************************************************************
1597 void XGUI_Workshop::registerValidators() const
1598 {
1599   SessionPtr aMgr = ModelAPI_Session::get();
1600   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1601 }
1602
1603 //**************************************************************
1604 void XGUI_Workshop::displayAllResults()
1605 {
1606   SessionPtr aMgr = ModelAPI_Session::get();
1607   DocumentPtr aRootDoc = aMgr->moduleDocument();
1608   displayDocumentResults(aRootDoc);
1609   for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1610     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1611     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1612     displayDocumentResults(aPart->partDoc());
1613   }
1614   myDisplayer->updateViewer();
1615 }
1616
1617 //**************************************************************
1618 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1619 {
1620   if (!theDoc)
1621     return;
1622   displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1623   displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1624 }
1625
1626 //**************************************************************
1627 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1628 {
1629   for (int i = 0; i < theDoc->size(theGroup); i++)
1630     displayObject(theDoc->object(theGroup, i));
1631 }
1632
1633 //**************************************************************
1634 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1635 {
1636   foreach(ObjectPtr aObj, theList) {
1637     myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1638   }
1639   if (theList.size() > 0)
1640     myDisplayer->updateViewer();
1641 }
1642
1643 //**************************************************************
1644 void XGUI_Workshop::closeDocument()
1645 {
1646   ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1647   while (anOperation) {
1648     anOperation->abort();
1649     anOperation = operationMgr()->currentOperation();
1650   }
1651   myDisplayer->closeLocalContexts();
1652   myDisplayer->eraseAll();
1653   objectBrowser()->clearContent();
1654
1655   SessionPtr aMgr = ModelAPI_Session::get();
1656   aMgr->closeAll();
1657   objectBrowser()->clearContent();
1658 }
1659
1660 //**************************************************************
1661 bool XGUI_Workshop::displayObject(ObjectPtr theObj)
1662 {
1663   if (!myModule->canDisplayObject(theObj))
1664     return false;
1665
1666   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
1667   if (aBody.get() != NULL) {
1668     int aNb = myDisplayer->objectsCount();
1669     myDisplayer->display(theObj, false);
1670     if (aNb == 0)
1671       viewer()->fitAll();
1672   } else 
1673     myDisplayer->display(theObj, false);
1674
1675   return true;
1676 }
1677
1678 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1679 {
1680   XGUI_HistoryMenu* aMenu = NULL;
1681   if (isSalomeMode()) {
1682     QAction* anAction = qobject_cast<QAction*>(theObject);
1683     if (!anAction)
1684       return;
1685     aMenu = new XGUI_HistoryMenu(anAction);
1686   } else {
1687     QToolButton* aButton =  qobject_cast<QToolButton*>(theObject);
1688     aMenu = new XGUI_HistoryMenu(aButton);
1689   }
1690   connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1691   connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1692 }
1693
1694 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1695 {
1696   QList<ActionInfo> aResult;
1697   std::list<std::string>::const_iterator it = theList.cbegin();
1698   for (; it != theList.cend(); it++) {
1699     QString anId = QString::fromStdString(*it);
1700     bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1701     if (isEditing) {
1702       anId.chop(ModuleBase_Operation::EditSuffix().size());
1703     }
1704     ActionInfo anInfo = myActionsMgr->actionInfoById(anId);
1705     if (isEditing) {
1706       anInfo.text = anInfo.text.prepend("Modify ");
1707     }
1708     aResult << anInfo;
1709   }
1710   return aResult;
1711 }