1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: XGUI_ErrorMgr.cpp
4 // Created: 22 July 2015
5 // Author: Sergey POKHODENKO
7 #include "XGUI_ErrorMgr.h"
9 #include "XGUI_OperationMgr.h"
11 #include <ModuleBase_IPropertyPanel.h>
12 #include <ModuleBase_ModelWidget.h>
13 #include <ModuleBase_OperationFeature.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_Session.h>
17 #include <ModelAPI_Validator.h>
21 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent /*= 0*/)
22 : ModuleBase_IErrorMgr(theParent)
27 XGUI_ErrorMgr::~XGUI_ErrorMgr()
32 const char* toString(ModelAPI_ExecState theExecState)
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: return "Unknown ExecState.";
46 void XGUI_ErrorMgr::onValidationStateChanged()
48 XGUI_OperationMgr* anOperationMgr = dynamic_cast<XGUI_OperationMgr*>(sender());
51 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
52 (anOperationMgr->currentOperation());
53 if (!myPropertyPanel || !aFOperation)
57 FeaturePtr aFeature = aFOperation->feature();
58 if (!aFeature.get() || !aFeature->data()->isValid())
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 );
67 anError = toString(aFeature->data()->execState());
70 QWidget* aWidget = myPropertyPanel->headerWidget();
72 aWidget->setToolTip(anError);
73 aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
77 void XGUI_ErrorMgr::onWidgetChanged()
79 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
81 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
82 if (!aModelWidget || !aModelWidget->feature().get())
85 std::string anAttributeID = aModelWidget->attributeID();
86 AttributePtr anAttribute = aModelWidget->feature()->attribute(anAttributeID);
87 if (!anAttribute.get())
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 = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
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;
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())
111 aTTip += "Errors:\n" + anError;
113 aWidget->setToolTip(aTTip);
114 aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");