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_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>
44 #include <ModuleBase_ResultValidators.h>
46 #include <Config_Common.h>
47 #include <Config_FeatureMessage.h>
48 #include <Config_PointerMessage.h>
49 #include <Config_ModuleReader.h>
51 #include <QApplication>
52 #include <QFileDialog>
53 #include <QMessageBox>
54 #include <QMdiSubWindow>
55 #include <QPushButton>
56 #include <QDockWidget>
71 QMap<QString, QString> XGUI_Workshop::myIcons;
73 QString XGUI_Workshop::featureIcon(const std::string& theId)
75 QString aId(theId.c_str());
76 if (myIcons.contains(aId))
81 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
83 myCurrentDir(QString()),
85 mySalomeConnector(theConnector),
90 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
92 myDisplayer = new XGUI_Displayer(this);
94 mySelector = new XGUI_SelectionMgr(this);
95 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
97 myOperationMgr = new XGUI_OperationMgr(this);
98 myActionsMgr = new XGUI_ActionsMgr(this);
99 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
100 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
101 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
102 this, SLOT(onContextMenuCommand(const QString&, bool)));
104 myViewerProxy = new XGUI_ViewerProxy(this);
105 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
107 myModuleConnector = new XGUI_ModuleConnector(this);
109 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
110 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
111 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
112 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
113 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
114 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
115 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
118 //******************************************************
119 XGUI_Workshop::~XGUI_Workshop(void)
123 //******************************************************
124 void XGUI_Workshop::startApplication()
127 //Initialize event listening
128 Events_Loop* aLoop = Events_Loop::loop();
129 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
130 //TODO(sbh): Implement static method to extract event id [SEID]
131 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
132 // TODO Is it good to use non standard event within workshop?
133 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
134 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
136 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
137 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
139 registerValidators();
142 myMainWindow->show();
143 updateCommandStatus();
148 //******************************************************
149 void XGUI_Workshop::initMenu()
151 myContextMenuMgr->createActions();
153 if (isSalomeMode()) {
154 // Create only Undo, Redo commands
155 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD",
156 tr("Undo"), tr("Undo last command"),
157 QIcon(":pictures/undo.png"),
158 QKeySequence::Undo, false);
159 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
160 aAction = salomeConnector()->addEditCommand("REDO_CMD",
161 tr("Redo"), tr("Redo last command"),
162 QIcon(":pictures/redo.png"),
163 QKeySequence::Redo, false);
164 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
165 salomeConnector()->addEditMenuSeparator();
168 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
170 // File commands group
171 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
173 XGUI_Command* aCommand;
175 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
176 QIcon(":pictures/save.png"), QKeySequence::Save);
177 aCommand->connectTo(this, SLOT(onSave()));
178 //aCommand->disable();
180 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
181 QIcon(":pictures/undo.png"), QKeySequence::Undo);
182 aCommand->connectTo(this, SLOT(onUndo()));
184 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
185 QIcon(":pictures/redo.png"), QKeySequence::Redo);
186 aCommand->connectTo(this, SLOT(onRedo()));
188 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
189 QIcon(":pictures/rebuild.png"));
191 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
192 QIcon(":pictures/save.png"));
193 aCommand->connectTo(this, SLOT(onSaveAs()));
194 //aCommand->disable();
196 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
197 QIcon(":pictures/open.png"), QKeySequence::Open);
198 aCommand->connectTo(this, SLOT(onOpen()));
200 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
201 // QIcon(":pictures/new.png"), QKeySequence::New);
202 //aCommand->connectTo(this, SLOT(onNew()));
204 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
205 QIcon(":pictures/close.png"), QKeySequence::Close);
206 aCommand->connectTo(this, SLOT(onExit()));
207 //FIXME: SBH's test action. Can be used for some GUI tests.
209 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
210 // QIcon(":pictures/close.png"));
211 // aCommand->connectTo(myActionsMgr, SLOT(update()));
215 //******************************************************
216 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
218 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
219 return aMenuBar->addWorkbench(theName);
222 //******************************************************
223 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
225 //A message to start feature creation received.
226 static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
227 if (theMessage->eventID() == aFeatureLoadedId) {
228 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
229 if(!aFeatureMsg->isInternal()) {
230 addFeature(aFeatureMsg);
235 // Process creation of Part
236 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
237 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
238 onFeatureCreatedMsg(aUpdMsg);
243 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
244 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
245 onFeatureRedisplayMsg(aUpdMsg);
249 //Update property panel on corresponding message. If there is no current operation (no
250 //property panel), or received message has different feature to the current - do nothing.
251 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
252 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
253 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
254 onFeatureUpdatedMsg(anUpdateMsg);
258 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
259 const ModelAPI_ObjectDeletedMessage* aDelMsg =
260 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
261 onObjectDeletedMsg(aDelMsg);
265 //An operation passed by message. Start it, process and commit.
266 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
267 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
268 //myPropertyPanel->cleanContent();
269 ModuleBase_Operation* anOperation = (ModuleBase_Operation*)aPartSetMsg->pointer();
271 if (myOperationMgr->startOperation(anOperation)) {
272 myPropertyPanel->updateContentWidget(anOperation->feature());
273 if (!anOperation->getDescription()->hasXmlRepresentation()) {
274 anOperation->commit();
275 updateCommandStatus();
280 //Show error dialog if error message received.
281 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
283 emit errorOccurred(QString::fromLatin1(anAppError->description()));
287 //******************************************************
288 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
290 std::set<ObjectPtr> aFeatures = theMsg->objects();
291 if (myOperationMgr->hasOperation())
293 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
294 std::set<ObjectPtr>::const_iterator aIt;
295 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
296 ObjectPtr aNewFeature = (*aIt);
297 if(aNewFeature == aCurrentFeature) {
298 myPropertyPanel->updateContentWidget(aCurrentFeature);
305 //******************************************************
306 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
308 std::set<ObjectPtr> aObjects = theMsg->objects();
309 std::set<ObjectPtr>::const_iterator aIt;
310 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
311 ObjectPtr aObj = (*aIt);
313 myDisplayer->erase(aObj, false);
315 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
316 myDisplayer->display(aObj, false); // In order to update presentation
319 myDisplayer->updateViewer();
322 //******************************************************
323 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
325 std::set<ObjectPtr> aObjects = theMsg->objects();
327 std::set<ObjectPtr>::const_iterator aIt;
328 bool aHasPart = false;
329 bool isDisplayed = false;
330 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
331 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
334 // If a feature is created from the aplication's python console
335 // it doesn't stored in the operation mgr and doesn't displayed
336 } else if(myOperationMgr->hasOperation()) {
337 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
338 if (aOperation->hasObject(*aIt)) { // Display only current operation results
339 myDisplayer->display(*aIt, false);
345 myDisplayer->updateViewer();
347 //The created part will be created in Object Browser later and we have to activate it
348 // only when it is created everywere
349 QTimer::singleShot(50, this, SLOT(activateLastPart()));
353 //******************************************************
354 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
356 //std::set<ObjectPtr> aFeatures = theMsg->objects();
359 //******************************************************
360 void XGUI_Workshop::onOperationStarted()
362 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
364 if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
365 connectWithOperation(aOperation);
368 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
369 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
371 myPropertyPanel->cleanContent();
372 aFactory.createWidget(myPropertyPanel->contentWidget());
374 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
375 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
376 ModuleBase_ModelWidget* aWidget;
377 for (; anIt != aLast; anIt++) {
379 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
380 QObject::connect(aWidget, SIGNAL(valuesChanged()),
381 this, SLOT(onWidgetValuesChanged()));
382 // Init default values
383 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
384 aWidget->storeValue(aOperation->feature());
388 myPropertyPanel->setModelWidgets(aWidgets);
389 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
391 updateCommandStatus();
394 //******************************************************
395 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
397 //!< No need for property panel
398 updateCommandStatus();
400 myPropertyPanel->cleanContent();
406 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
410 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
414 // Remember features icons
415 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
417 //Find or create Workbench
418 QString aWchName = QString::fromStdString(theMessage->workbenchId());
419 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
420 bool isUsePropPanel = theMessage->isUseInput();
421 QString aId = QString::fromStdString(theMessage->id());
422 if (isSalomeMode()) {
423 QAction* aAction = salomeConnector()->addFeature(aWchName,
425 QString::fromStdString(theMessage->text()),
426 QString::fromStdString(theMessage->tooltip()),
427 QIcon(theMessage->icon().c_str()),
428 QKeySequence(), isUsePropPanel);
429 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
430 myActionsMgr->addCommand(aAction);
431 myModule->featureCreated(aAction);
434 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
435 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
437 aPage = addWorkbench(aWchName);
439 //Find or create Group
440 QString aGroupName = QString::fromStdString(theMessage->groupId());
441 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
443 aGroup = aPage->addGroup(aGroupName);
446 XGUI_Command* aCommand = aGroup->addFeature(aId,
447 QString::fromStdString(theMessage->text()),
448 QString::fromStdString(theMessage->tooltip()),
449 QIcon(theMessage->icon().c_str()),
450 QKeySequence(), isUsePropPanel);
451 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
452 myActionsMgr->addCommand(aCommand);
453 myModule->featureCreated(aCommand);
458 * Makes a signal/slot connections between Property Panel
459 * and given operation. The given operation becomes a
460 * current operation and previous operation if exists
462 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
464 QAction* aCommand = 0;
465 if (isSalomeMode()) {
466 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
468 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
469 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
471 //Abort operation on uncheck the command
473 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
477 * Saves document with given name.
479 void XGUI_Workshop::saveDocument(QString theName)
481 QApplication::restoreOverrideCursor();
482 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
483 DocumentPtr aDoc = aMgr->rootDocument();
484 aDoc->save(theName.toLatin1().constData());
485 QApplication::restoreOverrideCursor();
488 //******************************************************
489 void XGUI_Workshop::onExit()
491 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
492 DocumentPtr aDoc = aMgr->rootDocument();
493 if(aDoc->isModified()) {
494 int anAnswer = QMessageBox::question(
495 myMainWindow, tr("Save current file"),
496 tr("The document is modified, save before exit?"),
497 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
498 if(anAnswer == QMessageBox::Save) {
499 bool saved = onSave();
503 } else if (anAnswer == QMessageBox::Cancel) {
510 //******************************************************
511 void XGUI_Workshop::onNew()
513 QApplication::setOverrideCursor(Qt::WaitCursor);
514 if (objectBrowser() == 0) {
516 mySelector->connectViewers();
518 myViewerProxy->connectToViewer();
520 if (!isSalomeMode()) {
521 myMainWindow->showPythonConsole();
522 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
523 aWnd->showMaximized();
524 updateCommandStatus();
526 myContextMenuMgr->connectViewer();
527 QApplication::restoreOverrideCursor();
530 //******************************************************
531 void XGUI_Workshop::onOpen()
533 //save current file before close if modified
534 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
535 DocumentPtr aDoc = aMgr->rootDocument();
536 if(aDoc->isModified()) {
537 //TODO(sbh): re-launch the app?
538 int anAnswer = QMessageBox::question(
539 myMainWindow, tr("Save current file"),
540 tr("The document is modified, save before opening another?"),
541 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
542 if(anAnswer == QMessageBox::Save) {
544 } else if (anAnswer == QMessageBox::Cancel) {
551 //show file dialog, check if readable and open
552 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
553 if(myCurrentDir.isEmpty())
555 QFileInfo aFileInfo(myCurrentDir);
556 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
557 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
561 QApplication::setOverrideCursor(Qt::WaitCursor);
562 aDoc->load(myCurrentDir.toLatin1().constData());
563 updateCommandStatus();
564 myObjectBrowser->rebuildDataTree();
565 QApplication::restoreOverrideCursor();
568 //******************************************************
569 bool XGUI_Workshop::onSave()
571 if(myCurrentDir.isEmpty()) {
574 saveDocument(myCurrentDir);
575 updateCommandStatus();
579 //******************************************************
580 bool XGUI_Workshop::onSaveAs()
582 QFileDialog dialog(mainWindow());
583 dialog.setWindowTitle(tr("Select directory to save files..."));
584 dialog.setFileMode(QFileDialog::Directory);
585 dialog.setFilter(tr("Folders (*)"));
586 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
587 dialog.setViewMode(QFileDialog::Detail);
592 QString aTempDir = dialog.selectedFiles().first();
594 if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
595 int answer = QMessageBox::question(myMainWindow,
596 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
598 tr("The folder already contains some files, save anyway?"),
599 QMessageBox::Save|QMessageBox::Cancel);
600 if(answer == QMessageBox::Cancel) {
604 myCurrentDir = aTempDir;
608 //******************************************************
609 void XGUI_Workshop::onUndo()
611 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
612 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
613 DocumentPtr aDoc = aMgr->rootDocument();
614 if (aDoc->isOperation())
615 operationMgr()->abortOperation();
617 updateCommandStatus();
620 //******************************************************
621 void XGUI_Workshop::onRedo()
623 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
624 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
625 DocumentPtr aDoc = aMgr->rootDocument();
626 if (aDoc->isOperation())
627 operationMgr()->abortOperation();
629 updateCommandStatus();
632 //******************************************************
633 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
636 QString::fromStdString(library(theModule.toStdString()));
637 if (libName.isEmpty()) {
639 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
644 CREATE_FUNC crtInst = 0;
647 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
651 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
652 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
653 QString aMsg((char*) &lpMsgBuf);
654 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
655 ::LocalFree(lpMsgBuf);
657 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
661 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
662 | FORMAT_MESSAGE_IGNORE_INSERTS,
663 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
664 QString aMsg((char*) &lpMsgBuf);
665 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
666 ::LocalFree(lpMsgBuf);
670 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
672 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
674 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
676 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
681 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
683 if (!err.isEmpty()) {
685 QMessageBox::warning(mainWindow(), tr("Error"), err);
687 qWarning( qPrintable( err ));
693 //******************************************************
694 bool XGUI_Workshop::activateModule()
696 Config_ModuleReader aModuleReader;
697 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
698 myModule = loadModule(moduleName);
701 myModule->createFeatures();
702 myActionsMgr->update();
706 //******************************************************
707 void XGUI_Workshop::updateCommandStatus()
709 QList<QAction*> aCommands;
710 if (isSalomeMode()) { // update commands in SALOME mode
711 aCommands = salomeConnector()->commandList();
713 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
714 foreach (XGUI_Command* aCmd, aMenuBar->features())
715 aCommands.append(aCmd);
717 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
718 if (aMgr->hasRootDocument()) {
721 foreach(QAction* aCmd, aCommands) {
722 QString aId = aCmd->data().toString();
723 if (aId == "UNDO_CMD")
725 else if (aId == "REDO_CMD")
727 else // Enable all commands
728 aCmd->setEnabled(true);
730 DocumentPtr aDoc = aMgr->rootDocument();
731 aUndoCmd->setEnabled(aDoc->canUndo());
732 aRedoCmd->setEnabled(aDoc->canRedo());
734 foreach(QAction* aCmd, aCommands) {
735 QString aId = aCmd->data().toString();
736 if (aId == "NEW_CMD")
737 aCmd->setEnabled(true);
738 else if (aId == "EXIT_CMD")
739 aCmd->setEnabled(true);
741 aCmd->setEnabled(false);
744 myActionsMgr->update();
747 //******************************************************
748 QList<QAction*> XGUI_Workshop::getModuleCommands() const
750 QList<QAction*> aCommands;
751 if (isSalomeMode()) { // update commands in SALOME mode
752 aCommands = salomeConnector()->commandList();
754 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
755 foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
756 if (aWb != aMenuBar->generalPage()) {
757 foreach(XGUI_Command* aCmd, aWb->features())
758 aCommands.append(aCmd);
765 //******************************************************
766 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
768 QDockWidget* aObjDock = new QDockWidget(theParent);
769 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
770 aObjDock->setWindowTitle(tr("Object browser"));
771 aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
772 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
773 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(changeCurrentDocument(ObjectPtr)));
774 aObjDock->setWidget(myObjectBrowser);
776 myContextMenuMgr->connectObjectBrowser();
780 //******************************************************
782 * Creates dock widgets, places them in corresponding area
783 * and tabifies if necessary.
785 void XGUI_Workshop::createDockWidgets()
787 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
789 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
790 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
791 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
792 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
793 hidePropertyPanel(); //<! Invisible by default
795 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
797 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
798 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
799 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
800 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
802 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
803 myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
805 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
806 myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
807 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
808 myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
811 //******************************************************
812 void XGUI_Workshop::showPropertyPanel()
814 QAction* aViewAct = myPropertyPanel->toggleViewAction();
815 //<! Restore ability to close panel from the window's menu
816 aViewAct->setEnabled(true);
817 myPropertyPanel->show();
818 myPropertyPanel->raise();
821 //******************************************************
822 void XGUI_Workshop::hidePropertyPanel()
824 QAction* aViewAct = myPropertyPanel->toggleViewAction();
825 //<! Do not allow to show empty property panel
826 aViewAct->setEnabled(false);
827 myPropertyPanel->hide();
830 //******************************************************
831 void XGUI_Workshop::showObjectBrowser()
833 myObjectBrowser->parentWidget()->show();
836 //******************************************************
837 void XGUI_Workshop::hideObjectBrowser()
839 myObjectBrowser->parentWidget()->hide();
842 //******************************************************
843 void XGUI_Workshop::onFeatureTriggered()
845 QAction* aCmd = dynamic_cast<QAction*>(sender());
847 QString aId = salomeConnector()->commandId(aCmd);
849 myModule->launchOperation(aId);
853 //******************************************************
854 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
856 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
858 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
860 DocumentPtr aPartDoc = aPart->partDoc();
862 aMgr->setCurrentDocument(aPartDoc);
867 aMgr->setCurrentDocument(aMgr->rootDocument());
870 //******************************************************
871 void XGUI_Workshop::salomeViewerSelectionChanged()
873 emit salomeViewerSelection();
877 //**************************************************************
878 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
880 return mySalomeConnector->viewer();
883 //**************************************************************
884 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
886 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
887 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
888 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
890 } else if (theId == "DEACTIVATE_PART_CMD")
891 activatePart(ResultPartPtr());
892 else if (theId == "DELETE_CMD")
893 deleteObjects(aObjects);
894 else if (theId == "SHOW_CMD")
895 showObjects(aObjects, true);
896 else if (theId == "HIDE_CMD")
897 showObjects(aObjects, false);
900 //**************************************************************
901 void XGUI_Workshop::onWidgetValuesChanged()
903 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
904 FeaturePtr aFeature = anOperation->feature();
906 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
908 // aCustom->storeValue(aFeature);
910 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
911 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
912 for (; anIt != aLast; anIt++) {
913 ModuleBase_ModelWidget* aCustom = *anIt;
914 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
915 aCustom->storeValue(aFeature);
920 //**************************************************************
921 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
923 changeCurrentDocument(theFeature);
924 myObjectBrowser->activatePart(theFeature);
927 //**************************************************************
928 void XGUI_Workshop::activateLastPart()
930 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
931 DocumentPtr aDoc = aMgr->rootDocument();
932 std::string aGrpName = ModelAPI_ResultPart::group();
933 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
934 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
939 //**************************************************************
940 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
942 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
943 QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"),
944 tr("Seleted features will be deleted. Continue?"),
945 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
946 // ToDo: definbe deleting method
947 if (aRes == QMessageBox::Yes) {
948 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
949 aMgr->rootDocument()->startOperation();
950 foreach (ObjectPtr aObj, theList) {
951 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
953 DocumentPtr aDoc = aPart->document();
954 if (aDoc == aMgr->currentDocument()) {
957 //aMgr->rootDocument()->removeFeature(aPart->owner());
959 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
961 aObj->document()->removeFeature(aFeature);
964 myDisplayer->updateViewer();
965 aMgr->rootDocument()->finishOperation();
969 //**************************************************************
970 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
972 foreach (ObjectPtr aObj, theList) {
973 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
976 myDisplayer->display(aRes, false);
978 myDisplayer->erase(aRes, false);
982 myDisplayer->updateViewer();
985 //**************************************************************
986 void XGUI_Workshop::updateCommandsOnViewSelection()
988 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
989 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
990 XGUI_Selection* aSelection = mySelector->selection();
992 QList<QAction*> aActions = getModuleCommands();
993 foreach(QAction* aAction, aActions) {
994 QString aId = aAction->data().toString();
995 const ModelAPI_Validator* aValidator = aFactory->validator(aId.toStdString());
997 const ModuleBase_SelectionValidator* aSelValidator =
998 dynamic_cast<const ModuleBase_SelectionValidator*>(aValidator);
1000 aAction->setEnabled(aSelValidator->isValid(aSelection));
1007 //**************************************************************
1008 void XGUI_Workshop::registerValidators() const
1010 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1011 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1013 aFactory->registerValidator("ModuleBase_ResulPointValidator", new ModuleBase_ResulPointValidator);
1014 aFactory->registerValidator("ModuleBase_ResulLineValidator", new ModuleBase_ResulLineValidator);
1015 aFactory->registerValidator("ModuleBase_ResulArcValidator", new ModuleBase_ResulArcValidator);