Salome HOME
Update Part item on activation of loaded document
[modules/shaper.git] / src / XGUI / XGUI_ErrorMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        XGUI_ErrorMgr.cpp
4 // Created:     22 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "XGUI_ErrorMgr.h"
8
9 #include "XGUI_OperationMgr.h"
10
11 #include <ModuleBase_IPropertyPanel.h>
12 #include <ModuleBase_ModelWidget.h>
13
14 #include <ModelAPI_Attribute.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
17
18 #include <QLabel>
19
20 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent /*= 0*/)
21   : ModuleBase_IErrorMgr(theParent)
22 {
23
24 }
25
26 XGUI_ErrorMgr::~XGUI_ErrorMgr()
27 {
28
29 }
30
31 const char* toString(ModelAPI_ExecState theExecState) 
32 {
33 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
34   switch (theExecState) {
35   TO_STRING(ModelAPI_StateDone)
36   TO_STRING(ModelAPI_StateMustBeUpdated)
37   TO_STRING(ModelAPI_StateExecFailed)
38   TO_STRING(ModelAPI_StateInvalidArgument)
39   TO_STRING(ModelAPI_StateNothing)
40   default: "Unknown ExecState.";
41   }
42 #undef TO_STRING
43 }
44
45 void XGUI_ErrorMgr::onValidationStateChanged()
46 {
47   XGUI_OperationMgr* anOperationMgr = dynamic_cast<XGUI_OperationMgr*>(sender());
48   if (!anOperationMgr || !anOperationMgr->currentOperation())
49     return;
50
51   if (!myPropertyPanel)
52     return;
53
54   // get feature
55   FeaturePtr aFeature = anOperationMgr->currentOperation()->feature();
56   if (!aFeature.get() || !aFeature->data()->isValid())
57     return;
58
59   // set error indication
60   QString anError = QString::fromStdString(aFeature->error());
61   if (anError.isEmpty()) {
62     bool isDone = ( aFeature->data()->execState() == ModelAPI_StateDone
63                  || aFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
64     if (!isDone)
65       anError = toString(aFeature->data()->execState());
66   }
67
68   QWidget* aWidget = myPropertyPanel->headerWidget();
69   if (aWidget) {
70     aWidget->setToolTip(anError);
71     aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
72   }
73 }
74
75 void XGUI_ErrorMgr::onWidgetChanged()
76 {
77   static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
78
79   ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
80   if (!aModelWidget || !aModelWidget->feature().get())
81     return;
82
83   std::string anAttributeID = aModelWidget->attributeID();
84   AttributePtr anAttribute = aModelWidget->feature()->attribute(anAttributeID);
85   if (!anAttribute.get())
86     return;
87
88   std::string aValidatorID;
89   std::string anErrorMsg;
90   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
91     if (anErrorMsg.empty())
92       anErrorMsg = "unknown error.";
93     anErrorMsg = "Attribute \"" + anAttributeID + "\" invalidated by \"" + aValidatorID + "\" with error: " + anErrorMsg;
94   }
95
96   QString anError = QString::fromStdString(anErrorMsg);
97   QList<QWidget*> aWidgetList = aModelWidget->getControls();
98   foreach(QWidget* aWidget, aWidgetList) {
99     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
100     // We won't set the effect to QLabels - it looks ugly
101     if (aLabel) continue;
102
103     // Get the original tool tip of the widget
104     QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
105     // Add the error message into the tool tip
106     if (!anError.isEmpty()) {
107       if (!aTTip.isEmpty())
108         aTTip.append('\n');
109       aTTip += "Errors:\n" + anError;
110     }
111     aWidget->setToolTip(aTTip);
112     aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
113   }
114 }