Salome HOME
Property pannel refactoring: focus processing in paged widgets
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 84d3d6a10354f5d6d8f7117f623b79823ff0cfb5..ff3347b02c3cfa371121b43c86049441565a11e2 100644 (file)
@@ -24,6 +24,7 @@
 #include <AppElements_MainMenu.h>
 #include <AppElements_MainWindow.h>
 #include <AppElements_MenuGroupPanel.h>
+#include <AppElements_Button.h>
 
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_Preferences.h>
@@ -268,7 +269,7 @@ void XGUI_Workshop::initMenu()
   aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
   aCommand->connectTo(this, SLOT(onUndo()));
-  QToolButton* aUndoButton = qobject_cast<QToolButton*>(aGroup->widget(aUndoId));
+  AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
   addHistoryMenu(aUndoButton,
                  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
                  SLOT(onUndo(int)));
@@ -277,7 +278,7 @@ void XGUI_Workshop::initMenu()
   aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
   aCommand->connectTo(this, SLOT(onRedo()));
-  QToolButton* aRedoButton = qobject_cast<QToolButton*>(aGroup->widget(aRedoId));
+  AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
   addHistoryMenu(aRedoButton,
                  SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
                  SLOT(onRedo(int)));
@@ -445,15 +446,13 @@ void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessa
                                aMsg->parameters());
       }
     }
-  }
-
-  
-  else {
+  } else {
     //Show error dialog if error message received.
     std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
     if (anAppError) {
       emit errorOccurred(QString::fromLatin1(anAppError->description()));
     }
+    return;
   }
   if (!isSalomeMode()) {
     SessionPtr aMgr = ModelAPI_Session::get();
@@ -647,17 +646,14 @@ void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
 
   myPropertyPanel->cleanContent();
   aFactory.createWidget(myPropertyPanel->contentWidget());
-  ModuleBase_Tools::zeroMargins(myPropertyPanel->contentWidget());
 
   QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
   foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
-    aWidget->setFeature(theOperation->feature());
+    bool isStoreValue = !theOperation->isEditOperation() &&
+                        !aWidget->getDefaultValue().empty() &&
+                        !aWidget->isComputedDefault();
+    aWidget->setFeature(theOperation->feature(), isStoreValue);
     aWidget->enableFocusProcessing();
-    QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
-    // Init default values
-    if (!theOperation->isEditOperation() && aWidget->isValueDefault() && !aWidget->isComputedDefault()) {
-      aWidget->storeValue();
-    }
   }
   
   myPropertyPanel->setModelWidgets(aWidgets);
@@ -725,8 +721,26 @@ void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& the
       aFeatureInfo.shortcut = aHotKey;
     }
     // Create feature...
-    AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo, aDocKind);
-    aCommand->setNestedCommands(aNestedFeatures);
+    AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
+                                                       aDocKind,
+                                                       aNestedFeatures);
+    // Enrich created button with accept/abort buttons if necessary
+    AppElements_Button* aButton = aCommand->button();
+    if (aButton->isColumnButton()) {
+      QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
+      QList<QAction*> anActList;
+      if (aNestedActions.contains("accept")) {
+        QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, aButton);
+        connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
+        anActList << anAction;
+      }
+      if (aNestedActions.contains("abort")) {
+        QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, aButton);
+        connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
+        anActList << anAction;
+      }
+      aButton->setAdditionalButtons(anActList);
+    }
     myActionsMgr->addCommand(aCommand);
     myModule->actionCreated(aCommand);
   }
@@ -1061,11 +1075,10 @@ void XGUI_Workshop::updateCommandStatus()
         // Enable all commands
         aCmd->setEnabled(true);
     }
-    aUndoCmd->setEnabled(aMgr->canUndo() && !aMgr->isOperation());
-    aRedoCmd->setEnabled(aMgr->canRedo() && !aMgr->isOperation());
 
+    aUndoCmd->setEnabled(myModule->canUndo());
+    aRedoCmd->setEnabled(myModule->canRedo());
     updateHistory();
-
   } else {
     foreach(QAction* aCmd, aCommands) {
       QString aId = aCmd->data().toString();
@@ -1084,28 +1097,12 @@ void XGUI_Workshop::updateCommandStatus()
 void XGUI_Workshop::updateHistory()
 {
   std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
-  std::list<std::string>::iterator it = aUndoList.begin();
-  QList<ActionInfo> aUndoRes;
-  for ( ; it != aUndoList.end(); it++) {
-    QString anId = QString::fromStdString(*it);
-    QIcon aIcon;
-    if (myIcons.contains(anId))
-      aIcon = QIcon(myIcons[anId]);
-    aUndoRes << ActionInfo(aIcon, anId);
-  }
+  QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
   emit updateUndoHistory(aUndoRes);
 
   std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
-  it = aRedoList.begin();
-  QList<ActionInfo> aRedoRes;
-  for ( ; it != aRedoList.end(); it++) {
-    QString anId = QString::fromStdString(*it);
-    QIcon aIcon;
-    if (myIcons.contains(anId))
-      aIcon = QIcon(myIcons[anId]);
-    aRedoRes << ActionInfo(aIcon, anId);
-  }
-  emit updateRedoHistory(aUndoRes);
+  QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
+  emit updateRedoHistory(aRedoRes);
 }
 
 //******************************************************
@@ -1136,24 +1133,26 @@ void XGUI_Workshop::createDockWidgets()
   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
+  myPropertyPanel->setupActions(myActionsMgr);
   myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
-
-  connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
-
   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
   hidePropertyPanel();  ///<! Invisible by default
   hideObjectBrowser();
   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
   myPropertyPanel->installEventFilter(myOperationMgr);
 
-  QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(PROP_PANEL_OK);
-  connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
-  QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(PROP_PANEL_CANCEL);
-  connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
-  connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
-          SLOT(onKeyReleased(QKeyEvent*)));
-  connect(myOperationMgr, SIGNAL(applyEnableChanged(bool)), myPropertyPanel,
-          SLOT(setAcceptEnabled(bool)));
+  QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
+  connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
+  QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
+  connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
+  connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
+  connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
+          myOperationMgr,  SLOT(onKeyReleased(QKeyEvent*)));
+  connect(myOperationMgr,  SIGNAL(validationStateChanged(bool)),
+          aOkAct,          SLOT(setEnabled(bool)));
+  QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
+  connect(myOperationMgr,  SIGNAL(nestedStateChanged(bool)),
+          aAcceptAllAct,   SLOT(setEnabled(bool)));
 
 }
 
@@ -1258,24 +1257,6 @@ void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
   }
 }
 
-//**************************************************************
-void XGUI_Workshop::onWidgetValuesChanged()
-{
-  ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
-  FeaturePtr aFeature = anOperation->feature();
-
-  ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
-
-  const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
-  QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
-  for (; anIt != aLast; anIt++) {
-    ModuleBase_ModelWidget* aCustom = *anIt;
-    if (aCustom && (aCustom == aSenderWidget)) {
-      aCustom->storeValue();
-    }
-  }
-}
-
 //**************************************************************
 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
 {
@@ -1306,8 +1287,15 @@ void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
 //**************************************************************
 void XGUI_Workshop::deleteObjects(const QObjectPtrList& theList)
 {
-  QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
+  // the active operation should not be aborted if there is an empty list of objects
+  // it is possible that delete shortcut should be processed in the active operation.
+  if (theList.empty())
+    return;
+
+  if (!isActiveOperationAborted())
+    return;
 
+  QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
   std::set<FeaturePtr> aRefFeatures;
   foreach (ObjectPtr aObj, theList)
   {
@@ -1345,7 +1333,15 @@ These features will be deleted also. Would you like to continue?")).arg(aNames),
   }
 
   SessionPtr aMgr = ModelAPI_Session::get();
-  aMgr->startOperation("DeleteObjects");
+  QString aDescription = tr("Delete %1");
+  QStringList aObjectNames;
+  foreach (ObjectPtr aObj, theList) {
+    if (!aObj->data().get())
+      continue;
+    aObjectNames << QString::fromStdString(aObj->data()->name());
+  }
+  aDescription = aDescription.arg(aObjectNames.join(", "));
+  aMgr->startOperation(aDescription.toStdString());
   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
                                        aLast = aRefFeatures.end();
   for (; anIt != aLast; anIt++) {
@@ -1373,6 +1369,7 @@ These features will be deleted also. Would you like to continue?")).arg(aNames),
 
   myDisplayer->updateViewer();
   aMgr->finishOperation();
+  updateCommandStatus();
 }
 
 //**************************************************************
@@ -1487,5 +1484,23 @@ void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, co
   }
   connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
   connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
+}
 
+QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
+{
+  QList<ActionInfo> aResult;
+  std::list<std::string>::const_iterator it = theList.cbegin();
+  for (; it != theList.cend(); it++) {
+    QString anId = QString::fromStdString(*it);
+    bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
+    if (isEditing) {
+      anId.chop(ModuleBase_Operation::EditSuffix().size());
+    }
+    ActionInfo anInfo = myActionsMgr->actionInfoById(anId);
+    if (isEditing) {
+      anInfo.text = anInfo.text.prepend("Modify ");
+    }
+    aResult << anInfo;
+  }
+  return aResult;
 }