Salome HOME
Issue #1055: abort all operations on Property panel closing
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_PropertyPanel.cpp
5  *
6  *  Created on: Apr 29, 2014
7  *      Author: sbh
8  */
9
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ActionsMgr.h>
12 #include <XGUI_OperationMgr.h>
13 //#include <AppElements_Constants.h>
14 #include <ModuleBase_WidgetMultiSelector.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_PageBase.h>
17 #include <ModuleBase_PageWidget.h>
18
19 #include <QEvent>
20 #include <QFrame>
21 #include <QIcon>
22 #include <QKeyEvent>
23 #include <QLayoutItem>
24 #include <QToolButton>
25 #include <QVBoxLayout>
26 #include <QGridLayout>
27 #include <QWidget>
28 #include <QToolButton>
29 #include <QAction>
30
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
36     : ModuleBase_IPropertyPanel(theParent), 
37     myActiveWidget(NULL),
38     myPreselectionWidget(NULL),
39     myPanelPage(NULL),
40     myOperationMgr(theMgr)
41 {
42   this->setWindowTitle(tr("Property Panel"));
43   QAction* aViewAct = this->toggleViewAction();
44   this->setObjectName(PROP_PANEL);
45   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
46
47   QWidget* aContent = new QWidget(this);
48   QGridLayout* aMainLayout = new QGridLayout(aContent);
49   const int kPanelColumn = 0;
50   int aPanelRow = 0;
51   aMainLayout->setContentsMargins(3, 3, 3, 3);
52   this->setWidget(aContent);
53
54   QFrame* aFrm = new QFrame(aContent);
55   aFrm->setFrameStyle(QFrame::Raised);
56   aFrm->setFrameShape(QFrame::Panel);
57   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
58   ModuleBase_Tools::zeroMargins(aBtnLay);
59   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
60
61   myHeaderWidget = aFrm;
62
63   QStringList aBtnNames;
64   aBtnNames << QString(PROP_PANEL_HELP)
65             << QString(PROP_PANEL_OK)
66             << QString(PROP_PANEL_CANCEL);
67   foreach(QString eachBtnName, aBtnNames) {
68     QToolButton* aBtn = new QToolButton(aFrm);
69     aBtn->setObjectName(eachBtnName);
70     aBtn->setAutoRaise(true);
71     aBtnLay->addWidget(aBtn);
72   }
73   aBtnLay->insertStretch(1, 1);
74
75   myPanelPage = new ModuleBase_PageWidget(aContent);
76   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
77   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
78 }
79
80 XGUI_PropertyPanel::~XGUI_PropertyPanel()
81 {
82 }
83
84 void XGUI_PropertyPanel::cleanContent()
85 {
86   if (myActiveWidget)
87     myActiveWidget->deactivate();
88
89   /// as the widgets are deleted later, it is important that the signals
90   /// of these widgets are not processed. An example of the error is issue 986.
91   /// In the given case, the property panel is firstly filled by new widgets
92   /// of restarted operation and after that the mouse release signal come from
93   /// the widget of the previous operation (Point2d widget about mouse is released
94   /// and focus is out of this widget)
95   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
96                                                  aLast = myWidgets.end();
97   for (; anIt != aLast; anIt++) {
98     QWidget* aWidget = *anIt;
99     if (aWidget) {
100       aWidget->blockSignals(true);
101     }
102   }
103
104   myWidgets.clear();
105   myPanelPage->clearPage();
106   myActiveWidget = NULL;
107   setWindowTitle(tr("Property Panel"));
108 }
109
110 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
111 {
112   myWidgets = theWidgets;
113   if (theWidgets.empty()) return;
114   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
115     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
116             this,    SLOT(activateWidget(ModuleBase_ModelWidget*)));
117     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
118             this,    SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
119     connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
120             this,    SIGNAL(keyReleased(QKeyEvent*)));
121   }
122   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
123   if (aLastWidget) {
124     QList<QWidget*> aControls = aLastWidget->getControls();
125     if (!aControls.empty()) {
126       QWidget* aLastControl = aControls.last();
127
128       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
129       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
130
131       setTabOrder(aLastControl, anOkBtn);
132       setTabOrder(anOkBtn, aCancelBtn);
133     }
134   }
135 }
136
137 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
138 {
139   return myWidgets;
140 }
141
142 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
143 {
144
145   return static_cast<ModuleBase_PageBase*>(myPanelPage);
146 }
147
148 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
149 {
150   // Invalid feature case on abort of the operation
151   if (theFeature.get() == NULL)
152     return;
153   if (theFeature->isAction() || !theFeature->data())
154     return;
155   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
156     if (!eachWidget->feature().get())
157       eachWidget->setFeature(theFeature);
158     eachWidget->restoreValue();
159   }
160   // the repaint is used here to immediately react in GUI to the values change.
161   repaint();
162 }
163
164 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
165 {
166   // TO CHECK: Editing operation does not have automatical activation of widgets
167   if (isEditingMode()) {
168     activateWidget(NULL);
169     return;
170   }
171   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
172   bool isFoundWidget = false;
173   activateWindow();
174   for (; anIt != aLast; anIt++) {
175     if (isFoundWidget || !theWidget) {
176       if ((*anIt)->focusTo()) {
177         return;
178       }
179     }
180     isFoundWidget = (*anIt) == theWidget;
181   }
182   activateWidget(NULL);
183 }
184
185 void XGUI_PropertyPanel::activateNextWidget()
186 {
187   activateNextWidget(myActiveWidget);
188 }
189
190 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
191 {
192   // Avoid activation of already actve widget. It could happen on focusIn event many times
193   if (theWidget == myActiveWidget) {
194     return;
195   }
196   if(myActiveWidget) {
197     myActiveWidget->deactivate();
198     myActiveWidget->setHighlighted(false);
199   }
200   if(theWidget) {
201     emit beforeWidgetActivated(theWidget);
202     theWidget->setHighlighted(true);
203     theWidget->activate();
204   }
205   myActiveWidget = theWidget;
206   if (myActiveWidget) {
207     emit widgetActivated(theWidget);
208   } else if (!isEditingMode()) {
209     emit noMoreWidgets();
210   }
211 }
212
213 void XGUI_PropertyPanel::setFocusOnOkButton()
214 {
215   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
216   anOkBtn->setFocus();
217 }
218
219 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
220 {
221   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
222   anCancelBtn->setEnabled(theEnabled);
223 }
224
225 bool XGUI_PropertyPanel::isCancelEnabled() const
226 {
227   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
228   return anCancelBtn->isEnabled();
229 }
230
231 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
232 {
233   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
234   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
235     aWgt->setEditingMode(isEditing);
236   }
237 }
238
239 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
240 {
241   QStringList aButtonNames;
242   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
243   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
244   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
245   for (int i = 0; i < aButtonNames.size(); ++i) {
246     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
247     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
248     aBtn->setDefaultAction(anAct);
249   }
250 }
251
252 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
253 {
254   return myPreselectionWidget;
255 }
256
257 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
258 {
259   myPreselectionWidget = theWidget;
260 }
261
262
263 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
264 {
265   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
266   if (aOp) {
267     if (myOperationMgr->canStopOperation(aOp)) {
268       myOperationMgr->abortAllOperations();
269       theEvent->accept();
270     } else 
271       theEvent->ignore();
272   } else
273     ModuleBase_IPropertyPanel::closeEvent(theEvent);
274 }