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_ResultValidators.h>
47 #include "ModuleBase_WidgetFactory.h"
49 #include <Config_Common.h>
50 #include <Config_FeatureMessage.h>
51 #include <Config_PointerMessage.h>
52 #include <Config_ModuleReader.h>
54 #include <QApplication>
55 #include <QFileDialog>
56 #include <QMessageBox>
57 #include <QMdiSubWindow>
58 #include <QPushButton>
59 #include <QDockWidget>
73 QMap<QString, QString> XGUI_Workshop::myIcons;
75 QString XGUI_Workshop::featureIcon(const std::string& theId)
77 QString aId(theId.c_str());
78 if (myIcons.contains(aId))
83 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
85 myCurrentDir(QString()),
87 mySalomeConnector(theConnector),
92 myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
94 myDisplayer = new XGUI_Displayer(this);
96 mySelector = new XGUI_SelectionMgr(this);
97 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
99 myOperationMgr = new XGUI_OperationMgr(this);
100 myActionsMgr = new XGUI_ActionsMgr(this);
101 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
102 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
103 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
104 SLOT(onContextMenuCommand(const QString&, bool)));
106 myViewerProxy = new XGUI_ViewerProxy(this);
107 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
109 myModuleConnector = new XGUI_ModuleConnector(this);
111 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
112 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
113 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
114 SLOT(onOperationStopped(ModuleBase_Operation*)));
115 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
116 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
117 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
119 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
122 //******************************************************
123 XGUI_Workshop::~XGUI_Workshop(void)
127 //******************************************************
128 void XGUI_Workshop::startApplication()
131 //Initialize event listening
132 Events_Loop* aLoop = Events_Loop::loop();
133 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
134 //TODO(sbh): Implement static method to extract event id [SEID]
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
136 // TODO Is it good to use non standard event within workshop?
137 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
138 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
141 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
142 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
144 registerValidators();
147 myMainWindow->show();
148 updateCommandStatus();
150 XGUI_Preferences::loadCustomProps();
154 //******************************************************
155 void XGUI_Workshop::initMenu()
157 myContextMenuMgr->createActions();
159 if (isSalomeMode()) {
160 // Create only Undo, Redo commands
161 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
162 tr("Undo last command"),
163 QIcon(":pictures/undo.png"),
164 QKeySequence::Undo, false);
165 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
166 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
167 QIcon(":pictures/redo.png"), QKeySequence::Redo,
169 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
170 salomeConnector()->addEditMenuSeparator();
173 // File commands group
174 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
176 XGUI_Command* aCommand;
178 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
179 QIcon(":pictures/save.png"), QKeySequence::Save);
180 aCommand->connectTo(this, SLOT(onSave()));
181 //aCommand->disable();
183 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
184 QIcon(":pictures/undo.png"), QKeySequence::Undo);
185 aCommand->connectTo(this, SLOT(onUndo()));
187 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
188 QIcon(":pictures/redo.png"), QKeySequence::Redo);
189 aCommand->connectTo(this, SLOT(onRedo()));
191 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
192 QIcon(":pictures/rebuild.png"));
194 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
195 QIcon(":pictures/save.png"));
196 aCommand->connectTo(this, SLOT(onSaveAs()));
197 //aCommand->disable();
199 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
200 QIcon(":pictures/open.png"), QKeySequence::Open);
201 aCommand->connectTo(this, SLOT(onOpen()));
203 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
204 // QIcon(":pictures/new.png"), QKeySequence::New);
205 //aCommand->connectTo(this, SLOT(onNew()));
207 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
208 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
209 aCommand->connectTo(this, SLOT(onPreferences()));
211 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
212 QIcon(":pictures/close.png"), QKeySequence::Close);
213 aCommand->connectTo(this, SLOT(onExit()));
214 //FIXME: SBH's test action. Can be used for some GUI tests.
216 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
217 // QIcon(":pictures/close.png"), QKeySequence(), true);
218 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
222 //******************************************************
223 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
225 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
226 return aMenuBar->addWorkbench(theName);
229 //******************************************************
230 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
232 //A message to start feature creation received.
233 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
234 const Config_FeatureMessage* aFeatureMsg =
235 dynamic_cast<const Config_FeatureMessage*>(theMessage);
236 if (!aFeatureMsg->isInternal()) {
237 addFeature(aFeatureMsg);
242 // Process creation of Part
243 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
244 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
245 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
246 onFeatureCreatedMsg(aUpdMsg);
251 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
252 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
253 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 =
286 dynamic_cast<const Config_PointerMessage*>(theMessage);
287 //myPropertyPanel->cleanContent();
288 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
290 if (myOperationMgr->startOperation(anOperation)) {
291 myPropertyPanel->updateContentWidget(anOperation->feature());
292 if (!anOperation->getDescription()->hasXmlRepresentation()) {
293 if (anOperation->commit())
294 updateCommandStatus();
299 //Show error dialog if error message received.
300 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
302 emit errorOccurred(QString::fromLatin1(anAppError->description()));
306 //******************************************************
307 void XGUI_Workshop::onStartWaiting()
309 if (Events_LongOp::isPerformed()) {
310 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
314 //******************************************************
315 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
317 std::set<ObjectPtr> aFeatures = theMsg->objects();
318 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);
329 myOperationMgr->validateCurrentOperation();
332 //******************************************************
333 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
335 std::set<ObjectPtr> aObjects = theMsg->objects();
336 std::set<ObjectPtr>::const_iterator aIt;
337 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
338 ObjectPtr aObj = (*aIt);
339 if (!aObj->data() || !aObj->data()->isValid())
340 myDisplayer->erase(aObj, false);
342 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
343 myDisplayer->display(aObj, false); // In order to update presentation
345 if (myOperationMgr->hasOperation()) {
346 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
347 if (aOperation->hasObject(aObj)) { // Display only current operation results
348 myDisplayer->display(aObj, false);
354 myDisplayer->updateViewer();
357 //******************************************************
358 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
360 std::set<ObjectPtr> aObjects = theMsg->objects();
362 std::set<ObjectPtr>::const_iterator aIt;
363 bool aHasPart = false;
364 bool isDisplayed = false;
365 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
366 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
369 // If a feature is created from the aplication's python console
370 // it doesn't stored in the operation mgr and doesn't displayed
371 } else if (myOperationMgr->hasOperation()) {
372 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
373 if (aOperation->hasObject(*aIt)) { // Display only current operation results
374 myDisplayer->display(*aIt, false);
380 myDisplayer->updateViewer();
382 //The created part will be created in Object Browser later and we have to activate it
383 // only when it is created everywere
384 QTimer::singleShot(50, this, SLOT(activateLastPart()));
388 //******************************************************
389 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
391 //std::set<ObjectPtr> aFeatures = theMsg->objects();
394 //******************************************************
395 void XGUI_Workshop::onOperationStarted()
397 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
398 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
400 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
401 connectWithOperation(aOperation);
404 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
405 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
408 myPropertyPanel->cleanContent();
409 aFactory.createWidget(myPropertyPanel->contentWidget());
411 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
412 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
413 ModuleBase_ModelWidget* aWidget;
414 for (; anIt != aLast; anIt++) {
416 aWidget->setFeature(aOperation->feature());
417 //QObject::connect(aWidget, SIGNAL(valuesChanged()), aOperation, SLOT(storeCustomValue()));
418 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
419 // Init default values
420 if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
421 //aWidget->storeValue(aOperation->feature());
423 aWidget->storeValue();
427 myPropertyPanel->setModelWidgets(aWidgets);
428 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
430 updateCommandStatus();
433 //******************************************************
434 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
436 //!< No need for property panel
437 updateCommandStatus();
439 myPropertyPanel->cleanContent();
445 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
449 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
453 // Remember features icons
454 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
456 //Find or create Workbench
457 QString aWchName = QString::fromStdString(theMessage->workbenchId());
458 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
459 bool isUsePropPanel = theMessage->isUseInput();
460 QString aFeatureId = QString::fromStdString(theMessage->id());
461 if (isSalomeMode()) {
462 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
463 QString::fromStdString(theMessage->text()),
464 QString::fromStdString(theMessage->tooltip()),
465 QIcon(theMessage->icon().c_str()),
466 QKeySequence(), isUsePropPanel);
467 salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" "));
468 myActionsMgr->addCommand(aAction);
469 myModule->featureCreated(aAction);
472 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
473 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
475 aPage = addWorkbench(aWchName);
477 //Find or create Group
478 QString aGroupName = QString::fromStdString(theMessage->groupId());
479 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
481 aGroup = aPage->addGroup(aGroupName);
483 // Check if hotkey sequence is already defined:
484 QKeySequence aHotKey = myActionsMgr->registerShortcut(
485 QString::fromStdString(theMessage->keysequence()));
487 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
488 QString::fromStdString(theMessage->text()),
489 QString::fromStdString(theMessage->tooltip()),
490 QIcon(theMessage->icon().c_str()), aHotKey,
492 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
493 myActionsMgr->addCommand(aCommand);
494 myModule->featureCreated(aCommand);
499 * Makes a signal/slot connections between Property Panel
500 * and given operation. The given operation becomes a
501 * current operation and previous operation if exists
503 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
505 QAction* aCommand = 0;
506 if (isSalomeMode()) {
507 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
509 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
510 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
512 //Abort operation on uncheck the command
514 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
518 * Saves document with given name.
520 void XGUI_Workshop::saveDocument(QString theName)
522 QApplication::restoreOverrideCursor();
523 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
524 DocumentPtr aDoc = aMgr->rootDocument();
525 aDoc->save(theName.toLatin1().constData());
526 QApplication::restoreOverrideCursor();
529 //******************************************************
530 void XGUI_Workshop::onExit()
532 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
533 DocumentPtr aDoc = aMgr->rootDocument();
534 if (aDoc->isModified()) {
535 int anAnswer = QMessageBox::question(
536 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
537 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
538 if (anAnswer == QMessageBox::Save) {
539 bool saved = onSave();
543 } else if (anAnswer == QMessageBox::Cancel) {
550 //******************************************************
551 void XGUI_Workshop::onNew()
553 QApplication::setOverrideCursor(Qt::WaitCursor);
554 if (objectBrowser() == 0) {
556 mySelector->connectViewers();
558 myViewerProxy->connectToViewer();
560 if (!isSalomeMode()) {
561 myMainWindow->showPythonConsole();
562 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
563 aWnd->showMaximized();
564 updateCommandStatus();
566 myContextMenuMgr->connectViewer();
567 QApplication::restoreOverrideCursor();
570 //******************************************************
571 void XGUI_Workshop::onOpen()
573 //save current file before close if modified
574 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
575 DocumentPtr aDoc = aMgr->rootDocument();
576 if (aDoc->isModified()) {
577 //TODO(sbh): re-launch the app?
578 int anAnswer = QMessageBox::question(
579 myMainWindow, tr("Save current file"),
580 tr("The document is modified, save before opening another?"),
581 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
582 if (anAnswer == QMessageBox::Save) {
584 } else if (anAnswer == QMessageBox::Cancel) {
591 //show file dialog, check if readable and open
592 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
593 if (myCurrentDir.isEmpty())
595 QFileInfo aFileInfo(myCurrentDir);
596 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
597 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
601 QApplication::setOverrideCursor(Qt::WaitCursor);
602 aDoc->load(myCurrentDir.toLatin1().constData());
603 myObjectBrowser->rebuildDataTree();
605 updateCommandStatus();
606 QApplication::restoreOverrideCursor();
609 //******************************************************
610 bool XGUI_Workshop::onSave()
612 if (myCurrentDir.isEmpty()) {
615 saveDocument(myCurrentDir);
616 updateCommandStatus();
620 //******************************************************
621 bool XGUI_Workshop::onSaveAs()
623 QFileDialog dialog(mainWindow());
624 dialog.setWindowTitle(tr("Select directory to save files..."));
625 dialog.setFileMode(QFileDialog::Directory);
626 dialog.setFilter(tr("Folders (*)"));
627 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
628 dialog.setViewMode(QFileDialog::Detail);
630 if (!dialog.exec()) {
633 QString aTempDir = dialog.selectedFiles().first();
635 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
636 int answer = QMessageBox::question(
638 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
640 tr("The folder already contains some files, save anyway?"),
641 QMessageBox::Save | QMessageBox::Cancel);
642 if (answer == QMessageBox::Cancel) {
646 myCurrentDir = aTempDir;
650 //******************************************************
651 void XGUI_Workshop::onUndo()
653 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
654 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
655 DocumentPtr aDoc = aMgr->rootDocument();
656 if (aDoc->isOperation())
657 operationMgr()->abortOperation();
659 updateCommandStatus();
662 //******************************************************
663 void XGUI_Workshop::onRedo()
665 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
666 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
667 DocumentPtr aDoc = aMgr->rootDocument();
668 if (aDoc->isOperation())
669 operationMgr()->abortOperation();
671 updateCommandStatus();
674 //******************************************************
675 void XGUI_Workshop::onPreferences()
678 XGUI_Preferences::editPreferences(aModif);
679 if (aModif.size() > 0) {
681 foreach (XGUI_Pref aPref, aModif)
683 aSection = aPref.first;
684 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
686 myMainWindow->viewer()->updateFromResources();
687 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
689 myMainWindow->menuObject()->updateFromResources();
695 //******************************************************
696 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
698 QString libName = QString::fromStdString(library(theModule.toStdString()));
699 if (libName.isEmpty()) {
700 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
705 CREATE_FUNC crtInst = 0;
708 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
712 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
713 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
714 QString aMsg((char*) &lpMsgBuf);
715 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
716 ::LocalFree(lpMsgBuf);
718 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
722 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
723 | FORMAT_MESSAGE_IGNORE_INSERTS,
724 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
725 QString aMsg((char*) &lpMsgBuf);
726 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
727 ::LocalFree(lpMsgBuf);
731 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
733 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
735 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
737 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
742 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
744 if (!err.isEmpty()) {
746 QMessageBox::warning(mainWindow(), tr("Error"), err);
748 qWarning(qPrintable(err));
754 //******************************************************
755 bool XGUI_Workshop::activateModule()
757 Config_ModuleReader aModuleReader;
758 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
759 myModule = loadModule(moduleName);
762 myModule->createFeatures();
763 myActionsMgr->update();
767 //******************************************************
768 void XGUI_Workshop::updateCommandStatus()
770 QList<QAction*> aCommands;
771 if (isSalomeMode()) { // update commands in SALOME mode
772 aCommands = salomeConnector()->commandList();
774 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
775 foreach (XGUI_Command* aCmd, aMenuBar->features())
776 aCommands.append(aCmd);
778 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
779 if (aMgr->hasRootDocument()) {
782 foreach(QAction* aCmd, aCommands)
784 QString aId = aCmd->data().toString();
785 if (aId == "UNDO_CMD")
787 else if (aId == "REDO_CMD")
790 // Enable all commands
791 aCmd->setEnabled(true);
793 DocumentPtr aDoc = aMgr->rootDocument();
794 aUndoCmd->setEnabled(aDoc->canUndo());
795 aRedoCmd->setEnabled(aDoc->canRedo());
797 foreach(QAction* aCmd, aCommands)
799 QString aId = aCmd->data().toString();
800 if (aId == "NEW_CMD")
801 aCmd->setEnabled(true);
802 else if (aId == "EXIT_CMD")
803 aCmd->setEnabled(true);
805 aCmd->setEnabled(false);
808 myActionsMgr->update();
811 //******************************************************
812 QList<QAction*> XGUI_Workshop::getModuleCommands() const
814 QList<QAction*> aCommands;
815 if (isSalomeMode()) { // update commands in SALOME mode
816 aCommands = salomeConnector()->commandList();
818 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
819 foreach(XGUI_Command* aCmd, aMenuBar->features())
821 aCommands.append(aCmd);
827 //******************************************************
828 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
830 QDockWidget* aObjDock = new QDockWidget(theParent);
831 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
832 aObjDock->setWindowTitle(tr("Object browser"));
833 aObjDock->setStyleSheet(
834 "::title { position: relative; padding-left: 5px; text-align: left center }");
835 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
836 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
837 SLOT(changeCurrentDocument(ObjectPtr)));
838 aObjDock->setWidget(myObjectBrowser);
840 myContextMenuMgr->connectObjectBrowser();
844 //******************************************************
846 * Creates dock widgets, places them in corresponding area
847 * and tabifies if necessary.
849 void XGUI_Workshop::createDockWidgets()
851 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
852 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
853 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
854 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
855 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
856 hidePropertyPanel(); //<! Invisible by default
858 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
860 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
861 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
862 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
863 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
865 connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)), myOperationMgr,
866 SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
868 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
869 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
870 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
871 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
872 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
873 SLOT(setAcceptEnabled(bool)));
877 //******************************************************
878 void XGUI_Workshop::showPropertyPanel()
880 QAction* aViewAct = myPropertyPanel->toggleViewAction();
881 //<! Restore ability to close panel from the window's menu
882 aViewAct->setEnabled(true);
883 myPropertyPanel->show();
884 myPropertyPanel->raise();
887 //******************************************************
888 void XGUI_Workshop::hidePropertyPanel()
890 QAction* aViewAct = myPropertyPanel->toggleViewAction();
891 //<! Do not allow to show empty property panel
892 aViewAct->setEnabled(false);
893 myPropertyPanel->hide();
896 //******************************************************
897 void XGUI_Workshop::showObjectBrowser()
899 myObjectBrowser->parentWidget()->show();
902 //******************************************************
903 void XGUI_Workshop::hideObjectBrowser()
905 myObjectBrowser->parentWidget()->hide();
908 //******************************************************
909 void XGUI_Workshop::onFeatureTriggered()
911 QAction* aCmd = dynamic_cast<QAction*>(sender());
913 QString aId = salomeConnector()->commandId(aCmd);
915 myModule->launchOperation(aId);
919 //******************************************************
920 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
922 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
924 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
926 DocumentPtr aPartDoc = aPart->partDoc();
928 aMgr->setCurrentDocument(aPartDoc);
933 aMgr->setCurrentDocument(aMgr->rootDocument());
936 //******************************************************
937 void XGUI_Workshop::salomeViewerSelectionChanged()
939 emit salomeViewerSelection();
942 //**************************************************************
943 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
945 return mySalomeConnector->viewer();
948 //**************************************************************
949 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
951 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
952 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
953 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
955 } else if (theId == "DEACTIVATE_PART_CMD")
956 activatePart(ResultPartPtr());
957 else if (theId == "DELETE_CMD")
958 deleteObjects(aObjects);
959 else if (theId == "SHOW_CMD")
960 showObjects(aObjects, true);
961 else if (theId == "HIDE_CMD")
962 showObjects(aObjects, false);
965 //**************************************************************
966 void XGUI_Workshop::onWidgetValuesChanged()
968 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
969 FeaturePtr aFeature = anOperation->feature();
971 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
973 // aCustom->storeValue(aFeature);
975 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
976 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
977 for (; anIt != aLast; anIt++) {
978 ModuleBase_ModelWidget* aCustom = *anIt;
979 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
980 //aCustom->storeValue(aFeature);
981 aCustom->storeValue();
986 //**************************************************************
987 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
989 changeCurrentDocument(theFeature);
990 myObjectBrowser->activatePart(theFeature);
993 //**************************************************************
994 void XGUI_Workshop::activateLastPart()
996 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
997 DocumentPtr aDoc = aMgr->rootDocument();
998 std::string aGrpName = ModelAPI_ResultPart::group();
999 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1000 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1002 activatePart(aPart);
1005 //**************************************************************
1006 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1008 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1009 QMessageBox::StandardButton aRes = QMessageBox::warning(
1010 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1011 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1012 // ToDo: definbe deleting method
1013 if (aRes == QMessageBox::Yes) {
1014 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1015 aMgr->rootDocument()->startOperation();
1016 foreach (ObjectPtr aObj, theList)
1018 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1020 DocumentPtr aDoc = aPart->document();
1021 if (aDoc == aMgr->currentDocument()) {
1024 //aMgr->rootDocument()->removeFeature(aPart->owner());
1026 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1028 aObj->document()->removeFeature(aFeature);
1031 myDisplayer->updateViewer();
1032 aMgr->rootDocument()->finishOperation();
1036 //**************************************************************
1037 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1039 foreach (ObjectPtr aObj, theList)
1041 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1044 myDisplayer->display(aRes, false);
1046 myDisplayer->erase(aRes, false);
1050 myDisplayer->updateViewer();
1053 //**************************************************************
1054 void XGUI_Workshop::updateCommandsOnViewSelection()
1056 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1057 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1058 XGUI_Selection* aSelection = mySelector->selection();
1059 if (aSelection->getSelected().size() == 0)
1062 QList<QAction*> aActions = getModuleCommands();
1063 foreach(QAction* aAction, aActions)
1065 QString aId = aAction->data().toString();
1066 std::list<ModelAPI_Validator*> aValidators;
1067 aFactory->validators(aId.toStdString(), aValidators);
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);