Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 49bf2a2bad6ea372d58700f246a529ce840db2fe..f3ac95d98e222d23cba387e0a7cfba64caca25e7 100644 (file)
@@ -19,6 +19,7 @@
 #include "XGUI_ErrorDialog.h"
 #include "XGUI_ViewerProxy.h"
 #include "XGUI_PropertyPanel.h"
+#include "XGUI_ContextMenuMgr.h"
 
 #include <Model_Events.h>
 #include <ModelAPI_PluginManager.h>
@@ -34,6 +35,7 @@
 #include <Config_Common.h>
 #include <Config_FeatureMessage.h>
 #include <Config_PointerMessage.h>
+#include <Config_ModuleReader.h>
 
 #include <QApplication>
 #include <QFileDialog>
@@ -78,11 +80,11 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
   myDisplayer = new XGUI_Displayer(this);
 
   mySelector = new XGUI_SelectionMgr(this);
-  connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(changeCurrentDocument()));
 
   myOperationMgr = new XGUI_OperationMgr(this);
   myActionsMgr = new XGUI_ActionsMgr(this);
   myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
+  myContextMenuMgr = new XGUI_ContextMenuMgr(this);
 
   myViewerProxy = new XGUI_ViewerProxy(this);
 
@@ -177,6 +179,7 @@ void XGUI_Workshop::initMenu()
                                 QIcon(":pictures/close.png"), QKeySequence::Close);
   aCommand->connectTo(this, SLOT(onExit()));
 
+  myContextMenuMgr->createActions();
 }
 
 //******************************************************
@@ -283,7 +286,7 @@ void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
     return;
   }
   // Remember features icons
-  myIcons[QString(theMessage->id().c_str())] = QString(theMessage->icon().c_str());
+  myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
 
   //Find or create Workbench
   QString aWchName = QString::fromStdString(theMessage->workbenchId());
@@ -421,7 +424,7 @@ void XGUI_Workshop::onOpen()
   }
 
   //show file dialog, check if readable and open
-  myCurrentFile = QFileDialog::getOpenFileName(mainWindow());
+  myCurrentFile = QFileDialog::getExistingDirectory(mainWindow());
   if(myCurrentFile.isEmpty())
     return;
   QFileInfo aFileInfo(myCurrentFile);
@@ -470,6 +473,8 @@ void XGUI_Workshop::onUndo()
   objectBrowser()->setCurrentIndex(QModelIndex());
   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  if (!operationMgr()->abortOperation())
+    return;
   aDoc->undo();
   updateCommandStatus();
 }
@@ -499,7 +504,6 @@ XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
   CREATE_FUNC crtInst = 0;
 
 #ifdef WIN32
-
   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
   if (!modLib) {
     LPVOID lpMsgBuf;
@@ -524,23 +528,24 @@ XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
   }
 #else
   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
-  if ( !modLib )
-  err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
-  else
-  {
+  if ( !modLib ) {
+    err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
+  } else {
     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
-    if ( !crtInst )
-    err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
+    if ( !crtInst ) {
+      err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
+    }
   }
 #endif
 
   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
 
   if (!err.isEmpty()) {
-    if (mainWindow() && mainWindow()->isVisible())
+    if (mainWindow()) {
       QMessageBox::warning(mainWindow(), tr("Error"), err);
-    else
+    } else {
       qWarning( qPrintable( err ));
+    }
   }
   return aModule;
 }
@@ -548,7 +553,9 @@ XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
 //******************************************************
 bool XGUI_Workshop::activateModule()
 {
-  myPartSetModule = loadModule("PartSet");
+  Config_ModuleReader aModuleReader;
+  QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
+  myPartSetModule = loadModule(moduleName);
   if (!myPartSetModule)
     return false;
   myPartSetModule->createFeatures();
@@ -569,25 +576,25 @@ void XGUI_Workshop::updateCommandStatus()
   if (aMgr->hasRootDocument()) {
     XGUI_Command* aUndoCmd;
     XGUI_Command* aRedoCmd;
-    for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
-      if ((*aIt)->id() == "UNDO_CMD")
-        aUndoCmd = (*aIt);
-      else if ((*aIt)->id() == "REDO_CMD")
-        aRedoCmd = (*aIt);
+    foreach(XGUI_Command* aCmd, aCommands) {
+      if (aCmd->id() == "UNDO_CMD")
+        aUndoCmd = aCmd;
+      else if (aCmd->id() == "REDO_CMD")
+        aRedoCmd = aCmd;
       else // Enable all commands
-        (*aIt)->enable();
+        aCmd->enable();
     }
     boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
     aUndoCmd->setEnabled(aDoc->canUndo());
     aRedoCmd->setEnabled(aDoc->canRedo());
   } else {
-    for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
-      if ((*aIt)->id() == "NEW_CMD")
-        (*aIt)->enable();
-      else if ((*aIt)->id() == "EXIT_CMD")
-        (*aIt)->enable();
+    foreach(XGUI_Command* aCmd, aCommands) {
+      if (aCmd->id() == "NEW_CMD")
+        aCmd->enable();
+      else if (aCmd->id() == "EXIT_CMD")
+        aCmd->enable();
       else 
-        (*aIt)->disable();
+        aCmd->disable();
     }
   }
 }
@@ -599,7 +606,10 @@ QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
   aObjDock->setWindowTitle(tr("Object browser"));
   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
+  connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
   aObjDock->setWidget(myObjectBrowser);
+
+  myContextMenuMgr->connectObjectBrowser();
   return aObjDock;
 }
 
@@ -664,18 +674,15 @@ void XGUI_Workshop::onFeatureTriggered()
 }
 
 //******************************************************
-void XGUI_Workshop::changeCurrentDocument()
+void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
 {
-  QFeatureList aFeatures = objectBrowser()->selectedFeatures();
-  
-  // Set current document
-  if (aFeatures.size() > 0) {
-    FeaturePtr aFeature = aFeatures.first();
-
-    boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-    boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
+  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
+  if (thePart) {
+    boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = thePart->data()->docRef("PartDocument");
     if (aDocRef)
       aMgr->setCurrentDocument(aDocRef->value());
+  } else {
+    aMgr->setCurrentDocument(aMgr->rootDocument());
   }
 }