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>
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>
67 #include <QApplication>
68 #include <QFileDialog>
69 #include <QMessageBox>
70 #include <QMdiSubWindow>
71 #include <QPushButton>
72 #include <QDockWidget>
77 #include <QToolButton>
80 #include <QDialogButtonBox>
81 #include <QHBoxLayout>
82 #include <QtxColorButton.h>
95 //#define DEBUG_FEATURE_CREATED
96 //#define DEBUG_FEATURE_REDISPLAY
98 QString objectInfo(ObjectPtr theObj)
100 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
101 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
102 QString aFeatureStr = "feature";
104 aFeatureStr.append("(Result)");
105 aFeature = ModelAPI_Feature::feature(aRes);
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());
116 QMap<QString, QString> XGUI_Workshop::myIcons;
119 QIcon XGUI_Workshop::featureIcon(const FeaturePtr& theFeature)
123 std::string aKind = theFeature->getKind();
124 QString aId(aKind.c_str());
125 if (!myIcons.contains(aId))
128 QString anIconString = myIcons[aId];
130 ModelAPI_ExecState aState = theFeature->data()->execState();
132 case ModelAPI_StateDone:
133 case ModelAPI_StateNothing: {
134 anIcon = QIcon(anIconString);
137 case ModelAPI_StateMustBeUpdated: {
138 anIcon = ModuleBase_Tools::lighter(anIconString);
141 case ModelAPI_StateExecFailed: {
142 anIcon = ModuleBase_Tools::composite(":pictures/exec_state_failed.png", anIconString);
145 case ModelAPI_StateInvalidArgument: {
146 anIcon = ModuleBase_Tools::composite(":pictures/exec_state_invalid_parameters.png",
155 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
157 myCurrentDir(QString()),
159 mySalomeConnector(theConnector),
163 myUpdatePrefs(false),
164 myPartActivating(false)
166 myMainWindow = mySalomeConnector ? 0 : new AppElements_MainWindow();
168 myDisplayer = new XGUI_Displayer(this);
170 mySelector = new XGUI_SelectionMgr(this);
171 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
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)));
180 myViewerProxy = new XGUI_ViewerProxy(this);
181 connect(myViewerProxy, SIGNAL(selectionChanged()),
182 myActionsMgr, SLOT(updateOnViewSelection()));
184 myModuleConnector = new XGUI_ModuleConnector(this);
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&)));
200 //******************************************************
201 XGUI_Workshop::~XGUI_Workshop(void)
206 //******************************************************
207 void XGUI_Workshop::startApplication()
211 Config_PropManager::registerProp("Plugins", "default_path", "Default Path",
212 Config_Prop::Directory, "");
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));
230 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
231 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
233 registerValidators();
235 // Calling of loadCustomProps before activating module is required
236 // by Config_PropManger to restore user-defined path to plugins
237 ModuleBase_Preferences::loadCustomProps();
240 myMainWindow->show();
241 updateCommandStatus();
246 emit applicationStarted();
249 //******************************************************
250 void XGUI_Workshop::initMenu()
252 myContextMenuMgr->createActions();
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)));
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)));
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");
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()));
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");
289 // File commands group
290 AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
292 AppElements_Command* aCommand;
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();
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>&)),
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>&)),
317 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
318 QIcon(":pictures/rebuild.png"), QKeySequence());
319 aCommand->connectTo(this, SLOT(onRebuild()));
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();
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()));
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()));
334 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
335 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
336 aCommand->connectTo(this, SLOT(onPreferences()));
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.
343 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
344 // QIcon(":pictures/close.png"), QKeySequence(), true);
345 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
349 //******************************************************
350 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
352 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
353 return aMenuBar->addWorkbench(theName);
356 //******************************************************
357 void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessage)
359 if (QApplication::instance()->thread() != QThread::currentThread()) {
361 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
363 SessionPtr aMgr = ModelAPI_Session::get();
364 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
365 QApplication::postEvent(this, aPostponeEvent);
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);
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);
383 if (mySalomeConnector)
384 mySalomeConnector->createPreferences();
385 myUpdatePrefs = false;
388 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
389 myUpdatePrefs = true;
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);
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));
411 QApplication::restoreOverrideCursor();
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)
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)
430 showObjects(aList, false);
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();
439 if (myOperationMgr->startOperation(anOperation)) {
440 myPropertyPanel->updateContentWidget(anOperation->feature());
441 if (!anOperation->getDescription()->hasXmlRepresentation()) {
442 if (anOperation->commit())
443 updateCommandStatus();
446 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
447 myActionsMgr->update();
448 // Find and Activate active part
449 if (myPartActivating)
451 SessionPtr aMgr = ModelAPI_Session::get();
452 DocumentPtr aActiveDoc = aMgr->activeDocument();
453 DocumentPtr aDoc = aMgr->moduleDocument();
454 if (aActiveDoc == aDoc) {
455 activatePart(ResultPartPtr());
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
466 // If not found then activate global document
467 activatePart(ResultPartPtr());
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);
474 ModuleBase_FilterFactory* aFactory = moduleConnector()->selectionFilters();
475 if (!aMsg->attributeId().empty()) {
476 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
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);
487 //Show error dialog if error message received.
488 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
490 emit errorOccurred(QString::fromLatin1(anAppError->description()));
494 if (!isSalomeMode()) {
495 SessionPtr aMgr = ModelAPI_Session::get();
496 if (aMgr->isModified() != myMainWindow->isModifiedState())
497 myMainWindow->setModifiedState(aMgr->isModified());
501 //******************************************************
502 QMainWindow* XGUI_Workshop::desktop() const
504 return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
507 //******************************************************
508 void XGUI_Workshop::onStartWaiting()
510 if (Events_LongOp::isPerformed()) {
511 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
515 //******************************************************
516 void XGUI_Workshop::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
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);
530 myOperationMgr->onValidateOperation();
532 myObjectBrowser->processEvent(theMsg);
535 //******************************************************
536 void XGUI_Workshop::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
538 std::set<ObjectPtr> aObjects = theMsg->objects();
539 std::set<ObjectPtr>::const_iterator aIt;
541 #ifdef DEBUG_FEATURE_REDISPLAY
543 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
544 anInfo.append(objectInfo((*aIt)));
546 QString anInfoStr = anInfo.join(", ");
547 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
550 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
551 ObjectPtr aObj = (*aIt);
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();
560 myDisplayer->erase(aObj, false);
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());
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);
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);
590 myDisplayer->updateViewer();
593 //******************************************************
594 void XGUI_Workshop::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
596 std::set<ObjectPtr> aObjects = theMsg->objects();
597 std::set<ObjectPtr>::const_iterator aIt;
598 #ifdef DEBUG_FEATURE_CREATED
600 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
601 anInfo.append(objectInfo((*aIt)));
603 QString anInfoStr = anInfo.join(", ");
604 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
607 //bool aHasPart = false;
608 bool isDisplayed = false;
609 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
611 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
614 // If a feature is created from the aplication's python console
615 // it doesn't stored in the operation mgr and doesn't displayed
617 isDisplayed = displayObject(*aIt);
621 myObjectBrowser->processEvent(theMsg);
623 myDisplayer->updateViewer();
624 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
625 // activateLastPart();
629 //******************************************************
630 void XGUI_Workshop::onObjectDeletedMsg(const std::shared_ptr<ModelAPI_ObjectDeletedMessage>& theMsg)
633 myObjectBrowser->processEvent(theMsg);
634 //std::set<ObjectPtr> aFeatures = theMsg->objects();
637 //******************************************************
638 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
640 setNestedFeatures(theOperation);
642 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
643 connectWithOperation(theOperation);
644 setPropertyPanel(theOperation);
646 updateCommandStatus();
648 myModule->operationStarted(theOperation);
651 //******************************************************
652 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
654 setNestedFeatures(theOperation);
656 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
657 // connectWithOperation(theOperation); already connected
658 setPropertyPanel(theOperation);
660 updateCommandStatus();
662 myModule->operationResumed(theOperation);
666 //******************************************************
667 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
669 ModuleBase_ISelection* aSel = mySelector->selection();
670 QObjectPtrList aObj = aSel->selectedPresentations();
671 //!< No need for property panel
672 updateCommandStatus();
674 myPropertyPanel->cleanContent();
676 // Activate objects created by current operation
677 // in order to clean selection modes
679 myDisplayer->activateObjects(aModes);
680 myModule->operationStopped(theOperation);
684 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
686 myModule->operationCommitted(theOperation);
689 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
691 myModule->operationAborted(theOperation);
694 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
696 if (this->isSalomeMode())
697 theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
699 theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
702 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
705 QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
706 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
709 myPropertyPanel->cleanContent();
710 aFactory.createWidget(myPropertyPanel->contentWidget());
712 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
713 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
714 bool isStoreValue = !theOperation->isEditOperation() &&
715 !aWidget->getDefaultValue().empty() &&
716 !aWidget->isComputedDefault();
717 aWidget->setFeature(theOperation->feature(), isStoreValue);
718 aWidget->enableFocusProcessing();
721 myPropertyPanel->setModelWidgets(aWidgets);
722 theOperation->setPropertyPanel(myPropertyPanel);
724 myModule->propertyPanelDefined(theOperation);
726 myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
729 bool XGUI_Workshop::event(QEvent * theEvent)
731 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
733 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
734 processEvent(aEventPtr);
743 void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
747 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
751 ActionInfo aFeatureInfo;
752 aFeatureInfo.initFrom(theMessage);
753 // Remember features icons
754 myIcons[QString::fromStdString(theMessage->id())] = aFeatureInfo.iconFile;
756 QString aWchName = QString::fromStdString(theMessage->workbenchId());
757 QStringList aNestedFeatures =
758 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
759 QString aDocKind = QString::fromStdString(theMessage->documentKind());
760 if (isSalomeMode()) {
761 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo);
762 salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures);
763 salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind);
765 myActionsMgr->addCommand(aAction);
766 myModule->actionCreated(aAction);
768 //Find or create Workbench
769 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
770 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
772 aPage = addWorkbench(aWchName);
774 //Find or create Group
775 QString aGroupName = QString::fromStdString(theMessage->groupId());
776 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
778 aGroup = aPage->addGroup(aGroupName);
780 // Check if hotkey sequence is already defined:
781 QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut);
782 if(aHotKey != aFeatureInfo.shortcut) {
783 aFeatureInfo.shortcut = aHotKey;
786 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
789 // Enrich created button with accept/abort buttons if necessary
790 AppElements_Button* aButton = aCommand->button();
791 if (aButton->isColumnButton()) {
792 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
793 QList<QAction*> anActList;
794 if (aNestedActions.contains("accept")) {
795 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, aButton);
796 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
797 anActList << anAction;
799 if (aNestedActions.contains("abort")) {
800 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, aButton);
801 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
802 anActList << anAction;
804 aButton->setAdditionalButtons(anActList);
806 myActionsMgr->addCommand(aCommand);
807 myModule->actionCreated(aCommand);
812 * Makes a signal/slot connections between Property Panel
813 * and given operation. The given operation becomes a
814 * current operation and previous operation if exists
816 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
818 QAction* aCommand = 0;
819 if (isSalomeMode()) {
820 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
822 AppElements_MainMenu* aMenu = myMainWindow->menuObject();
823 FeaturePtr aFeature = theOperation->feature();
825 aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
827 //Abort operation on uncheck the command
829 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
834 * Saves document with given name.
836 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
838 QApplication::restoreOverrideCursor();
839 SessionPtr aMgr = ModelAPI_Session::get();
840 aMgr->save(theName.toLatin1().constData(), theFileNames);
841 QApplication::restoreOverrideCursor();
844 bool XGUI_Workshop::isActiveOperationAborted()
846 return myOperationMgr->abortAllOperations();
849 //******************************************************
850 void XGUI_Workshop::onExit()
852 SessionPtr aMgr = ModelAPI_Session::get();
853 if (aMgr->isModified()) {
854 int anAnswer = QMessageBox::question(
855 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
856 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
857 if (anAnswer == QMessageBox::Save) {
858 bool saved = onSave();
862 } else if (anAnswer == QMessageBox::Cancel) {
869 //******************************************************
870 void XGUI_Workshop::onNew()
872 QApplication::setOverrideCursor(Qt::WaitCursor);
873 if (objectBrowser() == 0) {
875 mySelector->connectViewers();
877 myViewerProxy->connectToViewer();
879 if (!isSalomeMode()) {
880 myMainWindow->showPythonConsole();
881 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
882 aWnd->showMaximized();
883 updateCommandStatus();
885 myContextMenuMgr->connectViewer();
886 QApplication::restoreOverrideCursor();
889 //******************************************************
890 void XGUI_Workshop::onOpen()
892 if(!isActiveOperationAborted())
894 //save current file before close if modified
895 SessionPtr aSession = ModelAPI_Session::get();
896 if (aSession->isModified()) {
897 //TODO(sbh): re-launch the app?
898 int anAnswer = QMessageBox::question(
899 myMainWindow, tr("Save current file"),
900 tr("The document is modified, save before opening another?"),
901 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
902 if (anAnswer == QMessageBox::Save) {
904 } else if (anAnswer == QMessageBox::Cancel) {
907 aSession->closeAll();
911 //show file dialog, check if readable and open
912 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
913 if (myCurrentDir.isEmpty())
915 QFileInfo aFileInfo(myCurrentDir);
916 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
917 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
921 QApplication::setOverrideCursor(Qt::WaitCursor);
922 aSession->load(myCurrentDir.toLatin1().constData());
923 myObjectBrowser->rebuildDataTree();
925 updateCommandStatus();
926 QApplication::restoreOverrideCursor();
929 //******************************************************
930 bool XGUI_Workshop::onSave()
932 if(!isActiveOperationAborted())
934 if (myCurrentDir.isEmpty()) {
937 std::list<std::string> aFiles;
938 saveDocument(myCurrentDir, aFiles);
939 updateCommandStatus();
941 myMainWindow->setModifiedState(false);
945 //******************************************************
946 bool XGUI_Workshop::onSaveAs()
948 if(!isActiveOperationAborted())
950 QFileDialog dialog(mainWindow());
951 dialog.setWindowTitle(tr("Select directory to save files..."));
952 dialog.setFileMode(QFileDialog::Directory);
953 dialog.setFilter(tr("Folders (*)"));
954 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
955 dialog.setViewMode(QFileDialog::Detail);
957 if (!dialog.exec()) {
960 QString aTempDir = dialog.selectedFiles().first();
962 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
963 int answer = QMessageBox::question(
965 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
967 tr("The folder already contains some files, save anyway?"),
968 QMessageBox::Save | QMessageBox::Cancel);
969 if (answer == QMessageBox::Cancel) {
973 myCurrentDir = aTempDir;
974 if (!isSalomeMode()) {
975 myMainWindow->setCurrentDir(myCurrentDir, false);
976 myMainWindow->setModifiedState(false);
981 //******************************************************
982 void XGUI_Workshop::onUndo(int theTimes)
984 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
985 SessionPtr aMgr = ModelAPI_Session::get();
986 if (aMgr->isOperation())
987 operationMgr()->onAbortOperation();
988 for (int i = 0; i < theTimes; ++i) {
991 updateCommandStatus();
994 //******************************************************
995 void XGUI_Workshop::onRedo(int theTimes)
997 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
998 SessionPtr aMgr = ModelAPI_Session::get();
999 if (aMgr->isOperation())
1000 operationMgr()->onAbortOperation();
1001 for (int i = 0; i < theTimes; ++i) {
1004 updateCommandStatus();
1007 //******************************************************
1008 void XGUI_Workshop::onRebuild()
1010 SessionPtr aMgr = ModelAPI_Session::get();
1011 bool aWasOperation = aMgr->isOperation(); // keep this value
1012 if (!aWasOperation) {
1013 aMgr->startOperation("Rebuild");
1015 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
1016 Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
1017 new Events_Message(aRebuildEvent, this)));
1018 if (!aWasOperation) {
1019 aMgr->finishOperation();
1023 //******************************************************
1024 void XGUI_Workshop::onPreferences()
1026 ModuleBase_Prefs aModif;
1027 ModuleBase_Preferences::editPreferences(aModif);
1028 if (aModif.size() > 0) {
1030 foreach (ModuleBase_Pref aPref, aModif)
1032 aSection = aPref.first;
1033 if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
1034 if (!isSalomeMode())
1035 myMainWindow->viewer()->updateFromResources();
1036 } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
1037 if (!isSalomeMode())
1038 myMainWindow->menuObject()->updateFromResources();
1044 //******************************************************
1045 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
1047 QString libName = QString::fromStdString(library(theModule.toStdString()));
1048 if (libName.isEmpty()) {
1049 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
1054 CREATE_FUNC crtInst = 0;
1057 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
1061 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1062 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1063 QString aMsg((char*) &lpMsgBuf);
1064 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
1065 ::LocalFree(lpMsgBuf);
1067 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
1071 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
1072 | FORMAT_MESSAGE_IGNORE_INSERTS,
1073 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1074 QString aMsg((char*) &lpMsgBuf);
1075 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
1076 ::LocalFree(lpMsgBuf);
1080 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
1082 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
1084 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
1086 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
1091 ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
1093 if (!err.isEmpty()) {
1095 Events_Error::send(err.toStdString());
1097 qWarning(qPrintable(err));
1103 //******************************************************
1104 bool XGUI_Workshop::activateModule()
1106 Config_ModuleReader aModuleReader;
1107 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
1108 myModule = loadModule(moduleName);
1112 connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
1113 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
1114 connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
1115 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
1117 myModule->createFeatures();
1118 myActionsMgr->update();
1122 //******************************************************
1123 void XGUI_Workshop::updateCommandStatus()
1125 QList<QAction*> aCommands;
1126 if (isSalomeMode()) { // update commands in SALOME mode
1127 aCommands = salomeConnector()->commandList();
1129 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
1130 foreach (AppElements_Command* aCmd, aMenuBar->features())
1131 aCommands.append(aCmd);
1133 SessionPtr aMgr = ModelAPI_Session::get();
1134 if (aMgr->hasModuleDocument()) {
1135 QAction *aUndoCmd, *aRedoCmd;
1136 foreach(QAction* aCmd, aCommands) {
1137 QString aId = aCmd->data().toString();
1138 if (aId == "UNDO_CMD")
1140 else if (aId == "REDO_CMD")
1143 // Enable all commands
1144 aCmd->setEnabled(true);
1147 aUndoCmd->setEnabled(myModule->canUndo());
1148 aRedoCmd->setEnabled(myModule->canRedo());
1151 foreach(QAction* aCmd, aCommands) {
1152 QString aId = aCmd->data().toString();
1153 if (aId == "NEW_CMD")
1154 aCmd->setEnabled(true);
1155 else if (aId == "EXIT_CMD")
1156 aCmd->setEnabled(true);
1158 aCmd->setEnabled(false);
1161 myActionsMgr->update();
1162 emit commandStatusUpdated();
1165 void XGUI_Workshop::updateHistory()
1167 std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
1168 QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
1169 emit updateUndoHistory(aUndoRes);
1171 std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
1172 QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
1173 emit updateRedoHistory(aRedoRes);
1176 //******************************************************
1177 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
1179 QDockWidget* aObjDock = new QDockWidget(theParent);
1180 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1181 aObjDock->setWindowTitle(tr("Object browser"));
1182 aObjDock->setStyleSheet(
1183 "::title { position: relative; padding-left: 5px; text-align: left center }");
1184 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
1185 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
1186 SLOT(changeCurrentDocument(ObjectPtr)));
1187 aObjDock->setWidget(myObjectBrowser);
1189 myContextMenuMgr->connectObjectBrowser();
1193 //******************************************************
1195 * Creates dock widgets, places them in corresponding area
1196 * and tabifies if necessary.
1198 void XGUI_Workshop::createDockWidgets()
1200 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1201 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
1202 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
1203 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
1204 myPropertyPanel->setupActions(myActionsMgr);
1205 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1206 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
1207 hidePropertyPanel(); ///<! Invisible by default
1208 hideObjectBrowser();
1209 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
1210 myPropertyPanel->installEventFilter(myOperationMgr);
1212 QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
1213 connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
1214 QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
1215 connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
1216 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
1217 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
1218 myOperationMgr, SLOT(onKeyReleased(QKeyEvent*)));
1219 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
1220 aOkAct, SLOT(setEnabled(bool)));
1221 QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
1222 connect(myOperationMgr, SIGNAL(nestedStateChanged(bool)),
1223 aAcceptAllAct, SLOT(setEnabled(bool)));
1227 //******************************************************
1228 void XGUI_Workshop::showPropertyPanel()
1230 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1231 ///<! Restore ability to close panel from the window's menu
1232 aViewAct->setEnabled(true);
1233 myPropertyPanel->show();
1234 myPropertyPanel->raise();
1237 //******************************************************
1238 void XGUI_Workshop::hidePropertyPanel()
1240 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1241 ///<! Do not allow to show empty property panel
1242 aViewAct->setEnabled(false);
1243 myPropertyPanel->hide();
1246 //******************************************************
1247 void XGUI_Workshop::showObjectBrowser()
1249 myObjectBrowser->parentWidget()->show();
1252 //******************************************************
1253 void XGUI_Workshop::hideObjectBrowser()
1255 myObjectBrowser->parentWidget()->hide();
1258 //******************************************************
1259 void XGUI_Workshop::onFeatureTriggered()
1261 QAction* aCmd = dynamic_cast<QAction*>(sender());
1263 QString aId = salomeConnector()->commandId(aCmd);
1265 myModule->launchOperation(aId);
1269 //******************************************************
1270 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1272 SessionPtr aMgr = ModelAPI_Session::get();
1274 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1276 DocumentPtr aPartDoc = aPart->partDoc();
1278 aMgr->setActiveDocument(aPartDoc);
1283 aMgr->setActiveDocument(aMgr->moduleDocument());
1286 //******************************************************
1287 void XGUI_Workshop::salomeViewerSelectionChanged()
1289 emit salomeViewerSelection();
1292 //**************************************************************
1293 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
1295 return mySalomeConnector->viewer();
1298 //**************************************************************
1299 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1301 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1302 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1303 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1304 activatePart(aPart);
1305 } else if (theId == "DEACTIVATE_PART_CMD")
1306 activatePart(ResultPartPtr());
1307 else if (theId == "DELETE_CMD")
1309 else if (theId == "COLOR_CMD")
1310 changeColor(aObjects);
1311 else if (theId == "SHOW_CMD")
1312 showObjects(aObjects, true);
1313 else if (theId == "HIDE_CMD")
1314 showObjects(aObjects, false);
1315 else if (theId == "SHOW_ONLY_CMD")
1316 showOnlyObjects(aObjects);
1317 else if (theId == "SHADING_CMD")
1318 setDisplayMode(aObjects, XGUI_Displayer::Shading);
1319 else if (theId == "WIREFRAME_CMD")
1320 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1321 else if (theId == "HIDEALL_CMD")
1322 myDisplayer->eraseAll();
1323 else if (theId == "EDIT_CMD") {
1324 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
1326 myModule->editFeature(aFeature);
1330 //**************************************************************
1331 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1333 if (!myPartActivating) {
1334 myPartActivating = true;
1336 theFeature->activate();
1337 changeCurrentDocument(theFeature);
1338 myObjectBrowser->activatePart(theFeature);
1339 myPartActivating = false;
1341 updateCommandStatus();
1344 //**************************************************************
1345 //void XGUI_Workshop::activateLastPart()
1347 // SessionPtr aMgr = ModelAPI_Session::get();
1348 // DocumentPtr aDoc = aMgr->moduleDocument();
1349 // std::string aGrpName = ModelAPI_ResultPart::group();
1350 // ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1351 // ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1353 // activatePart(aPart);
1357 //**************************************************************
1358 void XGUI_Workshop::deleteObjects()
1360 ModuleBase_IModule* aModule = module();
1361 // 1. allow the module to delete objects, do nothing if it has succeed
1362 if (aModule->deleteObjects())
1365 if (!isActiveOperationAborted())
1367 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1368 // 1. start operation
1369 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1370 aDescription += tr(" %1");
1371 QStringList aObjectNames;
1372 foreach (ObjectPtr aObj, anObjects) {
1373 if (!aObj->data().get())
1375 aObjectNames << QString::fromStdString(aObj->data()->name());
1377 aDescription = aDescription.arg(aObjectNames.join(", "));
1379 SessionPtr aMgr = ModelAPI_Session::get();
1380 aMgr->startOperation(aDescription.toStdString());
1381 // 2. close the documents of the removed parts if the result part is in a list of selected objects
1382 foreach (ObjectPtr aObj, anObjects)
1384 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1386 DocumentPtr aDoc = aObj->document();
1387 if (aDoc == aMgr->activeDocument()) {
1392 // 3. delete objects
1393 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1394 std::set<FeaturePtr> anIgnoredFeatures;
1395 if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1396 myDisplayer->updateViewer();
1397 aMgr->finishOperation();
1398 updateCommandStatus();
1401 aMgr->abortOperation();
1405 //**************************************************************
1406 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1407 std::set<FeaturePtr> theIgnoredFeatures,
1409 const bool theAskAboutDeleteReferences)
1411 // 1. find all referenced features
1412 std::set<FeaturePtr> aRefFeatures;
1413 foreach (ObjectPtr aObj, theList) {
1414 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1415 if (aFeature.get() != NULL) {
1416 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
1419 // 2. warn about the references remove, break the delete operation if the user chose it
1420 if (theAskAboutDeleteReferences && !aRefFeatures.empty()) {
1421 QStringList aRefNames;
1422 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1423 aLast = aRefFeatures.end();
1424 for (; anIt != aLast; anIt++) {
1425 aRefNames.append((*anIt)->name().c_str());
1427 QString aNames = aRefNames.join(", ");
1429 QMessageBox::StandardButton aRes = QMessageBox::warning(
1430 theParent, tr("Delete features"),
1431 QString(tr("Selected features are used in the following features: %1.\
1432 These features will be deleted also. Would you like to continue?")).arg(aNames),
1433 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1434 if (aRes != QMessageBox::Yes)
1438 // 3. remove referenced features
1439 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1440 aLast = aRefFeatures.end();
1441 for (; anIt != aLast; anIt++) {
1442 FeaturePtr aFeature = (*anIt);
1443 DocumentPtr aDoc = aFeature->document();
1444 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1445 aDoc->removeFeature(aFeature);
1448 // 4. remove the parameter features
1449 foreach (ObjectPtr aObj, theList) {
1450 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1452 DocumentPtr aDoc = aObj->document();
1453 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1454 aDoc->removeFeature(aFeature);
1460 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1462 bool isFoundResultType = false;
1463 foreach(ObjectPtr anObj, theObjects)
1465 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1466 if (aResult.get() == NULL)
1469 isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1470 if (isFoundResultType)
1473 return isFoundResultType;
1476 //**************************************************************
1477 bool XGUI_Workshop::canChangeColor() const
1479 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1481 std::set<std::string> aTypes;
1482 aTypes.insert(ModelAPI_ResultGroup::group());
1483 aTypes.insert(ModelAPI_ResultConstruction::group());
1484 aTypes.insert(ModelAPI_ResultBody::group());
1485 return hasResults(aObjects, aTypes);
1488 //**************************************************************
1489 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1491 std::vector<int> aColor;
1492 foreach(ObjectPtr anObject, theObjects) {
1494 AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1496 anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1497 if (!aColor.empty())
1500 if (aColor.size() != 3)
1503 // 2. show the dialog to change the value
1504 QDialog* aDlg = new QDialog();
1505 QVBoxLayout* aLay = new QVBoxLayout(aDlg);
1507 QtxColorButton* aColorBtn = new QtxColorButton(aDlg);
1508 aColorBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1510 aLay->addWidget(aColorBtn);
1511 aColorBtn->setColor(QColor(aColor[0], aColor[1], aColor[2]));
1513 QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
1514 Qt::Horizontal, aDlg);
1515 connect(aButtons, SIGNAL(accepted()), aDlg, SLOT(accept()));
1516 connect(aButtons, SIGNAL(rejected()), aDlg, SLOT(reject()));
1517 aLay->addWidget(aButtons);
1519 aDlg->move(QCursor::pos());
1520 bool isDone = aDlg->exec() == QDialog::Accepted;
1524 QColor aColorResult = aColorBtn->color();
1525 int aRedResult = aColorResult.red(),
1526 aGreenResult = aColorResult.green(),
1527 aBlueResult = aColorResult.blue();
1529 if (aRedResult == aColor[0] && aGreenResult == aColor[1] && aBlueResult == aColor[2])
1532 // 3. abort the previous operation and start a new one
1533 SessionPtr aMgr = ModelAPI_Session::get();
1534 bool aWasOperation = aMgr->isOperation(); // keep this value
1535 if (!aWasOperation) {
1536 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1537 aMgr->startOperation(aDescription.toStdString());
1540 // 4. set the value to all results
1541 AttributeIntArrayPtr aColorAttr;
1542 foreach(ObjectPtr anObj, theObjects) {
1543 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1544 if (aResult.get() != NULL) {
1545 aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1546 if (aColorAttr.get() != NULL) {
1547 if (!aColorAttr->size()) {
1548 aColorAttr->setSize(3);
1550 aColorAttr->setValue(0, aRedResult);
1551 aColorAttr->setValue(1, aGreenResult);
1552 aColorAttr->setValue(2, aBlueResult);
1557 aMgr->finishOperation();
1558 updateCommandStatus();
1561 //**************************************************************
1562 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1564 foreach (ObjectPtr aObj, theList)
1567 displayObject(aObj);
1569 myDisplayer->erase(aObj, false);
1572 myDisplayer->updateViewer();
1575 //**************************************************************
1576 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1578 myDisplayer->showOnly(theList);
1582 //**************************************************************
1583 void XGUI_Workshop::registerValidators() const
1585 SessionPtr aMgr = ModelAPI_Session::get();
1586 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1589 //**************************************************************
1590 void XGUI_Workshop::displayAllResults()
1592 SessionPtr aMgr = ModelAPI_Session::get();
1593 DocumentPtr aRootDoc = aMgr->moduleDocument();
1594 displayDocumentResults(aRootDoc);
1595 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1596 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1597 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1598 displayDocumentResults(aPart->partDoc());
1600 myDisplayer->updateViewer();
1603 //**************************************************************
1604 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1608 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1609 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1612 //**************************************************************
1613 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1615 for (int i = 0; i < theDoc->size(theGroup); i++)
1616 displayObject(theDoc->object(theGroup, i));
1619 //**************************************************************
1620 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1622 foreach(ObjectPtr aObj, theList) {
1623 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1625 if (theList.size() > 0)
1626 myDisplayer->updateViewer();
1629 //**************************************************************
1630 void XGUI_Workshop::closeDocument()
1632 ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1633 while (anOperation) {
1634 anOperation->abort();
1635 anOperation = operationMgr()->currentOperation();
1637 myDisplayer->closeLocalContexts();
1638 myDisplayer->eraseAll();
1639 objectBrowser()->clearContent();
1641 SessionPtr aMgr = ModelAPI_Session::get();
1643 objectBrowser()->clearContent();
1646 //**************************************************************
1647 bool XGUI_Workshop::displayObject(ObjectPtr theObj)
1649 if (!myModule->canDisplayObject(theObj))
1652 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
1653 if (aBody.get() != NULL) {
1654 int aNb = myDisplayer->objectsCount();
1655 myDisplayer->display(theObj, false);
1659 myDisplayer->display(theObj, false);
1664 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1666 XGUI_HistoryMenu* aMenu = NULL;
1667 if (isSalomeMode()) {
1668 QAction* anAction = qobject_cast<QAction*>(theObject);
1671 aMenu = new XGUI_HistoryMenu(anAction);
1673 QToolButton* aButton = qobject_cast<QToolButton*>(theObject);
1674 aMenu = new XGUI_HistoryMenu(aButton);
1676 connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1677 connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1680 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1682 QList<ActionInfo> aResult;
1683 std::list<std::string>::const_iterator it = theList.cbegin();
1684 for (; it != theList.cend(); it++) {
1685 QString anId = QString::fromStdString(*it);
1686 bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1688 anId.chop(ModuleBase_Operation::EditSuffix().size());
1690 ActionInfo anInfo = myActionsMgr->actionInfoById(anId);
1692 anInfo.text = anInfo.text.prepend("Modify ");