Salome HOME
9e379850c166e734290e95753d20f16c45c29c18
[modules/shaper.git] / src / XGUI / XGUI_ErrorMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        XGUI_ErrorMgr.cpp
4 // Created:     22 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "XGUI_ErrorMgr.h"
8
9 #include "XGUI_OperationMgr.h"
10 #include "XGUI_ModuleConnector.h"
11 #include "XGUI_Workshop.h"
12 #include "XGUI_ActionsMgr.h"
13
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>
19
20 #include <ModelAPI_Attribute.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Validator.h>
23
24 #include <QLabel>
25 #include <QAction>
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QDialog>
29 #include <QCursor>
30 #include <QHBoxLayout>
31 #include <QLabel>
32
33 const QString INVALID_VALUE = "invalid_action";
34
35
36 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
37   : ModuleBase_IErrorMgr(theParent),
38     myErrorDialog(0),
39     myErrorLabel(0),
40     myWorkshop(theWorkshop)
41 {
42
43 }
44
45 XGUI_ErrorMgr::~XGUI_ErrorMgr()
46 {
47
48 }
49
50 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
51 {
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);
58     
59     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
60     bool isApplyEnabledByActiveWidget = false;
61     if (anActiveWidget)
62       isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
63                                      ModuleBase_ModelWidget::ModifiedInPP;
64     QString anError = "";
65     QString aWidgetError = "";
66     if (!isApplyEnabledByActiveWidget) {
67       anError = myWorkshop->module()->getFeatureError(theFeature);
68       if (anActiveWidget)
69         aWidgetError = anActiveWidget->getError();
70       if (anError.isEmpty())
71         anError = aWidgetError;
72     }
73     updateActionState(anOkAction, anError);
74     updateToolTip(anActiveWidget, aWidgetError);
75   }
76 }
77
78 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
79 {
80   QString anError = myWorkshop->module()->getFeatureError(theFeature);
81   if (anError.isEmpty()) {
82     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
83     if (anActiveWidget)
84       anError = anActiveWidget->getError();
85   }
86   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
87   if (workshop()->isFeatureOfNested(theFeature)) {
88     QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
89     bool anEnabled = anError.isEmpty();
90     anAcceptAllAction->setEnabled(anEnabled);
91     anAcceptAllAction->setToolTip(anError);
92   }
93 }
94
95 bool XGUI_ErrorMgr::isApplyEnabled() const
96 {
97   bool isEnabled = false;
98   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
99   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
100                                       (workshop()->operationMgr()->currentOperation());
101   if (aFOperation) {
102     QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
103     isEnabled = anOkAction && anOkAction->isEnabled();
104   }
105   return isEnabled;
106 }
107
108 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const QString& theError)
109 {
110   bool anEnabled = theError.isEmpty();
111
112   theAction->setEnabled(anEnabled);
113   // some operations have no property panel, so it is important to check that it is not null
114   if (myPropertyPanel) {
115     // update controls error information
116     QWidget* aWidget = myPropertyPanel->headerWidget();
117     if (aWidget)
118       aWidget->setToolTip(theError);
119   }
120 }
121 void XGUI_ErrorMgr::onWidgetChanged()
122 {
123   ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
124   if (!aModelWidget || !aModelWidget->feature().get())
125     return;
126
127   QString aWidgetError = aModelWidget->getError();
128   updateToolTip(aModelWidget, aWidgetError);
129 }
130
131 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
132                                   const QString& theError)
133 {
134   if (!theWidget)
135     return;
136
137   QList<QWidget*> aWidgetList = theWidget->getControls();
138   foreach(QWidget* aWidget, aWidgetList) {
139     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
140     // We won't set the effect to QLabels - it looks ugly
141     if (aLabel) continue;
142
143     // Get the original tool tip of the widget
144     QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
145     // Add the error message into the tool tip
146     if (!theError.isEmpty()) {
147       if (!aTTip.isEmpty())
148         aTTip.append('\n');
149       aTTip += "Errors:\n" + theError;
150     }
151     aWidget->setToolTip(aTTip);
152     //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
153   }
154 }
155
156 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
157 {
158   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
159   return aConnector->workshop();
160 }
161
162 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
163 {
164   ModuleBase_ModelWidget* anActiveWidget = 0;
165
166   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
167                                       (workshop()->operationMgr()->currentOperation());
168   if (aFOperation) {
169     ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
170     if (aPropertyPanel) {
171       anActiveWidget = aPropertyPanel->activeWidget();
172     }
173   }
174   return anActiveWidget;
175 }
176