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 "XGUI_SelectionMgr.h"
12 #include "XGUI_Selection.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_SalomeViewer.h"
18 #include "XGUI_ActionsMgr.h"
19 #include "XGUI_ErrorDialog.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_PropertyPanel.h"
22 #include "XGUI_ContextMenuMgr.h"
23 #include "XGUI_ModuleConnector.h"
24 #include "XGUI_Preferences.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_WidgetFactory.h"
48 #include <Config_Common.h>
49 #include <Config_FeatureMessage.h>
50 #include <Config_PointerMessage.h>
51 #include <Config_ModuleReader.h>
53 #include <QApplication>
54 #include <QFileDialog>
55 #include <QMessageBox>
56 #include <QMdiSubWindow>
57 #include <QPushButton>
58 #include <QDockWidget>
72 QMap<QString, QString> XGUI_Workshop::myIcons;
74 QString XGUI_Workshop::featureIcon(const std::string& theId)
76 QString aId(theId.c_str());
77 if (myIcons.contains(aId))
82 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
84 myCurrentDir(QString()),
86 mySalomeConnector(theConnector),
91 myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
93 myDisplayer = new XGUI_Displayer(this);
95 mySelector = new XGUI_SelectionMgr(this);
96 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
98 myOperationMgr = new XGUI_OperationMgr(this);
99 myActionsMgr = new XGUI_ActionsMgr(this);
100 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
101 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
102 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
103 SLOT(onContextMenuCommand(const QString&, bool)));
105 myViewerProxy = new XGUI_ViewerProxy(this);
106 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
108 myModuleConnector = new XGUI_ModuleConnector(this);
110 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
111 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
112 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
113 SLOT(onOperationStopped(ModuleBase_Operation*)));
114 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
115 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
116 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
118 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
121 //******************************************************
122 XGUI_Workshop::~XGUI_Workshop(void)
126 //******************************************************
127 void XGUI_Workshop::startApplication()
130 //Initialize event listening
131 Events_Loop* aLoop = Events_Loop::loop();
132 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
133 //TODO(sbh): Implement static method to extract event id [SEID]
134 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
135 // TODO Is it good to use non standard event within workshop?
136 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
137 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
138 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
141 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
143 registerValidators();
146 myMainWindow->show();
147 updateCommandStatus();
149 XGUI_Preferences::loadCustomProps();
153 //******************************************************
154 void XGUI_Workshop::initMenu()
156 myContextMenuMgr->createActions();
158 if (isSalomeMode()) {
159 // Create only Undo, Redo commands
160 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
161 tr("Undo last command"),
162 QIcon(":pictures/undo.png"),
163 QKeySequence::Undo, false);
164 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
165 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
166 QIcon(":pictures/redo.png"), QKeySequence::Redo,
168 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
169 salomeConnector()->addEditMenuSeparator();
172 // File commands group
173 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
175 XGUI_Command* aCommand;
177 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
178 QIcon(":pictures/save.png"), QKeySequence::Save);
179 aCommand->connectTo(this, SLOT(onSave()));
180 //aCommand->disable();
182 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
183 QIcon(":pictures/undo.png"), QKeySequence::Undo);
184 aCommand->connectTo(this, SLOT(onUndo()));
186 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
187 QIcon(":pictures/redo.png"), QKeySequence::Redo);
188 aCommand->connectTo(this, SLOT(onRedo()));
190 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
191 QIcon(":pictures/rebuild.png"));
193 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
194 QIcon(":pictures/save.png"));
195 aCommand->connectTo(this, SLOT(onSaveAs()));
196 //aCommand->disable();
198 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
199 QIcon(":pictures/open.png"), QKeySequence::Open);
200 aCommand->connectTo(this, SLOT(onOpen()));
202 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
203 // QIcon(":pictures/new.png"), QKeySequence::New);
204 //aCommand->connectTo(this, SLOT(onNew()));
206 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
207 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
208 aCommand->connectTo(this, SLOT(onPreferences()));
210 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
211 QIcon(":pictures/close.png"), QKeySequence::Close);
212 aCommand->connectTo(this, SLOT(onExit()));
213 //FIXME: SBH's test action. Can be used for some GUI tests.
215 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
216 // QIcon(":pictures/close.png"), QKeySequence(), true);
217 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
221 //******************************************************
222 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
224 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
225 return aMenuBar->addWorkbench(theName);
228 //******************************************************
229 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
231 //A message to start feature creation received.
232 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
233 const Config_FeatureMessage* aFeatureMsg =
234 dynamic_cast<const Config_FeatureMessage*>(theMessage);
235 if (!aFeatureMsg->isInternal()) {
236 addFeature(aFeatureMsg);
241 // Process creation of Part
242 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
243 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
244 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
245 onFeatureCreatedMsg(aUpdMsg);
250 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
251 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
252 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
253 onFeatureRedisplayMsg(aUpdMsg);
257 //Update property panel on corresponding message. If there is no current operation (no
258 //property panel), or received message has different feature to the current - do nothing.
259 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
260 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
261 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
262 onFeatureUpdatedMsg(anUpdateMsg);
266 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
267 const ModelAPI_ObjectDeletedMessage* aDelMsg =
268 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
269 onObjectDeletedMsg(aDelMsg);
273 if (theMessage->eventID() == Events_LongOp::eventID()) {
274 if (Events_LongOp::isPerformed())
275 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
276 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
278 QApplication::restoreOverrideCursor();
282 //An operation passed by message. Start it, process and commit.
283 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
284 const Config_PointerMessage* aPartSetMsg =
285 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()) {
318 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
319 std::set<ObjectPtr>::const_iterator aIt;
320 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
321 ObjectPtr aNewFeature = (*aIt);
322 if (aNewFeature == aCurrentFeature) {
323 myPropertyPanel->updateContentWidget(aCurrentFeature);
328 myOperationMgr->validateCurrentOperation();
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();
397 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
399 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
400 connectWithOperation(aOperation);
403 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
404 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
407 myPropertyPanel->cleanContent();
408 aFactory.createWidget(myPropertyPanel->contentWidget());
410 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
411 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
412 ModuleBase_ModelWidget* aWidget;
413 for (; anIt != aLast; anIt++) {
415 aWidget->setFeature(aOperation->feature());
416 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
417 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
418 // Init default values
419 if (!aOperation->isEditOperation()) {
420 //aWidget->storeValue(aOperation->feature());
422 aWidget->storeValue();
426 myPropertyPanel->setModelWidgets(aWidgets);
427 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
429 updateCommandStatus();
432 //******************************************************
433 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
435 //!< No need for property panel
436 updateCommandStatus();
438 myPropertyPanel->cleanContent();
444 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
448 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
452 // Remember features icons
453 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
455 //Find or create Workbench
456 QString aWchName = QString::fromStdString(theMessage->workbenchId());
457 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
458 bool isUsePropPanel = theMessage->isUseInput();
459 QString aFeatureId = QString::fromStdString(theMessage->id());
460 if (isSalomeMode()) {
461 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
462 QString::fromStdString(theMessage->text()),
463 QString::fromStdString(theMessage->tooltip()),
464 QIcon(theMessage->icon().c_str()),
465 QKeySequence(), isUsePropPanel);
466 salomeConnector()->setNestedActions(aFeatureId, 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);
482 // Check if hotkey sequence is already defined:
483 QKeySequence aHotKey = myActionsMgr->registerShortcut(
484 QString::fromStdString(theMessage->keysequence()));
486 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
487 QString::fromStdString(theMessage->text()),
488 QString::fromStdString(theMessage->tooltip()),
489 QIcon(theMessage->icon().c_str()), aHotKey,
491 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
492 myActionsMgr->addCommand(aCommand);
493 myModule->featureCreated(aCommand);
498 * Makes a signal/slot connections between Property Panel
499 * and given operation. The given operation becomes a
500 * current operation and previous operation if exists
502 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
504 QAction* aCommand = 0;
505 if (isSalomeMode()) {
506 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
508 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
509 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
511 //Abort operation on uncheck the command
513 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
517 * Saves document with given name.
519 void XGUI_Workshop::saveDocument(QString theName)
521 QApplication::restoreOverrideCursor();
522 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
523 DocumentPtr aDoc = aMgr->rootDocument();
524 aDoc->save(theName.toLatin1().constData());
525 QApplication::restoreOverrideCursor();
528 //******************************************************
529 void XGUI_Workshop::onExit()
531 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
532 DocumentPtr aDoc = aMgr->rootDocument();
533 if (aDoc->isModified()) {
534 int anAnswer = QMessageBox::question(
535 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
536 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
537 if (anAnswer == QMessageBox::Save) {
538 bool saved = onSave();
542 } else if (anAnswer == QMessageBox::Cancel) {
549 //******************************************************
550 void XGUI_Workshop::onNew()
552 QApplication::setOverrideCursor(Qt::WaitCursor);
553 if (objectBrowser() == 0) {
555 mySelector->connectViewers();
557 myViewerProxy->connectToViewer();
559 if (!isSalomeMode()) {
560 myMainWindow->showPythonConsole();
561 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
562 aWnd->showMaximized();
563 updateCommandStatus();
565 myContextMenuMgr->connectViewer();
566 QApplication::restoreOverrideCursor();
569 //******************************************************
570 void XGUI_Workshop::onOpen()
572 //save current file before close if modified
573 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
574 DocumentPtr aDoc = aMgr->rootDocument();
575 if (aDoc->isModified()) {
576 //TODO(sbh): re-launch the app?
577 int anAnswer = QMessageBox::question(
578 myMainWindow, tr("Save current file"),
579 tr("The document is modified, save before opening another?"),
580 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
581 if (anAnswer == QMessageBox::Save) {
583 } else if (anAnswer == QMessageBox::Cancel) {
590 //show file dialog, check if readable and open
591 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
592 if (myCurrentDir.isEmpty())
594 QFileInfo aFileInfo(myCurrentDir);
595 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
596 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
600 QApplication::setOverrideCursor(Qt::WaitCursor);
601 aDoc->load(myCurrentDir.toLatin1().constData());
602 myObjectBrowser->rebuildDataTree();
604 updateCommandStatus();
605 QApplication::restoreOverrideCursor();
608 //******************************************************
609 bool XGUI_Workshop::onSave()
611 if (myCurrentDir.isEmpty()) {
614 saveDocument(myCurrentDir);
615 updateCommandStatus();
619 //******************************************************
620 bool XGUI_Workshop::onSaveAs()
622 QFileDialog dialog(mainWindow());
623 dialog.setWindowTitle(tr("Select directory to save files..."));
624 dialog.setFileMode(QFileDialog::Directory);
625 dialog.setFilter(tr("Folders (*)"));
626 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
627 dialog.setViewMode(QFileDialog::Detail);
629 if (!dialog.exec()) {
632 QString aTempDir = dialog.selectedFiles().first();
634 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
635 int answer = QMessageBox::question(
637 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
639 tr("The folder already contains some files, save anyway?"),
640 QMessageBox::Save | QMessageBox::Cancel);
641 if (answer == QMessageBox::Cancel) {
645 myCurrentDir = aTempDir;
649 //******************************************************
650 void XGUI_Workshop::onUndo()
652 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
653 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
654 DocumentPtr aDoc = aMgr->rootDocument();
655 if (aDoc->isOperation())
656 operationMgr()->abortOperation();
658 updateCommandStatus();
661 //******************************************************
662 void XGUI_Workshop::onRedo()
664 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
665 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
666 DocumentPtr aDoc = aMgr->rootDocument();
667 if (aDoc->isOperation())
668 operationMgr()->abortOperation();
670 updateCommandStatus();
673 //******************************************************
674 void XGUI_Workshop::onPreferences()
677 XGUI_Preferences::editPreferences(aModif);
678 if (aModif.size() > 0) {
680 foreach (XGUI_Pref aPref, aModif)
682 aSection = aPref.first;
683 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
685 myMainWindow->viewer()->updateFromResources();
686 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
688 myMainWindow->menuObject()->updateFromResources();
694 //******************************************************
695 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
697 QString libName = QString::fromStdString(library(theModule.toStdString()));
698 if (libName.isEmpty()) {
699 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
704 CREATE_FUNC crtInst = 0;
707 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
711 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
712 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
713 QString aMsg((char*) &lpMsgBuf);
714 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
715 ::LocalFree(lpMsgBuf);
717 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
721 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
722 | FORMAT_MESSAGE_IGNORE_INSERTS,
723 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
724 QString aMsg((char*) &lpMsgBuf);
725 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
726 ::LocalFree(lpMsgBuf);
730 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
732 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
734 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
736 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
741 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
743 if (!err.isEmpty()) {
745 QMessageBox::warning(mainWindow(), tr("Error"), err);
747 qWarning(qPrintable(err));
753 //******************************************************
754 bool XGUI_Workshop::activateModule()
756 Config_ModuleReader aModuleReader;
757 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
758 myModule = loadModule(moduleName);
761 myModule->createFeatures();
762 myActionsMgr->update();
766 //******************************************************
767 void XGUI_Workshop::updateCommandStatus()
769 QList<QAction*> aCommands;
770 if (isSalomeMode()) { // update commands in SALOME mode
771 aCommands = salomeConnector()->commandList();
773 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
774 foreach (XGUI_Command* aCmd, aMenuBar->features())
775 aCommands.append(aCmd);
777 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
778 if (aMgr->hasRootDocument()) {
781 foreach(QAction* aCmd, aCommands)
783 QString aId = aCmd->data().toString();
784 if (aId == "UNDO_CMD")
786 else if (aId == "REDO_CMD")
789 // Enable all commands
790 aCmd->setEnabled(true);
792 DocumentPtr aDoc = aMgr->rootDocument();
793 aUndoCmd->setEnabled(aDoc->canUndo());
794 aRedoCmd->setEnabled(aDoc->canRedo());
796 foreach(QAction* aCmd, aCommands)
798 QString aId = aCmd->data().toString();
799 if (aId == "NEW_CMD")
800 aCmd->setEnabled(true);
801 else if (aId == "EXIT_CMD")
802 aCmd->setEnabled(true);
804 aCmd->setEnabled(false);
807 myActionsMgr->update();
810 //******************************************************
811 QList<QAction*> XGUI_Workshop::getModuleCommands() const
813 QList<QAction*> aCommands;
814 if (isSalomeMode()) { // update commands in SALOME mode
815 aCommands = salomeConnector()->commandList();
817 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
818 foreach(XGUI_Command* aCmd, aMenuBar->features())
820 aCommands.append(aCmd);
826 //******************************************************
827 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
829 QDockWidget* aObjDock = new QDockWidget(theParent);
830 //aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
831 aObjDock->setWindowTitle(tr("Object browser"));
832 aObjDock->setStyleSheet(
833 "::title { position: relative; padding-left: 5px; text-align: left center }");
834 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
835 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
836 SLOT(changeCurrentDocument(ObjectPtr)));
837 aObjDock->setWidget(myObjectBrowser);
839 myContextMenuMgr->connectObjectBrowser();
843 //******************************************************
845 * Creates dock widgets, places them in corresponding area
846 * and tabifies if necessary.
848 void XGUI_Workshop::createDockWidgets()
850 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
851 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
852 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
853 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
854 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
855 hidePropertyPanel(); //<! Invisible by default
857 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
859 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
860 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
861 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
862 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
864 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)), myOperationMgr,
865 SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
867 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
868 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
869 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
870 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
871 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
872 SLOT(setAcceptEnabled(bool)));
876 //******************************************************
877 void XGUI_Workshop::showPropertyPanel()
879 QAction* aViewAct = myPropertyPanel->toggleViewAction();
880 //<! Restore ability to close panel from the window's menu
881 aViewAct->setEnabled(true);
882 myPropertyPanel->show();
883 myPropertyPanel->raise();
886 //******************************************************
887 void XGUI_Workshop::hidePropertyPanel()
889 QAction* aViewAct = myPropertyPanel->toggleViewAction();
890 //<! Do not allow to show empty property panel
891 aViewAct->setEnabled(false);
892 myPropertyPanel->hide();
895 //******************************************************
896 void XGUI_Workshop::showObjectBrowser()
898 myObjectBrowser->parentWidget()->show();
901 //******************************************************
902 void XGUI_Workshop::hideObjectBrowser()
904 myObjectBrowser->parentWidget()->hide();
907 //******************************************************
908 void XGUI_Workshop::onFeatureTriggered()
910 QAction* aCmd = dynamic_cast<QAction*>(sender());
912 QString aId = salomeConnector()->commandId(aCmd);
914 myModule->launchOperation(aId);
918 //******************************************************
919 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
921 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
923 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
925 DocumentPtr aPartDoc = aPart->partDoc();
927 aMgr->setCurrentDocument(aPartDoc);
932 aMgr->setCurrentDocument(aMgr->rootDocument());
935 //******************************************************
936 void XGUI_Workshop::salomeViewerSelectionChanged()
938 emit salomeViewerSelection();
941 //**************************************************************
942 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
944 return mySalomeConnector->viewer();
947 //**************************************************************
948 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
950 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
951 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
952 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
954 } else if (theId == "DEACTIVATE_PART_CMD")
955 activatePart(ResultPartPtr());
956 else if (theId == "DELETE_CMD")
957 deleteObjects(aObjects);
958 else if (theId == "SHOW_CMD")
959 showObjects(aObjects, true);
960 else if (theId == "HIDE_CMD")
961 showObjects(aObjects, false);
964 //**************************************************************
965 void XGUI_Workshop::onWidgetValuesChanged()
967 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
968 FeaturePtr aFeature = anOperation->feature();
970 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
972 // aCustom->storeValue(aFeature);
974 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
975 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
976 for (; anIt != aLast; anIt++) {
977 ModuleBase_ModelWidget* aCustom = *anIt;
978 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
979 //aCustom->storeValue(aFeature);
980 aCustom->storeValue();
985 //**************************************************************
986 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
988 changeCurrentDocument(theFeature);
989 myObjectBrowser->activatePart(theFeature);
992 //**************************************************************
993 void XGUI_Workshop::activateLastPart()
995 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
996 DocumentPtr aDoc = aMgr->rootDocument();
997 std::string aGrpName = ModelAPI_ResultPart::group();
998 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
999 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1001 activatePart(aPart);
1004 //**************************************************************
1005 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1007 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1008 QMessageBox::StandardButton aRes = QMessageBox::warning(
1009 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1010 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1011 // ToDo: definbe deleting method
1012 if (aRes == QMessageBox::Yes) {
1013 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1014 aMgr->rootDocument()->startOperation();
1015 foreach (ObjectPtr aObj, theList)
1017 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1019 DocumentPtr aDoc = aPart->document();
1020 if (aDoc == aMgr->currentDocument()) {
1023 //aMgr->rootDocument()->removeFeature(aPart->owner());
1025 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1027 aObj->document()->removeFeature(aFeature);
1030 myDisplayer->updateViewer();
1031 aMgr->rootDocument()->finishOperation();
1035 //**************************************************************
1036 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1038 foreach (ObjectPtr aObj, theList)
1040 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1043 myDisplayer->display(aRes, false);
1045 myDisplayer->erase(aRes, false);
1049 myDisplayer->updateViewer();
1052 //**************************************************************
1053 void XGUI_Workshop::updateCommandsOnViewSelection()
1055 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1056 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1057 XGUI_Selection* aSelection = mySelector->selection();
1058 if (aSelection->getSelected().size() == 0)
1061 QList<QAction*> aActions = getModuleCommands();
1062 foreach(QAction* aAction, aActions)
1064 QString aId = aAction->data().toString();
1065 std::list<ModelAPI_Validator*> aValidators;
1066 std::list<std::list<std::string> > anArguments;
1067 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1068 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1069 for (; aValidator != aValidators.end(); aValidator++) {
1071 const ModuleBase_SelectionValidator* aSelValidator =
1072 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1073 if (aSelValidator) {
1074 aAction->setEnabled(aSelValidator->isValid(aSelection));
1081 //**************************************************************
1082 void XGUI_Workshop::registerValidators() const
1084 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1085 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1088 //**************************************************************
1089 void XGUI_Workshop::displayAllResults()
1091 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1092 DocumentPtr aRootDoc = aMgr->rootDocument();
1093 displayDocumentResults(aRootDoc);
1094 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1095 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1096 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1097 displayDocumentResults(aPart->partDoc());
1099 myDisplayer->updateViewer();
1102 //**************************************************************
1103 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1105 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1106 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1109 //**************************************************************
1110 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1112 for (int i = 0; i < theDoc->size(theGroup); i++)
1113 myDisplayer->display(theDoc->object(theGroup, i), false);