]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Correction tab logic.
[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 <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <QEvent>
23 #include <QFrame>
24 #include <QIcon>
25 #include <QKeyEvent>
26 #include <QLayoutItem>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
30 #include <QWidget>
31 #include <QToolButton>
32 #include <QAction>
33
34 #ifdef _DEBUG
35 #include <iostream>
36 #endif
37
38 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
39     : ModuleBase_IPropertyPanel(theParent), 
40     myActiveWidget(NULL),
41     myPreselectionWidget(NULL),
42     myPanelPage(NULL),
43     myOperationMgr(theMgr)
44 {
45   this->setWindowTitle(tr("Property Panel"));
46   QAction* aViewAct = this->toggleViewAction();
47   this->setObjectName(PROP_PANEL);
48   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
49
50   QWidget* aContent = new QWidget(this);
51   QGridLayout* aMainLayout = new QGridLayout(aContent);
52   const int kPanelColumn = 0;
53   int aPanelRow = 0;
54   aMainLayout->setContentsMargins(3, 3, 3, 3);
55   this->setWidget(aContent);
56
57   QFrame* aFrm = new QFrame(aContent);
58   aFrm->setFrameStyle(QFrame::Raised);
59   aFrm->setFrameShape(QFrame::Panel);
60   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
61   ModuleBase_Tools::zeroMargins(aBtnLay);
62   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
63
64   myHeaderWidget = aFrm;
65
66   QStringList aBtnNames;
67   aBtnNames << QString(PROP_PANEL_HELP)
68             << QString(PROP_PANEL_OK)
69             << QString(PROP_PANEL_CANCEL);
70   foreach(QString eachBtnName, aBtnNames) {
71     QToolButton* aBtn = new QToolButton(aFrm);
72     aBtn->setObjectName(eachBtnName);
73     aBtn->setAutoRaise(true);
74     aBtnLay->addWidget(aBtn);
75   }
76   aBtnLay->insertStretch(1, 1);
77
78   myPanelPage = new ModuleBase_PageWidget(aContent);
79   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
80   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
81 }
82
83 XGUI_PropertyPanel::~XGUI_PropertyPanel()
84 {
85 }
86
87 void XGUI_PropertyPanel::cleanContent()
88 {
89   if (myActiveWidget)
90     myActiveWidget->deactivate();
91
92   /// as the widgets are deleted later, it is important that the signals
93   /// of these widgets are not processed. An example of the error is issue 986.
94   /// In the given case, the property panel is firstly filled by new widgets
95   /// of restarted operation and after that the mouse release signal come from
96   /// the widget of the previous operation (Point2d widget about mouse is released
97   /// and focus is out of this widget)
98   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
99                                                  aLast = myWidgets.end();
100   for (; anIt != aLast; anIt++) {
101     QWidget* aWidget = *anIt;
102     if (aWidget) {
103       aWidget->blockSignals(true);
104     }
105   }
106
107   myWidgets.clear();
108   myPanelPage->clearPage();
109   myActiveWidget = NULL;
110   setWindowTitle(tr("Property Panel"));
111 }
112
113 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
114 {
115   myWidgets = theWidgets;
116   if (theWidgets.empty()) return;
117   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
118     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
119             this,    SLOT(activateWidget(ModuleBase_ModelWidget*)));
120     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
121             this,    SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
122     connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
123             this,    SIGNAL(keyReleased(QKeyEvent*)));
124     connect(aWidget, SIGNAL(enterClicked()),
125             this,    SIGNAL(enterClicked()));
126
127   }
128 }
129
130 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
131 {
132   return myWidgets;
133 }
134
135 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
136 {
137   return static_cast<ModuleBase_PageBase*>(myPanelPage);
138 }
139
140 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
141 {
142   // Invalid feature case on abort of the operation
143   if (theFeature.get() == NULL)
144     return;
145   if (theFeature->isAction() || !theFeature->data())
146     return;
147   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
148     if (!eachWidget->feature().get())
149       eachWidget->setFeature(theFeature);
150     eachWidget->restoreValue();
151   }
152   // the repaint is used here to immediately react in GUI to the values change.
153   repaint();
154 }
155
156 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
157 {
158   // TO CHECK: Editing operation does not have automatical activation of widgets
159   if (isEditingMode()) {
160     activateWidget(NULL);
161     return;
162   }
163   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
164
165   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
166   bool isFoundWidget = false;
167   ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
168   for (; anIt != aLast; anIt++) {
169     ModuleBase_ModelWidget* aCurrentWidget = *anIt;
170     if (isFoundWidget || !theWidget) {
171
172       if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
173         continue; // this attribute is not participated in the current case
174
175       if (aCurrentWidget->focusTo()) {
176         return;
177       }
178     }
179     isFoundWidget = isFoundWidget || (*anIt) == theWidget;
180   }
181   activateWidget(NULL);
182 }
183
184 #ifdef DEBUG_TAB
185 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
186 {
187   QList<QWidget*> aWidgets;
188
189   if (theParent) {
190     QLayout* aLayout = theParent->layout();
191     if (aLayout) {
192       for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
193         QLayoutItem* anItem = aLayout->itemAt(i);
194         QWidget* aWidget = anItem ? anItem->widget() : 0;
195         if (aWidget && aWidget->isVisible()) {
196           if (aWidget->focusPolicy() != Qt::NoFocus)
197             theWidgets.append(aWidget);
198           findDirectChildren(aWidget, theWidgets, false);
199         }
200         else {
201           if (aWidget) {
202             QString anInfo = QString("widget [%1] is visible = %2, nofocus policy = %3").arg(aWidget->objectName()).
203               arg(aWidget->isVisible()).arg(aWidget->focusPolicy() == Qt::NoFocus);
204             qDebug(anInfo.toStdString().c_str());
205           }
206         }
207       }
208     }
209   }
210   if (theDebug) {
211     QStringList aWidgetTypes;
212     QList<QWidget*>::const_iterator anIt =  theWidgets.begin(), aLast = theWidgets.end();
213     for (; anIt != aLast; anIt++) {
214       QWidget* aWidget = *anIt;
215       if (aWidget)
216         aWidgetTypes.append(aWidget->objectName());
217     }
218     QString anInfo = QString("theWidgets[%1]: %2").arg(theWidgets.count()).arg(aWidgetTypes.join(","));
219     qDebug(anInfo.toStdString().c_str());
220   }
221 }
222
223 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
224 {
225   // it wraps the Tabs clicking to follow in the chain:
226   // controls, last control, Apply, Cancel, first control, controls
227   bool isChangedFocus = false;
228
229   QWidget* aFocusWidget = focusWidget();
230   if (aFocusWidget) {
231     QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
232     qDebug(anInfo.toStdString().c_str());
233   }
234
235   QWidget* aNewFocusWidget = 0;
236   if (theIsNext) {
237     if (aFocusWidget) {
238       QList<QWidget*> aChildren;
239       findDirectChildren(this, aChildren, true);
240
241       int aChildrenCount = aChildren.count();
242       int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
243       if (aFocusWidgetIndex >= 0) {
244         if (aFocusWidgetIndex == aChildrenCount-1) {
245           QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
246           aNewFocusWidget = anOkBtn;
247         }
248         else {
249           aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
250         }
251       }
252     }
253   }
254   else {
255   }
256   if (aNewFocusWidget) {
257     ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
258     isChangedFocus = true;
259   }
260   return isChangedFocus;
261 }
262 #else
263 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
264 {
265   // it wraps the Tabs clicking to follow in the chain:
266   // controls, last control, Apply, Cancel, first control, controls
267   bool isChangedFocus = false;
268
269   if (theIsNext) { // forward by Tab
270     QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
271     if (aCancelBtn->hasFocus()) {
272       // after cancel, the first control should be focused
273       QWidget* aFirstControl = 0;
274       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
275         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
276       if (aFirstControl)
277         ModuleBase_Tools::setFocus(aFirstControl, "XGUI_PropertyPanel::focusNextPrevChild()");
278         isChangedFocus = true;
279     }
280     else {
281       // after the last control, the Apply button should be focused
282       QWidget* aLastControl = 0;
283       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
284         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
285       if (aLastControl && aLastControl->hasFocus()) {
286         setFocusOnOkButton();
287         isChangedFocus = true;
288       }
289     }
290   }
291   else { // backward by SHIFT + Tab
292     QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
293     if (anOkBtn->hasFocus()) {
294       // after Apply, the last control should be focused
295       QWidget* aLastControl = 0;
296       for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
297         aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
298       if (aLastControl)
299         ModuleBase_Tools::setFocus(aLastControl, "XGUI_PropertyPanel::focusNextPrevChild()");
300         isChangedFocus = true;
301     }
302     else {
303       // after the first control, the Cancel button should be focused
304       QWidget* aFirstControl = 0;
305       for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
306         aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
307       if (aFirstControl && aFirstControl->hasFocus()) {
308         QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
309         ModuleBase_Tools::setFocus(aCancelBtn, "XGUI_PropertyPanel::focusNextPrevChild()");
310         isChangedFocus = true;
311       }
312     }
313   }
314
315   if (!isChangedFocus)
316     isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
317   return isChangedFocus;
318 }
319
320 #endif
321 void XGUI_PropertyPanel::activateNextWidget()
322 {
323   activateNextWidget(myActiveWidget);
324 }
325
326 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
327 {
328   std::string aPreviosAttributeID;
329   if(myActiveWidget)
330     aPreviosAttributeID = myActiveWidget->attributeID();
331
332   // Avoid activation of already actve widget. It could happen on focusIn event many times
333   if (setActiveWidget(theWidget)) {
334     emit widgetActivated(myActiveWidget);
335     if (!myActiveWidget && !isEditingMode()) {
336       emit noMoreWidgets(aPreviosAttributeID);
337     }
338   }
339 }
340
341 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
342 {
343   // Avoid activation of already actve widget. It could happen on focusIn event many times
344   if (theWidget == myActiveWidget) {
345     return false;
346   }
347   std::string aPreviosAttributeID;
348   if(myActiveWidget) {
349     aPreviosAttributeID = myActiveWidget->attributeID();
350     myActiveWidget->deactivate();
351     myActiveWidget->setHighlighted(false);
352   }
353   if(theWidget) {
354     emit beforeWidgetActivated(theWidget);
355     theWidget->setHighlighted(true);
356     theWidget->activate();
357   }
358   myActiveWidget = theWidget;
359   return true;
360 }
361
362 void XGUI_PropertyPanel::setFocusOnOkButton()
363 {
364   QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
365   ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
366 }
367
368 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
369 {
370   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
371   anCancelBtn->setEnabled(theEnabled);
372 }
373
374 bool XGUI_PropertyPanel::isCancelEnabled() const
375 {
376   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
377   return anCancelBtn->isEnabled();
378 }
379
380 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
381 {
382   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
383   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
384     aWgt->setEditingMode(isEditing);
385   }
386 }
387
388 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
389 {
390   QStringList aButtonNames;
391   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
392   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
393   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
394   for (int i = 0; i < aButtonNames.size(); ++i) {
395     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
396     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
397     aBtn->setDefaultAction(anAct);
398   }
399 }
400
401 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
402 {
403   return myPreselectionWidget;
404 }
405
406 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
407 {
408   myPreselectionWidget = theWidget;
409 }
410
411
412 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
413 {
414   ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
415   if (aOp) {
416     if (myOperationMgr->abortAllOperations()) {
417       theEvent->accept();
418     } else 
419       theEvent->ignore();
420   } else
421     ModuleBase_IPropertyPanel::closeEvent(theEvent);
422 }