1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
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>
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>
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_Preferences.h>
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>
44 //#include <PartSetPlugin_Part.h>
46 #include <Events_Loop.h>
47 #include <Events_Error.h>
48 #include <Events_LongOp.h>
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 #include <ModuleBase_Tools.h>
61 #include <Config_Common.h>
62 #include <Config_FeatureMessage.h>
63 #include <Config_PointerMessage.h>
64 #include <Config_ModuleReader.h>
65 #include <Config_PropManager.h>
66 #include <Config_SelectionFilterMessage.h>
68 #include <QApplication>
69 #include <QFileDialog>
70 #include <QMessageBox>
71 #include <QMdiSubWindow>
72 #include <QPushButton>
73 #include <QDockWidget>
78 #include <QToolButton>
81 #include <QDialogButtonBox>
82 #include <QHBoxLayout>
83 #include <QtxColorButton.h>
96 //#define DEBUG_FEATURE_CREATED
97 //#define DEBUG_FEATURE_REDISPLAY
99 QMap<QString, QString> XGUI_Workshop::myIcons;
102 QIcon XGUI_Workshop::featureIcon(const FeaturePtr& theFeature)
106 std::string aKind = theFeature->getKind();
107 QString aId(aKind.c_str());
108 if (!myIcons.contains(aId))
111 QString anIconString = myIcons[aId];
113 ModelAPI_ExecState aState = theFeature->data()->execState();
115 case ModelAPI_StateDone:
116 case ModelAPI_StateNothing: {
117 anIcon = QIcon(anIconString);
120 case ModelAPI_StateMustBeUpdated: {
121 anIcon = ModuleBase_Tools::lighter(anIconString);
124 case ModelAPI_StateExecFailed: {
125 anIcon = ModuleBase_Tools::composite(":pictures/exec_state_failed.png", anIconString);
128 case ModelAPI_StateInvalidArgument: {
129 anIcon = ModuleBase_Tools::composite(":pictures/exec_state_invalid_parameters.png",
138 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
140 myCurrentDir(QString()),
142 mySalomeConnector(theConnector),
146 myUpdatePrefs(false),
147 myPartActivating(false)
149 myMainWindow = mySalomeConnector ? 0 : new AppElements_MainWindow();
151 myDisplayer = new XGUI_Displayer(this);
153 mySelector = new XGUI_SelectionMgr(this);
154 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
156 myOperationMgr = new XGUI_OperationMgr(this);
157 myActionsMgr = new XGUI_ActionsMgr(this);
158 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
159 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
160 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
161 SLOT(onContextMenuCommand(const QString&, bool)));
163 myViewerProxy = new XGUI_ViewerProxy(this);
164 connect(myViewerProxy, SIGNAL(selectionChanged()),
165 myActionsMgr, SLOT(updateOnViewSelection()));
167 myModuleConnector = new XGUI_ModuleConnector(this);
169 connect(myOperationMgr, SIGNAL(operationStarted(ModuleBase_Operation*)),
170 SLOT(onOperationStarted(ModuleBase_Operation*)));
171 connect(myOperationMgr, SIGNAL(operationResumed(ModuleBase_Operation*)),
172 SLOT(onOperationResumed(ModuleBase_Operation*)));
173 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
174 SLOT(onOperationStopped(ModuleBase_Operation*)));
175 connect(myOperationMgr, SIGNAL(operationCommitted(ModuleBase_Operation*)),
176 SLOT(onOperationCommitted(ModuleBase_Operation*)));
177 connect(myOperationMgr, SIGNAL(operationAborted(ModuleBase_Operation*)),
178 SLOT(onOperationAborted(ModuleBase_Operation*)));
179 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
180 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
183 //******************************************************
184 XGUI_Workshop::~XGUI_Workshop(void)
189 //******************************************************
190 void XGUI_Workshop::startApplication()
194 Config_PropManager::registerProp("Plugins", "default_path", "Default Path",
195 Config_Prop::Directory, "");
197 //Initialize event listening
198 Events_Loop* aLoop = Events_Loop::loop();
199 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
200 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
201 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
202 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
203 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
204 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
205 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
206 aLoop->registerListener(this, Events_LongOp::eventID());
207 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
208 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
209 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOSHOW));
210 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
211 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
213 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
214 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
216 registerValidators();
218 // Calling of loadCustomProps before activating module is required
219 // by Config_PropManger to restore user-defined path to plugins
220 ModuleBase_Preferences::loadCustomProps();
223 myMainWindow->show();
224 updateCommandStatus();
229 emit applicationStarted();
232 //******************************************************
233 void XGUI_Workshop::initMenu()
235 myContextMenuMgr->createActions();
237 if (isSalomeMode()) {
238 // Create only Undo, Redo commands
239 QAction* aAction = salomeConnector()->addDesktopCommand("UNDO_CMD", tr("Undo"),
240 tr("Undo last command"),
241 QIcon(":pictures/undo.png"),
242 QKeySequence::Undo, false, "MEN_DESK_EDIT");
243 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
244 addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
246 aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
247 QIcon(":pictures/redo.png"), QKeySequence::Redo,
248 false, "MEN_DESK_EDIT");
249 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
250 addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
252 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
253 aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
254 QIcon(":pictures/rebuild.png"), QKeySequence(),
255 false, "MEN_DESK_EDIT");
256 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRebuild()));
257 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
259 aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Export NewGeom..."), tr("Export the current document into a NewGeom file"),
260 QIcon(), QKeySequence(),
261 false, "MEN_DESK_FILE");
262 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSaveAs()));
264 aAction = salomeConnector()->addDesktopCommand("OPEN_CMD", tr("Import NewGeom..."), tr("Import a NewGeom file"),
265 QIcon(), QKeySequence(),
266 false, "MEN_DESK_FILE");
267 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
268 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_FILE");
272 // File commands group
273 AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
275 AppElements_Command* aCommand;
277 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
278 QIcon(":pictures/save.png"), QKeySequence::Save);
279 aCommand->connectTo(this, SLOT(onSave()));
280 //aCommand->disable();
282 QString aUndoId = "UNDO_CMD";
283 aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
284 QIcon(":pictures/undo.png"), QKeySequence::Undo);
285 aCommand->connectTo(this, SLOT(onUndo()));
286 AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
287 addHistoryMenu(aUndoButton,
288 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
291 QString aRedoId = "REDO_CMD";
292 aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
293 QIcon(":pictures/redo.png"), QKeySequence::Redo);
294 aCommand->connectTo(this, SLOT(onRedo()));
295 AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
296 addHistoryMenu(aRedoButton,
297 SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
300 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
301 QIcon(":pictures/rebuild.png"), QKeySequence());
302 aCommand->connectTo(this, SLOT(onRebuild()));
304 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
305 QIcon(":pictures/save.png"), QKeySequence());
306 aCommand->connectTo(this, SLOT(onSaveAs()));
307 //aCommand->disable();
309 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
310 QIcon(":pictures/open.png"), QKeySequence::Open);
311 aCommand->connectTo(this, SLOT(onOpen()));
313 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
314 // QIcon(":pictures/new.png"), QKeySequence::New);
315 //aCommand->connectTo(this, SLOT(onNew()));
317 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
318 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
319 aCommand->connectTo(this, SLOT(onPreferences()));
321 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
322 QIcon(":pictures/close.png"), QKeySequence::Close);
323 aCommand->connectTo(this, SLOT(onExit()));
324 //FIXME: SBH's test action. Can be used for some GUI tests.
326 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
327 // QIcon(":pictures/close.png"), QKeySequence(), true);
328 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
332 //******************************************************
333 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
335 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
336 return aMenuBar->addWorkbench(theName);
339 //******************************************************
340 void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessage)
342 if (QApplication::instance()->thread() != QThread::currentThread()) {
344 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
346 SessionPtr aMgr = ModelAPI_Session::get();
347 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
348 QApplication::postEvent(this, aPostponeEvent);
352 //A message to start feature creation received.
353 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
354 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
355 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
356 if (!aFeatureMsg->isInternal()) {
357 addFeature(aFeatureMsg);
360 // Process creation of Part
361 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
362 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
363 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
364 onFeatureCreatedMsg(aUpdMsg);
366 if (mySalomeConnector)
367 mySalomeConnector->createPreferences();
368 myUpdatePrefs = false;
371 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
372 myUpdatePrefs = true;
375 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
376 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
377 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
378 onFeatureRedisplayMsg(aUpdMsg);
380 //Update property panel on corresponding message. If there is no current operation (no
381 //property panel), or received message has different feature to the current - do nothing.
382 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
383 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
384 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
385 onFeatureUpdatedMsg(anUpdateMsg);
386 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
387 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDelMsg =
388 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
389 onObjectDeletedMsg(aDelMsg);
390 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
391 if (Events_LongOp::isPerformed()) {
392 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
394 QApplication::restoreOverrideCursor();
396 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW)) {
397 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
398 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
399 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
400 QObjectPtrList aList;
401 std::set<ObjectPtr>::const_iterator aIt;
402 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
404 showObjects(aList, true);
405 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE)) {
406 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
407 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
408 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
409 QObjectPtrList aList;
410 std::set<ObjectPtr>::const_iterator aIt;
411 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
413 showObjects(aList, false);
415 //An operation passed by message. Start it, process and commit.
416 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
417 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
418 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
419 //myPropertyPanel->cleanContent();
420 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
422 if (myOperationMgr->startOperation(anOperation)) {
423 myPropertyPanel->updateContentWidget(anOperation->feature());
424 if (!anOperation->getDescription()->hasXmlRepresentation()) {
425 if (anOperation->commit())
426 updateCommandStatus();
429 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
430 myActionsMgr->update();
431 // Find and Activate active part
432 if (myPartActivating)
434 SessionPtr aMgr = ModelAPI_Session::get();
435 DocumentPtr aActiveDoc = aMgr->activeDocument();
436 DocumentPtr aDoc = aMgr->moduleDocument();
437 if (aActiveDoc == aDoc) {
438 activatePart(ResultPartPtr());
441 std::string aGrpName = ModelAPI_ResultPart::group();
442 for (int i = 0; i < aDoc->size(aGrpName); i++) {
443 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
444 if (aPart->partDoc() == aActiveDoc) {
445 activatePart(aPart); // Activate a part which corresponds to active Doc
449 // If not found then activate global document
450 activatePart(ResultPartPtr());
453 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
454 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
455 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
457 ModuleBase_FilterFactory* aFactory = moduleConnector()->selectionFilters();
458 if (!aMsg->attributeId().empty()) {
459 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
463 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
464 // the viewer's update context will not happens until viewer updated is emitted
465 myDisplayer->enableUpdateViewer(false);
466 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
467 // the viewer's update context is unblocked, the viewer's update works
468 myDisplayer->enableUpdateViewer(true);
470 //Show error dialog if error message received.
471 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
473 emit errorOccurred(QString::fromLatin1(anAppError->description()));
477 if (!isSalomeMode()) {
478 SessionPtr aMgr = ModelAPI_Session::get();
479 if (aMgr->isModified() != myMainWindow->isModifiedState())
480 myMainWindow->setModifiedState(aMgr->isModified());
484 //******************************************************
485 QMainWindow* XGUI_Workshop::desktop() const
487 return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
490 //******************************************************
491 void XGUI_Workshop::onStartWaiting()
493 if (Events_LongOp::isPerformed()) {
494 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
498 //******************************************************
499 void XGUI_Workshop::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
501 std::set<ObjectPtr> aFeatures = theMsg->objects();
502 if (myOperationMgr->hasOperation()) {
503 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
504 std::set<ObjectPtr>::const_iterator aIt;
505 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
506 ObjectPtr aNewFeature = (*aIt);
507 if (aNewFeature == aCurrentFeature) {
508 myPropertyPanel->updateContentWidget(aCurrentFeature);
513 myOperationMgr->onValidateOperation();
515 myObjectBrowser->processEvent(theMsg);
518 //******************************************************
519 void XGUI_Workshop::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
521 std::set<ObjectPtr> aObjects = theMsg->objects();
522 std::set<ObjectPtr>::const_iterator aIt;
524 #ifdef DEBUG_FEATURE_REDISPLAY
526 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
527 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
529 QString anInfoStr = anInfo.join(", ");
530 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
533 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
534 ObjectPtr aObj = (*aIt);
536 // Hide the object if it is invalid or concealed one
537 bool aHide = !aObj->data() || !aObj->data()->isValid();
538 if (!aHide) { // check that this is not hidden result
539 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
540 aHide = aRes && aRes->isConcealed();
543 myDisplayer->erase(aObj, false);
545 // Redisplay the visible object or the object of the current operation
546 bool isVisibleObject = myDisplayer->isVisible(aObj);
547 #ifdef DEBUG_FEATURE_REDISPLAY
548 //QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
549 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
552 if (isVisibleObject) { // redisplay visible object
553 //displayObject(aObj); // In order to update presentation
554 // in order to avoid the check whether the object can be redisplayed, the exact method
555 // of redisplay is called. This modification is made in order to have the line is updated
556 // by creation of a horizontal constraint on the line by preselection
557 myDisplayer->redisplay(aObj, false);
558 if (myOperationMgr->hasOperation()) {
559 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
560 if (!aOperation->isEditOperation() &&
561 aOperation->hasObject(aObj) && myDisplayer->isActive(aObj))
562 myDisplayer->deactivate(aObj);
564 } else { // display object if the current operation has it
565 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
566 if (aOperation && aOperation->hasObject(aObj)) {
567 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
568 if (displayObject(aObj)) {
569 // Deactivate object of current operation from selection
570 if (myDisplayer->isActive(aObj))
571 myDisplayer->deactivate(aObj);
577 myDisplayer->updateViewer();
580 //******************************************************
581 void XGUI_Workshop::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
583 std::set<ObjectPtr> aObjects = theMsg->objects();
584 std::set<ObjectPtr>::const_iterator aIt;
585 #ifdef DEBUG_FEATURE_CREATED
587 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
588 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
590 QString anInfoStr = anInfo.join(", ");
591 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
594 //bool aHasPart = false;
595 bool isDisplayed = false;
596 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
597 ObjectPtr anObject = *aIt;
598 // the validity of the data should be checked here in order to avoid display of the objects,
599 // which were created, then deleted, but flush for the creation event happens after that
600 if (!anObject->data() || !anObject->data()->isValid())
602 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
605 // If a feature is created from the aplication's python console
606 // it doesn't stored in the operation mgr and doesn't displayed
608 isDisplayed = displayObject(*aIt);
612 myObjectBrowser->processEvent(theMsg);
614 myDisplayer->updateViewer();
615 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
616 // activateLastPart();
620 //******************************************************
621 void XGUI_Workshop::onObjectDeletedMsg(const std::shared_ptr<ModelAPI_ObjectDeletedMessage>& theMsg)
624 myObjectBrowser->processEvent(theMsg);
625 //std::set<ObjectPtr> aFeatures = theMsg->objects();
628 //******************************************************
629 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
631 setNestedFeatures(theOperation);
633 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
634 connectWithOperation(theOperation);
635 setPropertyPanel(theOperation);
637 updateCommandStatus();
639 myModule->operationStarted(theOperation);
642 //******************************************************
643 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
645 setNestedFeatures(theOperation);
647 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
648 // connectWithOperation(theOperation); already connected
649 setPropertyPanel(theOperation);
651 updateCommandStatus();
653 myModule->operationResumed(theOperation);
657 //******************************************************
658 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
660 ModuleBase_ISelection* aSel = mySelector->selection();
661 QObjectPtrList aObj = aSel->selectedPresentations();
662 //!< No need for property panel
663 updateCommandStatus();
665 myPropertyPanel->cleanContent();
667 // Activate objects created by current operation
668 // in order to clean selection modes
669 // the deactivation should be pefromed in the same place, where the mode is activated,
670 // e.g. activation in the current widget activation, deactivation - in the widget's deactivation
672 //myDisplayer->activateObjects(aModes);
673 myModule->operationStopped(theOperation);
677 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
679 myModule->operationCommitted(theOperation);
682 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
684 myModule->operationAborted(theOperation);
687 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
689 if (this->isSalomeMode())
690 theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
692 theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
695 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
698 QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
699 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
702 myPropertyPanel->cleanContent();
703 aFactory.createWidget(myPropertyPanel->contentWidget());
705 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
706 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
707 bool isStoreValue = !theOperation->isEditOperation() &&
708 !aWidget->getDefaultValue().empty() &&
709 !aWidget->isComputedDefault();
710 aWidget->setFeature(theOperation->feature(), isStoreValue);
711 aWidget->enableFocusProcessing();
714 myPropertyPanel->setModelWidgets(aWidgets);
715 theOperation->setPropertyPanel(myPropertyPanel);
717 myModule->propertyPanelDefined(theOperation);
719 myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
722 bool XGUI_Workshop::event(QEvent * theEvent)
724 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
726 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
727 processEvent(aEventPtr);
736 void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
740 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
744 ActionInfo aFeatureInfo;
745 aFeatureInfo.initFrom(theMessage);
746 // Remember features icons
747 myIcons[QString::fromStdString(theMessage->id())] = aFeatureInfo.iconFile;
749 QString aWchName = QString::fromStdString(theMessage->workbenchId());
750 QStringList aNestedFeatures =
751 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
752 QString aDocKind = QString::fromStdString(theMessage->documentKind());
753 if (isSalomeMode()) {
754 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo);
755 salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures);
756 salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind);
758 myActionsMgr->addCommand(aAction);
759 myModule->actionCreated(aAction);
761 //Find or create Workbench
762 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
763 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
765 aPage = addWorkbench(aWchName);
767 //Find or create Group
768 QString aGroupName = QString::fromStdString(theMessage->groupId());
769 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
771 aGroup = aPage->addGroup(aGroupName);
773 // Check if hotkey sequence is already defined:
774 QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut);
775 if(aHotKey != aFeatureInfo.shortcut) {
776 aFeatureInfo.shortcut = aHotKey;
779 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
782 // Enrich created button with accept/abort buttons if necessary
783 AppElements_Button* aButton = aCommand->button();
784 if (aButton->isColumnButton()) {
785 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
786 QList<QAction*> anActList;
787 if (aNestedActions.contains("accept")) {
788 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, aButton);
789 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
790 anActList << anAction;
792 if (aNestedActions.contains("abort")) {
793 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, aButton);
794 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
795 anActList << anAction;
797 aButton->setAdditionalButtons(anActList);
799 myActionsMgr->addCommand(aCommand);
800 myModule->actionCreated(aCommand);
805 * Makes a signal/slot connections between Property Panel
806 * and given operation. The given operation becomes a
807 * current operation and previous operation if exists
809 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
811 QAction* aCommand = 0;
812 if (isSalomeMode()) {
813 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
815 AppElements_MainMenu* aMenu = myMainWindow->menuObject();
816 FeaturePtr aFeature = theOperation->feature();
818 aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
820 //Abort operation on uncheck the command
822 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
827 * Saves document with given name.
829 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
831 QApplication::restoreOverrideCursor();
832 SessionPtr aMgr = ModelAPI_Session::get();
833 aMgr->save(theName.toLatin1().constData(), theFileNames);
834 QApplication::restoreOverrideCursor();
837 bool XGUI_Workshop::isActiveOperationAborted()
839 return myOperationMgr->abortAllOperations();
842 //******************************************************
843 void XGUI_Workshop::onExit()
845 SessionPtr aMgr = ModelAPI_Session::get();
846 if (aMgr->isModified()) {
847 int anAnswer = QMessageBox::question(
848 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
849 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
850 if (anAnswer == QMessageBox::Save) {
851 bool saved = onSave();
855 } else if (anAnswer == QMessageBox::Cancel) {
862 //******************************************************
863 void XGUI_Workshop::onNew()
865 QApplication::setOverrideCursor(Qt::WaitCursor);
866 if (objectBrowser() == 0) {
868 mySelector->connectViewers();
870 myViewerProxy->connectToViewer();
872 if (!isSalomeMode()) {
873 myMainWindow->showPythonConsole();
874 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
875 aWnd->showMaximized();
876 updateCommandStatus();
878 myContextMenuMgr->connectViewer();
879 QApplication::restoreOverrideCursor();
882 //******************************************************
883 void XGUI_Workshop::onOpen()
885 if(!isActiveOperationAborted())
887 //save current file before close if modified
888 SessionPtr aSession = ModelAPI_Session::get();
889 if (aSession->isModified()) {
890 //TODO(sbh): re-launch the app?
891 int anAnswer = QMessageBox::question(
892 myMainWindow, tr("Save current file"),
893 tr("The document is modified, save before opening another?"),
894 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
895 if (anAnswer == QMessageBox::Save) {
897 } else if (anAnswer == QMessageBox::Cancel) {
900 aSession->closeAll();
904 //show file dialog, check if readable and open
905 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
906 if (myCurrentDir.isEmpty())
908 QFileInfo aFileInfo(myCurrentDir);
909 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
910 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
914 QApplication::setOverrideCursor(Qt::WaitCursor);
915 aSession->load(myCurrentDir.toLatin1().constData());
916 myObjectBrowser->rebuildDataTree();
918 updateCommandStatus();
919 QApplication::restoreOverrideCursor();
922 //******************************************************
923 bool XGUI_Workshop::onSave()
925 if(!isActiveOperationAborted())
927 if (myCurrentDir.isEmpty()) {
930 std::list<std::string> aFiles;
931 saveDocument(myCurrentDir, aFiles);
932 updateCommandStatus();
934 myMainWindow->setModifiedState(false);
938 //******************************************************
939 bool XGUI_Workshop::onSaveAs()
941 if(!isActiveOperationAborted())
943 QFileDialog dialog(mainWindow());
944 dialog.setWindowTitle(tr("Select directory to save files..."));
945 dialog.setFileMode(QFileDialog::Directory);
946 dialog.setFilter(tr("Folders (*)"));
947 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
948 dialog.setViewMode(QFileDialog::Detail);
950 if (!dialog.exec()) {
953 QString aTempDir = dialog.selectedFiles().first();
955 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
956 int answer = QMessageBox::question(
958 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
960 tr("The folder already contains some files, save anyway?"),
961 QMessageBox::Save | QMessageBox::Cancel);
962 if (answer == QMessageBox::Cancel) {
966 myCurrentDir = aTempDir;
967 if (!isSalomeMode()) {
968 myMainWindow->setCurrentDir(myCurrentDir, false);
969 myMainWindow->setModifiedState(false);
974 //******************************************************
975 void XGUI_Workshop::onUndo(int theTimes)
977 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
978 SessionPtr aMgr = ModelAPI_Session::get();
979 if (aMgr->isOperation())
980 operationMgr()->onAbortOperation();
981 for (int i = 0; i < theTimes; ++i) {
984 updateCommandStatus();
987 //******************************************************
988 void XGUI_Workshop::onRedo(int theTimes)
990 // the viewer update should be blocked in order to avoid the features blinking. For the created
991 // feature a results are created, the flush of the created signal caused the viewer redisplay for
992 // each created result. After a redisplay signal is flushed. So, the viewer update is blocked until
993 // redo of all possible objects happens
994 bool isUpdateEnabled = myDisplayer->enableUpdateViewer(false);
996 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
997 SessionPtr aMgr = ModelAPI_Session::get();
998 if (aMgr->isOperation())
999 operationMgr()->onAbortOperation();
1000 for (int i = 0; i < theTimes; ++i) {
1003 updateCommandStatus();
1005 // unblock the viewer update functionality and make update on purpose
1006 myDisplayer->enableUpdateViewer(isUpdateEnabled);
1007 myDisplayer->updateViewer();
1010 //******************************************************
1011 void XGUI_Workshop::onRebuild()
1013 SessionPtr aMgr = ModelAPI_Session::get();
1014 bool aWasOperation = aMgr->isOperation(); // keep this value
1015 if (!aWasOperation) {
1016 aMgr->startOperation("Rebuild");
1018 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
1019 Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
1020 new Events_Message(aRebuildEvent, this)));
1021 if (!aWasOperation) {
1022 aMgr->finishOperation();
1026 //******************************************************
1027 void XGUI_Workshop::onPreferences()
1029 ModuleBase_Prefs aModif;
1030 ModuleBase_Preferences::editPreferences(aModif);
1031 if (aModif.size() > 0) {
1033 foreach (ModuleBase_Pref aPref, aModif)
1035 aSection = aPref.first;
1036 if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
1037 if (!isSalomeMode())
1038 myMainWindow->viewer()->updateFromResources();
1039 } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
1040 if (!isSalomeMode())
1041 myMainWindow->menuObject()->updateFromResources();
1047 //******************************************************
1048 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
1050 QString libName = QString::fromStdString(library(theModule.toStdString()));
1051 if (libName.isEmpty()) {
1052 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
1057 CREATE_FUNC crtInst = 0;
1060 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
1064 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1065 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1066 QString aMsg((char*) &lpMsgBuf);
1067 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
1068 ::LocalFree(lpMsgBuf);
1070 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
1074 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
1075 | FORMAT_MESSAGE_IGNORE_INSERTS,
1076 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1077 QString aMsg((char*) &lpMsgBuf);
1078 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
1079 ::LocalFree(lpMsgBuf);
1083 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
1085 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
1087 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
1089 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
1094 ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
1096 if (!err.isEmpty()) {
1098 Events_Error::send(err.toStdString());
1100 qWarning(qPrintable(err));
1106 //******************************************************
1107 bool XGUI_Workshop::activateModule()
1109 Config_ModuleReader aModuleReader;
1110 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
1111 myModule = loadModule(moduleName);
1115 connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
1116 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
1117 connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
1118 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
1120 myModule->createFeatures();
1121 myActionsMgr->update();
1125 //******************************************************
1126 void XGUI_Workshop::updateCommandStatus()
1128 QList<QAction*> aCommands;
1129 if (isSalomeMode()) { // update commands in SALOME mode
1130 aCommands = salomeConnector()->commandList();
1132 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
1133 foreach (AppElements_Command* aCmd, aMenuBar->features())
1134 aCommands.append(aCmd);
1136 SessionPtr aMgr = ModelAPI_Session::get();
1137 if (aMgr->hasModuleDocument()) {
1138 QAction *aUndoCmd, *aRedoCmd;
1139 foreach(QAction* aCmd, aCommands) {
1140 QString aId = aCmd->data().toString();
1141 if (aId == "UNDO_CMD")
1143 else if (aId == "REDO_CMD")
1146 // Enable all commands
1147 aCmd->setEnabled(true);
1150 aUndoCmd->setEnabled(myModule->canUndo());
1151 aRedoCmd->setEnabled(myModule->canRedo());
1154 foreach(QAction* aCmd, aCommands) {
1155 QString aId = aCmd->data().toString();
1156 if (aId == "NEW_CMD")
1157 aCmd->setEnabled(true);
1158 else if (aId == "EXIT_CMD")
1159 aCmd->setEnabled(true);
1161 aCmd->setEnabled(false);
1164 myActionsMgr->update();
1165 emit commandStatusUpdated();
1168 void XGUI_Workshop::updateHistory()
1170 std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
1171 QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
1172 emit updateUndoHistory(aUndoRes);
1174 std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
1175 QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
1176 emit updateRedoHistory(aRedoRes);
1179 //******************************************************
1180 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
1182 QDockWidget* aObjDock = new QDockWidget(theParent);
1183 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1184 aObjDock->setWindowTitle(tr("Object browser"));
1185 aObjDock->setStyleSheet(
1186 "::title { position: relative; padding-left: 5px; text-align: left center }");
1187 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
1188 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
1189 SLOT(changeCurrentDocument(ObjectPtr)));
1190 aObjDock->setWidget(myObjectBrowser);
1192 myContextMenuMgr->connectObjectBrowser();
1196 //******************************************************
1198 * Creates dock widgets, places them in corresponding area
1199 * and tabifies if necessary.
1201 void XGUI_Workshop::createDockWidgets()
1203 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1204 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
1205 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
1206 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
1207 myPropertyPanel->setupActions(myActionsMgr);
1208 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1209 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
1210 hidePropertyPanel(); ///<! Invisible by default
1211 hideObjectBrowser();
1212 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
1213 myPropertyPanel->installEventFilter(myOperationMgr);
1215 QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
1216 connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
1217 QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
1218 connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
1219 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
1220 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
1221 myOperationMgr, SLOT(onKeyReleased(QKeyEvent*)));
1222 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
1223 aOkAct, SLOT(setEnabled(bool)));
1224 QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
1225 connect(myOperationMgr, SIGNAL(nestedStateChanged(bool)),
1226 aAcceptAllAct, SLOT(setEnabled(bool)));
1230 //******************************************************
1231 void XGUI_Workshop::showPropertyPanel()
1233 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1234 ///<! Restore ability to close panel from the window's menu
1235 aViewAct->setEnabled(true);
1236 myPropertyPanel->show();
1237 myPropertyPanel->raise();
1240 //******************************************************
1241 void XGUI_Workshop::hidePropertyPanel()
1243 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1244 ///<! Do not allow to show empty property panel
1245 aViewAct->setEnabled(false);
1246 myPropertyPanel->hide();
1249 //******************************************************
1250 void XGUI_Workshop::showObjectBrowser()
1252 myObjectBrowser->parentWidget()->show();
1255 //******************************************************
1256 void XGUI_Workshop::hideObjectBrowser()
1258 myObjectBrowser->parentWidget()->hide();
1261 //******************************************************
1262 void XGUI_Workshop::onFeatureTriggered()
1264 QAction* aCmd = dynamic_cast<QAction*>(sender());
1266 QString aId = salomeConnector()->commandId(aCmd);
1268 myModule->launchOperation(aId);
1272 //******************************************************
1273 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1275 SessionPtr aMgr = ModelAPI_Session::get();
1277 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1279 DocumentPtr aPartDoc = aPart->partDoc();
1281 aMgr->setActiveDocument(aPartDoc);
1286 aMgr->setActiveDocument(aMgr->moduleDocument());
1289 //******************************************************
1290 void XGUI_Workshop::salomeViewerSelectionChanged()
1292 emit salomeViewerSelection();
1295 //**************************************************************
1296 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
1298 return mySalomeConnector->viewer();
1301 //**************************************************************
1302 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1304 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1305 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1306 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1307 activatePart(aPart);
1308 } else if (theId == "DEACTIVATE_PART_CMD")
1309 activatePart(ResultPartPtr());
1310 else if (theId == "DELETE_CMD")
1312 else if (theId == "COLOR_CMD")
1313 changeColor(aObjects);
1314 else if (theId == "SHOW_CMD")
1315 showObjects(aObjects, true);
1316 else if (theId == "HIDE_CMD")
1317 showObjects(aObjects, false);
1318 else if (theId == "SHOW_ONLY_CMD")
1319 showOnlyObjects(aObjects);
1320 else if (theId == "SHADING_CMD")
1321 setDisplayMode(aObjects, XGUI_Displayer::Shading);
1322 else if (theId == "WIREFRAME_CMD")
1323 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1324 else if (theId == "HIDEALL_CMD")
1325 myDisplayer->eraseAll();
1326 else if (theId == "EDIT_CMD") {
1327 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
1329 myModule->editFeature(aFeature);
1333 //**************************************************************
1334 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1336 if (!myPartActivating) {
1337 myPartActivating = true;
1339 theFeature->activate();
1340 changeCurrentDocument(theFeature);
1341 myObjectBrowser->activatePart(theFeature);
1342 myPartActivating = false;
1344 updateCommandStatus();
1347 //**************************************************************
1348 //void XGUI_Workshop::activateLastPart()
1350 // SessionPtr aMgr = ModelAPI_Session::get();
1351 // DocumentPtr aDoc = aMgr->moduleDocument();
1352 // std::string aGrpName = ModelAPI_ResultPart::group();
1353 // ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1354 // ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1356 // activatePart(aPart);
1360 //**************************************************************
1361 void XGUI_Workshop::deleteObjects()
1363 ModuleBase_IModule* aModule = module();
1364 // 1. allow the module to delete objects, do nothing if it has succeed
1365 if (aModule->deleteObjects())
1368 if (!isActiveOperationAborted())
1370 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1371 // 1. start operation
1372 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1373 aDescription += tr(" %1");
1374 QStringList aObjectNames;
1375 foreach (ObjectPtr aObj, anObjects) {
1376 if (!aObj->data().get())
1378 aObjectNames << QString::fromStdString(aObj->data()->name());
1380 aDescription = aDescription.arg(aObjectNames.join(", "));
1382 SessionPtr aMgr = ModelAPI_Session::get();
1383 aMgr->startOperation(aDescription.toStdString());
1384 // 2. close the documents of the removed parts if the result part is in a list of selected objects
1385 foreach (ObjectPtr aObj, anObjects)
1387 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1389 DocumentPtr aDoc = aObj->document();
1390 if (aDoc == aMgr->activeDocument()) {
1395 // 3. delete objects
1396 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1397 std::set<FeaturePtr> anIgnoredFeatures;
1398 if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1399 myDisplayer->updateViewer();
1400 aMgr->finishOperation();
1401 updateCommandStatus();
1404 aMgr->abortOperation();
1408 //**************************************************************
1409 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1410 std::set<FeaturePtr> theIgnoredFeatures,
1412 const bool theAskAboutDeleteReferences)
1414 // 1. find all referenced features
1415 std::set<FeaturePtr> aRefFeatures;
1416 foreach (ObjectPtr aObj, theList) {
1417 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1418 if (aFeature.get() != NULL) {
1419 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
1422 // 2. warn about the references remove, break the delete operation if the user chose it
1423 if (theAskAboutDeleteReferences && !aRefFeatures.empty()) {
1424 QStringList aRefNames;
1425 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1426 aLast = aRefFeatures.end();
1427 for (; anIt != aLast; anIt++) {
1428 aRefNames.append((*anIt)->name().c_str());
1430 QString aNames = aRefNames.join(", ");
1432 QMessageBox::StandardButton aRes = QMessageBox::warning(
1433 theParent, tr("Delete features"),
1434 QString(tr("Selected features are used in the following features: %1.\
1435 These features will be deleted also. Would you like to continue?")).arg(aNames),
1436 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1437 if (aRes != QMessageBox::Yes)
1441 // 3. remove referenced features
1442 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1443 aLast = aRefFeatures.end();
1444 for (; anIt != aLast; anIt++) {
1445 FeaturePtr aFeature = (*anIt);
1446 DocumentPtr aDoc = aFeature->document();
1447 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1448 aDoc->removeFeature(aFeature);
1451 // 4. remove the parameter features
1452 foreach (ObjectPtr aObj, theList) {
1453 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1455 DocumentPtr aDoc = aObj->document();
1456 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1457 aDoc->removeFeature(aFeature);
1463 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1465 bool isFoundResultType = false;
1466 foreach(ObjectPtr anObj, theObjects)
1468 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1469 if (aResult.get() == NULL)
1472 isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1473 if (isFoundResultType)
1476 return isFoundResultType;
1479 //**************************************************************
1480 bool XGUI_Workshop::canChangeColor() const
1482 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1484 std::set<std::string> aTypes;
1485 aTypes.insert(ModelAPI_ResultGroup::group());
1486 aTypes.insert(ModelAPI_ResultConstruction::group());
1487 aTypes.insert(ModelAPI_ResultBody::group());
1488 return hasResults(aObjects, aTypes);
1491 //**************************************************************
1492 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1494 std::vector<int> aColor;
1495 foreach(ObjectPtr anObject, theObjects) {
1497 AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1499 anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1500 if (!aColor.empty())
1503 if (aColor.size() != 3)
1506 // 2. show the dialog to change the value
1507 QDialog* aDlg = new QDialog();
1508 QVBoxLayout* aLay = new QVBoxLayout(aDlg);
1510 QtxColorButton* aColorBtn = new QtxColorButton(aDlg);
1511 aColorBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1513 aLay->addWidget(aColorBtn);
1514 aColorBtn->setColor(QColor(aColor[0], aColor[1], aColor[2]));
1516 QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
1517 Qt::Horizontal, aDlg);
1518 connect(aButtons, SIGNAL(accepted()), aDlg, SLOT(accept()));
1519 connect(aButtons, SIGNAL(rejected()), aDlg, SLOT(reject()));
1520 aLay->addWidget(aButtons);
1522 aDlg->move(QCursor::pos());
1523 bool isDone = aDlg->exec() == QDialog::Accepted;
1527 QColor aColorResult = aColorBtn->color();
1528 int aRedResult = aColorResult.red(),
1529 aGreenResult = aColorResult.green(),
1530 aBlueResult = aColorResult.blue();
1532 if (aRedResult == aColor[0] && aGreenResult == aColor[1] && aBlueResult == aColor[2])
1535 // 3. abort the previous operation and start a new one
1536 SessionPtr aMgr = ModelAPI_Session::get();
1537 bool aWasOperation = aMgr->isOperation(); // keep this value
1538 if (!aWasOperation) {
1539 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1540 aMgr->startOperation(aDescription.toStdString());
1543 // 4. set the value to all results
1544 AttributeIntArrayPtr aColorAttr;
1545 foreach(ObjectPtr anObj, theObjects) {
1546 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1547 if (aResult.get() != NULL) {
1548 aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1549 if (aColorAttr.get() != NULL) {
1550 if (!aColorAttr->size()) {
1551 aColorAttr->setSize(3);
1553 aColorAttr->setValue(0, aRedResult);
1554 aColorAttr->setValue(1, aGreenResult);
1555 aColorAttr->setValue(2, aBlueResult);
1560 aMgr->finishOperation();
1561 updateCommandStatus();
1564 //**************************************************************
1565 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1567 foreach (ObjectPtr aObj, theList)
1570 displayObject(aObj);
1572 myDisplayer->erase(aObj, false);
1575 myDisplayer->updateViewer();
1578 //**************************************************************
1579 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1581 myDisplayer->showOnly(theList);
1585 //**************************************************************
1586 void XGUI_Workshop::registerValidators() const
1588 SessionPtr aMgr = ModelAPI_Session::get();
1589 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1592 //**************************************************************
1593 void XGUI_Workshop::displayAllResults()
1595 SessionPtr aMgr = ModelAPI_Session::get();
1596 DocumentPtr aRootDoc = aMgr->moduleDocument();
1597 displayDocumentResults(aRootDoc);
1598 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1599 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1600 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1601 displayDocumentResults(aPart->partDoc());
1603 myDisplayer->updateViewer();
1606 //**************************************************************
1607 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1611 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1612 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1615 //**************************************************************
1616 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1618 for (int i = 0; i < theDoc->size(theGroup); i++)
1619 displayObject(theDoc->object(theGroup, i));
1622 //**************************************************************
1623 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1625 foreach(ObjectPtr aObj, theList) {
1626 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1628 if (theList.size() > 0)
1629 myDisplayer->updateViewer();
1632 //**************************************************************
1633 void XGUI_Workshop::closeDocument()
1635 ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1636 while (anOperation) {
1637 anOperation->abort();
1638 anOperation = operationMgr()->currentOperation();
1640 myDisplayer->closeLocalContexts();
1641 myDisplayer->eraseAll();
1642 objectBrowser()->clearContent();
1644 SessionPtr aMgr = ModelAPI_Session::get();
1646 objectBrowser()->clearContent();
1649 //**************************************************************
1650 bool XGUI_Workshop::displayObject(ObjectPtr theObj)
1652 if (!myModule->canDisplayObject(theObj))
1655 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
1656 if (aBody.get() != NULL) {
1657 int aNb = myDisplayer->objectsCount();
1658 myDisplayer->display(theObj, false);
1662 myDisplayer->display(theObj, false);
1667 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1669 XGUI_HistoryMenu* aMenu = NULL;
1670 if (isSalomeMode()) {
1671 QAction* anAction = qobject_cast<QAction*>(theObject);
1674 aMenu = new XGUI_HistoryMenu(anAction);
1676 QToolButton* aButton = qobject_cast<QToolButton*>(theObject);
1677 aMenu = new XGUI_HistoryMenu(aButton);
1679 connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1680 connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1683 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1685 QList<ActionInfo> aResult;
1686 std::list<std::string>::const_iterator it = theList.cbegin();
1687 for (; it != theList.cend(); it++) {
1688 QString anId = QString::fromStdString(*it);
1689 bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1691 anId.chop(ModuleBase_Operation::EditSuffix().size());
1693 ActionInfo anInfo = myActionsMgr->actionInfoById(anId);
1695 anInfo.text = anInfo.text.prepend("Modify ");