1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_ErrorMgr.h"
23 #include "XGUI_OperationMgr.h"
24 #include "XGUI_ModuleConnector.h"
25 #include "XGUI_Workshop.h"
26 #include "XGUI_ActionsMgr.h"
28 #include <ModuleBase_IPropertyPanel.h>
29 #include <ModuleBase_IWorkshop.h>
30 #include <ModuleBase_IModule.h>
31 #include <ModuleBase_ModelWidget.h>
32 #include <ModuleBase_OperationFeature.h>
34 #include <ModuleBase_Tools.h>
37 #include <ModelAPI_Attribute.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Validator.h>
43 #include <QApplication>
44 #include <QDesktopWidget>
47 #include <QHBoxLayout>
50 const QString INVALID_VALUE = "invalid_action";
52 //#define DEBUG_ERROR_STATE
54 XGUI_ErrorMgr::XGUI_ErrorMgr(QObject* theParent, ModuleBase_IWorkshop* theWorkshop)
55 : ModuleBase_IErrorMgr(theParent),
58 myWorkshop(theWorkshop),
59 myAcceptAllToolTip(""),
60 myAcceptAllStatusTip(""),
66 XGUI_ErrorMgr::~XGUI_ErrorMgr()
70 void XGUI_ErrorMgr::updateActions(const FeaturePtr& theFeature)
72 //update Ok Action and header of property panel if the current operation started for the feature
73 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
74 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
75 (workshop()->operationMgr()->currentOperation());
76 if (aFOperation && aFOperation->feature() == theFeature) {
77 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
78 bool isApplyEnabledByActiveWidget = false;
80 isApplyEnabledByActiveWidget = anActiveWidget->getValueState() ==
81 ModuleBase_ModelWidget::ModifiedInPP;
83 QString aWidgetError = "";
84 if (!isApplyEnabledByActiveWidget) {
85 anError = myWorkshop->module()->getFeatureError(theFeature);
87 aWidgetError = anActiveWidget->getError();
88 if (anError.isEmpty())
89 anError = aWidgetError;
91 updateAcceptActionState(anError);
92 updateToolTip(anActiveWidget, aWidgetError);
93 myWorkshop->setStatusBarMessage(anError);
95 #ifdef DEBUG_ERROR_STATE
96 QString anInfo = ModuleBase_Tools::objectInfo(theFeature);
98 QString aResultInfo = QString("valid = %1, anError = %2, aWidgetError = %3")
99 .arg(anError.isEmpty()).arg(anError).arg(aWidgetError);
100 qDebug(QString("XGUI_ErrorMgr::updateActions for %1, result: %2").arg(anInfo)
101 .arg(aResultInfo).toStdString().c_str());
106 void XGUI_ErrorMgr::updateAcceptAllAction(const FeaturePtr& theFeature)
108 if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
109 storeInitialActionValues();
111 QString anError = "";
112 /// to allow the module have the button always enabled
113 bool isActionStateEnabled =
114 myWorkshop->module()->isActionEnableStateFixed(XGUI_ActionsMgr::AcceptAll);
115 if (!isActionStateEnabled) {
116 anError = myWorkshop->module()->getFeatureError(theFeature);
117 if (anError.isEmpty()) {
118 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
120 anError = anActiveWidget->getError();
123 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
124 if (workshop()->isFeatureOfNested(theFeature)) {
125 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
126 bool anEnabled = anError.isEmpty();
127 anAcceptAllAction->setEnabled(anEnabled);
128 anAcceptAllAction->setToolTip(!anEnabled ? anError : myAcceptAllToolTip);
129 anAcceptAllAction->setStatusTip(!anEnabled ? anError : myAcceptAllStatusTip);
133 bool XGUI_ErrorMgr::isApplyEnabled() const
135 bool isEnabled = false;
136 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
137 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
138 (workshop()->operationMgr()->currentOperation());
140 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
141 isEnabled = anOkAction && anOkAction->isEnabled();
146 void XGUI_ErrorMgr::storeInitialActionValues()
148 ModuleBase_ModelWidget* anActiveWidget = activeWidget();
149 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
150 QAction* anAcceptAllAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
151 myAcceptAllToolTip = anAcceptAllAction->toolTip();
152 myAcceptAllStatusTip = anAcceptAllAction->statusTip();
154 QAction* anOkAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
155 myAcceptToolTip = anOkAction->toolTip();
156 myAcceptStatusTip = anOkAction->toolTip();
159 void XGUI_ErrorMgr::updateAcceptActionState(const QString& theError)
161 XGUI_ActionsMgr* anActionsMgr = workshop()->actionsMgr();
162 QAction* anAcceptAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
164 if (myAcceptAllToolTip.isEmpty() && myAcceptToolTip.isEmpty())
165 storeInitialActionValues();
167 bool anEnabled = theError.isEmpty();
168 anAcceptAction->setEnabled(anEnabled);
169 anAcceptAction->setToolTip(anEnabled ? myAcceptToolTip : theError);
170 anAcceptAction->setStatusTip(anEnabled ? myAcceptStatusTip : theError);
171 // some operations have no property panel, so it is important to check that it is not null
172 if (myPropertyPanel) {
173 // update controls error information
174 QWidget* aWidget = myPropertyPanel->headerWidget();
176 aWidget->setToolTip(theError);
179 void XGUI_ErrorMgr::onWidgetChanged()
181 ModuleBase_ModelWidget* aModelWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
182 if (!aModelWidget || !aModelWidget->feature().get())
185 QString aWidgetError = aModelWidget->getError();
186 updateToolTip(aModelWidget, aWidgetError);
189 void XGUI_ErrorMgr::updateToolTip(ModuleBase_ModelWidget* theWidget,
190 const QString& theError)
195 QList<QWidget*> aWidgetList = theWidget->getControls();
196 foreach(QWidget* aWidget, aWidgetList) {
197 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
198 // We won't set the effect to QLabels - it looks ugly
199 if (aLabel) continue;
201 // Get the original tool tip of the widget
202 QString aTTip = aWidget->toolTip().section("Errors:\n", 0, 0).trimmed();
203 // Add the error message into the tool tip
204 if (!theError.isEmpty()) {
205 if (!aTTip.isEmpty())
207 aTTip += "Errors:\n" + theError;
209 aWidget->setToolTip(aTTip);
210 //aWidget->setStyleSheet(anError.isEmpty() ? "" : "background-color:pink;");
214 XGUI_Workshop* XGUI_ErrorMgr::workshop() const
216 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
217 return aConnector->workshop();
220 ModuleBase_ModelWidget* XGUI_ErrorMgr::activeWidget() const
222 ModuleBase_ModelWidget* anActiveWidget = 0;
224 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
225 (workshop()->operationMgr()->currentOperation());
227 ModuleBase_IPropertyPanel* aPropertyPanel = aFOperation->propertyPanel();
228 if (aPropertyPanel) {
229 anActiveWidget = aPropertyPanel->activeWidget();
232 return anActiveWidget;