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)
45 XGUI_ErrorMgr::~XGUI_ErrorMgr()
50 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
52 //update Ok Action and header of property panel if the current operation started for the feature
53 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
54 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
55 (workshop()->operationMgr()->currentOperation());
56 if (aFOperation && aFOperation->feature() == theFeature) {
57 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
59 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
60 bool isApplyEnabledByActiveWidget = false;
62 isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
63 ModuleBase_ModelWidget::ModifiedInPP;
65 QString aWidgetError = "";
66 if (!isApplyEnabledByActiveWidget) {
67 anError = myWorkshop->module()->getFeatureError(theFeature);
68 aWidgetError = myWorkshop->module()->getWidgetError(anActiveWidget);
69 if (anError.isEmpty())
70 anError = aWidgetError;
72 updateActionState(anOkAction, anError);
73 updateToolTip(anActiveWidget, aWidgetError);
77 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
79 QString anError = myWorkshop->module()->getFeatureError(theFeature);
80 if (anError.isEmpty())
81 anError = myWorkshop->module()->getWidgetError(activeWidget());
83 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
84 if (workshop()->isFeatureOfNested(theFeature)) {
85 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
86 bool anEnabled = anError.isEmpty();
87 anAcceptAllAction->setEnabled(anEnabled);
88 anAcceptAllAction->setToolTip(anError);
92 bool XGUI_ErrorMgr::isApplyEnabled() const
94 bool isEnabled = false;
95 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
96 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
97 (workshop()->operationMgr()->currentOperation());
99 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
100 isEnabled = anOkAction && anOkAction->isEnabled();
105 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const QString& theError)
107 bool anEnabled = theError.isEmpty();
109 theAction->setEnabled(anEnabled);
110 // some operations have no property panel, so it is important to check that it is not null
111 if (myPropertyPanel) {
112 // update controls error information
113 QWidget* aWidget = myPropertyPanel->headerWidget();
115 aWidget->setToolTip(theError);
118 void XGUI_ErrorMgr::onWidgetChanged()
120 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
121 if (!aModelWidget || !aModelWidget->feature().get())
124 QString aWidgetError = myWorkshop->module()->getWidgetError(aModelWidget);
125 updateToolTip(aModelWidget, aWidgetError);
128 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
129 const QString& theError)
134 QList<QWidget*> aWidgetList = theWidget->getControls();
135 foreach(QWidget* aWidget, aWidgetList) {
136 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
137 // We won't set the effect to QLabels - it looks ugly
138 if (aLabel) continue;
140 // Get the original tool tip of the widget
141 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
142 // Add the error message into the tool tip
143 if (!theError.isEmpty()) {
144 if (!aTTip.isEmpty())
146 aTTip += "Errors:\n" + theError;
148 aWidget->setToolTip(aTTip);
149 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
153 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
155 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
156 return aConnector->workshop();
159 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
161 ModuleBase_ModelWidget* anActiveWidget = 0;
163 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
164 (workshop()->operationMgr()->currentOperation());
166 ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
167 if (aPropertyPanel) {
168 anActiveWidget = aPropertyPanel->activeWidget();
171 return anActiveWidget;