Salome HOME
Define Rename as an operation
authorvsv <vitaly.smetannikov@opencascade.com>
Thu, 22 Oct 2015 09:55:42 +0000 (12:55 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Thu, 22 Oct 2015 09:55:42 +0000 (12:55 +0300)
src/XGUI/XGUI_ContextMenuMgr.cpp
src/XGUI/XGUI_ObjectsBrowser.cpp
src/XGUI/XGUI_Tools.cpp
src/XGUI/XGUI_Tools.h

index 406cc673c5671d15309d2c4dbc9bff28b490ae25..190f2ef3d50e54ab4cc89b3d33ffc4c52079ec43 100644 (file)
@@ -9,6 +9,8 @@
 #include "XGUI_Selection.h"
 #include "XGUI_SalomeConnector.h"
 #include "XGUI_DataModel.h"
+#include "XGUI_OperationMgr.h"
+#include "XGUI_Tools.h"
 
 #include <AppElements_MainWindow.h>
 
@@ -26,6 +28,7 @@
 
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_Tools.h>
+#include <ModuleBase_OperationAction.h>
 
 #include <QAction>
 #include <QContextMenuEvent>
@@ -492,5 +495,9 @@ QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
 
 void XGUI_ContextMenuMgr::onRename()
 {
+  ModuleBase_OperationAction* anAction = new ModuleBase_OperationAction("Rename", this);
+  XGUI_OperationMgr* anOpMgr = myWorkshop->operationMgr();
+  anOpMgr->startOperation(anAction);
   myWorkshop->objectBrowser()->onEditItem();
+  anOpMgr->commitOperation();
 }
index 74d04d9312a80ce65899a35b7495be2607559565..2df67c44963a3e33d72576739e1ae5b8c5bd2ff7 100644 (file)
@@ -8,6 +8,7 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Tools.h>
+#include <Events_Error.h>
 
 #include <ModuleBase_Tools.h>
 
@@ -88,23 +89,23 @@ void XGUI_DataTree::contextMenuEvent(QContextMenuEvent* theEvent)
 
 void XGUI_DataTree::commitData(QWidget* theEditor)
 {
-  QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
-  if (aEditor) {
-    QString aName = aEditor->text();
-    QModelIndexList aIndexList = selectionModel()->selectedIndexes();
-    XGUI_DataModel* aModel = dataModel();
-    ObjectPtr aObj = aModel->object(aIndexList.first());
-    SessionPtr aMgr = ModelAPI_Session::get();
-    aMgr->startOperation("Rename");
+  static int aEntrance = 0;
+  if (aEntrance == 0) {
+    // We have to check number of enter and exit of this function because it can be called recursively by Qt
+    // in order to avoid double modifying of a data
+    aEntrance = 1;
+    QLineEdit* aEditor = dynamic_cast<QLineEdit*>(theEditor);
+    if (aEditor) {
+      QString aName = aEditor->text();
+      QModelIndexList aIndexList = selectionModel()->selectedIndexes();
+      XGUI_DataModel* aModel = dataModel();
+      ObjectPtr aObj = aModel->object(aIndexList.first());
 
-    if (!XGUI_Tools::canRename(this, aObj, aName)) {
-      aMgr->abortOperation();
-      return;
+      if (XGUI_Tools::canRename(aObj, aName))
+        aObj->data()->setName(qPrintable(aName));
     }
-
-    aObj->data()->setName(qPrintable(aName));
-    aMgr->finishOperation();
   }
+  aEntrance = 0;
 }
 
 void XGUI_DataTree::clear() 
index fb0a8f2567dfa992722205900b0204dd9629a6a4..b0bc951061820d8ebcc7f9463e1a024bb1c605b1 100644 (file)
@@ -12,6 +12,7 @@
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_CompositeFeature.h>
 #include <ModelAPI_Tools.h>
+#include <Events_Error.h>
 
 #include <GeomAPI_Shape.h>
 
@@ -106,15 +107,16 @@ bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
 }
 
 //******************************************************************
-bool canRename(QWidget* theParent, const ObjectPtr& theObject, const QString& theName)
+bool canRename(const ObjectPtr& theObject, const QString& theName)
 {
   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
     double aValue;
     ResultParameterPtr aParam;
     if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
-      QMessageBox::information(theParent, QObject::tr("Rename parameter"),
-          QString(QObject::tr("Selected parameter can not be renamed to: %1. \
-There is a parameter with the same name. Its value is: %2.")).arg(qPrintable(theName)).arg(aValue));
+      QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
+ There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
+      // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
+      Events_Error::send(aErrMsg.toStdString());
       return false;
     }
   }
index 7cf55846e5bbd9762979d2f340cb33dca47191f4..21ca7356d04f531cd42ab29e7fc8b921ecb5440e 100644 (file)
@@ -80,11 +80,10 @@ bool XGUI_EXPORT canRemoveOrRename(QWidget* theParent, const QObjectPtrList& aLi
 
 /*! 
  Check possibility to rename object
- \param theParent a parent widget
  \param theObject an object to rename
  \param theName a name
  */
-bool canRename(QWidget* theParent, const ObjectPtr& theObject, const QString& theName);
+bool canRename(const ObjectPtr& theObject, const QString& theName);
 
 /*!
  Returns true if there are no parts in the document, which are not activated