1 #include "ModuleBase_IModule.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "ModuleBase_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_Selection.h"
14 #include "XGUI_ObjectsBrowser.h"
15 #include "XGUI_Displayer.h"
16 #include "XGUI_OperationMgr.h"
17 #include "XGUI_SalomeConnector.h"
18 #include "XGUI_SalomeViewer.h"
19 #include "XGUI_ActionsMgr.h"
20 #include "XGUI_ErrorDialog.h"
21 #include "XGUI_ViewerProxy.h"
22 #include "XGUI_PropertyPanel.h"
23 #include "XGUI_ContextMenuMgr.h"
24 #include "XGUI_ModuleConnector.h"
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_PluginManager.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_AttributeDocRef.h>
31 #include <ModelAPI_Object.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultBody.h>
36 #include <PartSetPlugin_Part.h>
38 #include <Events_Loop.h>
39 #include <Events_Error.h>
40 #include <Events_LongOp.h>
42 #include <ModuleBase_Operation.h>
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_OperationDescription.h>
45 #include <ModuleBase_SelectionValidator.h>
46 #include <ModuleBase_ResultValidators.h>
48 #include <Config_Common.h>
49 #include <Config_FeatureMessage.h>
50 #include <Config_PointerMessage.h>
51 #include <Config_ModuleReader.h>
53 #include <SUIT_ResourceMgr.h>
55 #include <QApplication>
56 #include <QFileDialog>
57 #include <QMessageBox>
58 #include <QMdiSubWindow>
59 #include <QPushButton>
60 #include <QDockWidget>
74 SUIT_ResourceMgr* XGUI_Workshop::myResourceMgr = 0;
76 QMap<QString, QString> XGUI_Workshop::myIcons;
78 QString XGUI_Workshop::featureIcon(const std::string& theId)
80 QString aId(theId.c_str());
81 if (myIcons.contains(aId))
86 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
88 myCurrentDir(QString()),
90 mySalomeConnector(theConnector),
96 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
97 myResourceMgr->setCurrentFormat("xml");
99 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
101 myDisplayer = new XGUI_Displayer(this);
103 mySelector = new XGUI_SelectionMgr(this);
104 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
106 myOperationMgr = new XGUI_OperationMgr(this);
107 myActionsMgr = new XGUI_ActionsMgr(this);
108 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
109 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
110 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
111 this, SLOT(onContextMenuCommand(const QString&, bool)));
113 myViewerProxy = new XGUI_ViewerProxy(this);
114 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
116 myModuleConnector = new XGUI_ModuleConnector(this);
118 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
119 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
120 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
121 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
122 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
123 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
124 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
127 //******************************************************
128 XGUI_Workshop::~XGUI_Workshop(void)
132 //******************************************************
133 void XGUI_Workshop::startApplication()
136 //Initialize event listening
137 Events_Loop* aLoop = Events_Loop::loop();
138 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
139 //TODO(sbh): Implement static method to extract event id [SEID]
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
141 // TODO Is it good to use non standard event within workshop?
142 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
143 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
144 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
145 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
146 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
147 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
149 registerValidators();
152 myMainWindow->show();
153 updateCommandStatus();
158 //******************************************************
159 void XGUI_Workshop::initMenu()
161 myContextMenuMgr->createActions();
163 if (isSalomeMode()) {
164 // Create only Undo, Redo commands
165 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD",
166 tr("Undo"), tr("Undo last command"),
167 QIcon(":pictures/undo.png"),
168 QKeySequence::Undo, false);
169 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
170 aAction = salomeConnector()->addEditCommand("REDO_CMD",
171 tr("Redo"), tr("Redo last command"),
172 QIcon(":pictures/redo.png"),
173 QKeySequence::Redo, false);
174 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
175 salomeConnector()->addEditMenuSeparator();
178 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
180 // File commands group
181 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
183 XGUI_Command* aCommand;
185 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
186 QIcon(":pictures/save.png"), QKeySequence::Save);
187 aCommand->connectTo(this, SLOT(onSave()));
188 //aCommand->disable();
190 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
191 QIcon(":pictures/undo.png"), QKeySequence::Undo);
192 aCommand->connectTo(this, SLOT(onUndo()));
194 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
195 QIcon(":pictures/redo.png"), QKeySequence::Redo);
196 aCommand->connectTo(this, SLOT(onRedo()));
198 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
199 QIcon(":pictures/rebuild.png"));
201 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
202 QIcon(":pictures/save.png"));
203 aCommand->connectTo(this, SLOT(onSaveAs()));
204 //aCommand->disable();
206 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
207 QIcon(":pictures/open.png"), QKeySequence::Open);
208 aCommand->connectTo(this, SLOT(onOpen()));
210 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
211 // QIcon(":pictures/new.png"), QKeySequence::New);
212 //aCommand->connectTo(this, SLOT(onNew()));
214 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
215 QIcon(":pictures/close.png"), QKeySequence::Close);
216 aCommand->connectTo(this, SLOT(onExit()));
217 //FIXME: SBH's test action. Can be used for some GUI tests.
219 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
220 // QIcon(":pictures/close.png"));
221 // aCommand->connectTo(myActionsMgr, SLOT(update()));
225 //******************************************************
226 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
228 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
229 return aMenuBar->addWorkbench(theName);
232 //******************************************************
233 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
235 //A message to start feature creation received.
236 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
237 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
238 if(!aFeatureMsg->isInternal()) {
239 addFeature(aFeatureMsg);
244 // Process creation of Part
245 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
246 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
247 onFeatureCreatedMsg(aUpdMsg);
252 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
253 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
254 onFeatureRedisplayMsg(aUpdMsg);
258 //Update property panel on corresponding message. If there is no current operation (no
259 //property panel), or received message has different feature to the current - do nothing.
260 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
261 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
262 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
263 onFeatureUpdatedMsg(anUpdateMsg);
267 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
268 const ModelAPI_ObjectDeletedMessage* aDelMsg =
269 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
270 onObjectDeletedMsg(aDelMsg);
274 if (theMessage->eventID() == Events_LongOp::eventID()) {
275 if (Events_LongOp::isPerformed())
276 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
277 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
279 QApplication::restoreOverrideCursor();
283 //An operation passed by message. Start it, process and commit.
284 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
285 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
286 //myPropertyPanel->cleanContent();
287 ModuleBase_Operation* anOperation = (ModuleBase_Operation*)aPartSetMsg->pointer();
289 if (myOperationMgr->startOperation(anOperation)) {
290 myPropertyPanel->updateContentWidget(anOperation->feature());
291 if (!anOperation->getDescription()->hasXmlRepresentation()) {
292 if (anOperation->commit())
293 updateCommandStatus();
298 //Show error dialog if error message received.
299 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
301 emit errorOccurred(QString::fromLatin1(anAppError->description()));
305 //******************************************************
306 void XGUI_Workshop::onStartWaiting()
308 if (Events_LongOp::isPerformed()) {
309 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
313 //******************************************************
314 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
316 std::set<ObjectPtr> aFeatures = theMsg->objects();
317 if (myOperationMgr->hasOperation())
319 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
320 std::set<ObjectPtr>::const_iterator aIt;
321 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
322 ObjectPtr aNewFeature = (*aIt);
323 if(aNewFeature == aCurrentFeature) {
324 myPropertyPanel->updateContentWidget(aCurrentFeature);
331 //******************************************************
332 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
334 std::set<ObjectPtr> aObjects = theMsg->objects();
335 std::set<ObjectPtr>::const_iterator aIt;
336 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
337 ObjectPtr aObj = (*aIt);
338 if (!aObj->data() || !aObj->data()->isValid())
339 myDisplayer->erase(aObj, false);
341 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
342 myDisplayer->display(aObj, false); // In order to update presentation
344 if(myOperationMgr->hasOperation()) {
345 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
346 if (aOperation->hasObject(aObj)) { // Display only current operation results
347 myDisplayer->display(aObj, false);
353 myDisplayer->updateViewer();
356 //******************************************************
357 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
359 std::set<ObjectPtr> aObjects = theMsg->objects();
361 std::set<ObjectPtr>::const_iterator aIt;
362 bool aHasPart = false;
363 bool isDisplayed = false;
364 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
365 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
368 // If a feature is created from the aplication's python console
369 // it doesn't stored in the operation mgr and doesn't displayed
370 } else if(myOperationMgr->hasOperation()) {
371 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
372 if (aOperation->hasObject(*aIt)) { // Display only current operation results
373 myDisplayer->display(*aIt, false);
379 myDisplayer->updateViewer();
381 //The created part will be created in Object Browser later and we have to activate it
382 // only when it is created everywere
383 QTimer::singleShot(50, this, SLOT(activateLastPart()));
387 //******************************************************
388 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
390 //std::set<ObjectPtr> aFeatures = theMsg->objects();
393 //******************************************************
394 void XGUI_Workshop::onOperationStarted()
396 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
398 if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
399 connectWithOperation(aOperation);
402 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
403 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
405 myPropertyPanel->cleanContent();
406 aFactory.createWidget(myPropertyPanel->contentWidget());
408 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
409 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
410 ModuleBase_ModelWidget* aWidget;
411 for (; anIt != aLast; anIt++) {
413 aWidget->setFeature(aOperation->feature());
414 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
415 QObject::connect(aWidget, SIGNAL(valuesChanged()),
416 this, SLOT(onWidgetValuesChanged()));
417 // Init default values
418 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
419 //aWidget->storeValue(aOperation->feature());
421 aWidget->storeValue();
425 myPropertyPanel->setModelWidgets(aWidgets);
426 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
428 updateCommandStatus();
431 //******************************************************
432 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
434 //!< No need for property panel
435 updateCommandStatus();
437 myPropertyPanel->cleanContent();
443 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
447 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
451 // Remember features icons
452 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
454 //Find or create Workbench
455 QString aWchName = QString::fromStdString(theMessage->workbenchId());
456 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
457 bool isUsePropPanel = theMessage->isUseInput();
458 QString aId = QString::fromStdString(theMessage->id());
459 if (isSalomeMode()) {
460 QAction* aAction = salomeConnector()->addFeature(aWchName,
462 QString::fromStdString(theMessage->text()),
463 QString::fromStdString(theMessage->tooltip()),
464 QIcon(theMessage->icon().c_str()),
465 QKeySequence(), isUsePropPanel);
466 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
467 myActionsMgr->addCommand(aAction);
468 myModule->featureCreated(aAction);
471 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
472 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
474 aPage = addWorkbench(aWchName);
476 //Find or create Group
477 QString aGroupName = QString::fromStdString(theMessage->groupId());
478 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
480 aGroup = aPage->addGroup(aGroupName);
483 XGUI_Command* aCommand = aGroup->addFeature(aId,
484 QString::fromStdString(theMessage->text()),
485 QString::fromStdString(theMessage->tooltip()),
486 QIcon(theMessage->icon().c_str()),
487 QKeySequence(), isUsePropPanel);
488 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
489 myActionsMgr->addCommand(aCommand);
490 myModule->featureCreated(aCommand);
495 * Makes a signal/slot connections between Property Panel
496 * and given operation. The given operation becomes a
497 * current operation and previous operation if exists
499 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
501 QAction* aCommand = 0;
502 if (isSalomeMode()) {
503 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
505 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
506 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
508 //Abort operation on uncheck the command
510 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
514 * Saves document with given name.
516 void XGUI_Workshop::saveDocument(QString theName)
518 QApplication::restoreOverrideCursor();
519 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
520 DocumentPtr aDoc = aMgr->rootDocument();
521 aDoc->save(theName.toLatin1().constData());
522 QApplication::restoreOverrideCursor();
525 //******************************************************
526 void XGUI_Workshop::onExit()
528 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
529 DocumentPtr aDoc = aMgr->rootDocument();
530 if(aDoc->isModified()) {
531 int anAnswer = QMessageBox::question(
532 myMainWindow, tr("Save current file"),
533 tr("The document is modified, save before exit?"),
534 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
535 if(anAnswer == QMessageBox::Save) {
536 bool saved = onSave();
540 } else if (anAnswer == QMessageBox::Cancel) {
547 //******************************************************
548 void XGUI_Workshop::onNew()
550 QApplication::setOverrideCursor(Qt::WaitCursor);
551 if (objectBrowser() == 0) {
553 mySelector->connectViewers();
555 myViewerProxy->connectToViewer();
557 if (!isSalomeMode()) {
558 myMainWindow->showPythonConsole();
559 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
560 aWnd->showMaximized();
561 updateCommandStatus();
563 myContextMenuMgr->connectViewer();
564 QApplication::restoreOverrideCursor();
567 //******************************************************
568 void XGUI_Workshop::onOpen()
570 //save current file before close if modified
571 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
572 DocumentPtr aDoc = aMgr->rootDocument();
573 if(aDoc->isModified()) {
574 //TODO(sbh): re-launch the app?
575 int anAnswer = QMessageBox::question(
576 myMainWindow, tr("Save current file"),
577 tr("The document is modified, save before opening another?"),
578 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
579 if(anAnswer == QMessageBox::Save) {
581 } else if (anAnswer == QMessageBox::Cancel) {
588 //show file dialog, check if readable and open
589 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
590 if(myCurrentDir.isEmpty())
592 QFileInfo aFileInfo(myCurrentDir);
593 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
594 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
598 QApplication::setOverrideCursor(Qt::WaitCursor);
599 aDoc->load(myCurrentDir.toLatin1().constData());
600 myObjectBrowser->rebuildDataTree();
602 updateCommandStatus();
603 QApplication::restoreOverrideCursor();
606 //******************************************************
607 bool XGUI_Workshop::onSave()
609 if(myCurrentDir.isEmpty()) {
612 saveDocument(myCurrentDir);
613 updateCommandStatus();
617 //******************************************************
618 bool XGUI_Workshop::onSaveAs()
620 QFileDialog dialog(mainWindow());
621 dialog.setWindowTitle(tr("Select directory to save files..."));
622 dialog.setFileMode(QFileDialog::Directory);
623 dialog.setFilter(tr("Folders (*)"));
624 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
625 dialog.setViewMode(QFileDialog::Detail);
630 QString aTempDir = dialog.selectedFiles().first();
632 if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
633 int answer = QMessageBox::question(myMainWindow,
634 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
636 tr("The folder already contains some files, save anyway?"),
637 QMessageBox::Save|QMessageBox::Cancel);
638 if(answer == QMessageBox::Cancel) {
642 myCurrentDir = aTempDir;
646 //******************************************************
647 void XGUI_Workshop::onUndo()
649 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
650 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
651 DocumentPtr aDoc = aMgr->rootDocument();
652 if (aDoc->isOperation())
653 operationMgr()->abortOperation();
655 updateCommandStatus();
658 //******************************************************
659 void XGUI_Workshop::onRedo()
661 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
662 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
663 DocumentPtr aDoc = aMgr->rootDocument();
664 if (aDoc->isOperation())
665 operationMgr()->abortOperation();
667 updateCommandStatus();
670 //******************************************************
671 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
674 QString::fromStdString(library(theModule.toStdString()));
675 if (libName.isEmpty()) {
677 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
682 CREATE_FUNC crtInst = 0;
685 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
689 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
690 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
691 QString aMsg((char*) &lpMsgBuf);
692 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
693 ::LocalFree(lpMsgBuf);
695 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
699 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
700 | FORMAT_MESSAGE_IGNORE_INSERTS,
701 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
702 QString aMsg((char*) &lpMsgBuf);
703 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
704 ::LocalFree(lpMsgBuf);
708 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
710 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
712 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
714 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
719 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
721 if (!err.isEmpty()) {
723 QMessageBox::warning(mainWindow(), tr("Error"), err);
725 qWarning( qPrintable( err ));
731 //******************************************************
732 bool XGUI_Workshop::activateModule()
734 Config_ModuleReader aModuleReader;
735 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
736 myModule = loadModule(moduleName);
739 myModule->createFeatures();
740 myActionsMgr->update();
744 //******************************************************
745 void XGUI_Workshop::updateCommandStatus()
747 QList<QAction*> aCommands;
748 if (isSalomeMode()) { // update commands in SALOME mode
749 aCommands = salomeConnector()->commandList();
751 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
752 foreach (XGUI_Command* aCmd, aMenuBar->features())
753 aCommands.append(aCmd);
755 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
756 if (aMgr->hasRootDocument()) {
759 foreach(QAction* aCmd, aCommands) {
760 QString aId = aCmd->data().toString();
761 if (aId == "UNDO_CMD")
763 else if (aId == "REDO_CMD")
765 else // Enable all commands
766 aCmd->setEnabled(true);
768 DocumentPtr aDoc = aMgr->rootDocument();
769 aUndoCmd->setEnabled(aDoc->canUndo());
770 aRedoCmd->setEnabled(aDoc->canRedo());
772 foreach(QAction* aCmd, aCommands) {
773 QString aId = aCmd->data().toString();
774 if (aId == "NEW_CMD")
775 aCmd->setEnabled(true);
776 else if (aId == "EXIT_CMD")
777 aCmd->setEnabled(true);
779 aCmd->setEnabled(false);
782 myActionsMgr->update();
785 //******************************************************
786 QList<QAction*> XGUI_Workshop::getModuleCommands() const
788 QList<QAction*> aCommands;
789 if (isSalomeMode()) { // update commands in SALOME mode
790 aCommands = salomeConnector()->commandList();
792 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
793 foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
794 if (aWb != aMenuBar->generalPage()) {
795 foreach(XGUI_Command* aCmd, aWb->features())
796 aCommands.append(aCmd);
803 //******************************************************
804 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
806 QDockWidget* aObjDock = new QDockWidget(theParent);
807 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
808 aObjDock->setWindowTitle(tr("Object browser"));
809 aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
810 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
811 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this, SLOT(changeCurrentDocument(ObjectPtr)));
812 aObjDock->setWidget(myObjectBrowser);
814 myContextMenuMgr->connectObjectBrowser();
818 //******************************************************
820 * Creates dock widgets, places them in corresponding area
821 * and tabifies if necessary.
823 void XGUI_Workshop::createDockWidgets()
825 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
827 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
828 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
829 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
830 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
831 hidePropertyPanel(); //<! Invisible by default
833 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
835 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
836 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
837 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
838 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
840 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
841 myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
843 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
844 myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
845 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
846 myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
849 //******************************************************
850 void XGUI_Workshop::showPropertyPanel()
852 QAction* aViewAct = myPropertyPanel->toggleViewAction();
853 //<! Restore ability to close panel from the window's menu
854 aViewAct->setEnabled(true);
855 myPropertyPanel->show();
856 myPropertyPanel->raise();
859 //******************************************************
860 void XGUI_Workshop::hidePropertyPanel()
862 QAction* aViewAct = myPropertyPanel->toggleViewAction();
863 //<! Do not allow to show empty property panel
864 aViewAct->setEnabled(false);
865 myPropertyPanel->hide();
868 //******************************************************
869 void XGUI_Workshop::showObjectBrowser()
871 myObjectBrowser->parentWidget()->show();
874 //******************************************************
875 void XGUI_Workshop::hideObjectBrowser()
877 myObjectBrowser->parentWidget()->hide();
880 //******************************************************
881 void XGUI_Workshop::onFeatureTriggered()
883 QAction* aCmd = dynamic_cast<QAction*>(sender());
885 QString aId = salomeConnector()->commandId(aCmd);
887 myModule->launchOperation(aId);
891 //******************************************************
892 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
894 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
896 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
898 DocumentPtr aPartDoc = aPart->partDoc();
900 aMgr->setCurrentDocument(aPartDoc);
905 aMgr->setCurrentDocument(aMgr->rootDocument());
908 //******************************************************
909 void XGUI_Workshop::salomeViewerSelectionChanged()
911 emit salomeViewerSelection();
915 //**************************************************************
916 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
918 return mySalomeConnector->viewer();
921 //**************************************************************
922 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
924 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
925 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
926 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
928 } else if (theId == "DEACTIVATE_PART_CMD")
929 activatePart(ResultPartPtr());
930 else if (theId == "DELETE_CMD")
931 deleteObjects(aObjects);
932 else if (theId == "SHOW_CMD")
933 showObjects(aObjects, true);
934 else if (theId == "HIDE_CMD")
935 showObjects(aObjects, false);
938 //**************************************************************
939 void XGUI_Workshop::onWidgetValuesChanged()
941 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
942 FeaturePtr aFeature = anOperation->feature();
944 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
946 // aCustom->storeValue(aFeature);
948 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
949 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
950 for (; anIt != aLast; anIt++) {
951 ModuleBase_ModelWidget* aCustom = *anIt;
952 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
953 //aCustom->storeValue(aFeature);
954 aCustom->storeValue();
959 //**************************************************************
960 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
962 changeCurrentDocument(theFeature);
963 myObjectBrowser->activatePart(theFeature);
966 //**************************************************************
967 void XGUI_Workshop::activateLastPart()
969 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
970 DocumentPtr aDoc = aMgr->rootDocument();
971 std::string aGrpName = ModelAPI_ResultPart::group();
972 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
973 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
978 //**************************************************************
979 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
981 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
982 QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"),
983 tr("Seleted features will be deleted. Continue?"),
984 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
985 // ToDo: definbe deleting method
986 if (aRes == QMessageBox::Yes) {
987 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
988 aMgr->rootDocument()->startOperation();
989 foreach (ObjectPtr aObj, theList) {
990 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
992 DocumentPtr aDoc = aPart->document();
993 if (aDoc == aMgr->currentDocument()) {
996 //aMgr->rootDocument()->removeFeature(aPart->owner());
998 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1000 aObj->document()->removeFeature(aFeature);
1003 myDisplayer->updateViewer();
1004 aMgr->rootDocument()->finishOperation();
1008 //**************************************************************
1009 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1011 foreach (ObjectPtr aObj, theList) {
1012 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1015 myDisplayer->display(aRes, false);
1017 myDisplayer->erase(aRes, false);
1021 myDisplayer->updateViewer();
1024 //**************************************************************
1025 void XGUI_Workshop::updateCommandsOnViewSelection()
1027 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1028 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1029 XGUI_Selection* aSelection = mySelector->selection();
1030 if (aSelection->getSelected().size() == 0)
1033 QList<QAction*> aActions = getModuleCommands();
1034 foreach(QAction* aAction, aActions) {
1035 QString aId = aAction->data().toString();
1036 std::list<ModelAPI_Validator*> aValidators;
1037 aFactory->validators(aId.toStdString(), aValidators);
1038 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1039 for(; aValidator != aValidators.end(); aValidator++) {
1041 const ModuleBase_SelectionValidator* aSelValidator =
1042 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1043 if (aSelValidator) {
1044 aAction->setEnabled(aSelValidator->isValid(aSelection));
1052 //**************************************************************
1053 void XGUI_Workshop::registerValidators() const
1055 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1056 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1060 //**************************************************************
1061 void XGUI_Workshop::displayAllResults()
1063 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1064 DocumentPtr aRootDoc = aMgr->rootDocument();
1065 displayDocumentResults(aRootDoc);
1066 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1067 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1068 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1069 displayDocumentResults(aPart->partDoc());
1071 myDisplayer->updateViewer();
1074 //**************************************************************
1075 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1077 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1078 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1081 //**************************************************************
1082 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1084 for (int i = 0; i < theDoc->size(theGroup); i++)
1085 myDisplayer->display(theDoc->object(theGroup, i), false);