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),
45 myAcceptAllToolTip(""),
46 myAcceptAllStatusTip(""),
52 XGUI_ErrorMgr::~XGUI_ErrorMgr()
56 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
58 //update Ok Action and header of property panel if the current operation started for the feature
59 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
60 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
61 (workshop()->operationMgr()->currentOperation());
62 if (aFOperation && aFOperation->feature() == theFeature) {
63 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
64 bool isApplyEnabledByActiveWidget = false;
66 isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
67 ModuleBase_ModelWidget::ModifiedInPP;
69 QString aWidgetError = "";
70 if (!isApplyEnabledByActiveWidget) {
71 anError = myWorkshop->module()->getFeatureError(theFeature);
73 aWidgetError = anActiveWidget->getError();
74 if (anError.isEmpty())
75 anError = aWidgetError;
77 updateAcceptActionState(anError);
78 updateToolTip(anActiveWidget, aWidgetError);
79 myWorkshop->setStatusBarMessage(anError);
81 #ifdef DEBUG_ERROR_STATE
82 QString anInfo = ModuleBase_Tools::objectInfo(theFeature);
84 QString aResultInfo = QString("valid = %1, anError = %2, aWidgetError = %3")
85 .arg(anError.isEmpty()).arg(anError).arg(aWidgetError);
86 qDebug(QString("XGUI_ErrorMgr::updateActions for %1, result: %2").arg(anInfo)
87 .arg(aResultInfo).toStdString().c_str());
92 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
94 if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
95 storeInitialActionValues();
98 /// to allow the module have the button always enabled
99 bool isActionStateEnabled =
100 myWorkshop->module()->isActionEnableStateFixed(XGUI_ActionsMgr::AcceptAll);
101 if (!isActionStateEnabled) {
102 anError = myWorkshop->module()->getFeatureError(theFeature);
103 if (anError.isEmpty()) {
104 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
106 anError = anActiveWidget->getError();
109 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
110 if (workshop()->isFeatureOfNested(theFeature)) {
111 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
112 bool anEnabled = anError.isEmpty();
113 anAcceptAllAction->setEnabled(anEnabled);
114 anAcceptAllAction->setToolTip(!anEnabled ? anError : myAcceptAllToolTip);
115 anAcceptAllAction->setStatusTip(!anEnabled ? anError : myAcceptAllStatusTip);
119 bool XGUI_ErrorMgr::isApplyEnabled() const
121 bool isEnabled = false;
122 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
123 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124 (workshop()->operationMgr()->currentOperation());
126 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
127 isEnabled = anOkAction && anOkAction->isEnabled();
132 void XGUI_ErrorMgr::storeInitialActionValues()
134 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
135 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
136 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
137 myAcceptAllToolTip = anAcceptAllAction->toolTip();
138 myAcceptAllStatusTip = anAcceptAllAction->statusTip();
140 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
141 myAcceptToolTip = anOkAction->toolTip();
142 myAcceptStatusTip = anOkAction->toolTip();
145 void XGUI_ErrorMgr::updateAcceptActionState(const QString& theError)
147 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
148 QAction* anAcceptAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
150 if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
151 storeInitialActionValues();
153 bool anEnabled = theError.isEmpty();
154 anAcceptAction->setEnabled(anEnabled);
155 anAcceptAction->setToolTip(anEnabled ? myAcceptToolTip : theError);
156 anAcceptAction->setStatusTip(anEnabled ? myAcceptStatusTip : theError);
157 // some operations have no property panel, so it is important to check that it is not null
158 if (myPropertyPanel) {
159 // update controls error information
160 QWidget* aWidget = myPropertyPanel->headerWidget();
162 aWidget->setToolTip(theError);
165 void XGUI_ErrorMgr::onWidgetChanged()
167 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
168 if (!aModelWidget || !aModelWidget->feature().get())
171 QString aWidgetError = aModelWidget->getError();
172 updateToolTip(aModelWidget, aWidgetError);
175 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
176 const QString& theError)
181 QList<QWidget*> aWidgetList = theWidget->getControls();
182 foreach(QWidget* aWidget, aWidgetList) {
183 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
184 // We won't set the effect to QLabels - it looks ugly
185 if (aLabel) continue;
187 // Get the original tool tip of the widget
188 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
189 // Add the error message into the tool tip
190 if (!theError.isEmpty()) {
191 if (!aTTip.isEmpty())
193 aTTip += "Errors:\n" + theError;
195 aWidget->setToolTip(aTTip);
196 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
200 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
202 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
203 return aConnector->workshop();
206 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
208 ModuleBase_ModelWidget* anActiveWidget = 0;
210 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
211 (workshop()->operationMgr()->currentOperation());
213 ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
214 if (aPropertyPanel) {
215 anActiveWidget = aPropertyPanel->activeWidget();
218 return anActiveWidget;