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 <ModelAPI_Attribute.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Validator.h>
26 #include <QApplication>
27 #include <QDesktopWidget>
30 #include <QHBoxLayout>
33 const QString INVALID_VALUE = "invalid_action";
36 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
37 : ModuleBase_IErrorMgr(theParent),
40 myWorkshop(theWorkshop)
42 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
43 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
44 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
45 myAcceptAllToolTip = anAcceptAllAction->toolTip();
47 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
48 myAcceptToolTip = anOkAction->toolTip();
51 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 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
65 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
66 bool isApplyEnabledByActiveWidget = false;
68 isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
69 ModuleBase_ModelWidget::ModifiedInPP;
71 QString aWidgetError = "";
72 if (!isApplyEnabledByActiveWidget) {
73 anError = myWorkshop->module()->getFeatureError(theFeature);
75 aWidgetError = anActiveWidget->getError();
76 if (anError.isEmpty())
77 anError = aWidgetError;
79 updateActionState(anOkAction, anError);
80 updateToolTip(anActiveWidget, aWidgetError);
84 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
86 QString anError = myWorkshop->module()->getFeatureError(theFeature);
87 if (anError.isEmpty()) {
88 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
90 anError = anActiveWidget->getError();
92 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
93 if (workshop()->isFeatureOfNested(theFeature)) {
94 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
95 bool anEnabled = anError.isEmpty();
96 anAcceptAllAction->setEnabled(anEnabled);
97 anAcceptAllAction->setToolTip(!anEnabled ? anError : myAcceptAllToolTip);
101 bool XGUI_ErrorMgr::isApplyEnabled() const
103 bool isEnabled = false;
104 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
105 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
106 (workshop()->operationMgr()->currentOperation());
108 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
109 isEnabled = anOkAction && anOkAction->isEnabled();
114 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const QString& theError)
116 bool anEnabled = theError.isEmpty();
118 theAction->setEnabled(anEnabled);
119 theAction->setToolTip(anEnabled ? myAcceptToolTip : theError);
120 // some operations have no property panel, so it is important to check that it is not null
121 if (myPropertyPanel) {
122 // update controls error information
123 QWidget* aWidget = myPropertyPanel->headerWidget();
125 aWidget->setToolTip(theError);
128 void XGUI_ErrorMgr::onWidgetChanged()
130 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
131 if (!aModelWidget || !aModelWidget->feature().get())
134 QString aWidgetError = aModelWidget->getError();
135 updateToolTip(aModelWidget, aWidgetError);
138 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
139 const QString& theError)
144 QList<QWidget*> aWidgetList = theWidget->getControls();
145 foreach(QWidget* aWidget, aWidgetList) {
146 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
147 // We won't set the effect to QLabels - it looks ugly
148 if (aLabel) continue;
150 // Get the original tool tip of the widget
151 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
152 // Add the error message into the tool tip
153 if (!theError.isEmpty()) {
154 if (!aTTip.isEmpty())
156 aTTip += "Errors:\n" + theError;
158 aWidget->setToolTip(aTTip);
159 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
163 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
165 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
166 return aConnector->workshop();
169 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
171 ModuleBase_ModelWidget* anActiveWidget = 0;
173 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
174 (workshop()->operationMgr()->currentOperation());
176 ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
177 if (aPropertyPanel) {
178 anActiveWidget = aPropertyPanel->activeWidget();
181 return anActiveWidget;