Salome HOME
Value state is provided in ModelWidget to remove 'myIsCurrentValueUnderModification...
[modules/shaper.git] / src / XGUI / XGUI_ErrorMgr.cpp
index f46a9291e36d0574b97d96be3470daebe7b2504e..97a163665599dc67817db6f2b04ffd5772dd11cf 100644 (file)
@@ -7,18 +7,37 @@
 #include "XGUI_ErrorMgr.h"
 
 #include "XGUI_OperationMgr.h"
+#include "XGUI_ModuleConnector.h"
+#include "XGUI_Workshop.h"
+#include "XGUI_ActionsMgr.h"
 
 #include <ModuleBase_IPropertyPanel.h>
+#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_IModule.h>
 #include <ModuleBase_ModelWidget.h>
+#include <ModuleBase_OperationFeature.h>
 
 #include <ModelAPI_Attribute.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 
 #include <QLabel>
+#include <QAction>
+#include <QApplication>
+#include <QDesktopWidget>
+#include <QDialog>
+#include <QCursor>
+#include <QHBoxLayout>
+#include <QLabel>
+
+const QString INVALID_VALUE = "invalid_action";
+
 
-XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent /*= 0*/)
-  : ModuleBase_IErrorMgr(theParent)
+XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
+  : ModuleBase_IErrorMgr(theParent),
+    myErrorDialog(0),
+    myErrorLabel(0),
+    myWorkshop(theWorkshop)
 {
 
 }
@@ -28,7 +47,85 @@ XGUI_ErrorMgr::~XGUI_ErrorMgr()
 
 }
 
-const char* toString(ModelAPI_ExecState theExecState) 
+void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
+{
+  QString anError = myWorkshop->module()->getFeatureError(theFeature);
+
+  //update Ok Action and header of property panel if the current operation started for the feature
+  XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                      (workshop()->operationMgr()->currentOperation());
+  if (aFOperation && aFOperation->feature() == theFeature) {
+    QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
+    updateActionState(anOkAction, theFeature);
+  }
+}
+
+void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
+{
+  QString anError = myWorkshop->module()->getFeatureError(theFeature);
+
+  XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
+  if (workshop()->isFeatureOfNested(theFeature)) {
+    QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
+    bool anEnabled = anError.isEmpty();
+    anAcceptAllAction->setEnabled(anEnabled);
+    anAcceptAllAction->setToolTip(anError);
+  }
+}
+
+bool XGUI_ErrorMgr::canProcessClick(QAction* theAction, const FeaturePtr& theFeature)
+{
+  QString anError = myWorkshop->module()->getFeatureError(theFeature);
+  bool isActionEnabled = anError.isEmpty();
+  if (!isActionEnabled && !anError.isEmpty()) {
+    if (!myErrorDialog) {
+      myErrorDialog = new QDialog(QApplication::desktop(), Qt::Popup);
+      QHBoxLayout* aLay = new QHBoxLayout(myErrorDialog);
+      aLay->setContentsMargins(0, 0, 0, 0);
+
+      QFrame* aMarginWidget = new QFrame(myErrorDialog);
+      aMarginWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
+
+      aLay->addWidget(aMarginWidget);
+      QHBoxLayout* aMarginLay = new QHBoxLayout(aMarginWidget);
+      aMarginLay->setContentsMargins(4, 4, 4, 4);
+
+      myErrorLabel = new QLabel(aMarginWidget);
+      aMarginLay->addWidget(myErrorLabel);
+    }
+    myErrorLabel->setText(anError);
+    myErrorDialog->move(QCursor::pos());
+    myErrorDialog->show();
+  }
+  return isActionEnabled;
+}
+
+void XGUI_ErrorMgr::updateActionState(QAction* theAction, const FeaturePtr& theFeature/*,
+                                      const bool theEnabled*/)
+{
+  QString anError = myWorkshop->module()->getFeatureError(theFeature);
+  bool anEnabled = anError.isEmpty();
+
+  bool isActionEnabled = theAction->data() != INVALID_VALUE;
+  if (anEnabled  != isActionEnabled) {
+    // update enable state of the button
+    theAction->setIcon(anEnabled ? QIcon(":pictures/button_ok.png"): QIcon(":pictures/button_ok_error.png"));
+    if (anEnabled)
+      theAction->setData("");
+    else
+      theAction->setData(INVALID_VALUE);
+  }
+  // some operations have no property panel, so it is important to check that it is not null
+  if (myPropertyPanel) {
+    // update controls error information
+    QWidget* aWidget = myPropertyPanel->headerWidget();
+    if (aWidget)
+      aWidget->setToolTip(anError);
+  }
+}
+
+/*const char* toString(ModelAPI_ExecState theExecState) 
 {
 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
   switch (theExecState) {
@@ -37,40 +134,49 @@ const char* toString(ModelAPI_ExecState theExecState)
   TO_STRING(ModelAPI_StateExecFailed)
   TO_STRING(ModelAPI_StateInvalidArgument)
   TO_STRING(ModelAPI_StateNothing)
-  default: "Unknown ExecState.";
+  default: return "Unknown ExecState.";
   }
 #undef TO_STRING
-}
+}*/
 
-void XGUI_ErrorMgr::onValidationStateChanged()
+/*void XGUI_ErrorMgr::onValidationStateChanged()
 {
   XGUI_OperationMgr* anOperationMgr = dynamic_cast<XGUI_OperationMgr*>(sender());
-  if (!anOperationMgr || !anOperationMgr->currentOperation())
+  if (!anOperationMgr)
     return;
-
-  if (!myPropertyPanel)
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                  (anOperationMgr->currentOperation());
+  if (!myPropertyPanel || !aFOperation)
     return;
 
+  FeaturePtr aFeature = aFOperation->feature();
+  QString anError = getFeatureError(aFeature);
+
+  QWidget* aWidget = myPropertyPanel->headerWidget();
+  if (aWidget) {
+    aWidget->setToolTip(anError);
+    aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
+  }
+}*/
+
+/*QString XGUI_ErrorMgr::getFeatureError(const FeaturePtr& theFeature) const
+{
+  QString anError;
   // get feature
-  FeaturePtr aFeature = anOperationMgr->currentOperation()->feature();
-  if (!aFeature.get())
-    return;
+  if (!theFeature.get() || !theFeature->data()->isValid())
+    return anError;
 
   // set error indication
-  QString anError = QString::fromStdString(aFeature->error());
+  anError = QString::fromStdString(theFeature->error());
   if (anError.isEmpty()) {
-    bool isDone = ( aFeature->data()->execState() == ModelAPI_StateDone
-                 || aFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
+    bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
+                 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
     if (!isDone)
-      anError = toString(aFeature->data()->execState());
+      anError = toString(theFeature->data()->execState());
   }
 
-  QWidget* aWidget = myPropertyPanel->headerWidget();
-  if (aWidget) {
-    aWidget->setToolTip(anError);
-    aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
-  }
-}
+  return anError;
+}*/
 
 void XGUI_ErrorMgr::onWidgetChanged()
 {
@@ -90,7 +196,7 @@ void XGUI_ErrorMgr::onWidgetChanged()
   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
     if (anErrorMsg.empty())
       anErrorMsg = "unknown error.";
-    anErrorMsg = "Attribute \"" + anAttributeID + "\" invalidated by \"" + aValidatorID + "\" with error: " + anErrorMsg;
+    anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
   }
 
   QString anError = QString::fromStdString(anErrorMsg);
@@ -109,6 +215,12 @@ void XGUI_ErrorMgr::onWidgetChanged()
       aTTip += "Errors:\n" + anError;
     }
     aWidget->setToolTip(aTTip);
-    aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
+    //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
   }
 }
+
+XGUI_Workshop* XGUI_ErrorMgr::workshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
+  return aConnector->workshop();
+}