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"
10 #include "XGUI_ModuleConnector.h"
11 #include "XGUI_Workshop.h"
12 #include "XGUI_ActionsMgr.h"
14 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_IModule.h>
17 #include <ModuleBase_ModelWidget.h>
18 #include <ModuleBase_OperationFeature.h>
20 #include <ModuleBase_Tools.h>
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Validator.h>
29 #include <QApplication>
30 #include <QDesktopWidget>
33 #include <QHBoxLayout>
36 const QString INVALID_VALUE = "invalid_action";
38 //#define DEBUG_ERROR_STATE
40 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
41 : ModuleBase_IErrorMgr(theParent),
44 myWorkshop(theWorkshop)
46 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
47 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
48 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
49 myAcceptAllToolTip = anAcceptAllAction->toolTip();
51 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
52 myAcceptToolTip = anOkAction->toolTip();
55 XGUI_ErrorMgr::~XGUI_ErrorMgr()
59 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
61 //update Ok Action and header of property panel if the current operation started for the feature
62 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
63 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
64 (workshop()->operationMgr()->currentOperation());
65 if (aFOperation && aFOperation->feature() == theFeature) {
66 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
67 bool isApplyEnabledByActiveWidget = false;
69 isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
70 ModuleBase_ModelWidget::ModifiedInPP;
72 QString aWidgetError = "";
73 if (!isApplyEnabledByActiveWidget) {
74 anError = myWorkshop->module()->getFeatureError(theFeature);
76 aWidgetError = anActiveWidget->getError();
77 if (anError.isEmpty())
78 anError = aWidgetError;
80 updateAcceptActionState(anError);
81 updateToolTip(anActiveWidget, aWidgetError);
82 myWorkshop->setStatusBarMessage(anError);
84 #ifdef DEBUG_ERROR_STATE
85 QString anInfo = ModuleBase_Tools::objectInfo(theFeature);
87 QString aResultInfo = QString("valid = %1, anError = %2, aWidgetError = %3")
88 .arg(anError.isEmpty()).arg(anError).arg(aWidgetError);
89 qDebug(QString("XGUI_ErrorMgr::updateActions for %1, result: %2").arg(anInfo)
90 .arg(aResultInfo).toStdString().c_str());
95 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
97 QString anError = myWorkshop->module()->getFeatureError(theFeature);
98 if (anError.isEmpty()) {
99 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
101 anError = anActiveWidget->getError();
103 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
104 if (workshop()->isFeatureOfNested(theFeature)) {
105 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
106 bool anEnabled = anError.isEmpty();
107 anAcceptAllAction->setEnabled(anEnabled);
108 anAcceptAllAction->setToolTip(!anEnabled ? anError : myAcceptAllToolTip);
112 bool XGUI_ErrorMgr::isApplyEnabled() const
114 bool isEnabled = false;
115 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
116 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
117 (workshop()->operationMgr()->currentOperation());
119 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
120 isEnabled = anOkAction && anOkAction->isEnabled();
125 void XGUI_ErrorMgr::updateAcceptActionState(const QString& theError)
127 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
128 QAction* anAcceptAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
130 bool anEnabled = theError.isEmpty();
132 anAcceptAction->setEnabled(anEnabled);
133 anAcceptAction->setToolTip(anEnabled ? myAcceptToolTip : theError);
134 // some operations have no property panel, so it is important to check that it is not null
135 if (myPropertyPanel) {
136 // update controls error information
137 QWidget* aWidget = myPropertyPanel->headerWidget();
139 aWidget->setToolTip(theError);
142 void XGUI_ErrorMgr::onWidgetChanged()
144 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
145 if (!aModelWidget || !aModelWidget->feature().get())
148 QString aWidgetError = aModelWidget->getError();
149 updateToolTip(aModelWidget, aWidgetError);
152 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
153 const QString& theError)
158 QList<QWidget*> aWidgetList = theWidget->getControls();
159 foreach(QWidget* aWidget, aWidgetList) {
160 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
161 // We won't set the effect to QLabels - it looks ugly
162 if (aLabel) continue;
164 // Get the original tool tip of the widget
165 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
166 // Add the error message into the tool tip
167 if (!theError.isEmpty()) {
168 if (!aTTip.isEmpty())
170 aTTip += "Errors:\n" + theError;
172 aWidget->setToolTip(aTTip);
173 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
177 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
179 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
180 return aConnector->workshop();
183 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
185 ModuleBase_ModelWidget* anActiveWidget = 0;
187 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
188 (workshop()->operationMgr()->currentOperation());
190 ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
191 if (aPropertyPanel) {
192 anActiveWidget = aPropertyPanel->activeWidget();
195 return anActiveWidget;