1 #include "ModuleBase_IModule.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "XGUI_SelectionMgr.h"
12 #include "XGUI_Selection.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_SalomeViewer.h"
18 #include "XGUI_ActionsMgr.h"
19 #include "XGUI_ErrorDialog.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_PropertyPanel.h"
22 #include "XGUI_ContextMenuMgr.h"
23 #include "XGUI_ModuleConnector.h"
24 #include "XGUI_Preferences.h"
25 #include <XGUI_QtEvents.h>
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_AttributeDocRef.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_ResultBody.h>
37 #include <PartSetPlugin_Part.h>
39 #include <Events_Loop.h>
40 #include <Events_Error.h>
41 #include <Events_LongOp.h>
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_Operation.h>
45 #include <ModuleBase_OperationDescription.h>
46 #include <ModuleBase_SelectionValidator.h>
47 #include "ModuleBase_WidgetFactory.h"
49 #include <Config_Common.h>
50 #include <Config_FeatureMessage.h>
51 #include <Config_PointerMessage.h>
52 #include <Config_ModuleReader.h>
54 #include <QApplication>
55 #include <QFileDialog>
56 #include <QMessageBox>
57 #include <QMdiSubWindow>
58 #include <QPushButton>
59 #include <QDockWidget>
75 QMap<QString, QString> XGUI_Workshop::myIcons;
77 QString XGUI_Workshop::featureIcon(const std::string& theId)
79 QString aId(theId.c_str());
80 if (myIcons.contains(aId))
85 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
87 myCurrentDir(QString()),
89 mySalomeConnector(theConnector),
94 myPartActivating(false)
96 myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
98 myDisplayer = new XGUI_Displayer(this);
100 mySelector = new XGUI_SelectionMgr(this);
101 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
103 myOperationMgr = new XGUI_OperationMgr(this);
104 myActionsMgr = new XGUI_ActionsMgr(this);
105 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
106 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
107 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
108 SLOT(onContextMenuCommand(const QString&, bool)));
110 myViewerProxy = new XGUI_ViewerProxy(this);
111 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
113 myModuleConnector = new XGUI_ModuleConnector(this);
115 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
116 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
117 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
118 SLOT(onOperationStopped(ModuleBase_Operation*)));
119 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
120 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
121 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
123 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
126 //******************************************************
127 XGUI_Workshop::~XGUI_Workshop(void)
132 //******************************************************
133 void XGUI_Workshop::startApplication()
136 //Initialize event listening
137 Events_Loop* aLoop = Events_Loop::loop();
138 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
139 //TODO(sbh): Implement static method to extract event id [SEID]
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
141 // TODO Is it good to use non standard event within workshop?
142 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
143 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
144 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
145 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
146 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
147 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
148 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
149 aLoop->registerListener(this, Events_Loop::eventByName("CurrentDocumentChanged"));
150 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOSHOW));
151 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
153 registerValidators();
156 myMainWindow->show();
157 updateCommandStatus();
159 XGUI_Preferences::loadCustomProps();
163 //******************************************************
164 void XGUI_Workshop::initMenu()
166 myContextMenuMgr->createActions();
168 if (isSalomeMode()) {
169 // Create only Undo, Redo commands
170 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
171 tr("Undo last command"),
172 QIcon(":pictures/undo.png"),
173 QKeySequence::Undo, false);
174 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
175 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
176 QIcon(":pictures/redo.png"), QKeySequence::Redo,
178 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
179 salomeConnector()->addEditMenuSeparator();
182 // File commands group
183 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
185 XGUI_Command* aCommand;
187 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
188 QIcon(":pictures/save.png"), QKeySequence::Save);
189 aCommand->connectTo(this, SLOT(onSave()));
190 //aCommand->disable();
192 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
193 QIcon(":pictures/undo.png"), QKeySequence::Undo);
194 aCommand->connectTo(this, SLOT(onUndo()));
196 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
197 QIcon(":pictures/redo.png"), QKeySequence::Redo);
198 aCommand->connectTo(this, SLOT(onRedo()));
200 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
201 QIcon(":pictures/rebuild.png"), QKeySequence());
202 aCommand->connectTo(this, SLOT(onRebuild()));
204 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
205 QIcon(":pictures/save.png"), QKeySequence());
206 aCommand->connectTo(this, SLOT(onSaveAs()));
207 //aCommand->disable();
209 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
210 QIcon(":pictures/open.png"), QKeySequence::Open);
211 aCommand->connectTo(this, SLOT(onOpen()));
213 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
214 // QIcon(":pictures/new.png"), QKeySequence::New);
215 //aCommand->connectTo(this, SLOT(onNew()));
217 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
218 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
219 aCommand->connectTo(this, SLOT(onPreferences()));
221 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
222 QIcon(":pictures/close.png"), QKeySequence::Close);
223 aCommand->connectTo(this, SLOT(onExit()));
224 //FIXME: SBH's test action. Can be used for some GUI tests.
226 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
227 // QIcon(":pictures/close.png"), QKeySequence(), true);
228 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
232 //******************************************************
233 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
235 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
236 return aMenuBar->addWorkbench(theName);
239 //******************************************************
240 void XGUI_Workshop::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
242 if (QApplication::instance()->thread() != QThread::currentThread()) {
244 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
246 SessionPtr aMgr = ModelAPI_Session::get();
247 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
248 QApplication::postEvent(this, aPostponeEvent);
252 //A message to start feature creation received.
253 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
254 boost::shared_ptr<Config_FeatureMessage> aFeatureMsg =
255 boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
256 if (!aFeatureMsg->isInternal()) {
257 addFeature(aFeatureMsg);
261 // Process creation of Part
262 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
263 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
264 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
265 onFeatureCreatedMsg(aUpdMsg);
267 if (mySalomeConnector)
268 mySalomeConnector->createPreferences();
269 myUpdatePrefs = false;
272 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
273 myUpdatePrefs = true;
277 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
278 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
279 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
280 onFeatureRedisplayMsg(aUpdMsg);
283 //Update property panel on corresponding message. If there is no current operation (no
284 //property panel), or received message has different feature to the current - do nothing.
285 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
286 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
287 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
288 onFeatureUpdatedMsg(anUpdateMsg);
291 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
292 boost::shared_ptr<ModelAPI_ObjectDeletedMessage> aDelMsg =
293 boost::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
294 onObjectDeletedMsg(aDelMsg);
297 else if (theMessage->eventID() == Events_LongOp::eventID()) {
298 if (Events_LongOp::isPerformed())
299 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
301 QApplication::restoreOverrideCursor();
304 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW)) {
305 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
306 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
307 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
308 QList<ObjectPtr> aList;
309 std::set<ObjectPtr>::const_iterator aIt;
310 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
312 showObjects(aList, true);
315 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE)) {
316 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
317 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
318 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
319 QList<ObjectPtr> aList;
320 std::set<ObjectPtr>::const_iterator aIt;
321 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
323 showObjects(aList, false);
326 //An operation passed by message. Start it, process and commit.
327 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
328 boost::shared_ptr<Config_PointerMessage> aPartSetMsg =
329 boost::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
330 //myPropertyPanel->cleanContent();
331 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
333 if (myOperationMgr->startOperation(anOperation)) {
334 myPropertyPanel->updateContentWidget(anOperation->feature());
335 if (!anOperation->getDescription()->hasXmlRepresentation()) {
336 if (anOperation->commit())
337 updateCommandStatus();
341 else if (theMessage->eventID() == Events_Loop::loop()->eventByName("CurrentDocumentChanged")) {
342 // Find and Activate active part
343 if (myPartActivating)
345 myActionsMgr->update();
346 SessionPtr aMgr = ModelAPI_Session::get();
347 DocumentPtr aActiveDoc = aMgr->activeDocument();
348 DocumentPtr aDoc = aMgr->moduleDocument();
349 if (aActiveDoc == aDoc) {
350 activatePart(ResultPartPtr());
353 std::string aGrpName = ModelAPI_ResultPart::group();
354 for (int i = 0; i < aDoc->size(aGrpName); i++) {
355 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
356 if (aPart->partDoc() == aActiveDoc) {
357 activatePart(aPart); // Activate a part which corresponds to active Doc
361 // If not found then activate global document
362 activatePart(ResultPartPtr());
365 //Show error dialog if error message received.
366 boost::shared_ptr<Events_Error> anAppError = boost::dynamic_pointer_cast<Events_Error>(theMessage);
368 emit errorOccurred(QString::fromLatin1(anAppError->description()));
371 if (!isSalomeMode()) {
372 SessionPtr aMgr = ModelAPI_Session::get();
373 if (aMgr->isModified() != myMainWindow->isModifiedState())
374 myMainWindow->setModifiedState(aMgr->isModified());
379 //******************************************************
380 void XGUI_Workshop::onStartWaiting()
382 if (Events_LongOp::isPerformed()) {
383 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
387 //******************************************************
388 void XGUI_Workshop::onFeatureUpdatedMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
390 std::set<ObjectPtr> aFeatures = theMsg->objects();
391 if (myOperationMgr->hasOperation()) {
392 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
393 std::set<ObjectPtr>::const_iterator aIt;
394 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
395 ObjectPtr aNewFeature = (*aIt);
396 if (aNewFeature == aCurrentFeature) {
397 myPropertyPanel->updateContentWidget(aCurrentFeature);
402 myOperationMgr->onValidateOperation();
404 myObjectBrowser->processEvent(theMsg);
407 //******************************************************
408 void XGUI_Workshop::onFeatureRedisplayMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
410 std::set<ObjectPtr> aObjects = theMsg->objects();
411 std::set<ObjectPtr>::const_iterator aIt;
412 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
413 ObjectPtr aObj = (*aIt);
414 if (!aObj->data() || !aObj->data()->isValid() || aObj->document()->isConcealed(aObj))
415 myDisplayer->erase(aObj, false);
417 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
418 myDisplayer->display(aObj, false); // In order to update presentation
420 if (myOperationMgr->hasOperation()) {
421 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
422 if (aOperation->hasObject(aObj)) { // Display only current operation results
423 myDisplayer->display(aObj, false);
429 myDisplayer->updateViewer();
432 //******************************************************
433 void XGUI_Workshop::onFeatureCreatedMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
435 std::set<ObjectPtr> aObjects = theMsg->objects();
437 std::set<ObjectPtr>::const_iterator aIt;
438 bool aHasPart = false;
439 bool isDisplayed = false;
440 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
441 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
444 // If a feature is created from the aplication's python console
445 // it doesn't stored in the operation mgr and doesn't displayed
446 } else if (myOperationMgr->hasOperation()) {
447 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
448 if (!(*aIt)->document()->isConcealed(*aIt) &&
449 aOperation->hasObject(*aIt)) { // Display only current operation results
450 myDisplayer->display(*aIt, false);
456 myObjectBrowser->processEvent(theMsg);
458 myDisplayer->updateViewer();
464 //******************************************************
465 void XGUI_Workshop::onObjectDeletedMsg(const boost::shared_ptr<ModelAPI_ObjectDeletedMessage>& theMsg)
468 myObjectBrowser->processEvent(theMsg);
469 //std::set<ObjectPtr> aFeatures = theMsg->objects();
472 //******************************************************
473 void XGUI_Workshop::onOperationStarted()
475 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
476 if (this->isSalomeMode())
477 aOperation->setNestedFeatures(mySalomeConnector->nestedActions(aOperation->id()));
479 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
481 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
482 connectWithOperation(aOperation);
485 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
486 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
489 myPropertyPanel->cleanContent();
490 aFactory.createWidget(myPropertyPanel->contentWidget());
492 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
493 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
494 ModuleBase_ModelWidget* aWidget;
495 for (; anIt != aLast; anIt++) {
497 aWidget->setFeature(aOperation->feature());
498 aWidget->enableFocusProcessing();
499 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
500 // Init default values
501 if (!aOperation->isEditOperation() && !aWidget->isComputedDefault()) {
502 aWidget->storeValue();
506 myPropertyPanel->setModelWidgets(aWidgets);
507 myPropertyPanel->onActivateNextWidget(NULL);
508 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
510 updateCommandStatus();
513 //******************************************************
514 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
516 //!< No need for property panel
517 updateCommandStatus();
519 myPropertyPanel->cleanContent();
522 bool XGUI_Workshop::event(QEvent * theEvent)
524 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
527 std::cout << "XGUI_Workshop::event " << "Got PostponeMessageQtEvent" << std::endl;
528 bool isMyThread = (QApplication::instance()->thread() == QThread::currentThread());
529 std::cout << "XGUI_Workshop::event " << "I am in the Qt's thread: "
530 << isMyThread << std::endl;
532 boost::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
533 processEvent(aEventPtr);
542 void XGUI_Workshop::addFeature(const boost::shared_ptr<Config_FeatureMessage>& theMessage)
546 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
550 // Remember features icons
551 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
553 //Find or create Workbench
554 QString aWchName = QString::fromStdString(theMessage->workbenchId());
555 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
556 bool isUsePropPanel = theMessage->isUseInput();
557 QString aFeatureId = QString::fromStdString(theMessage->id());
558 if (isSalomeMode()) {
559 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
560 QString::fromStdString(theMessage->text()),
561 QString::fromStdString(theMessage->tooltip()),
562 QIcon(theMessage->icon().c_str()),
565 salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
566 myActionsMgr->addCommand(aAction);
567 myModule->featureCreated(aAction);
570 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
571 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
573 aPage = addWorkbench(aWchName);
575 //Find or create Group
576 QString aGroupName = QString::fromStdString(theMessage->groupId());
577 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
579 aGroup = aPage->addGroup(aGroupName);
581 QString aDocKind = QString::fromStdString(theMessage->documentKind());
582 // Check if hotkey sequence is already defined:
583 QKeySequence aHotKey = myActionsMgr->registerShortcut(
584 QString::fromStdString(theMessage->keysequence()));
586 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
587 QString::fromStdString(theMessage->text()),
588 QString::fromStdString(theMessage->tooltip()),
589 QIcon(theMessage->icon().c_str()),
593 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
594 myActionsMgr->addCommand(aCommand);
595 myModule->featureCreated(aCommand);
600 * Makes a signal/slot connections between Property Panel
601 * and given operation. The given operation becomes a
602 * current operation and previous operation if exists
604 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
606 QAction* aCommand = 0;
607 if (isSalomeMode()) {
608 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
610 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
611 FeaturePtr aFeature = theOperation->feature();
613 aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
615 //Abort operation on uncheck the command
617 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
622 * Saves document with given name.
624 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
626 QApplication::restoreOverrideCursor();
627 SessionPtr aMgr = ModelAPI_Session::get();
628 aMgr->save(theName.toLatin1().constData(), theFileNames);
629 QApplication::restoreOverrideCursor();
632 bool XGUI_Workshop::isActiveOperationAborted()
634 return myOperationMgr->abortAllOperations();
637 //******************************************************
638 void XGUI_Workshop::onExit()
640 SessionPtr aMgr = ModelAPI_Session::get();
641 if (aMgr->isModified()) {
642 int anAnswer = QMessageBox::question(
643 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
644 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
645 if (anAnswer == QMessageBox::Save) {
646 bool saved = onSave();
650 } else if (anAnswer == QMessageBox::Cancel) {
657 //******************************************************
658 void XGUI_Workshop::onNew()
660 QApplication::setOverrideCursor(Qt::WaitCursor);
661 if (objectBrowser() == 0) {
663 mySelector->connectViewers();
665 myViewerProxy->connectToViewer();
667 if (!isSalomeMode()) {
668 myMainWindow->showPythonConsole();
669 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
670 aWnd->showMaximized();
671 updateCommandStatus();
673 myContextMenuMgr->connectViewer();
674 QApplication::restoreOverrideCursor();
677 //******************************************************
678 void XGUI_Workshop::onOpen()
680 if(!isActiveOperationAborted())
682 //save current file before close if modified
683 SessionPtr aSession = ModelAPI_Session::get();
684 if (aSession->isModified()) {
685 //TODO(sbh): re-launch the app?
686 int anAnswer = QMessageBox::question(
687 myMainWindow, tr("Save current file"),
688 tr("The document is modified, save before opening another?"),
689 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
690 if (anAnswer == QMessageBox::Save) {
692 } else if (anAnswer == QMessageBox::Cancel) {
695 aSession->moduleDocument()->close();
699 //show file dialog, check if readable and open
700 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
701 if (myCurrentDir.isEmpty())
703 QFileInfo aFileInfo(myCurrentDir);
704 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
705 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
709 QApplication::setOverrideCursor(Qt::WaitCursor);
710 aSession->load(myCurrentDir.toLatin1().constData());
711 myObjectBrowser->rebuildDataTree();
713 updateCommandStatus();
714 QApplication::restoreOverrideCursor();
717 //******************************************************
718 bool XGUI_Workshop::onSave()
720 if(!isActiveOperationAborted())
722 if (myCurrentDir.isEmpty()) {
725 std::list<std::string> aFiles;
726 saveDocument(myCurrentDir, aFiles);
727 updateCommandStatus();
728 myMainWindow->setModifiedState(false);
732 //******************************************************
733 bool XGUI_Workshop::onSaveAs()
735 if(!isActiveOperationAborted())
737 QFileDialog dialog(mainWindow());
738 dialog.setWindowTitle(tr("Select directory to save files..."));
739 dialog.setFileMode(QFileDialog::Directory);
740 dialog.setFilter(tr("Folders (*)"));
741 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
742 dialog.setViewMode(QFileDialog::Detail);
744 if (!dialog.exec()) {
747 QString aTempDir = dialog.selectedFiles().first();
749 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
750 int answer = QMessageBox::question(
752 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
754 tr("The folder already contains some files, save anyway?"),
755 QMessageBox::Save | QMessageBox::Cancel);
756 if (answer == QMessageBox::Cancel) {
760 myCurrentDir = aTempDir;
761 if (!isSalomeMode()) {
762 myMainWindow->setCurrentDir(myCurrentDir, false);
763 myMainWindow->setModifiedState(false);
768 //******************************************************
769 void XGUI_Workshop::onUndo()
771 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
772 SessionPtr aMgr = ModelAPI_Session::get();
773 if (aMgr->isOperation())
774 operationMgr()->onAbortOperation();
776 updateCommandStatus();
779 //******************************************************
780 void XGUI_Workshop::onRedo()
782 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
783 SessionPtr aMgr = ModelAPI_Session::get();
784 if (aMgr->isOperation())
785 operationMgr()->onAbortOperation();
787 updateCommandStatus();
790 //******************************************************
791 void XGUI_Workshop::onRebuild()
793 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
794 Events_Loop::loop()->send(boost::shared_ptr<Events_Message>(
795 new Events_Message(aRebuildEvent, this)));
798 //******************************************************
799 void XGUI_Workshop::onPreferences()
802 XGUI_Preferences::editPreferences(aModif);
803 if (aModif.size() > 0) {
805 foreach (XGUI_Pref aPref, aModif)
807 aSection = aPref.first;
808 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
810 myMainWindow->viewer()->updateFromResources();
811 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
813 myMainWindow->menuObject()->updateFromResources();
819 //******************************************************
820 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
822 QString libName = QString::fromStdString(library(theModule.toStdString()));
823 if (libName.isEmpty()) {
824 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
829 CREATE_FUNC crtInst = 0;
832 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
836 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
837 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
838 QString aMsg((char*) &lpMsgBuf);
839 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
840 ::LocalFree(lpMsgBuf);
842 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
846 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
847 | FORMAT_MESSAGE_IGNORE_INSERTS,
848 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
849 QString aMsg((char*) &lpMsgBuf);
850 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
851 ::LocalFree(lpMsgBuf);
855 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
857 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
859 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
861 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
866 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
868 if (!err.isEmpty()) {
870 QMessageBox::warning(mainWindow(), tr("Error"), err);
872 qWarning(qPrintable(err));
878 //******************************************************
879 bool XGUI_Workshop::activateModule()
881 Config_ModuleReader aModuleReader;
882 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
883 myModule = loadModule(moduleName);
886 myModule->createFeatures();
887 myActionsMgr->update();
891 //******************************************************
892 void XGUI_Workshop::updateCommandStatus()
894 QList<QAction*> aCommands;
895 if (isSalomeMode()) { // update commands in SALOME mode
896 aCommands = salomeConnector()->commandList();
898 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
899 foreach (XGUI_Command* aCmd, aMenuBar->features())
900 aCommands.append(aCmd);
902 SessionPtr aMgr = ModelAPI_Session::get();
903 if (aMgr->hasModuleDocument()) {
906 foreach(QAction* aCmd, aCommands) {
907 QString aId = aCmd->data().toString();
908 if (aId == "UNDO_CMD")
910 else if (aId == "REDO_CMD")
913 // Enable all commands
914 aCmd->setEnabled(true);
916 aUndoCmd->setEnabled(aMgr->canUndo());
917 aRedoCmd->setEnabled(aMgr->canRedo());
919 foreach(QAction* aCmd, aCommands) {
920 QString aId = aCmd->data().toString();
921 if (aId == "NEW_CMD")
922 aCmd->setEnabled(true);
923 else if (aId == "EXIT_CMD")
924 aCmd->setEnabled(true);
926 aCmd->setEnabled(false);
929 myActionsMgr->update();
932 //******************************************************
933 QList<QAction*> XGUI_Workshop::getModuleCommands() const
935 QList<QAction*> aCommands;
936 if (isSalomeMode()) { // update commands in SALOME mode
937 aCommands = salomeConnector()->commandList();
939 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
940 foreach(XGUI_Command* aCmd, aMenuBar->features())
942 aCommands.append(aCmd);
948 //******************************************************
949 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
951 QDockWidget* aObjDock = new QDockWidget(theParent);
952 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
953 aObjDock->setWindowTitle(tr("Object browser"));
954 aObjDock->setStyleSheet(
955 "::title { position: relative; padding-left: 5px; text-align: left center }");
956 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
957 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
958 SLOT(changeCurrentDocument(ObjectPtr)));
959 aObjDock->setWidget(myObjectBrowser);
961 myContextMenuMgr->connectObjectBrowser();
965 //******************************************************
967 * Creates dock widgets, places them in corresponding area
968 * and tabifies if necessary.
970 void XGUI_Workshop::createDockWidgets()
972 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
973 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
974 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
975 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
976 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
977 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
978 hidePropertyPanel(); //<! Invisible by default
980 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
982 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
983 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
984 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
985 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
986 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
987 SLOT(onKeyReleased(QKeyEvent*)));
988 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
989 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
990 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
991 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
992 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
993 SLOT(setAcceptEnabled(bool)));
997 //******************************************************
998 void XGUI_Workshop::showPropertyPanel()
1000 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1001 //<! Restore ability to close panel from the window's menu
1002 aViewAct->setEnabled(true);
1003 myPropertyPanel->show();
1004 myPropertyPanel->raise();
1007 //******************************************************
1008 void XGUI_Workshop::hidePropertyPanel()
1010 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1011 //<! Do not allow to show empty property panel
1012 aViewAct->setEnabled(false);
1013 myPropertyPanel->hide();
1016 //******************************************************
1017 void XGUI_Workshop::showObjectBrowser()
1019 myObjectBrowser->parentWidget()->show();
1022 //******************************************************
1023 void XGUI_Workshop::hideObjectBrowser()
1025 myObjectBrowser->parentWidget()->hide();
1028 //******************************************************
1029 void XGUI_Workshop::onFeatureTriggered()
1031 QAction* aCmd = dynamic_cast<QAction*>(sender());
1033 QString aId = salomeConnector()->commandId(aCmd);
1035 myModule->launchOperation(aId);
1039 //******************************************************
1040 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1042 SessionPtr aMgr = ModelAPI_Session::get();
1044 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1046 DocumentPtr aPartDoc = aPart->partDoc();
1048 aMgr->setActiveDocument(aPartDoc);
1053 aMgr->setActiveDocument(aMgr->moduleDocument());
1056 //******************************************************
1057 void XGUI_Workshop::salomeViewerSelectionChanged()
1059 emit salomeViewerSelection();
1062 //**************************************************************
1063 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
1065 return mySalomeConnector->viewer();
1068 //**************************************************************
1069 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1071 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
1072 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1073 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1074 activatePart(aPart);
1075 } else if (theId == "DEACTIVATE_PART_CMD")
1076 activatePart(ResultPartPtr());
1077 else if (theId == "DELETE_CMD")
1078 deleteObjects(aObjects);
1079 else if (theId == "SHOW_CMD")
1080 showObjects(aObjects, true);
1081 else if (theId == "HIDE_CMD")
1082 showObjects(aObjects, false);
1083 else if (theId == "SHOW_ONLY_CMD")
1084 showOnlyObjects(aObjects);
1085 else if (theId == "SHADING_CMD")
1086 setDisplayMode(aObjects, XGUI_Displayer::Shading);
1087 else if (theId == "WIREFRAME_CMD")
1088 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1091 //**************************************************************
1092 void XGUI_Workshop::onWidgetValuesChanged()
1094 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
1095 FeaturePtr aFeature = anOperation->feature();
1097 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
1099 // aCustom->storeValue(aFeature);
1101 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
1102 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
1103 for (; anIt != aLast; anIt++) {
1104 ModuleBase_ModelWidget* aCustom = *anIt;
1105 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
1106 //aCustom->storeValue(aFeature);
1107 aCustom->storeValue();
1112 //**************************************************************
1113 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1115 if (!myPartActivating) {
1116 myPartActivating = true;
1118 theFeature->activate();
1119 changeCurrentDocument(theFeature);
1120 myObjectBrowser->activatePart(theFeature);
1121 myPartActivating = false;
1125 //**************************************************************
1126 void XGUI_Workshop::activateLastPart()
1128 SessionPtr aMgr = ModelAPI_Session::get();
1129 DocumentPtr aDoc = aMgr->moduleDocument();
1130 std::string aGrpName = ModelAPI_ResultPart::group();
1131 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1132 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1134 activatePart(aPart);
1138 //**************************************************************
1139 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1141 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1142 QMessageBox::StandardButton aRes = QMessageBox::warning(
1143 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1144 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1145 // ToDo: definbe deleting method
1146 if (aRes == QMessageBox::Yes) {
1147 SessionPtr aMgr = ModelAPI_Session::get();
1148 aMgr->startOperation();
1149 foreach (ObjectPtr aObj, theList)
1151 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1153 DocumentPtr aDoc = aPart->document();
1154 if (aDoc == aMgr->activeDocument()) {
1157 //aMgr->moduleDocument()->removeFeature(aPart->owner());
1159 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1161 aObj->document()->removeFeature(aFeature);
1164 myDisplayer->updateViewer();
1165 aMgr->finishOperation();
1169 //**************************************************************
1170 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1172 foreach (ObjectPtr aObj, theList)
1174 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1177 myDisplayer->display(aRes, false);
1179 myDisplayer->erase(aRes, false);
1183 myDisplayer->updateViewer();
1186 //**************************************************************
1187 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1189 myDisplayer->eraseAll(false);
1190 showObjects(theList, true);
1194 //**************************************************************
1195 void XGUI_Workshop::updateCommandsOnViewSelection()
1197 XGUI_Selection* aSelection = mySelector->selection();
1198 if (aSelection->getSelected().size() == 0)
1201 // Restrict validators to manage only nested (child) features
1202 // of the current feature i.e. if current feature is Sketch -
1203 // Sketch Features & Constraints can be validated.
1204 QStringList aNestedIds;
1205 if(myOperationMgr->hasOperation()) {
1206 FeaturePtr aFeature = myOperationMgr->currentOperation()->feature();
1208 aNestedIds << myActionsMgr->nestedCommands(QString::fromStdString(aFeature->getKind()));
1211 SessionPtr aMgr = ModelAPI_Session::get();
1212 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1213 QList<QAction*> aActions = getModuleCommands();
1214 foreach(QAction* aAction, aActions) {
1215 QString aId = aAction->data().toString();
1216 if(!aNestedIds.contains(aId))
1218 std::list<ModelAPI_Validator*> aValidators;
1219 std::list<std::list<std::string> > anArguments;
1220 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1221 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1222 for (; aValidator != aValidators.end(); aValidator++) {
1224 const ModuleBase_SelectionValidator* aSelValidator =
1225 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1226 if (aSelValidator) {
1227 aAction->setEnabled(aSelValidator->isValid(aSelection));
1234 //**************************************************************
1235 void XGUI_Workshop::registerValidators() const
1237 SessionPtr aMgr = ModelAPI_Session::get();
1238 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1241 //**************************************************************
1242 void XGUI_Workshop::displayAllResults()
1244 SessionPtr aMgr = ModelAPI_Session::get();
1245 DocumentPtr aRootDoc = aMgr->moduleDocument();
1246 displayDocumentResults(aRootDoc);
1247 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1248 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1249 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1250 displayDocumentResults(aPart->partDoc());
1252 myDisplayer->updateViewer();
1255 //**************************************************************
1256 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1260 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1261 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1264 //**************************************************************
1265 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1267 for (int i = 0; i < theDoc->size(theGroup); i++)
1268 myDisplayer->display(theDoc->object(theGroup, i), false);
1271 //**************************************************************
1272 void XGUI_Workshop::setDisplayMode(const QList<ObjectPtr>& theList, int theMode)
1274 foreach(ObjectPtr aObj, theList) {
1275 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1277 if (theList.size() > 0)
1278 myDisplayer->updateViewer();