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>
14 #include <ModelAPI_Attribute.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
20 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent /*= 0*/)
21 : ModuleBase_IErrorMgr(theParent)
26 XGUI_ErrorMgr::~XGUI_ErrorMgr()
31 const char* toString(ModelAPI_ExecState theExecState)
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.";
45 void XGUI_ErrorMgr::onValidationStateChanged()
47 XGUI_OperationMgr* anOperationMgr = dynamic_cast<XGUI_OperationMgr*>(sender());
48 if (!anOperationMgr || !anOperationMgr->currentOperation())
55 FeaturePtr aFeature = anOperationMgr->currentOperation()->feature();
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 );
65 anError = toString(aFeature->data()->execState());
68 QWidget* aWidget = myPropertyPanel->headerWidget();
70 aWidget->setToolTip(anError);
71 aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
75 void XGUI_ErrorMgr::onWidgetChanged()
77 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
79 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
80 if (!aModelWidget || !aModelWidget->feature().get())
83 std::string anAttributeID = aModelWidget->attributeID();
84 AttributePtr anAttribute = aModelWidget->feature()->attribute(anAttributeID);
85 if (!anAttribute.get())
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;
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;
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())
109 aTTip += "Errors:\n" + anError;
111 aWidget->setToolTip(aTTip);
112 aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");