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),
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"));
143 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
145 registerValidators();
148 myMainWindow->show();
149 updateCommandStatus();
151 XGUI_Preferences::loadCustomProps();
155 //******************************************************
156 void XGUI_Workshop::initMenu()
158 myContextMenuMgr->createActions();
160 if (isSalomeMode()) {
161 // Create only Undo, Redo commands
162 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
163 tr("Undo last command"),
164 QIcon(":pictures/undo.png"),
165 QKeySequence::Undo, false);
166 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
167 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
168 QIcon(":pictures/redo.png"), QKeySequence::Redo,
170 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
171 salomeConnector()->addEditMenuSeparator();
174 // File commands group
175 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
177 XGUI_Command* aCommand;
179 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
180 QIcon(":pictures/save.png"), QKeySequence::Save);
181 aCommand->connectTo(this, SLOT(onSave()));
182 //aCommand->disable();
184 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
185 QIcon(":pictures/undo.png"), QKeySequence::Undo);
186 aCommand->connectTo(this, SLOT(onUndo()));
188 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
189 QIcon(":pictures/redo.png"), QKeySequence::Redo);
190 aCommand->connectTo(this, SLOT(onRedo()));
192 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
193 QIcon(":pictures/rebuild.png"));
195 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
196 QIcon(":pictures/save.png"));
197 aCommand->connectTo(this, SLOT(onSaveAs()));
198 //aCommand->disable();
200 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
201 QIcon(":pictures/open.png"), QKeySequence::Open);
202 aCommand->connectTo(this, SLOT(onOpen()));
204 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
205 // QIcon(":pictures/new.png"), QKeySequence::New);
206 //aCommand->connectTo(this, SLOT(onNew()));
208 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
209 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
210 aCommand->connectTo(this, SLOT(onPreferences()));
212 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
213 QIcon(":pictures/close.png"), QKeySequence::Close);
214 aCommand->connectTo(this, SLOT(onExit()));
215 //FIXME: SBH's test action. Can be used for some GUI tests.
217 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
218 // QIcon(":pictures/close.png"), QKeySequence(), true);
219 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
223 //******************************************************
224 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
226 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
227 return aMenuBar->addWorkbench(theName);
230 //******************************************************
231 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
233 //A message to start feature creation received.
234 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
235 const Config_FeatureMessage* aFeatureMsg =
236 dynamic_cast<const Config_FeatureMessage*>(theMessage);
237 if (!aFeatureMsg->isInternal()) {
238 addFeature(aFeatureMsg);
243 // Process creation of Part
244 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
245 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
246 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
247 onFeatureCreatedMsg(aUpdMsg);
249 if (mySalomeConnector)
250 mySalomeConnector->createPreferences();
251 myUpdatePrefs = false;
255 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
256 myUpdatePrefs = true;
261 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
262 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
263 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
264 onFeatureRedisplayMsg(aUpdMsg);
268 //Update property panel on corresponding message. If there is no current operation (no
269 //property panel), or received message has different feature to the current - do nothing.
270 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
271 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
272 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
273 onFeatureUpdatedMsg(anUpdateMsg);
277 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
278 const ModelAPI_ObjectDeletedMessage* aDelMsg =
279 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
280 onObjectDeletedMsg(aDelMsg);
284 if (theMessage->eventID() == Events_LongOp::eventID()) {
285 if (Events_LongOp::isPerformed())
286 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
287 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
289 QApplication::restoreOverrideCursor();
293 //An operation passed by message. Start it, process and commit.
294 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
295 const Config_PointerMessage* aPartSetMsg =
296 dynamic_cast<const Config_PointerMessage*>(theMessage);
297 //myPropertyPanel->cleanContent();
298 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
300 if (myOperationMgr->startOperation(anOperation)) {
301 myPropertyPanel->updateContentWidget(anOperation->feature());
302 if (!anOperation->getDescription()->hasXmlRepresentation()) {
303 if (anOperation->commit())
304 updateCommandStatus();
309 //Show error dialog if error message received.
310 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
312 emit errorOccurred(QString::fromLatin1(anAppError->description()));
317 //******************************************************
318 void XGUI_Workshop::onStartWaiting()
320 if (Events_LongOp::isPerformed()) {
321 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
325 //******************************************************
326 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
328 std::set<ObjectPtr> aFeatures = theMsg->objects();
329 if (myOperationMgr->hasOperation()) {
330 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
331 std::set<ObjectPtr>::const_iterator aIt;
332 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
333 ObjectPtr aNewFeature = (*aIt);
334 if (aNewFeature == aCurrentFeature) {
335 myPropertyPanel->updateContentWidget(aCurrentFeature);
340 myOperationMgr->validateCurrentOperation();
343 //******************************************************
344 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
346 std::set<ObjectPtr> aObjects = theMsg->objects();
347 std::set<ObjectPtr>::const_iterator aIt;
348 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
349 ObjectPtr aObj = (*aIt);
350 if (!aObj->data() || !aObj->data()->isValid())
351 myDisplayer->erase(aObj, false);
353 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
354 myDisplayer->display(aObj, false); // In order to update presentation
356 if (myOperationMgr->hasOperation()) {
357 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
358 if (aOperation->hasObject(aObj)) { // Display only current operation results
359 myDisplayer->display(aObj, false);
365 myDisplayer->updateViewer();
368 //******************************************************
369 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
371 std::set<ObjectPtr> aObjects = theMsg->objects();
373 std::set<ObjectPtr>::const_iterator aIt;
374 bool aHasPart = false;
375 bool isDisplayed = false;
376 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
377 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
380 // If a feature is created from the aplication's python console
381 // it doesn't stored in the operation mgr and doesn't displayed
382 } else if (myOperationMgr->hasOperation()) {
383 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
384 if (aOperation->hasObject(*aIt)) { // Display only current operation results
385 myDisplayer->display(*aIt, false);
391 myDisplayer->updateViewer();
393 //The created part will be created in Object Browser later and we have to activate it
394 // only when it is created everywere
395 QTimer::singleShot(50, this, SLOT(activateLastPart()));
399 //******************************************************
400 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
402 //std::set<ObjectPtr> aFeatures = theMsg->objects();
405 //******************************************************
406 void XGUI_Workshop::onOperationStarted()
408 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
409 if (this->isSalomeMode())
410 aOperation->setNestedFeatures(mySalomeConnector->nestedActions(aOperation->id()));
412 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
414 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
415 connectWithOperation(aOperation);
418 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
419 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
422 myPropertyPanel->cleanContent();
423 aFactory.createWidget(myPropertyPanel->contentWidget());
425 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
426 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
427 ModuleBase_ModelWidget* aWidget;
428 for (; anIt != aLast; anIt++) {
430 aWidget->setFeature(aOperation->feature());
431 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
432 // Init default values
433 if (!aOperation->isEditOperation() && !aWidget->isComputedDefault()) {
434 aWidget->storeValue();
438 myPropertyPanel->setModelWidgets(aWidgets);
439 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
441 updateCommandStatus();
444 //******************************************************
445 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
447 //!< No need for property panel
448 updateCommandStatus();
450 myPropertyPanel->cleanContent();
456 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
460 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
464 // Remember features icons
465 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
467 //Find or create Workbench
468 QString aWchName = QString::fromStdString(theMessage->workbenchId());
469 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
470 bool isUsePropPanel = theMessage->isUseInput();
471 QString aFeatureId = QString::fromStdString(theMessage->id());
472 if (isSalomeMode()) {
473 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
474 QString::fromStdString(theMessage->text()),
475 QString::fromStdString(theMessage->tooltip()),
476 QIcon(theMessage->icon().c_str()),
477 QKeySequence(), isUsePropPanel);
478 salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
479 myActionsMgr->addCommand(aAction);
480 myModule->featureCreated(aAction);
483 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
484 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
486 aPage = addWorkbench(aWchName);
488 //Find or create Group
489 QString aGroupName = QString::fromStdString(theMessage->groupId());
490 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
492 aGroup = aPage->addGroup(aGroupName);
494 // Check if hotkey sequence is already defined:
495 QKeySequence aHotKey = myActionsMgr->registerShortcut(
496 QString::fromStdString(theMessage->keysequence()));
498 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
499 QString::fromStdString(theMessage->text()),
500 QString::fromStdString(theMessage->tooltip()),
501 QIcon(theMessage->icon().c_str()), aHotKey,
503 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
504 myActionsMgr->addCommand(aCommand);
505 myModule->featureCreated(aCommand);
510 * Makes a signal/slot connections between Property Panel
511 * and given operation. The given operation becomes a
512 * current operation and previous operation if exists
514 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
516 QAction* aCommand = 0;
517 if (isSalomeMode()) {
518 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
520 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
521 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
523 //Abort operation on uncheck the command
525 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
529 * Saves document with given name.
531 void XGUI_Workshop::saveDocument(QString theName)
533 QApplication::restoreOverrideCursor();
534 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
535 DocumentPtr aDoc = aMgr->rootDocument();
536 std::list<std::string> aFileNames;
537 aDoc->save(theName.toLatin1().constData(), aFileNames);
538 QApplication::restoreOverrideCursor();
541 //******************************************************
542 void XGUI_Workshop::onExit()
544 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
545 DocumentPtr aDoc = aMgr->rootDocument();
546 if (aDoc->isModified()) {
547 int anAnswer = QMessageBox::question(
548 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
549 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
550 if (anAnswer == QMessageBox::Save) {
551 bool saved = onSave();
555 } else if (anAnswer == QMessageBox::Cancel) {
562 //******************************************************
563 void XGUI_Workshop::onNew()
565 QApplication::setOverrideCursor(Qt::WaitCursor);
566 if (objectBrowser() == 0) {
568 mySelector->connectViewers();
570 myViewerProxy->connectToViewer();
572 if (!isSalomeMode()) {
573 myMainWindow->showPythonConsole();
574 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
575 aWnd->showMaximized();
576 updateCommandStatus();
578 myContextMenuMgr->connectViewer();
579 QApplication::restoreOverrideCursor();
582 //******************************************************
583 void XGUI_Workshop::onOpen()
585 //save current file before close if modified
586 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
587 DocumentPtr aDoc = aMgr->rootDocument();
588 if (aDoc->isModified()) {
589 //TODO(sbh): re-launch the app?
590 int anAnswer = QMessageBox::question(
591 myMainWindow, tr("Save current file"),
592 tr("The document is modified, save before opening another?"),
593 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
594 if (anAnswer == QMessageBox::Save) {
596 } else if (anAnswer == QMessageBox::Cancel) {
603 //show file dialog, check if readable and open
604 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
605 if (myCurrentDir.isEmpty())
607 QFileInfo aFileInfo(myCurrentDir);
608 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
609 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
613 QApplication::setOverrideCursor(Qt::WaitCursor);
614 aDoc->load(myCurrentDir.toLatin1().constData());
615 myObjectBrowser->rebuildDataTree();
617 updateCommandStatus();
618 QApplication::restoreOverrideCursor();
621 //******************************************************
622 bool XGUI_Workshop::onSave()
624 if (myCurrentDir.isEmpty()) {
627 saveDocument(myCurrentDir);
628 updateCommandStatus();
632 //******************************************************
633 bool XGUI_Workshop::onSaveAs()
635 QFileDialog dialog(mainWindow());
636 dialog.setWindowTitle(tr("Select directory to save files..."));
637 dialog.setFileMode(QFileDialog::Directory);
638 dialog.setFilter(tr("Folders (*)"));
639 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
640 dialog.setViewMode(QFileDialog::Detail);
642 if (!dialog.exec()) {
645 QString aTempDir = dialog.selectedFiles().first();
647 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
648 int answer = QMessageBox::question(
650 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
652 tr("The folder already contains some files, save anyway?"),
653 QMessageBox::Save | QMessageBox::Cancel);
654 if (answer == QMessageBox::Cancel) {
658 myCurrentDir = aTempDir;
662 //******************************************************
663 void XGUI_Workshop::onUndo()
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::onRedo()
677 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
678 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
679 DocumentPtr aDoc = aMgr->rootDocument();
680 if (aDoc->isOperation())
681 operationMgr()->abortOperation();
683 updateCommandStatus();
686 //******************************************************
687 void XGUI_Workshop::onPreferences()
690 XGUI_Preferences::editPreferences(aModif);
691 if (aModif.size() > 0) {
693 foreach (XGUI_Pref aPref, aModif)
695 aSection = aPref.first;
696 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
698 myMainWindow->viewer()->updateFromResources();
699 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
701 myMainWindow->menuObject()->updateFromResources();
707 //******************************************************
708 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
710 QString libName = QString::fromStdString(library(theModule.toStdString()));
711 if (libName.isEmpty()) {
712 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
717 CREATE_FUNC crtInst = 0;
720 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
724 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
725 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
726 QString aMsg((char*) &lpMsgBuf);
727 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
728 ::LocalFree(lpMsgBuf);
730 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
734 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
735 | FORMAT_MESSAGE_IGNORE_INSERTS,
736 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
737 QString aMsg((char*) &lpMsgBuf);
738 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
739 ::LocalFree(lpMsgBuf);
743 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
745 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
747 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
749 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
754 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
756 if (!err.isEmpty()) {
758 QMessageBox::warning(mainWindow(), tr("Error"), err);
760 qWarning(qPrintable(err));
766 //******************************************************
767 bool XGUI_Workshop::activateModule()
769 Config_ModuleReader aModuleReader;
770 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
771 myModule = loadModule(moduleName);
774 myModule->createFeatures();
775 myActionsMgr->update();
779 //******************************************************
780 void XGUI_Workshop::updateCommandStatus()
782 QList<QAction*> aCommands;
783 if (isSalomeMode()) { // update commands in SALOME mode
784 aCommands = salomeConnector()->commandList();
786 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
787 foreach (XGUI_Command* aCmd, aMenuBar->features())
788 aCommands.append(aCmd);
790 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
791 if (aMgr->hasRootDocument()) {
794 foreach(QAction* aCmd, aCommands)
796 QString aId = aCmd->data().toString();
797 if (aId == "UNDO_CMD")
799 else if (aId == "REDO_CMD")
802 // Enable all commands
803 aCmd->setEnabled(true);
805 DocumentPtr aDoc = aMgr->rootDocument();
806 aUndoCmd->setEnabled(aDoc->canUndo());
807 aRedoCmd->setEnabled(aDoc->canRedo());
809 foreach(QAction* aCmd, aCommands)
811 QString aId = aCmd->data().toString();
812 if (aId == "NEW_CMD")
813 aCmd->setEnabled(true);
814 else if (aId == "EXIT_CMD")
815 aCmd->setEnabled(true);
817 aCmd->setEnabled(false);
820 myActionsMgr->update();
823 //******************************************************
824 QList<QAction*> XGUI_Workshop::getModuleCommands() const
826 QList<QAction*> aCommands;
827 if (isSalomeMode()) { // update commands in SALOME mode
828 aCommands = salomeConnector()->commandList();
830 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
831 foreach(XGUI_Command* aCmd, aMenuBar->features())
833 aCommands.append(aCmd);
839 //******************************************************
840 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
842 QDockWidget* aObjDock = new QDockWidget(theParent);
843 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
844 aObjDock->setWindowTitle(tr("Object browser"));
845 aObjDock->setStyleSheet(
846 "::title { position: relative; padding-left: 5px; text-align: left center }");
847 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
848 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
849 SLOT(changeCurrentDocument(ObjectPtr)));
850 aObjDock->setWidget(myObjectBrowser);
852 myContextMenuMgr->connectObjectBrowser();
856 //******************************************************
858 * Creates dock widgets, places them in corresponding area
859 * and tabifies if necessary.
861 void XGUI_Workshop::createDockWidgets()
863 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
864 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
865 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
866 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
867 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
868 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
869 hidePropertyPanel(); //<! Invisible by default
871 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
873 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
874 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
875 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
876 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
877 //TODO(sbh): KeyReleasedProblem
878 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
879 SLOT(onKeyReleased(QKeyEvent*)));
881 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
882 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
883 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
884 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
885 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
886 SLOT(setAcceptEnabled(bool)));
890 //******************************************************
891 void XGUI_Workshop::showPropertyPanel()
893 QAction* aViewAct = myPropertyPanel->toggleViewAction();
894 //<! Restore ability to close panel from the window's menu
895 aViewAct->setEnabled(true);
896 myPropertyPanel->show();
897 myPropertyPanel->raise();
900 //******************************************************
901 void XGUI_Workshop::hidePropertyPanel()
903 QAction* aViewAct = myPropertyPanel->toggleViewAction();
904 //<! Do not allow to show empty property panel
905 aViewAct->setEnabled(false);
906 myPropertyPanel->hide();
909 //******************************************************
910 void XGUI_Workshop::showObjectBrowser()
912 myObjectBrowser->parentWidget()->show();
915 //******************************************************
916 void XGUI_Workshop::hideObjectBrowser()
918 myObjectBrowser->parentWidget()->hide();
921 //******************************************************
922 void XGUI_Workshop::onFeatureTriggered()
924 QAction* aCmd = dynamic_cast<QAction*>(sender());
926 QString aId = salomeConnector()->commandId(aCmd);
928 myModule->launchOperation(aId);
932 //******************************************************
933 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
935 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
937 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
939 DocumentPtr aPartDoc = aPart->partDoc();
941 aMgr->setCurrentDocument(aPartDoc);
946 aMgr->setCurrentDocument(aMgr->rootDocument());
949 //******************************************************
950 void XGUI_Workshop::salomeViewerSelectionChanged()
952 emit salomeViewerSelection();
955 //**************************************************************
956 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
958 return mySalomeConnector->viewer();
961 //**************************************************************
962 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
964 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
965 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
966 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
968 } else if (theId == "DEACTIVATE_PART_CMD")
969 activatePart(ResultPartPtr());
970 else if (theId == "DELETE_CMD")
971 deleteObjects(aObjects);
972 else if (theId == "SHOW_CMD")
973 showObjects(aObjects, true);
974 else if (theId == "HIDE_CMD")
975 showObjects(aObjects, false);
976 else if (theId == "SHOW_ONLY_CMD")
977 showOnlyObjects(aObjects);
980 //**************************************************************
981 void XGUI_Workshop::onWidgetValuesChanged()
983 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
984 FeaturePtr aFeature = anOperation->feature();
986 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
988 // aCustom->storeValue(aFeature);
990 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
991 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
992 for (; anIt != aLast; anIt++) {
993 ModuleBase_ModelWidget* aCustom = *anIt;
994 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
995 //aCustom->storeValue(aFeature);
996 aCustom->storeValue();
1001 //**************************************************************
1002 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1005 theFeature->activate();
1006 changeCurrentDocument(theFeature);
1007 myObjectBrowser->activatePart(theFeature);
1010 //**************************************************************
1011 void XGUI_Workshop::activateLastPart()
1013 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1014 DocumentPtr aDoc = aMgr->rootDocument();
1015 std::string aGrpName = ModelAPI_ResultPart::group();
1016 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1017 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1019 activatePart(aPart);
1022 //**************************************************************
1023 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1025 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1026 QMessageBox::StandardButton aRes = QMessageBox::warning(
1027 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1028 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1029 // ToDo: definbe deleting method
1030 if (aRes == QMessageBox::Yes) {
1031 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1032 aMgr->rootDocument()->startOperation();
1033 foreach (ObjectPtr aObj, theList)
1035 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1037 DocumentPtr aDoc = aPart->document();
1038 if (aDoc == aMgr->currentDocument()) {
1041 //aMgr->rootDocument()->removeFeature(aPart->owner());
1043 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1045 aObj->document()->removeFeature(aFeature);
1048 myDisplayer->updateViewer();
1049 aMgr->rootDocument()->finishOperation();
1053 //**************************************************************
1054 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1056 foreach (ObjectPtr aObj, theList)
1058 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1061 myDisplayer->display(aRes, false);
1063 myDisplayer->erase(aRes, false);
1067 myDisplayer->updateViewer();
1070 //**************************************************************
1071 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1073 myDisplayer->eraseAll(false);
1074 showObjects(theList, true);
1078 //**************************************************************
1079 void XGUI_Workshop::updateCommandsOnViewSelection()
1081 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1082 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1083 XGUI_Selection* aSelection = mySelector->selection();
1084 if (aSelection->getSelected().size() == 0)
1087 QList<QAction*> aActions = getModuleCommands();
1088 foreach(QAction* aAction, aActions)
1090 QString aId = aAction->data().toString();
1091 std::list<ModelAPI_Validator*> aValidators;
1092 std::list<std::list<std::string> > anArguments;
1093 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1094 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1095 for (; aValidator != aValidators.end(); aValidator++) {
1097 const ModuleBase_SelectionValidator* aSelValidator =
1098 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1099 if (aSelValidator) {
1100 aAction->setEnabled(aSelValidator->isValid(aSelection));
1107 //**************************************************************
1108 void XGUI_Workshop::registerValidators() const
1110 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1111 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1114 //**************************************************************
1115 void XGUI_Workshop::displayAllResults()
1117 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
1118 DocumentPtr aRootDoc = aMgr->rootDocument();
1119 displayDocumentResults(aRootDoc);
1120 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1121 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1122 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1123 displayDocumentResults(aPart->partDoc());
1125 myDisplayer->updateViewer();
1128 //**************************************************************
1129 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1133 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1134 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1137 //**************************************************************
1138 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1140 for (int i = 0; i < theDoc->size(theGroup); i++)
1141 myDisplayer->display(theDoc->object(theGroup, i), false);