Salome HOME
Change color for construction/body/group.
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index b6ba2aa7101640473a22f93e0e2022c20c4173a2..45f12d0d571c8b32b0b65b9b20ab790d4bd4c0f7 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>
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Validator.h>
+#include <ModelAPI_ResultGroup.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_AttributeIntArray.h>
 
 //#include <PartSetPlugin_Part.h>
 
@@ -52,6 +55,7 @@
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_IViewer.h>
 #include<ModuleBase_FilterFactory.h>
+#include <ModuleBase_PageBase.h>
 
 #include <Config_Common.h>
 #include <Config_FeatureMessage.h>
 #include <QMenu>
 #include <QToolButton>
 #include <QAction>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QHBoxLayout>
+#include <QtxColorButton.h>
 
 #ifdef _DEBUG
 #include <QDebug>
@@ -268,7 +276,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 +285,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 +453,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,12 +653,12 @@ 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) {
     bool isStoreValue = !theOperation->isEditOperation() &&
-                        aWidget->isValueDefault() && !aWidget->isComputedDefault();
+                        !aWidget->getDefaultValue().empty() &&
+                        !aWidget->isComputedDefault();
     aWidget->setFeature(theOperation->feature(), isStoreValue);
     aWidget->enableFocusProcessing();
   }
@@ -722,8 +728,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);
   }
@@ -1058,11 +1082,10 @@ void XGUI_Workshop::updateCommandStatus()
         // Enable all commands
         aCmd->setEnabled(true);
     }
+
     aUndoCmd->setEnabled(myModule->canUndo());
     aRedoCmd->setEnabled(myModule->canRedo());
-
     updateHistory();
-
   } else {
     foreach(QAction* aCmd, aCommands) {
       QString aId = aCmd->data().toString();
@@ -1081,28 +1104,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);
 }
 
 //******************************************************
@@ -1133,24 +1140,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)));
 
 }
 
@@ -1236,6 +1245,8 @@ void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
     activatePart(ResultPartPtr());
   else if (theId == "DELETE_CMD")
     deleteObjects(aObjects);
+  else if (theId == "COLOR_CMD")
+    changeColor(aObjects);
   else if (theId == "SHOW_CMD")
     showObjects(aObjects, true);
   else if (theId == "HIDE_CMD")
@@ -1285,8 +1296,14 @@ void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
 //**************************************************************
 void XGUI_Workshop::deleteObjects(const QObjectPtrList& theList)
 {
-  QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
+  ModuleBase_IModule* aModule = module();
+  if (aModule->deleteObjects())
+    return;
+
+  if (!isActiveOperationAborted())
+    return;
 
+  QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
   std::set<FeaturePtr> aRefFeatures;
   foreach (ObjectPtr aObj, theList)
   {
@@ -1323,8 +1340,16 @@ These features will be deleted also. Would you like to continue?")).arg(aNames),
       return;
   }
 
+  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(", "));
   SessionPtr aMgr = ModelAPI_Session::get();
-  aMgr->startOperation("DeleteObjects");
+  aMgr->startOperation(aDescription.toStdString());
   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
                                        aLast = aRefFeatures.end();
   for (; anIt != aLast; anIt++) {
@@ -1352,6 +1377,112 @@ These features will be deleted also. Would you like to continue?")).arg(aNames),
 
   myDisplayer->updateViewer();
   aMgr->finishOperation();
+  updateCommandStatus();
+}
+
+bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
+{
+  bool isFoundResultType = false;
+  foreach(ObjectPtr anObj, theObjects)
+  {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (aResult.get() == NULL)
+      continue;
+
+    isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
+    if (isFoundResultType)
+      break;
+  }
+  return isFoundResultType;
+}
+
+//**************************************************************
+bool XGUI_Workshop::canChangeColor() const
+{
+  QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
+
+  std::set<std::string> aTypes;
+  aTypes.insert(ModelAPI_ResultGroup::group());
+  aTypes.insert(ModelAPI_ResultConstruction::group());
+  aTypes.insert(ModelAPI_ResultBody::group());
+  return hasResults(aObjects, aTypes);
+}
+
+//**************************************************************
+void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
+{
+  // 1. find the initial value of the color
+  AttributeIntArrayPtr aColorAttr;
+  foreach(ObjectPtr anObj, theObjects) {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (aResult.get() != NULL) {
+      aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID());
+    }
+  }
+  // there is no object with the color attribute
+  if (aColorAttr.get() == NULL || aColorAttr->size() == 0)
+    return;
+  int aRed = aColorAttr->value(0);
+  int aGreen = aColorAttr->value(1);
+  int aBlue = aColorAttr->value(2);
+
+  // 2. show the dialog to change the value
+  QDialog* aDlg = new QDialog();
+  QVBoxLayout* aLay = new QVBoxLayout(aDlg);
+
+  QtxColorButton* aColorBtn = new QtxColorButton(aDlg);
+  aColorBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
+
+  aLay->addWidget(aColorBtn);
+  aColorBtn->setColor(QColor(aRed, aGreen, aBlue));
+
+  QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
+                                                    Qt::Horizontal, aDlg);
+  connect(aButtons, SIGNAL(accepted()), aDlg, SLOT(accept()));
+  connect(aButtons, SIGNAL(rejected()), aDlg, SLOT(reject()));
+  aLay->addWidget(aButtons);
+
+  aDlg->move(QCursor::pos());
+  bool isDone = aDlg->exec() == QDialog::Accepted;
+  if (!isDone)
+    return;
+
+  QColor aColorResult = aColorBtn->color();
+  int aRedResult = aColorResult.red(),
+      aGreenResult = aColorResult.green(),
+      aBlueResult = aColorResult.blue();
+
+  if (aRedResult == aRed && aGreenResult == aGreen && aBlueResult == aBlue)
+    return;
+
+  // 3. abort the previous operation and start a new one
+  SessionPtr aMgr = ModelAPI_Session::get();
+  bool aWasOperation = aMgr->isOperation(); // keep this value
+  if (!aWasOperation) {
+    QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
+    aMgr->startOperation(aDescription.toStdString());
+  }
+
+  // 4. set the value to all results
+  static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  foreach(ObjectPtr anObj, theObjects) {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (aResult.get() != NULL) {
+      aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID());
+      if (aColorAttr.get() != NULL) {
+        if (!aColorAttr->size()) {
+          aColorAttr->setSize(3);
+        }
+        aColorAttr->setValue(0, aRedResult);
+        aColorAttr->setValue(1, aGreenResult);
+        aColorAttr->setValue(2, aBlueResult);
+        ModelAPI_EventCreator::get()->sendUpdated(anObj, EVENT_DISP);
+      }
+    }
+  }
+  if (!aWasOperation)
+    aMgr->finishOperation();
+  updateCommandStatus();
 }
 
 //**************************************************************
@@ -1466,5 +1597,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;
 }