Salome HOME
Issue #1062: Parameter value is thrown down in the point selector control
[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     QString anError = myWorkshop->module()->getFeatureError(theFeature);
59
60     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
61     QString aWidgetError = myWorkshop->module()->getWidgetError(anActiveWidget);
62     if (anError.isEmpty())
63       anError = aWidgetError;
64
65     updateActionState(anOkAction, anError);
66     updateToolTip(anActiveWidget, aWidgetError);
67   }
68 }
69
70 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
71 {
72   QString anError = myWorkshop->module()->getFeatureError(theFeature);
73   if (anError.isEmpty())
74     anError = myWorkshop->module()->getWidgetError(activeWidget());
75
76   XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
77   if (workshop()->isFeatureOfNested(theFeature)) {
78     QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
79     bool anEnabled = anError.isEmpty();
80     anAcceptAllAction->setEnabled(anEnabled);
81     anAcceptAllAction->setToolTip(anError);
82   }
83 }
84
85 void XGUI_ErrorMgr::updateActionState(QAction* theAction, const QString& theError)
86 {
87   bool anEnabled = theError.isEmpty();
88
89   theAction->setEnabled(anEnabled);
90   // some operations have no property panel, so it is important to check that it is not null
91   if (myPropertyPanel) {
92     // update controls error information
93     QWidget* aWidget = myPropertyPanel->headerWidget();
94     if (aWidget)
95       aWidget->setToolTip(theError);
96   }
97 }
98 void XGUI_ErrorMgr::onWidgetChanged()
99 {
100   ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
101   if (!aModelWidget || !aModelWidget->feature().get())
102     return;
103
104   QString aWidgetError = myWorkshop->module()->getWidgetError(aModelWidget);
105   updateToolTip(aModelWidget, aWidgetError);
106 }
107
108 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
109                                   const QString& theError)
110 {
111   if (!theWidget)
112     return;
113
114   QList<QWidget*> aWidgetList = theWidget->getControls();
115   foreach(QWidget* aWidget, aWidgetList) {
116     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
117     // We won't set the effect to QLabels - it looks ugly
118     if (aLabel) continue;
119
120     // Get the original tool tip of the widget
121     QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
122     // Add the error message into the tool tip
123     if (!theError.isEmpty()) {
124       if (!aTTip.isEmpty())
125         aTTip.append('\n');
126       aTTip += "Errors:\n" + theError;
127     }
128     aWidget->setToolTip(aTTip);
129     //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
130   }
131 }
132
133 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
134 {
135   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
136   return aConnector->workshop();
137 }
138
139 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
140 {
141   ModuleBase_ModelWidget* anActiveWidget = 0;
142
143   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
144                                       (workshop()->operationMgr()->currentOperation());
145   if (aFOperation) {
146     ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
147     if (aPropertyPanel) {
148       anActiveWidget = aPropertyPanel->activeWidget();
149     }
150   }
151   return anActiveWidget;
152 }
153