Salome HOME
Make same planes cannot be used twice in partition tool
[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   ModuleBase_ModelWidget* anActiveWidget = activeWidget();
43   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
44   QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
45   myAcceptAllToolTip = anAcceptAllAction->toolTip();
46
47   QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
48   myAcceptToolTip = anOkAction->toolTip();
49 }
50
51 XGUI_ErrorMgr::~XGUI_ErrorMgr()
52 {
53
54 }
55
56 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
57 {
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);
64     
65     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
66     bool isApplyEnabledByActiveWidget = false;
67     if (anActiveWidget)
68       isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
69                                      ModuleBase_ModelWidget::ModifiedInPP;
70     QString anError = "";
71     QString aWidgetError = "";
72     if (!isApplyEnabledByActiveWidget) {
73       anError = myWorkshop->module()->getFeatureError(theFeature);
74       if (anActiveWidget)
75         aWidgetError = anActiveWidget->getError();
76       if (anError.isEmpty())
77         anError = aWidgetError;
78     }
79     updateActionState(anOkAction, anError);
80     updateToolTip(anActiveWidget, aWidgetError);
81   }
82 }
83
84 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
85 {
86   QString anError = myWorkshop->module()->getFeatureError(theFeature);
87   if (anError.isEmpty()) {
88     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
89     if (anActiveWidget)
90       anError = anActiveWidget->getError();
91   }
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);
98   }
99 }
100
101 bool XGUI_ErrorMgr::isApplyEnabled() const
102 {
103   bool isEnabled = false;
104   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
105   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
106                                       (workshop()->operationMgr()->currentOperation());
107   if (aFOperation) {
108     QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
109     isEnabled = anOkAction && anOkAction->isEnabled();
110   }
111   return isEnabled;
112 }
113
114 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const QString& theError)
115 {
116   bool anEnabled = theError.isEmpty();
117
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();
124     if (aWidget)
125       aWidget->setToolTip(theError);
126   }
127 }
128 void XGUI_ErrorMgr::onWidgetChanged()
129 {
130   ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
131   if (!aModelWidget || !aModelWidget->feature().get())
132     return;
133
134   QString aWidgetError = aModelWidget->getError();
135   updateToolTip(aModelWidget, aWidgetError);
136 }
137
138 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
139                                   const QString& theError)
140 {
141   if (!theWidget)
142     return;
143
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;
149
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())
155         aTTip.append('\n');
156       aTTip += "Errors:\n" + theError;
157     }
158     aWidget->setToolTip(aTTip);
159     //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
160   }
161 }
162
163 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
164 {
165   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
166   return aConnector->workshop();
167 }
168
169 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
170 {
171   ModuleBase_ModelWidget* anActiveWidget = 0;
172
173   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
174                                       (workshop()->operationMgr()->currentOperation());
175   if (aFOperation) {
176     ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
177     if (aPropertyPanel) {
178       anActiveWidget = aPropertyPanel->activeWidget();
179     }
180   }
181   return anActiveWidget;
182 }
183