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>
34 #include <PartSetPlugin_Part.h>
36 #include <Events_Loop.h>
37 #include <Events_Error.h>
39 #include <ModuleBase_Operation.h>
40 #include <ModuleBase_Operation.h>
41 #include <ModuleBase_OperationDescription.h>
42 #include <ModuleBase_SelectionValidator.h>
44 #include <Config_Common.h>
45 #include <Config_FeatureMessage.h>
46 #include <Config_PointerMessage.h>
47 #include <Config_ModuleReader.h>
49 #include <QApplication>
50 #include <QFileDialog>
51 #include <QMessageBox>
52 #include <QMdiSubWindow>
53 #include <QPushButton>
54 #include <QDockWidget>
69 QMap<QString, QString> XGUI_Workshop::myIcons;
71 QString XGUI_Workshop::featureIcon(const std::string& theId)
73 QString aId(theId.c_str());
74 if (myIcons.contains(aId))
79 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
81 myCurrentDir(QString()),
83 mySalomeConnector(theConnector),
88 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
90 myDisplayer = new XGUI_Displayer(this);
92 mySelector = new XGUI_SelectionMgr(this);
93 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
95 myOperationMgr = new XGUI_OperationMgr(this);
96 myActionsMgr = new XGUI_ActionsMgr(this);
97 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
98 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
99 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
100 this, SLOT(onContextMenuCommand(const QString&, bool)));
102 myViewerProxy = new XGUI_ViewerProxy(this);
103 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
105 myModuleConnector = new XGUI_ModuleConnector(this);
107 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
108 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
109 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
110 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
111 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
112 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
113 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
116 //******************************************************
117 XGUI_Workshop::~XGUI_Workshop(void)
121 //******************************************************
122 void XGUI_Workshop::startApplication()
125 //Initialize event listening
126 Events_Loop* aLoop = Events_Loop::loop();
127 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
128 //TODO(sbh): Implement static method to extract event id [SEID]
129 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
130 // TODO Is it good to use non standard event within workshop?
131 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
132 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
133 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
134 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_TO_REDISPLAY));
138 myMainWindow->show();
139 updateCommandStatus();
144 //******************************************************
145 void XGUI_Workshop::initMenu()
147 myContextMenuMgr->createActions();
149 if (isSalomeMode()) {
150 // Create only Undo, Redo commands
151 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD",
152 tr("Undo"), tr("Undo last command"),
153 QIcon(":pictures/undo.png"),
154 QKeySequence::Undo, false);
155 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
156 aAction = salomeConnector()->addEditCommand("REDO_CMD",
157 tr("Redo"), tr("Redo last command"),
158 QIcon(":pictures/redo.png"),
159 QKeySequence::Redo, false);
160 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
161 salomeConnector()->addEditMenuSeparator();
164 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
166 // File commands group
167 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
169 XGUI_Command* aCommand;
171 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
172 QIcon(":pictures/save.png"), QKeySequence::Save);
173 aCommand->connectTo(this, SLOT(onSave()));
174 //aCommand->disable();
176 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
177 QIcon(":pictures/undo.png"), QKeySequence::Undo);
178 aCommand->connectTo(this, SLOT(onUndo()));
180 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
181 QIcon(":pictures/redo.png"), QKeySequence::Redo);
182 aCommand->connectTo(this, SLOT(onRedo()));
184 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
185 QIcon(":pictures/rebuild.png"));
187 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
188 QIcon(":pictures/save.png"));
189 aCommand->connectTo(this, SLOT(onSaveAs()));
190 //aCommand->disable();
192 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
193 QIcon(":pictures/open.png"), QKeySequence::Open);
194 aCommand->connectTo(this, SLOT(onOpen()));
196 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
197 // QIcon(":pictures/new.png"), QKeySequence::New);
198 //aCommand->connectTo(this, SLOT(onNew()));
200 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
201 QIcon(":pictures/close.png"), QKeySequence::Close);
202 aCommand->connectTo(this, SLOT(onExit()));
203 //FIXME: SBH's test action. Can be used for some GUI tests.
205 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
206 // QIcon(":pictures/close.png"));
207 // aCommand->connectTo(myActionsMgr, SLOT(update()));
211 //******************************************************
212 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
214 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
215 return aMenuBar->addWorkbench(theName);
218 //******************************************************
219 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
221 //A message to start feature creation received.
222 static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
223 if (theMessage->eventID() == aFeatureLoadedId) {
224 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
225 if(!aFeatureMsg->isInternal()) {
226 addFeature(aFeatureMsg);
231 // Process creation of Part
232 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
233 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
234 onFeatureCreatedMsg(aUpdMsg);
238 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_TO_REDISPLAY)) {
239 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
240 onFeatureRedisplayMsg(aUpdMsg);
243 //Update property panel on corresponding message. If there is no current operation (no
244 //property panel), or received message has different feature to the current - do nothing.
245 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
246 const Model_FeatureUpdatedMessage* anUpdateMsg =
247 dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
248 onFeatureUpdatedMsg(anUpdateMsg);
251 //An operation passed by message. Start it, process and commit.
252 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
253 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
254 //myPropertyPanel->cleanContent();
255 ModuleBase_Operation* anOperation = (ModuleBase_Operation*)aPartSetMsg->pointer();
257 if (myOperationMgr->startOperation(anOperation)) {
258 myPropertyPanel->updateContentWidget(anOperation->feature());
259 if (!anOperation->getDescription()->hasXmlRepresentation()) {
260 anOperation->commit();
261 updateCommandStatus();
266 //Show error dialog if error message received.
267 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
269 emit errorOccurred(QString::fromLatin1(anAppError->description()));
273 //******************************************************
274 void XGUI_Workshop::onFeatureUpdatedMsg(const Model_FeatureUpdatedMessage* theMsg)
276 std::set<FeaturePtr> aFeatures = theMsg->features();
277 if (myOperationMgr->hasOperation())
279 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
280 std::set<FeaturePtr>::const_iterator aIt;
281 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
282 FeaturePtr aNewFeature = (*aIt);
283 if(aNewFeature == aCurrentFeature) {
284 myPropertyPanel->updateContentWidget(aCurrentFeature);
291 //******************************************************
292 void XGUI_Workshop::onFeatureRedisplayMsg(const Model_FeatureUpdatedMessage* theMsg)
294 std::set<FeaturePtr> aFeatures = theMsg->features();
295 std::set<FeaturePtr>::const_iterator aIt;
296 bool isDisplayed = false;
297 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
298 FeaturePtr aFeature = (*aIt);
299 if (aFeature->getKind() != PARTSET_PART_KIND) {
300 isDisplayed = myDisplayer->redisplay(aFeature, false);
304 myDisplayer->updateViewer();
307 //******************************************************
308 void XGUI_Workshop::onFeatureCreatedMsg(const Model_FeatureUpdatedMessage* theMsg)
310 std::set<FeaturePtr> aFeatures = theMsg->features();
312 std::set<FeaturePtr>::const_iterator aIt;
313 bool aHasPart = false;
314 bool isDisplayed = false;
315 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
316 FeaturePtr aFeature = (*aIt);
317 if (aFeature->getKind() == PARTSET_PART_KIND) {
321 isDisplayed = myDisplayer->display(aFeature, false);
325 myDisplayer->updateViewer();
327 //The created part will be created in Object Browser later and we have to activate it
328 // only when it is created everywere
329 QTimer::singleShot(50, this, SLOT(activateLastPart()));
333 //******************************************************
334 void XGUI_Workshop::onOperationStarted()
336 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
338 if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
339 connectWithOperation(aOperation);
342 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
343 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
345 myPropertyPanel->cleanContent();
346 aFactory.createWidget(myPropertyPanel->contentWidget());
348 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
349 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
350 ModuleBase_ModelWidget* aWidget;
351 for (; anIt != aLast; anIt++) {
353 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
354 QObject::connect(aWidget, SIGNAL(valuesChanged()),
355 this, SLOT(onWidgetValuesChanged()));
356 // Init default values
357 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
358 aWidget->storeValue(aOperation->feature());
362 myPropertyPanel->setModelWidgets(aWidgets);
363 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
365 updateCommandStatus();
368 //******************************************************
369 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
371 //!< No need for property panel
372 updateCommandStatus();
374 myPropertyPanel->cleanContent();
380 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
384 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
388 // Remember features icons
389 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
391 //Find or create Workbench
392 QString aWchName = QString::fromStdString(theMessage->workbenchId());
393 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
394 bool isUsePropPanel = theMessage->isUseInput();
395 QString aId = QString::fromStdString(theMessage->id());
396 if (isSalomeMode()) {
397 QAction* aAction = salomeConnector()->addFeature(aWchName,
399 QString::fromStdString(theMessage->text()),
400 QString::fromStdString(theMessage->tooltip()),
401 QIcon(theMessage->icon().c_str()),
402 QKeySequence(), isUsePropPanel);
403 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
404 myActionsMgr->addCommand(aAction);
405 myModule->featureCreated(aAction);
408 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
409 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
411 aPage = addWorkbench(aWchName);
413 //Find or create Group
414 QString aGroupName = QString::fromStdString(theMessage->groupId());
415 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
417 aGroup = aPage->addGroup(aGroupName);
420 XGUI_Command* aCommand = aGroup->addFeature(aId,
421 QString::fromStdString(theMessage->text()),
422 QString::fromStdString(theMessage->tooltip()),
423 QIcon(theMessage->icon().c_str()),
424 QKeySequence(), isUsePropPanel);
425 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
426 myActionsMgr->addCommand(aCommand);
427 myModule->featureCreated(aCommand);
432 * Makes a signal/slot connections between Property Panel
433 * and given operation. The given operation becomes a
434 * current operation and previous operation if exists
436 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
438 QAction* aCommand = 0;
439 if (isSalomeMode()) {
440 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
442 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
443 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
445 //Abort operation on uncheck the command
446 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
450 * Saves document with given name.
452 void XGUI_Workshop::saveDocument(QString theName)
454 QApplication::restoreOverrideCursor();
455 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
456 DocumentPtr aDoc = aMgr->rootDocument();
457 aDoc->save(theName.toLatin1().constData());
458 QApplication::restoreOverrideCursor();
461 //******************************************************
462 void XGUI_Workshop::onExit()
464 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
465 DocumentPtr aDoc = aMgr->rootDocument();
466 if(aDoc->isModified()) {
467 int anAnswer = QMessageBox::question(
468 myMainWindow, tr("Save current file"),
469 tr("The document is modified, save before exit?"),
470 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
471 if(anAnswer == QMessageBox::Save) {
472 bool saved = onSave();
476 } else if (anAnswer == QMessageBox::Cancel) {
483 //******************************************************
484 void XGUI_Workshop::onNew()
486 QApplication::setOverrideCursor(Qt::WaitCursor);
487 if (objectBrowser() == 0) {
489 mySelector->connectViewers();
491 myViewerProxy->connectToViewer();
493 if (!isSalomeMode()) {
494 myMainWindow->showPythonConsole();
495 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
496 aWnd->showMaximized();
497 updateCommandStatus();
499 myContextMenuMgr->connectViewer();
500 QApplication::restoreOverrideCursor();
503 //******************************************************
504 void XGUI_Workshop::onOpen()
506 //save current file before close if modified
507 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
508 DocumentPtr aDoc = aMgr->rootDocument();
509 if(aDoc->isModified()) {
510 //TODO(sbh): re-launch the app?
511 int anAnswer = QMessageBox::question(
512 myMainWindow, tr("Save current file"),
513 tr("The document is modified, save before opening another?"),
514 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
515 if(anAnswer == QMessageBox::Save) {
517 } else if (anAnswer == QMessageBox::Cancel) {
524 //show file dialog, check if readable and open
525 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
526 if(myCurrentDir.isEmpty())
528 QFileInfo aFileInfo(myCurrentDir);
529 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
530 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
534 QApplication::setOverrideCursor(Qt::WaitCursor);
535 aDoc->load(myCurrentDir.toLatin1().constData());
536 updateCommandStatus();
537 myObjectBrowser->rebuildDataTree();
538 QApplication::restoreOverrideCursor();
541 //******************************************************
542 bool XGUI_Workshop::onSave()
544 if(myCurrentDir.isEmpty()) {
547 saveDocument(myCurrentDir);
548 updateCommandStatus();
552 //******************************************************
553 bool XGUI_Workshop::onSaveAs()
555 QFileDialog dialog(mainWindow());
556 dialog.setWindowTitle(tr("Select directory to save files..."));
557 dialog.setFileMode(QFileDialog::Directory);
558 dialog.setFilter(tr("Folders (*)"));
559 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
560 dialog.setViewMode(QFileDialog::Detail);
565 QString aTempDir = dialog.selectedFiles().first();
567 if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
568 int answer = QMessageBox::question(myMainWindow,
569 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
571 tr("The folder already contains some files, save anyway?"),
572 QMessageBox::Save|QMessageBox::Cancel);
573 if(answer == QMessageBox::Cancel) {
577 myCurrentDir = aTempDir;
581 //******************************************************
582 void XGUI_Workshop::onUndo()
584 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
585 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
586 DocumentPtr aDoc = aMgr->rootDocument();
587 if (aDoc->isOperation())
588 operationMgr()->abortOperation();
590 updateCommandStatus();
593 //******************************************************
594 void XGUI_Workshop::onRedo()
596 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
597 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
598 DocumentPtr aDoc = aMgr->rootDocument();
599 if (aDoc->isOperation())
600 operationMgr()->abortOperation();
602 updateCommandStatus();
605 //******************************************************
606 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
609 QString::fromStdString(library(theModule.toStdString()));
610 if (libName.isEmpty()) {
612 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
617 CREATE_FUNC crtInst = 0;
620 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
624 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
625 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
626 QString aMsg((char*) &lpMsgBuf);
627 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
628 ::LocalFree(lpMsgBuf);
630 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
634 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
635 | FORMAT_MESSAGE_IGNORE_INSERTS,
636 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
637 QString aMsg((char*) &lpMsgBuf);
638 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
639 ::LocalFree(lpMsgBuf);
643 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
645 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
647 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
649 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
654 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
656 if (!err.isEmpty()) {
658 QMessageBox::warning(mainWindow(), tr("Error"), err);
660 qWarning( qPrintable( err ));
666 //******************************************************
667 bool XGUI_Workshop::activateModule()
669 Config_ModuleReader aModuleReader;
670 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
671 myModule = loadModule(moduleName);
674 myModule->createFeatures();
675 myActionsMgr->update();
679 //******************************************************
680 void XGUI_Workshop::updateCommandStatus()
682 QList<QAction*> aCommands;
683 if (isSalomeMode()) { // update commands in SALOME mode
684 aCommands = salomeConnector()->commandList();
686 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
687 foreach (XGUI_Command* aCmd, aMenuBar->features())
688 aCommands.append(aCmd);
690 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
691 if (aMgr->hasRootDocument()) {
694 foreach(QAction* aCmd, aCommands) {
695 QString aId = aCmd->data().toString();
696 if (aId == "UNDO_CMD")
698 else if (aId == "REDO_CMD")
700 else // Enable all commands
701 aCmd->setEnabled(true);
703 DocumentPtr aDoc = aMgr->rootDocument();
704 aUndoCmd->setEnabled(aDoc->canUndo());
705 aRedoCmd->setEnabled(aDoc->canRedo());
707 foreach(QAction* aCmd, aCommands) {
708 QString aId = aCmd->data().toString();
709 if (aId == "NEW_CMD")
710 aCmd->setEnabled(true);
711 else if (aId == "EXIT_CMD")
712 aCmd->setEnabled(true);
714 aCmd->setEnabled(false);
717 myActionsMgr->update();
720 //******************************************************
721 QList<QAction*> XGUI_Workshop::getModuleCommands() const
723 QList<QAction*> aCommands;
724 if (isSalomeMode()) { // update commands in SALOME mode
725 aCommands = salomeConnector()->commandList();
727 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
728 foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
729 if (aWb != aMenuBar->generalPage()) {
730 foreach(XGUI_Command* aCmd, aWb->features())
731 aCommands.append(aCmd);
738 //******************************************************
739 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
741 QDockWidget* aObjDock = new QDockWidget(theParent);
742 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
743 aObjDock->setWindowTitle(tr("Object browser"));
744 aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
745 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
746 connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
747 aObjDock->setWidget(myObjectBrowser);
749 myContextMenuMgr->connectObjectBrowser();
753 //******************************************************
755 * Creates dock widgets, places them in corresponding area
756 * and tabifies if necessary.
758 void XGUI_Workshop::createDockWidgets()
760 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
762 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
763 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
764 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
765 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
766 hidePropertyPanel(); //<! Invisible by default
768 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
770 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
771 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
772 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
773 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
775 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
776 myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
778 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
779 myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
780 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
781 myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
784 //******************************************************
785 void XGUI_Workshop::showPropertyPanel()
787 QAction* aViewAct = myPropertyPanel->toggleViewAction();
788 //<! Restore ability to close panel from the window's menu
789 aViewAct->setEnabled(true);
790 myPropertyPanel->show();
791 myPropertyPanel->raise();
794 //******************************************************
795 void XGUI_Workshop::hidePropertyPanel()
797 QAction* aViewAct = myPropertyPanel->toggleViewAction();
798 //<! Do not allow to show empty property panel
799 aViewAct->setEnabled(false);
800 myPropertyPanel->hide();
803 //******************************************************
804 void XGUI_Workshop::showObjectBrowser()
806 myObjectBrowser->parentWidget()->show();
809 //******************************************************
810 void XGUI_Workshop::hideObjectBrowser()
812 myObjectBrowser->parentWidget()->hide();
815 //******************************************************
816 void XGUI_Workshop::onFeatureTriggered()
818 QAction* aCmd = dynamic_cast<QAction*>(sender());
820 QString aId = salomeConnector()->commandId(aCmd);
822 myModule->launchOperation(aId);
826 //******************************************************
827 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
829 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
832 if (!XGUI_Tools::isModelObject(thePart)) {
833 aFeaDoc = thePart->data()->docRef("PartDocument")->value();
835 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(thePart);
836 aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
839 aMgr->setCurrentDocument(aFeaDoc);
841 aMgr->setCurrentDocument(aMgr->rootDocument());
845 //******************************************************
846 void XGUI_Workshop::salomeViewerSelectionChanged()
848 emit salomeViewerSelection();
852 //**************************************************************
853 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
855 return mySalomeConnector->viewer();
858 //**************************************************************
859 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
861 QFeatureList aFeatures = mySelector->selection()->selectedFeatures();
862 if ((theId == "ACTIVATE_PART_CMD") && (aFeatures.size() > 0))
863 activatePart(aFeatures.first());
864 else if (theId == "DEACTIVATE_PART_CMD")
865 activatePart(FeaturePtr());
866 else if (theId == "DELETE_CMD")
867 deleteFeatures(aFeatures);
868 else if (theId == "SHOW_CMD")
869 showFeatures(aFeatures, true);
870 else if (theId == "HIDE_CMD")
871 showFeatures(aFeatures, false);
874 //**************************************************************
875 void XGUI_Workshop::onWidgetValuesChanged()
877 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
878 FeaturePtr aFeature = anOperation->feature();
880 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
882 // aCustom->storeValue(aFeature);
884 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
885 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
886 for (; anIt != aLast; anIt++) {
887 ModuleBase_ModelWidget* aCustom = *anIt;
888 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
889 aCustom->storeValue(aFeature);
894 //**************************************************************
895 void XGUI_Workshop::activatePart(FeaturePtr theFeature)
897 changeCurrentDocument(theFeature);
898 myObjectBrowser->activatePart(theFeature);
901 //**************************************************************
902 void XGUI_Workshop::activateLastPart()
904 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
905 DocumentPtr aDoc = aMgr->rootDocument();
906 FeaturePtr aLastPart = aDoc->feature(PARTS_GROUP, aDoc->size(PARTS_GROUP) - 1, true);
907 activatePart(aLastPart);
910 //**************************************************************
911 void XGUI_Workshop::deleteFeatures(QFeatureList theList)
913 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
914 QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"),
915 tr("Seleted features will be deleted. Continue?"),
916 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
917 if (aRes == QMessageBox::Yes) {
918 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
919 aMgr->rootDocument()->startOperation();
920 foreach (FeaturePtr aFeature, theList) {
921 if (aFeature->getKind() == PARTSET_PART_KIND) {
923 if (!XGUI_Tools::isModelObject(aFeature)) {
924 aDoc = aFeature->data()->docRef("PartDocument")->value();
926 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
927 aDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
928 aFeature = aObject->featureRef();
930 if (aDoc == aMgr->currentDocument()) {
934 if (XGUI_Tools::isModelObject(aFeature)) {
935 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
936 aFeature = aObject->featureRef();
939 if (myDisplayer->isVisible(aFeature))
940 myDisplayer->erase(aFeature, false);
941 aFeature->document()->removeFeature(aFeature);
943 myDisplayer->updateViewer();
944 aMgr->rootDocument()->finishOperation();
948 //**************************************************************
949 void XGUI_Workshop::showFeatures(QFeatureList theList, bool isVisible)
952 foreach (FeaturePtr aFeature, theList) {
953 myDisplayer->display(aFeature, false);
956 foreach (FeaturePtr aFeature, theList) {
957 myDisplayer->erase(aFeature, false);
960 myDisplayer->updateViewer();
963 //**************************************************************
964 void XGUI_Workshop::updateCommandsOnViewSelection()
966 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
967 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
968 XGUI_Selection* aSelection = mySelector->selection();
970 QList<QAction*> aActions = getModuleCommands();
971 foreach(QAction* aAction, aActions) {
972 QString aId = aAction->data().toString();
973 const ModelAPI_Validator* aValidator = aFactory->validator(aId.toStdString());
975 const ModuleBase_SelectionValidator* aSelValidator =
976 dynamic_cast<const ModuleBase_SelectionValidator*>(aValidator);
978 aAction->setEnabled(aSelValidator->isValid(aSelection));