Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 112f3bd1d41a0d8c2e9ec58f0e1897734ecf7115..40c545b2c2567000db3f10b3f75d8d6357be071b 100644 (file)
 #include "XGUI_ErrorDialog.h"
 #include "XGUI_ViewerProxy.h"
 #include "XGUI_PropertyPanel.h"
+#include "XGUI_ContextMenuMgr.h"
 
 #include <Model_Events.h>
 #include <ModelAPI_PluginManager.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_AttributeDocRef.h>
+#include <ModelAPI_Object.h>
 
 #include <Events_Loop.h>
 #include <Events_Error.h>
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_OperationDescription.h>
+#include <Config_Common.h>
 #include <Config_FeatureMessage.h>
 #include <Config_PointerMessage.h>
+#include <Config_ModuleReader.h>
 
 #include <QApplication>
 #include <QFileDialog>
 #include <dlfcn.h>
 #endif
 
+
+QMap<QString, QString> XGUI_Workshop::myIcons;
+
+QString XGUI_Workshop::featureIcon(const std::string& theId)
+{
+  QString aId(theId.c_str());
+  if (myIcons.contains(aId))
+    return myIcons[aId];
+  return QString();
+}
+
 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
   : QObject(),
   myCurrentFile(QString()),
@@ -66,15 +81,18 @@ 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);
+  connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
+          this, SLOT(onContextMenuCommand(const QString&, bool)));
 
   myViewerProxy = new XGUI_ViewerProxy(this);
 
   connect(myOperationMgr, SIGNAL(operationStarted()),  this, SLOT(onOperationStarted()));
+  connect(myOperationMgr, SIGNAL(operationResumed()),  this, SLOT(onOperationStarted()));
   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
@@ -110,18 +128,20 @@ void XGUI_Workshop::startApplication()
 //******************************************************
 void XGUI_Workshop::initMenu()
 {
+  myContextMenuMgr->createActions();
+
   if (isSalomeMode()) {
     // Create only Undo, Redo commands
-    salomeConnector()->addEditCommand("UNDO_CMD", 
+    QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", 
                                       tr("Undo"), tr("Undo last command"),
                                       QIcon(":pictures/undo.png"), 
-                                      false, this, SLOT(onUndo()),
-                                      QKeySequence::Undo);
-    salomeConnector()->addEditCommand("REDO_CMD", 
+                                      QKeySequence::Undo, false);
+    connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
+    aAction = salomeConnector()->addEditCommand("REDO_CMD", 
                                       tr("Redo"), tr("Redo last command"),
                                       QIcon(":pictures/redo.png"), 
-                                      false, this, SLOT(onRedo()),
-                                      QKeySequence::Redo);
+                                      QKeySequence::Redo, false);
+    connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
     salomeConnector()->addEditMenuSeparator();
     return;
   }
@@ -164,7 +184,6 @@ void XGUI_Workshop::initMenu()
   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
                                 QIcon(":pictures/close.png"), QKeySequence::Close);
   aCommand->connectTo(this, SLOT(onExit()));
-
 }
 
 //******************************************************
@@ -191,8 +210,8 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage)
   {
     const Model_FeatureUpdatedMessage* anUpdateMsg =
         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
-    boost::shared_ptr<ModelAPI_Feature> aNewFeature = anUpdateMsg->feature();
-    boost::shared_ptr<ModelAPI_Feature> aCurrentFeature = myOperationMgr->currentOperation()->feature();
+    FeaturePtr aNewFeature = anUpdateMsg->feature();
+    FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
     if(aNewFeature == aCurrentFeature) {
       myPropertyPanel->updateContentWidget(aCurrentFeature);
     }
@@ -204,6 +223,7 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage)
         (ModuleBase_Operation*)(aPartSetMsg->pointer());
 
     if (myOperationMgr->startOperation(anOperation)) {
+      myPropertyPanel->updateContentWidget(anOperation->feature());
       if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
         anOperation->commit();
         updateCommandStatus();
@@ -269,22 +289,24 @@ void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
 #endif
     return;
   }
+  // Remember features icons
+  myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
+
   //Find or create Workbench
   QString aWchName = QString::fromStdString(theMessage->workbenchId());
   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
   bool isUsePropPanel = theMessage->isUseInput();
   if (isSalomeMode()) {
     QString aId = QString::fromStdString(theMessage->id());
-    salomeConnector()->addFeature(aWchName,
-                                  QString::fromStdString(theMessage->id()),
-                                  aId,
-                                  QString::fromStdString(theMessage->tooltip()),
-                                  QIcon(theMessage->icon().c_str()),
-                                  isUsePropPanel, this, 
-                                  SLOT(onFeatureTriggered()), QKeySequence());
-    myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
+    QAction* aAction = salomeConnector()->addFeature(aWchName,
+                              aId,
+                              QString::fromStdString(theMessage->text()),
+                              QString::fromStdString(theMessage->tooltip()),
+                              QIcon(theMessage->icon().c_str()),
+                              QKeySequence(), isUsePropPanel);
+    myActionsMgr->addCommand(aAction);
     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
-
+    myPartSetModule->featureCreated(aAction);
   } else {
 
     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
@@ -317,11 +339,6 @@ void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
  */
 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
 {
-  QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
-  connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
-  QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
-  connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
-
   QAction* aCommand = 0;
   if (isSalomeMode()) {
     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
@@ -339,8 +356,8 @@ void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
 void XGUI_Workshop::saveDocument(QString theName)
 {
   QApplication::restoreOverrideCursor();
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  DocumentPtr aDoc = aMgr->rootDocument();
   aDoc->save(theName.toLatin1().constData());
   QApplication::restoreOverrideCursor();
 }
@@ -348,8 +365,8 @@ void XGUI_Workshop::saveDocument(QString theName)
 //******************************************************
 void XGUI_Workshop::onExit()
 {
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  DocumentPtr aDoc = aMgr->rootDocument();
   if(aDoc->isModified()) {
     int anAnswer = QMessageBox::question(
         myMainWindow, tr("Save current file"),
@@ -387,8 +404,8 @@ void XGUI_Workshop::onNew()
 void XGUI_Workshop::onOpen()
 {
   //save current file before close if modified
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  DocumentPtr aDoc = aMgr->rootDocument();
   if(aDoc->isModified()) {
     //TODO(sbh): re-launch the app?
     int anAnswer = QMessageBox::question(
@@ -405,7 +422,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);
@@ -451,9 +468,11 @@ void XGUI_Workshop::onSaveAs()
 //******************************************************
 void XGUI_Workshop::onUndo()
 {
-  objectBrowser()->setCurrentIndex(QModelIndex());
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  DocumentPtr aDoc = aMgr->rootDocument();
+  if (aDoc->isOperation())
+    operationMgr()->abortOperation();
   aDoc->undo();
   updateCommandStatus();
 }
@@ -461,9 +480,9 @@ void XGUI_Workshop::onUndo()
 //******************************************************
 void XGUI_Workshop::onRedo()
 {
-  objectBrowser()->setCurrentIndex(QModelIndex());
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
-  boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+  objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  DocumentPtr aDoc = aMgr->rootDocument();
   aDoc->redo();
   updateCommandStatus();
 }
@@ -471,7 +490,8 @@ void XGUI_Workshop::onRedo()
 //******************************************************
 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
 {
-  QString libName = library(theModule);
+  QString libName =
+      QString::fromStdString(library(theModule.toStdString()));
   if (libName.isEmpty()) {
     qWarning(
     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
@@ -482,7 +502,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;
@@ -507,23 +526,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;
 }
@@ -531,7 +551,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();
@@ -541,36 +563,39 @@ bool XGUI_Workshop::activateModule()
 //******************************************************
 void XGUI_Workshop::updateCommandStatus()
 {
-  if (isSalomeMode()) // TODO: update commands in SALOME
-    return;
-  XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
-
-  QList<XGUI_Command*> aCommands = aMenuBar->features();
-  QList<XGUI_Command*>::const_iterator aIt;
-
-  boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
+  QList<QAction*> aCommands;
+  if (isSalomeMode()) { // update commands in SALOME mode
+    aCommands = salomeConnector()->commandList();
+  } else {
+    XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
+    foreach (XGUI_Command* aCmd, aMenuBar->features())
+      aCommands.append(aCmd);
+  }
+  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
   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);
+    QAction* aUndoCmd;
+    QAction* aRedoCmd;
+    foreach(QAction* aCmd, aCommands) {
+      QString aId = aCmd->data().toString();
+      if (aId == "UNDO_CMD")
+        aUndoCmd = aCmd;
+      else if (aId == "REDO_CMD")
+        aRedoCmd = aCmd;
       else // Enable all commands
-        (*aIt)->enable();
+        aCmd->setEnabled(true);
     }
-    boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
+    DocumentPtr 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(QAction* aCmd, aCommands) {
+      QString aId = aCmd->data().toString();
+      if (aId == "NEW_CMD")
+        aCmd->setEnabled(true);
+      else if (aId == "EXIT_CMD")
+        aCmd->setEnabled(true);
       else 
-        (*aIt)->disable();
+        aCmd->setEnabled(false);
     }
   }
 }
@@ -582,7 +607,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;
 }
 
@@ -602,6 +630,11 @@ void XGUI_Workshop::createDockWidgets()
   hidePropertyPanel(); //<! Invisible by default
   hideObjectBrowser();
   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
+
+  QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
+  connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
+  QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
+  connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
 }
 
 //******************************************************
@@ -647,18 +680,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");
+  PluginManagerPtr 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());
   }
 }
 
@@ -674,3 +704,29 @@ XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
 { 
   return mySalomeConnector->viewer(); 
 }
+
+//**************************************************************
+void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
+{
+  if (theId == "ACTIVATE_PART_CMD")
+    activatePart(true);
+  else if (theId == "DEACTIVATE_PART_CMD") 
+    activatePart(false);
+
+}
+
+//**************************************************************
+void XGUI_Workshop::activatePart(bool toActivate)
+{
+  if (toActivate) {
+    QFeatureList aFeatures = mySelector->selectedFeatures();
+    if (aFeatures.size() > 0) {
+      changeCurrentDocument(aFeatures.first());
+      myObjectBrowser->activateCurrentPart(true);
+    }
+  } else {
+    changeCurrentDocument(FeaturePtr());
+    myObjectBrowser->activateCurrentPart(false);
+  }
+}
+