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 <Model_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_ResultPart.h>
35 #include <PartSetPlugin_Part.h>
37 #include <Events_Loop.h>
38 #include <Events_Error.h>
40 #include <ModuleBase_Operation.h>
41 #include <ModuleBase_Operation.h>
42 #include <ModuleBase_OperationDescription.h>
43 #include <ModuleBase_SelectionValidator.h>
45 #include <Config_Common.h>
46 #include <Config_FeatureMessage.h>
47 #include <Config_PointerMessage.h>
48 #include <Config_ModuleReader.h>
50 #include <QApplication>
51 #include <QFileDialog>
52 #include <QMessageBox>
53 #include <QMdiSubWindow>
54 #include <QPushButton>
55 #include <QDockWidget>
70 QMap<QString, QString> XGUI_Workshop::myIcons;
72 QString XGUI_Workshop::featureIcon(const std::string& theId)
74 QString aId(theId.c_str());
75 if (myIcons.contains(aId))
80 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
82 myCurrentDir(QString()),
84 mySalomeConnector(theConnector),
89 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
91 myDisplayer = new XGUI_Displayer(this);
93 mySelector = new XGUI_SelectionMgr(this);
94 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
96 myOperationMgr = new XGUI_OperationMgr(this);
97 myActionsMgr = new XGUI_ActionsMgr(this);
98 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
99 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
100 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
101 this, SLOT(onContextMenuCommand(const QString&, bool)));
103 myViewerProxy = new XGUI_ViewerProxy(this);
104 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
106 myModuleConnector = new XGUI_ModuleConnector(this);
108 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
109 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
110 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
111 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
112 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
113 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
114 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
117 //******************************************************
118 XGUI_Workshop::~XGUI_Workshop(void)
122 //******************************************************
123 void XGUI_Workshop::startApplication()
126 //Initialize event listening
127 Events_Loop* aLoop = Events_Loop::loop();
128 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
129 //TODO(sbh): Implement static method to extract event id [SEID]
130 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
131 // TODO Is it good to use non standard event within workshop?
132 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
133 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
134 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
136 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
140 myMainWindow->show();
141 updateCommandStatus();
146 //******************************************************
147 void XGUI_Workshop::initMenu()
149 myContextMenuMgr->createActions();
151 if (isSalomeMode()) {
152 // Create only Undo, Redo commands
153 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD",
154 tr("Undo"), tr("Undo last command"),
155 QIcon(":pictures/undo.png"),
156 QKeySequence::Undo, false);
157 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
158 aAction = salomeConnector()->addEditCommand("REDO_CMD",
159 tr("Redo"), tr("Redo last command"),
160 QIcon(":pictures/redo.png"),
161 QKeySequence::Redo, false);
162 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
163 salomeConnector()->addEditMenuSeparator();
166 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
168 // File commands group
169 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
171 XGUI_Command* aCommand;
173 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
174 QIcon(":pictures/save.png"), QKeySequence::Save);
175 aCommand->connectTo(this, SLOT(onSave()));
176 //aCommand->disable();
178 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
179 QIcon(":pictures/undo.png"), QKeySequence::Undo);
180 aCommand->connectTo(this, SLOT(onUndo()));
182 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
183 QIcon(":pictures/redo.png"), QKeySequence::Redo);
184 aCommand->connectTo(this, SLOT(onRedo()));
186 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
187 QIcon(":pictures/rebuild.png"));
189 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
190 QIcon(":pictures/save.png"));
191 aCommand->connectTo(this, SLOT(onSaveAs()));
192 //aCommand->disable();
194 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
195 QIcon(":pictures/open.png"), QKeySequence::Open);
196 aCommand->connectTo(this, SLOT(onOpen()));
198 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
199 // QIcon(":pictures/new.png"), QKeySequence::New);
200 //aCommand->connectTo(this, SLOT(onNew()));
202 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
203 QIcon(":pictures/close.png"), QKeySequence::Close);
204 aCommand->connectTo(this, SLOT(onExit()));
205 //FIXME: SBH's test action. Can be used for some GUI tests.
207 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
208 // QIcon(":pictures/close.png"));
209 // aCommand->connectTo(myActionsMgr, SLOT(update()));
213 //******************************************************
214 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
216 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
217 return aMenuBar->addWorkbench(theName);
220 //******************************************************
221 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
223 //A message to start feature creation received.
224 static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
225 if (theMessage->eventID() == aFeatureLoadedId) {
226 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
227 if(!aFeatureMsg->isInternal()) {
228 addFeature(aFeatureMsg);
233 // Process creation of Part
234 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
235 const Model_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const Model_ObjectUpdatedMessage*>(theMessage);
236 onFeatureCreatedMsg(aUpdMsg);
241 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
242 const Model_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const Model_ObjectUpdatedMessage*>(theMessage);
243 onFeatureRedisplayMsg(aUpdMsg);
247 //Update property panel on corresponding message. If there is no current operation (no
248 //property panel), or received message has different feature to the current - do nothing.
249 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
250 const Model_ObjectUpdatedMessage* anUpdateMsg =
251 dynamic_cast<const Model_ObjectUpdatedMessage*>(theMessage);
252 onFeatureUpdatedMsg(anUpdateMsg);
256 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
257 const Model_ObjectDeletedMessage* aDelMsg =
258 dynamic_cast<const Model_ObjectDeletedMessage*>(theMessage);
259 onObjectDeletedMsg(aDelMsg);
263 //An operation passed by message. Start it, process and commit.
264 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
265 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
266 //myPropertyPanel->cleanContent();
267 ModuleBase_Operation* anOperation = (ModuleBase_Operation*)aPartSetMsg->pointer();
269 if (myOperationMgr->startOperation(anOperation)) {
270 myPropertyPanel->updateContentWidget(anOperation->feature());
271 if (!anOperation->getDescription()->hasXmlRepresentation()) {
272 anOperation->commit();
273 updateCommandStatus();
278 //Show error dialog if error message received.
279 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
281 emit errorOccurred(QString::fromLatin1(anAppError->description()));
285 //******************************************************
286 void XGUI_Workshop::onFeatureUpdatedMsg(const Model_ObjectUpdatedMessage* theMsg)
288 std::set<ObjectPtr> aFeatures = theMsg->objects();
289 if (myOperationMgr->hasOperation())
291 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
292 std::set<ObjectPtr>::const_iterator aIt;
293 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
294 ObjectPtr aNewFeature = (*aIt);
295 if(aNewFeature == aCurrentFeature) {
296 myPropertyPanel->updateContentWidget(aCurrentFeature);
303 //******************************************************
304 void XGUI_Workshop::onFeatureRedisplayMsg(const Model_ObjectUpdatedMessage* theMsg)
306 std::set<ObjectPtr> aObjects = theMsg->objects();
307 std::set<ObjectPtr>::const_iterator aIt;
308 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
309 ObjectPtr aObj = (*aIt);
311 myDisplayer->display(aObj, false);
313 myDisplayer->erase(aObj, false);
316 myDisplayer->updateViewer();
319 //******************************************************
320 void XGUI_Workshop::onFeatureCreatedMsg(const Model_ObjectUpdatedMessage* theMsg)
322 std::set<ObjectPtr> aObjects = theMsg->objects();
324 std::set<ObjectPtr>::const_iterator aIt;
325 bool aHasPart = false;
326 bool isDisplayed = false;
327 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
328 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
333 myDisplayer->display(*aIt, false);
338 myDisplayer->updateViewer();
340 //The created part will be created in Object Browser later and we have to activate it
341 // only when it is created everywere
342 QTimer::singleShot(50, this, SLOT(activateLastPart()));
346 //******************************************************
347 void XGUI_Workshop::onObjectDeletedMsg(const Model_ObjectDeletedMessage* theMsg)
349 //std::set<ObjectPtr> aFeatures = theMsg->objects();
352 //******************************************************
353 void XGUI_Workshop::onOperationStarted()
355 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
357 if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
358 connectWithOperation(aOperation);
361 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
362 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
364 myPropertyPanel->cleanContent();
365 aFactory.createWidget(myPropertyPanel->contentWidget());
367 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
368 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
369 ModuleBase_ModelWidget* aWidget;
370 for (; anIt != aLast; anIt++) {
372 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
373 QObject::connect(aWidget, SIGNAL(valuesChanged()),
374 this, SLOT(onWidgetValuesChanged()));
375 // Init default values
376 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
377 aWidget->storeValue(aOperation->feature());
381 myPropertyPanel->setModelWidgets(aWidgets);
382 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
384 updateCommandStatus();
387 //******************************************************
388 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
390 //!< No need for property panel
391 updateCommandStatus();
393 myPropertyPanel->cleanContent();
399 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
403 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
407 // Remember features icons
408 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
410 //Find or create Workbench
411 QString aWchName = QString::fromStdString(theMessage->workbenchId());
412 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
413 bool isUsePropPanel = theMessage->isUseInput();
414 QString aId = QString::fromStdString(theMessage->id());
415 if (isSalomeMode()) {
416 QAction* aAction = salomeConnector()->addFeature(aWchName,
418 QString::fromStdString(theMessage->text()),
419 QString::fromStdString(theMessage->tooltip()),
420 QIcon(theMessage->icon().c_str()),
421 QKeySequence(), isUsePropPanel);
422 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
423 myActionsMgr->addCommand(aAction);
424 myModule->featureCreated(aAction);
427 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
428 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
430 aPage = addWorkbench(aWchName);
432 //Find or create Group
433 QString aGroupName = QString::fromStdString(theMessage->groupId());
434 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
436 aGroup = aPage->addGroup(aGroupName);
439 XGUI_Command* aCommand = aGroup->addFeature(aId,
440 QString::fromStdString(theMessage->text()),
441 QString::fromStdString(theMessage->tooltip()),
442 QIcon(theMessage->icon().c_str()),
443 QKeySequence(), isUsePropPanel);
444 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
445 myActionsMgr->addCommand(aCommand);
446 myModule->featureCreated(aCommand);
451 * Makes a signal/slot connections between Property Panel
452 * and given operation. The given operation becomes a
453 * current operation and previous operation if exists
455 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
457 QAction* aCommand = 0;
458 if (isSalomeMode()) {
459 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
461 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
462 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
464 //Abort operation on uncheck the command
465 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
469 * Saves document with given name.
471 void XGUI_Workshop::saveDocument(QString theName)
473 QApplication::restoreOverrideCursor();
474 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
475 DocumentPtr aDoc = aMgr->rootDocument();
476 aDoc->save(theName.toLatin1().constData());
477 QApplication::restoreOverrideCursor();
480 //******************************************************
481 void XGUI_Workshop::onExit()
483 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
484 DocumentPtr aDoc = aMgr->rootDocument();
485 if(aDoc->isModified()) {
486 int anAnswer = QMessageBox::question(
487 myMainWindow, tr("Save current file"),
488 tr("The document is modified, save before exit?"),
489 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
490 if(anAnswer == QMessageBox::Save) {
491 bool saved = onSave();
495 } else if (anAnswer == QMessageBox::Cancel) {
502 //******************************************************
503 void XGUI_Workshop::onNew()
505 QApplication::setOverrideCursor(Qt::WaitCursor);
506 if (objectBrowser() == 0) {
508 mySelector->connectViewers();
510 myViewerProxy->connectToViewer();
512 if (!isSalomeMode()) {
513 myMainWindow->showPythonConsole();
514 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
515 aWnd->showMaximized();
516 updateCommandStatus();
518 myContextMenuMgr->connectViewer();
519 QApplication::restoreOverrideCursor();
522 //******************************************************
523 void XGUI_Workshop::onOpen()
525 //save current file before close if modified
526 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
527 DocumentPtr aDoc = aMgr->rootDocument();
528 if(aDoc->isModified()) {
529 //TODO(sbh): re-launch the app?
530 int anAnswer = QMessageBox::question(
531 myMainWindow, tr("Save current file"),
532 tr("The document is modified, save before opening another?"),
533 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
534 if(anAnswer == QMessageBox::Save) {
536 } else if (anAnswer == QMessageBox::Cancel) {
543 //show file dialog, check if readable and open
544 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
545 if(myCurrentDir.isEmpty())
547 QFileInfo aFileInfo(myCurrentDir);
548 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
549 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
553 QApplication::setOverrideCursor(Qt::WaitCursor);
554 aDoc->load(myCurrentDir.toLatin1().constData());
555 updateCommandStatus();
556 myObjectBrowser->rebuildDataTree();
557 QApplication::restoreOverrideCursor();
560 //******************************************************
561 bool XGUI_Workshop::onSave()
563 if(myCurrentDir.isEmpty()) {
566 saveDocument(myCurrentDir);
567 updateCommandStatus();
571 //******************************************************
572 bool XGUI_Workshop::onSaveAs()
574 QFileDialog dialog(mainWindow());
575 dialog.setWindowTitle(tr("Select directory to save files..."));
576 dialog.setFileMode(QFileDialog::Directory);
577 dialog.setFilter(tr("Folders (*)"));
578 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
579 dialog.setViewMode(QFileDialog::Detail);
584 QString aTempDir = dialog.selectedFiles().first();
586 if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
587 int answer = QMessageBox::question(myMainWindow,
588 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
590 tr("The folder already contains some files, save anyway?"),
591 QMessageBox::Save|QMessageBox::Cancel);
592 if(answer == QMessageBox::Cancel) {
596 myCurrentDir = aTempDir;
600 //******************************************************
601 void XGUI_Workshop::onUndo()
603 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
604 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
605 DocumentPtr aDoc = aMgr->rootDocument();
606 if (aDoc->isOperation())
607 operationMgr()->abortOperation();
609 updateCommandStatus();
612 //******************************************************
613 void XGUI_Workshop::onRedo()
615 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
616 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
617 DocumentPtr aDoc = aMgr->rootDocument();
618 if (aDoc->isOperation())
619 operationMgr()->abortOperation();
621 updateCommandStatus();
624 //******************************************************
625 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
628 QString::fromStdString(library(theModule.toStdString()));
629 if (libName.isEmpty()) {
631 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
636 CREATE_FUNC crtInst = 0;
639 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
643 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
644 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
645 QString aMsg((char*) &lpMsgBuf);
646 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
647 ::LocalFree(lpMsgBuf);
649 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
653 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
654 | FORMAT_MESSAGE_IGNORE_INSERTS,
655 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
656 QString aMsg((char*) &lpMsgBuf);
657 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
658 ::LocalFree(lpMsgBuf);
662 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
664 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
666 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
668 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
673 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
675 if (!err.isEmpty()) {
677 QMessageBox::warning(mainWindow(), tr("Error"), err);
679 qWarning( qPrintable( err ));
685 //******************************************************
686 bool XGUI_Workshop::activateModule()
688 Config_ModuleReader aModuleReader;
689 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
690 myModule = loadModule(moduleName);
693 myModule->createFeatures();
694 myActionsMgr->update();
698 //******************************************************
699 void XGUI_Workshop::updateCommandStatus()
701 QList<QAction*> aCommands;
702 if (isSalomeMode()) { // update commands in SALOME mode
703 aCommands = salomeConnector()->commandList();
705 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
706 foreach (XGUI_Command* aCmd, aMenuBar->features())
707 aCommands.append(aCmd);
709 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
710 if (aMgr->hasRootDocument()) {
713 foreach(QAction* aCmd, aCommands) {
714 QString aId = aCmd->data().toString();
715 if (aId == "UNDO_CMD")
717 else if (aId == "REDO_CMD")
719 else // Enable all commands
720 aCmd->setEnabled(true);
722 DocumentPtr aDoc = aMgr->rootDocument();
723 aUndoCmd->setEnabled(aDoc->canUndo());
724 aRedoCmd->setEnabled(aDoc->canRedo());
726 foreach(QAction* aCmd, aCommands) {
727 QString aId = aCmd->data().toString();
728 if (aId == "NEW_CMD")
729 aCmd->setEnabled(true);
730 else if (aId == "EXIT_CMD")
731 aCmd->setEnabled(true);
733 aCmd->setEnabled(false);
736 myActionsMgr->update();
739 //******************************************************
740 QList<QAction*> XGUI_Workshop::getModuleCommands() const
742 QList<QAction*> aCommands;
743 if (isSalomeMode()) { // update commands in SALOME mode
744 aCommands = salomeConnector()->commandList();
746 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
747 foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
748 if (aWb != aMenuBar->generalPage()) {
749 foreach(XGUI_Command* aCmd, aWb->features())
750 aCommands.append(aCmd);
757 //******************************************************
758 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
760 QDockWidget* aObjDock = new QDockWidget(theParent);
761 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
762 aObjDock->setWindowTitle(tr("Object browser"));
763 aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
764 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
765 connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
766 aObjDock->setWidget(myObjectBrowser);
768 myContextMenuMgr->connectObjectBrowser();
772 //******************************************************
774 * Creates dock widgets, places them in corresponding area
775 * and tabifies if necessary.
777 void XGUI_Workshop::createDockWidgets()
779 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
781 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
782 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
783 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
784 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
785 hidePropertyPanel(); //<! Invisible by default
787 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
789 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
790 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
791 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
792 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
794 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
795 myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
797 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
798 myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
799 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
800 myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
803 //******************************************************
804 void XGUI_Workshop::showPropertyPanel()
806 QAction* aViewAct = myPropertyPanel->toggleViewAction();
807 //<! Restore ability to close panel from the window's menu
808 aViewAct->setEnabled(true);
809 myPropertyPanel->show();
810 myPropertyPanel->raise();
813 //******************************************************
814 void XGUI_Workshop::hidePropertyPanel()
816 QAction* aViewAct = myPropertyPanel->toggleViewAction();
817 //<! Do not allow to show empty property panel
818 aViewAct->setEnabled(false);
819 myPropertyPanel->hide();
822 //******************************************************
823 void XGUI_Workshop::showObjectBrowser()
825 myObjectBrowser->parentWidget()->show();
828 //******************************************************
829 void XGUI_Workshop::hideObjectBrowser()
831 myObjectBrowser->parentWidget()->hide();
834 //******************************************************
835 void XGUI_Workshop::onFeatureTriggered()
837 QAction* aCmd = dynamic_cast<QAction*>(sender());
839 QString aId = salomeConnector()->commandId(aCmd);
841 myModule->launchOperation(aId);
845 //******************************************************
846 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
848 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
850 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
852 DocumentPtr aPartDoc = aPart->partDoc();
854 aMgr->setCurrentDocument(aPartDoc);
859 aMgr->setCurrentDocument(aMgr->rootDocument());
862 //******************************************************
863 void XGUI_Workshop::salomeViewerSelectionChanged()
865 emit salomeViewerSelection();
869 //**************************************************************
870 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
872 return mySalomeConnector->viewer();
875 //**************************************************************
876 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
878 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
879 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
880 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
882 } else if (theId == "DEACTIVATE_PART_CMD")
883 activatePart(ResultPartPtr());
884 else if (theId == "DELETE_CMD")
885 deleteObjects(aObjects);
886 else if (theId == "SHOW_CMD")
887 showObjects(aObjects, true);
888 else if (theId == "HIDE_CMD")
889 showObjects(aObjects, false);
892 //**************************************************************
893 void XGUI_Workshop::onWidgetValuesChanged()
895 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
896 FeaturePtr aFeature = anOperation->feature();
898 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
900 // aCustom->storeValue(aFeature);
902 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
903 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
904 for (; anIt != aLast; anIt++) {
905 ModuleBase_ModelWidget* aCustom = *anIt;
906 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
907 aCustom->storeValue(aFeature);
912 //**************************************************************
913 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
915 changeCurrentDocument(theFeature);
916 myObjectBrowser->activatePart(theFeature);
919 //**************************************************************
920 void XGUI_Workshop::activateLastPart()
922 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
923 DocumentPtr aDoc = aMgr->rootDocument();
924 std::string aGrpName = ModelAPI_ResultPart::group();
925 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
926 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
931 //**************************************************************
932 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
934 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
935 QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"),
936 tr("Seleted features will be deleted. Continue?"),
937 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
938 // ToDo: definbe deleting method
939 if (aRes == QMessageBox::Yes) {
940 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
941 aMgr->rootDocument()->startOperation();
942 foreach (ObjectPtr aObj, theList) {
943 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
945 DocumentPtr aDoc = aPart->document();
946 if (aDoc == aMgr->currentDocument()) {
949 //aMgr->rootDocument()->removeFeature(aPart->owner());
951 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
953 aObj->document()->removeFeature(aFeature);
956 myDisplayer->updateViewer();
957 aMgr->rootDocument()->finishOperation();
961 //**************************************************************
962 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
964 foreach (ObjectPtr aObj, theList) {
965 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
968 myDisplayer->display(aRes, false);
970 myDisplayer->erase(aRes, false);
974 myDisplayer->updateViewer();
977 //**************************************************************
978 void XGUI_Workshop::updateCommandsOnViewSelection()
980 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
981 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
982 XGUI_Selection* aSelection = mySelector->selection();
984 QList<QAction*> aActions = getModuleCommands();
985 foreach(QAction* aAction, aActions) {
986 QString aId = aAction->data().toString();
987 const ModelAPI_Validator* aValidator = aFactory->validator(aId.toStdString());
989 const ModuleBase_SelectionValidator* aSelValidator =
990 dynamic_cast<const ModuleBase_SelectionValidator*>(aValidator);
992 aAction->setEnabled(aSelValidator->isValid(aSelection));