]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ErrorMgr.cpp
Salome HOME
Issue #1860: fix end lines with spaces
[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 <ModuleBase_Tools.h>
21 #include <QDebug>
22
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Validator.h>
26
27 #include <QLabel>
28 #include <QAction>
29 #include <QApplication>
30 #include <QDesktopWidget>
31 #include <QDialog>
32 #include <QCursor>
33 #include <QHBoxLayout>
34 #include <QLabel>
35
36 const QString INVALID_VALUE = "invalid_action";
37
38 //#define DEBUG_ERROR_STATE
39
40 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
41   : ModuleBase_IErrorMgr(theParent),
42     myErrorDialog(0),
43     myErrorLabel(0),
44     myWorkshop(theWorkshop),
45     myAcceptAllToolTip(""),
46     myAcceptAllStatusTip(""),
47     myAcceptToolTip(""),
48     myAcceptStatusTip("")
49 {
50 }
51
52 XGUI_ErrorMgr::~XGUI_ErrorMgr()
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     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
64     bool isApplyEnabledByActiveWidget = false;
65     if (anActiveWidget)
66       isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
67                                      ModuleBase_ModelWidget::ModifiedInPP;
68     QString anError = "";
69     QString aWidgetError = "";
70     if (!isApplyEnabledByActiveWidget) {
71       anError = myWorkshop->module()->getFeatureError(theFeature);
72       if (anActiveWidget)
73         aWidgetError = anActiveWidget->getError();
74       if (anError.isEmpty())
75         anError = aWidgetError;
76     }
77     updateAcceptActionState(anError);
78     updateToolTip(anActiveWidget, aWidgetError);
79     myWorkshop->setStatusBarMessage(anError);
80
81 #ifdef DEBUG_ERROR_STATE
82     QString anInfo = ModuleBase_Tools::objectInfo(theFeature);
83
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());
88 #endif
89   }
90 }
91
92 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
93 {
94   if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
95     storeInitialActionValues();
96
97   QString anError = "";
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();
105       if (anActiveWidget)
106         anError = anActiveWidget->getError();
107     }
108   }
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);
116   }
117 }
118
119 bool XGUI_ErrorMgr::isApplyEnabled() const
120 {
121   bool isEnabled = false;
122   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
123   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124                                       (workshop()->operationMgr()->currentOperation());
125   if (aFOperation) {
126     QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
127     isEnabled = anOkAction && anOkAction->isEnabled();
128   }
129   return isEnabled;
130 }
131
132 void XGUI_ErrorMgr::storeInitialActionValues()
133 {
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();
139
140   QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
141   myAcceptToolTip = anOkAction->toolTip();
142   myAcceptStatusTip = anOkAction->toolTip();
143 }
144
145 void XGUI_ErrorMgr::updateAcceptActionState(const QString& theError)
146 {
147   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
148   QAction* anAcceptAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
149
150   if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
151     storeInitialActionValues();
152
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();
161     if (aWidget)
162       aWidget->setToolTip(theError);
163   }
164 }
165 void XGUI_ErrorMgr::onWidgetChanged()
166 {
167   ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
168   if (!aModelWidget || !aModelWidget->feature().get())
169     return;
170
171   QString aWidgetError = aModelWidget->getError();
172   updateToolTip(aModelWidget, aWidgetError);
173 }
174
175 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
176                                   const QString& theError)
177 {
178   if (!theWidget)
179     return;
180
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;
186
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())
192         aTTip.append('\n');
193       aTTip += "Errors:\n" + theError;
194     }
195     aWidget->setToolTip(aTTip);
196     //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
197   }
198 }
199
200 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
201 {
202   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
203   return aConnector->workshop();
204 }
205
206 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
207 {
208   ModuleBase_ModelWidget* anActiveWidget = 0;
209
210   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
211                                       (workshop()->operationMgr()->currentOperation());
212   if (aFOperation) {
213     ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
214     if (aPropertyPanel) {
215       anActiveWidget = aPropertyPanel->activeWidget();
216     }
217   }
218   return anActiveWidget;
219 }
220