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 "ModuleBase_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_Selection.h"
14 #include "XGUI_ObjectsBrowser.h"
15 #include "XGUI_Displayer.h"
16 #include "XGUI_OperationMgr.h"
17 #include "XGUI_SalomeConnector.h"
18 #include "XGUI_SalomeViewer.h"
19 #include "XGUI_ActionsMgr.h"
20 #include "XGUI_ErrorDialog.h"
21 #include "XGUI_ViewerProxy.h"
22 #include "XGUI_PropertyPanel.h"
23 #include "XGUI_ContextMenuMgr.h"
24 #include "XGUI_ModuleConnector.h"
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_PluginManager.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_AttributeDocRef.h>
31 #include <ModelAPI_Object.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultBody.h>
36 #include <PartSetPlugin_Part.h>
38 #include <Events_Loop.h>
39 #include <Events_Error.h>
40 #include <Events_LongOp.h>
42 #include <ModuleBase_Operation.h>
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_OperationDescription.h>
45 #include <ModuleBase_SelectionValidator.h>
46 #include <ModuleBase_ResultValidators.h>
48 #include <Config_Common.h>
49 #include <Config_FeatureMessage.h>
50 #include <Config_PointerMessage.h>
51 #include <Config_ModuleReader.h>
53 #include <QApplication>
54 #include <QFileDialog>
55 #include <QMessageBox>
56 #include <QMdiSubWindow>
57 #include <QPushButton>
58 #include <QDockWidget>
73 QMap<QString, QString> XGUI_Workshop::myIcons;
75 QString XGUI_Workshop::featureIcon(const std::string& theId)
77 QString aId(theId.c_str());
78 if (myIcons.contains(aId))
83 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
85 myCurrentDir(QString()),
87 mySalomeConnector(theConnector),
92 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
94 myDisplayer = new XGUI_Displayer(this);
96 mySelector = new XGUI_SelectionMgr(this);
97 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
99 myOperationMgr = new XGUI_OperationMgr(this);
100 myActionsMgr = new XGUI_ActionsMgr(this);
101 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
102 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
103 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
104 this, SLOT(onContextMenuCommand(const QString&, bool)));
106 myViewerProxy = new XGUI_ViewerProxy(this);
107 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
109 myModuleConnector = new XGUI_ModuleConnector(this);
111 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
112 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
113 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
114 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
115 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
116 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
117 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
120 //******************************************************
121 XGUI_Workshop::~XGUI_Workshop(void)
125 //******************************************************
126 void XGUI_Workshop::startApplication()
129 //Initialize event listening
130 Events_Loop* aLoop = Events_Loop::loop();
131 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
132 //TODO(sbh): Implement static method to extract event id [SEID]
133 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
134 // TODO Is it good to use non standard event within workshop?
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
136 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
137 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
138 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
140 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
142 registerValidators();
145 myMainWindow->show();
146 updateCommandStatus();
151 //******************************************************
152 void XGUI_Workshop::initMenu()
154 myContextMenuMgr->createActions();
156 if (isSalomeMode()) {
157 // Create only Undo, Redo commands
158 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD",
159 tr("Undo"), tr("Undo last command"),
160 QIcon(":pictures/undo.png"),
161 QKeySequence::Undo, false);
162 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
163 aAction = salomeConnector()->addEditCommand("REDO_CMD",
164 tr("Redo"), tr("Redo last command"),
165 QIcon(":pictures/redo.png"),
166 QKeySequence::Redo, false);
167 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
168 salomeConnector()->addEditMenuSeparator();
171 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
173 // File commands group
174 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
176 XGUI_Command* aCommand;
178 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
179 QIcon(":pictures/save.png"), QKeySequence::Save);
180 aCommand->connectTo(this, SLOT(onSave()));
181 //aCommand->disable();
183 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
184 QIcon(":pictures/undo.png"), QKeySequence::Undo);
185 aCommand->connectTo(this, SLOT(onUndo()));
187 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
188 QIcon(":pictures/redo.png"), QKeySequence::Redo);
189 aCommand->connectTo(this, SLOT(onRedo()));
191 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
192 QIcon(":pictures/rebuild.png"));
194 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
195 QIcon(":pictures/save.png"));
196 aCommand->connectTo(this, SLOT(onSaveAs()));
197 //aCommand->disable();
199 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
200 QIcon(":pictures/open.png"), QKeySequence::Open);
201 aCommand->connectTo(this, SLOT(onOpen()));
203 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
204 // QIcon(":pictures/new.png"), QKeySequence::New);
205 //aCommand->connectTo(this, SLOT(onNew()));
207 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
208 QIcon(":pictures/close.png"), QKeySequence::Close);
209 aCommand->connectTo(this, SLOT(onExit()));
210 //FIXME: SBH's test action. Can be used for some GUI tests.
212 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
213 // QIcon(":pictures/close.png"));
214 // aCommand->connectTo(myActionsMgr, SLOT(update()));
218 //******************************************************
219 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
221 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
222 return aMenuBar->addWorkbench(theName);
225 //******************************************************
226 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
228 //A message to start feature creation received.
229 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
230 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
231 if(!aFeatureMsg->isInternal()) {
232 addFeature(aFeatureMsg);
237 // Process creation of Part
238 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
239 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
240 onFeatureCreatedMsg(aUpdMsg);
245 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
246 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
247 onFeatureRedisplayMsg(aUpdMsg);
251 //Update property panel on corresponding message. If there is no current operation (no
252 //property panel), or received message has different feature to the current - do nothing.
253 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
254 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
255 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
256 onFeatureUpdatedMsg(anUpdateMsg);
260 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
261 const ModelAPI_ObjectDeletedMessage* aDelMsg =
262 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
263 onObjectDeletedMsg(aDelMsg);
267 if (theMessage->eventID() == Events_LongOp::eventID()) {
268 if (Events_LongOp::isPerformed())
269 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
270 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
272 QApplication::restoreOverrideCursor();
276 //An operation passed by message. Start it, process and commit.
277 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
278 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
279 //myPropertyPanel->cleanContent();
280 ModuleBase_Operation* anOperation = (ModuleBase_Operation*)aPartSetMsg->pointer();
282 if (myOperationMgr->startOperation(anOperation)) {
283 myPropertyPanel->updateContentWidget(anOperation->feature());
284 if (!anOperation->getDescription()->hasXmlRepresentation()) {
285 if (anOperation->commit())
286 updateCommandStatus();
291 //Show error dialog if error message received.
292 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
294 emit errorOccurred(QString::fromLatin1(anAppError->description()));
298 //******************************************************
299 void XGUI_Workshop::onStartWaiting()
301 if (Events_LongOp::isPerformed()) {
302 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
306 //******************************************************
307 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
309 std::set<ObjectPtr> aFeatures = theMsg->objects();
310 if (myOperationMgr->hasOperation())
312 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
313 std::set<ObjectPtr>::const_iterator aIt;
314 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
315 ObjectPtr aNewFeature = (*aIt);
316 if(aNewFeature == aCurrentFeature) {
317 myPropertyPanel->updateContentWidget(aCurrentFeature);
324 //******************************************************
325 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
327 std::set<ObjectPtr> aObjects = theMsg->objects();
328 std::set<ObjectPtr>::const_iterator aIt;
329 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
330 ObjectPtr aObj = (*aIt);
331 if (!aObj->data() || !aObj->data()->isValid())
332 myDisplayer->erase(aObj, false);
334 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
335 myDisplayer->display(aObj, false); // In order to update presentation
337 if(myOperationMgr->hasOperation()) {
338 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
339 if (aOperation->hasObject(aObj)) { // Display only current operation results
340 myDisplayer->display(aObj, false);
346 myDisplayer->updateViewer();
349 //******************************************************
350 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
352 std::set<ObjectPtr> aObjects = theMsg->objects();
354 std::set<ObjectPtr>::const_iterator aIt;
355 bool aHasPart = false;
356 bool isDisplayed = false;
357 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
358 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
361 // If a feature is created from the aplication's python console
362 // it doesn't stored in the operation mgr and doesn't displayed
363 } else if(myOperationMgr->hasOperation()) {
364 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
365 if (aOperation->hasObject(*aIt)) { // Display only current operation results
366 myDisplayer->display(*aIt, false);
372 myDisplayer->updateViewer();
374 //The created part will be created in Object Browser later and we have to activate it
375 // only when it is created everywere
376 QTimer::singleShot(50, this, SLOT(activateLastPart()));
380 //******************************************************
381 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
383 //std::set<ObjectPtr> aFeatures = theMsg->objects();
386 //******************************************************
387 void XGUI_Workshop::onOperationStarted()
389 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
391 if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
392 connectWithOperation(aOperation);
395 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
396 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
398 myPropertyPanel->cleanContent();
399 aFactory.createWidget(myPropertyPanel->contentWidget());
401 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
402 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
403 ModuleBase_ModelWidget* aWidget;
404 for (; anIt != aLast; anIt++) {
406 aWidget->setFeature(aOperation->feature());
407 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
408 QObject::connect(aWidget, SIGNAL(valuesChanged()),
409 this, SLOT(onWidgetValuesChanged()));
410 // Init default values
411 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
412 //aWidget->storeValue(aOperation->feature());
414 aWidget->storeValue();
418 myPropertyPanel->setModelWidgets(aWidgets);
419 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
421 updateCommandStatus();
424 //******************************************************
425 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
427 //!< No need for property panel
428 updateCommandStatus();
430 myPropertyPanel->cleanContent();
436 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
440 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
444 // Remember features icons
445 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
447 //Find or create Workbench
448 QString aWchName = QString::fromStdString(theMessage->workbenchId());
449 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
450 bool isUsePropPanel = theMessage->isUseInput();
451 QString aId = QString::fromStdString(theMessage->id());
452 if (isSalomeMode()) {
453 QAction* aAction = salomeConnector()->addFeature(aWchName,
455 QString::fromStdString(theMessage->text()),
456 QString::fromStdString(theMessage->tooltip()),
457 QIcon(theMessage->icon().c_str()),
458 QKeySequence(), isUsePropPanel);
459 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
460 myActionsMgr->addCommand(aAction);
461 myModule->featureCreated(aAction);
464 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
465 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
467 aPage = addWorkbench(aWchName);
469 //Find or create Group
470 QString aGroupName = QString::fromStdString(theMessage->groupId());
471 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
473 aGroup = aPage->addGroup(aGroupName);
476 XGUI_Command* aCommand = aGroup->addFeature(aId,
477 QString::fromStdString(theMessage->text()),
478 QString::fromStdString(theMessage->tooltip()),
479 QIcon(theMessage->icon().c_str()),
480 QKeySequence(), isUsePropPanel);
481 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
482 myActionsMgr->addCommand(aCommand);
483 myModule->featureCreated(aCommand);
488 * Makes a signal/slot connections between Property Panel
489 * and given operation. The given operation becomes a
490 * current operation and previous operation if exists
492 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
494 QAction* aCommand = 0;
495 if (isSalomeMode()) {
496 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
498 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
499 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
501 //Abort operation on uncheck the command
503 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
507 * Saves document with given name.
509 void XGUI_Workshop::saveDocument(QString theName)
511 QApplication::restoreOverrideCursor();
512 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
513 DocumentPtr aDoc = aMgr->rootDocument();
514 aDoc->save(theName.toLatin1().constData());
515 QApplication::restoreOverrideCursor();
518 //******************************************************
519 void XGUI_Workshop::onExit()
521 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
522 DocumentPtr aDoc = aMgr->rootDocument();
523 if(aDoc->isModified()) {
524 int anAnswer = QMessageBox::question(
525 myMainWindow, tr("Save current file"),
526 tr("The document is modified, save before exit?"),
527 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
528 if(anAnswer == QMessageBox::Save) {
529 bool saved = onSave();
533 } else if (anAnswer == QMessageBox::Cancel) {
540 //******************************************************
541 void XGUI_Workshop::onNew()
543 QApplication::setOverrideCursor(Qt::WaitCursor);
544 if (objectBrowser() == 0) {
546 mySelector->connectViewers();
548 myViewerProxy->connectToViewer();
550 if (!isSalomeMode()) {
551 myMainWindow->showPythonConsole();
552 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
553 aWnd->showMaximized();
554 updateCommandStatus();
556 myContextMenuMgr->connectViewer();
557 QApplication::restoreOverrideCursor();
560 //******************************************************
561 void XGUI_Workshop::onOpen()
563 //save current file before close if modified
564 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
565 DocumentPtr aDoc = aMgr->rootDocument();
566 if(aDoc->isModified()) {
567 //TODO(sbh): re-launch the app?
568 int anAnswer = QMessageBox::question(
569 myMainWindow, tr("Save current file"),
570 tr("The document is modified, save before opening another?"),
571 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
572 if(anAnswer == QMessageBox::Save) {
574 } else if (anAnswer == QMessageBox::Cancel) {
581 //show file dialog, check if readable and open
582 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
583 if(myCurrentDir.isEmpty())
585 QFileInfo aFileInfo(myCurrentDir);
586 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
587 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
591 QApplication::setOverrideCursor(Qt::WaitCursor);
592 aDoc->load(myCurrentDir.toLatin1().constData());
593 myObjectBrowser->rebuildDataTree();
595 updateCommandStatus();
596 QApplication::restoreOverrideCursor();
599 //******************************************************
600 bool XGUI_Workshop::onSave()
602 if(myCurrentDir.isEmpty()) {
605 saveDocument(myCurrentDir);
606 updateCommandStatus();
610 //******************************************************
611 bool XGUI_Workshop::onSaveAs()
613 QFileDialog dialog(mainWindow());
614 dialog.setWindowTitle(tr("Select directory to save files..."));
615 dialog.setFileMode(QFileDialog::Directory);
616 dialog.setFilter(tr("Folders (*)"));
617 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
618 dialog.setViewMode(QFileDialog::Detail);
623 QString aTempDir = dialog.selectedFiles().first();
625 if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
626 int answer = QMessageBox::question(myMainWindow,
627 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
629 tr("The folder already contains some files, save anyway?"),
630 QMessageBox::Save|QMessageBox::Cancel);
631 if(answer == QMessageBox::Cancel) {
635 myCurrentDir = aTempDir;
639 //******************************************************
640 void XGUI_Workshop::onUndo()
642 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
643 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
644 DocumentPtr aDoc = aMgr->rootDocument();
645 if (aDoc->isOperation())
646 operationMgr()->abortOperation();
648 updateCommandStatus();
651 //******************************************************
652 void XGUI_Workshop::onRedo()
654 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
655 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
656 DocumentPtr aDoc = aMgr->rootDocument();
657 if (aDoc->isOperation())
658 operationMgr()->abortOperation();
660 updateCommandStatus();
663 //******************************************************
664 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
667 QString::fromStdString(library(theModule.toStdString()));
668 if (libName.isEmpty()) {
670 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
675 CREATE_FUNC crtInst = 0;
678 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
682 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
683 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
684 QString aMsg((char*) &lpMsgBuf);
685 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
686 ::LocalFree(lpMsgBuf);
688 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
692 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
693 | FORMAT_MESSAGE_IGNORE_INSERTS,
694 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
695 QString aMsg((char*) &lpMsgBuf);
696 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
697 ::LocalFree(lpMsgBuf);
701 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
703 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
705 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
707 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
712 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
714 if (!err.isEmpty()) {
716 QMessageBox::warning(mainWindow(), tr("Error"), err);
718 qWarning( qPrintable( err ));
724 //******************************************************
725 bool XGUI_Workshop::activateModule()
727 Config_ModuleReader aModuleReader;
728 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
729 myModule = loadModule(moduleName);
732 myModule->createFeatures();
733 myActionsMgr->update();
737 //******************************************************
738 void XGUI_Workshop::updateCommandStatus()
740 QList<QAction*> aCommands;
741 if (isSalomeMode()) { // update commands in SALOME mode
742 aCommands = salomeConnector()->commandList();
744 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
745 foreach (XGUI_Command* aCmd, aMenuBar->features())
746 aCommands.append(aCmd);
748 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
749 if (aMgr->hasRootDocument()) {
752 foreach(QAction* aCmd, aCommands) {
753 QString aId = aCmd->data().toString();
754 if (aId == "UNDO_CMD")
756 else if (aId == "REDO_CMD")
758 else // Enable all commands
759 aCmd->setEnabled(true);
761 DocumentPtr aDoc = aMgr->rootDocument();
762 aUndoCmd->setEnabled(aDoc->canUndo());
763 aRedoCmd->setEnabled(aDoc->canRedo());
765 foreach(QAction* aCmd, aCommands) {
766 QString aId = aCmd->data().toString();
767 if (aId == "NEW_CMD")
768 aCmd->setEnabled(true);
769 else if (aId == "EXIT_CMD")
770 aCmd->setEnabled(true);
772 aCmd->setEnabled(false);
775 myActionsMgr->update();
778 //******************************************************
779 QList<QAction*> XGUI_Workshop::getModuleCommands() const
781 QList<QAction*> aCommands;
782 if (isSalomeMode()) { // update commands in SALOME mode
783 aCommands = salomeConnector()->commandList();
785 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
786 foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
787 if (aWb != aMenuBar->generalPage()) {
788 foreach(XGUI_Command* aCmd, aWb->features())
789 aCommands.append(aCmd);
796 //******************************************************
797 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
799 QDockWidget* aObjDock = new QDockWidget(theParent);
800 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
801 aObjDock->setWindowTitle(tr("Object browser"));
802 aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
803 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
804 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(changeCurrentDocument(ObjectPtr)));
805 aObjDock->setWidget(myObjectBrowser);
807 myContextMenuMgr->connectObjectBrowser();
811 //******************************************************
813 * Creates dock widgets, places them in corresponding area
814 * and tabifies if necessary.
816 void XGUI_Workshop::createDockWidgets()
818 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
820 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
821 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
822 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
823 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
824 hidePropertyPanel(); //<! Invisible by default
826 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
828 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
829 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
830 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
831 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
833 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
834 myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
836 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
837 myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
838 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
839 myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
842 //******************************************************
843 void XGUI_Workshop::showPropertyPanel()
845 QAction* aViewAct = myPropertyPanel->toggleViewAction();
846 //<! Restore ability to close panel from the window's menu
847 aViewAct->setEnabled(true);
848 myPropertyPanel->show();
849 myPropertyPanel->raise();
852 //******************************************************
853 void XGUI_Workshop::hidePropertyPanel()
855 QAction* aViewAct = myPropertyPanel->toggleViewAction();
856 //<! Do not allow to show empty property panel
857 aViewAct->setEnabled(false);
858 myPropertyPanel->hide();
861 //******************************************************
862 void XGUI_Workshop::showObjectBrowser()
864 myObjectBrowser->parentWidget()->show();
867 //******************************************************
868 void XGUI_Workshop::hideObjectBrowser()
870 myObjectBrowser->parentWidget()->hide();
873 //******************************************************
874 void XGUI_Workshop::onFeatureTriggered()
876 QAction* aCmd = dynamic_cast<QAction*>(sender());
878 QString aId = salomeConnector()->commandId(aCmd);
880 myModule->launchOperation(aId);
884 //******************************************************
885 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
887 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
889 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
891 DocumentPtr aPartDoc = aPart->partDoc();
893 aMgr->setCurrentDocument(aPartDoc);
898 aMgr->setCurrentDocument(aMgr->rootDocument());
901 //******************************************************
902 void XGUI_Workshop::salomeViewerSelectionChanged()
904 emit salomeViewerSelection();
908 //**************************************************************
909 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
911 return mySalomeConnector->viewer();
914 //**************************************************************
915 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
917 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
918 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
919 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
921 } else if (theId == "DEACTIVATE_PART_CMD")
922 activatePart(ResultPartPtr());
923 else if (theId == "DELETE_CMD")
924 deleteObjects(aObjects);
925 else if (theId == "SHOW_CMD")
926 showObjects(aObjects, true);
927 else if (theId == "HIDE_CMD")
928 showObjects(aObjects, false);
931 //**************************************************************
932 void XGUI_Workshop::onWidgetValuesChanged()
934 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
935 FeaturePtr aFeature = anOperation->feature();
937 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
939 // aCustom->storeValue(aFeature);
941 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
942 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
943 for (; anIt != aLast; anIt++) {
944 ModuleBase_ModelWidget* aCustom = *anIt;
945 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
946 //aCustom->storeValue(aFeature);
947 aCustom->storeValue();
952 //**************************************************************
953 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
955 changeCurrentDocument(theFeature);
956 myObjectBrowser->activatePart(theFeature);
959 //**************************************************************
960 void XGUI_Workshop::activateLastPart()
962 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
963 DocumentPtr aDoc = aMgr->rootDocument();
964 std::string aGrpName = ModelAPI_ResultPart::group();
965 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
966 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
971 //**************************************************************
972 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
974 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
975 QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"),
976 tr("Seleted features will be deleted. Continue?"),
977 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
978 // ToDo: definbe deleting method
979 if (aRes == QMessageBox::Yes) {
980 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
981 aMgr->rootDocument()->startOperation();
982 foreach (ObjectPtr aObj, theList) {
983 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
985 DocumentPtr aDoc = aPart->document();
986 if (aDoc == aMgr->currentDocument()) {
989 //aMgr->rootDocument()->removeFeature(aPart->owner());
991 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
993 aObj->document()->removeFeature(aFeature);
996 myDisplayer->updateViewer();
997 aMgr->rootDocument()->finishOperation();
1001 //**************************************************************
1002 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1004 foreach (ObjectPtr aObj, theList) {
1005 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1008 myDisplayer->display(aRes, false);
1010 myDisplayer->erase(aRes, false);
1014 myDisplayer->updateViewer();
1017 //**************************************************************
1018 void XGUI_Workshop::updateCommandsOnViewSelection()
1020 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1021 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1022 XGUI_Selection* aSelection = mySelector->selection();
1023 if (aSelection->getSelected().size() == 0)
1026 QList<QAction*> aActions = getModuleCommands();
1027 foreach(QAction* aAction, aActions) {
1028 QString aId = aAction->data().toString();
1029 std::list<ModelAPI_Validator*> aValidators;
1030 aFactory->validators(aId.toStdString(), aValidators);
1031 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1032 for(; aValidator != aValidators.end(); aValidator++) {
1034 const ModuleBase_SelectionValidator* aSelValidator =
1035 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1036 if (aSelValidator) {
1037 aAction->setEnabled(aSelValidator->isValid(aSelection));
1045 //**************************************************************
1046 void XGUI_Workshop::registerValidators() const
1048 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1049 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1053 //**************************************************************
1054 void XGUI_Workshop::displayAllResults()
1056 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1057 DocumentPtr aRootDoc = aMgr->rootDocument();
1058 displayDocumentResults(aRootDoc);
1059 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1060 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1061 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1062 displayDocumentResults(aPart->partDoc());
1064 myDisplayer->updateViewer();
1067 //**************************************************************
1068 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1070 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1071 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1074 //**************************************************************
1075 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1077 for (int i = 0; i < theDoc->size(theGroup); i++)
1078 myDisplayer->display(theDoc->object(theGroup, i), false);