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