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 #include <QApplication>
22 #include <QDesktopWidget>
25 #include <QHBoxLayout>
28 const QString INVALID_VALUE = "invalid_action";
31 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent /*= 0*/)
32 : ModuleBase_IErrorMgr(theParent),
39 XGUI_ErrorMgr::~XGUI_ErrorMgr()
44 bool XGUI_ErrorMgr::canProcessClick(QAction* theAction, const FeaturePtr& theFeature)
46 QString aData = theAction->data().toString();
48 bool isActionEnabled = theAction->data() != INVALID_VALUE;
50 QString anError = getFeatureError(theFeature);
51 if (!isActionEnabled && !anError.isEmpty()) {
53 myErrorDialog = new QDialog(QApplication::desktop(), Qt::Popup);
54 QHBoxLayout* aLay = new QHBoxLayout(myErrorDialog);
55 aLay->setContentsMargins(0, 0, 0, 0);
57 QFrame* aMarginWidget = new QFrame(myErrorDialog);
58 aMarginWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
60 aLay->addWidget(aMarginWidget);
61 QHBoxLayout* aMarginLay = new QHBoxLayout(aMarginWidget);
62 aMarginLay->setContentsMargins(4, 4, 4, 4);
64 myErrorLabel = new QLabel(aMarginWidget);
65 aMarginLay->addWidget(myErrorLabel);
67 myErrorLabel->setText(anError);
68 myErrorDialog->move(QCursor::pos());
69 myErrorDialog->show();
71 return isActionEnabled;
74 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const FeaturePtr& theFeature,
75 const bool theEnabled)
77 bool isActionEnabled = theAction->data() != INVALID_VALUE;
78 if (theEnabled == isActionEnabled)
81 theAction->setIcon(theEnabled ? QIcon(":pictures/button_ok.png"): QIcon(":pictures/button_ok_error.png"));
82 QWidget* aWidget = myPropertyPanel->headerWidget();
84 theAction->setData("");
85 aWidget->setToolTip("");
88 theAction->setData(INVALID_VALUE);
89 aWidget->setToolTip(getFeatureError(theFeature));
93 const char* toString(ModelAPI_ExecState theExecState)
95 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
96 switch (theExecState) {
97 TO_STRING(ModelAPI_StateDone)
98 TO_STRING(ModelAPI_StateMustBeUpdated)
99 TO_STRING(ModelAPI_StateExecFailed)
100 TO_STRING(ModelAPI_StateInvalidArgument)
101 TO_STRING(ModelAPI_StateNothing)
102 default: return "Unknown ExecState.";
107 /*void XGUI_ErrorMgr::onValidationStateChanged()
109 XGUI_OperationMgr* anOperationMgr = dynamic_cast<XGUI_OperationMgr*>(sender());
112 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
113 (anOperationMgr->currentOperation());
114 if (!myPropertyPanel || !aFOperation)
117 FeaturePtr aFeature = aFOperation->feature();
118 QString anError = getFeatureError(aFeature);
120 QWidget* aWidget = myPropertyPanel->headerWidget();
122 aWidget->setToolTip(anError);
123 aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
127 QString XGUI_ErrorMgr::getFeatureError(const FeaturePtr& theFeature) const
131 if (!theFeature.get() || !theFeature->data()->isValid())
134 // set error indication
135 anError = QString::fromStdString(theFeature->error());
136 if (anError.isEmpty()) {
137 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
138 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
140 anError = toString(theFeature->data()->execState());
146 void XGUI_ErrorMgr::onWidgetChanged()
148 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
150 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
151 if (!aModelWidget || !aModelWidget->feature().get())
154 std::string anAttributeID = aModelWidget->attributeID();
155 AttributePtr anAttribute = aModelWidget->feature()->attribute(anAttributeID);
156 if (!anAttribute.get())
159 std::string aValidatorID;
160 std::string anErrorMsg;
161 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
162 if (anErrorMsg.empty())
163 anErrorMsg = "unknown error.";
164 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
167 QString anError = QString::fromStdString(anErrorMsg);
168 QList<QWidget*> aWidgetList = aModelWidget->getControls();
169 foreach(QWidget* aWidget, aWidgetList) {
170 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
171 // We won't set the effect to QLabels - it looks ugly
172 if (aLabel) continue;
174 // Get the original tool tip of the widget
175 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
176 // Add the error message into the tool tip
177 if (!anError.isEmpty()) {
178 if (!aTTip.isEmpty())
180 aTTip += "Errors:\n" + anError;
182 aWidget->setToolTip(aTTip);
183 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");