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_ActionsMgr.h>
22 #include <XGUI_ActiveControlMgr.h>
23 #include <XGUI_SelectionActivate.h>
24 #include <XGUI_ActiveControlSelector.h>
25 #include <XGUI_PropertyPanel.h>
26 #include <XGUI_PropertyPanelSelector.h>
27 #include <XGUI_OperationMgr.h>
28 #include <XGUI_Tools.h>
29 #include <XGUI_Workshop.h>
31 //#include <AppElements_Constants.h>
32 #include <ModuleBase_WidgetMultiSelector.h>
33 #include <ModuleBase_Tools.h>
34 #include <ModuleBase_PageBase.h>
35 #include <ModuleBase_PageWidget.h>
36 #include <ModuleBase_WidgetFactory.h>
37 #include <ModuleBase_OperationDescription.h>
38 #include <ModuleBase_Events.h>
40 #include <Events_Loop.h>
42 #include <ModelAPI_Session.h>
43 #include <ModelAPI_Validator.h>
49 #include <QLayoutItem>
50 #include <QToolButton>
51 #include <QVBoxLayout>
52 #include <QGridLayout>
54 #include <QToolButton>
61 //#define DEBUG_TAB_WIDGETS
63 //#define DEBUG_ACTIVE_WIDGET
65 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
66 : ModuleBase_IPropertyPanel(theParent),
68 myPreselectionWidget(NULL),
70 myOperationMgr(theMgr)
72 setWindowTitle(tr("Property Panel"));
73 QAction* aViewAct = toggleViewAction();
74 setObjectName(PROP_PANEL);
75 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
77 QWidget* aContent = new QWidget(this);
78 QGridLayout* aMainLayout = new QGridLayout(aContent);
79 const int kPanelColumn = 0;
81 aMainLayout->setContentsMargins(3, 3, 3, 3);
84 QFrame* aFrm = new QFrame(aContent);
85 aFrm->setFrameStyle(QFrame::Raised);
86 aFrm->setFrameShape(QFrame::Panel);
87 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
88 ModuleBase_Tools::zeroMargins(aBtnLay);
89 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
91 myHeaderWidget = aFrm;
93 QStringList aBtnNames;
94 aBtnNames << QString(PROP_PANEL_HELP)
95 << QString(PROP_PANEL_OK)
96 << QString(PROP_PANEL_CANCEL);
97 foreach(QString eachBtnName, aBtnNames) {
98 QToolButton* aBtn = new QToolButton(aFrm);
99 aBtn->setObjectName(eachBtnName);
100 aBtn->setAutoRaise(true);
101 aBtnLay->addWidget(aBtn);
103 aBtnLay->insertStretch(1, 1);
105 myPanelPage = new ModuleBase_PageWidget(aContent);
106 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
107 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
109 // spit to make the preview button on the bottom of the panel
110 aMainLayout->setRowStretch(aPanelRow++, 1);
112 // preview button on the bottom of panel
113 aFrm = new QFrame(aContent);
114 aBtnLay = new QHBoxLayout(aFrm);
115 aBtnLay->addStretch(1);
116 ModuleBase_Tools::zeroMargins(aBtnLay);
117 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
119 QToolButton* aBtn = new QToolButton(aFrm);
120 aBtn->setObjectName(PROP_PANEL_PREVIEW);
121 aBtnLay->addWidget(aBtn);
124 XGUI_PropertyPanel::~XGUI_PropertyPanel()
128 void XGUI_PropertyPanel::cleanContent()
131 myActiveWidget->deactivate();
133 XGUI_ActiveControlSelector* aPPSelector = XGUI_Tools::workshop(myOperationMgr->workshop())->
134 activeControlMgr()->getSelector(XGUI_PropertyPanelSelector::Type());
135 aPPSelector->reset(); // it removes need to be updated widget link
137 /// as the widgets are deleted later, it is important that the signals
138 /// of these widgets are not processed. An example of the error is issue 986.
139 /// In the given case, the property panel is firstly filled by new widgets
140 /// of restarted operation and after that the mouse release signal come from
141 /// the widget of the previous operation (Point2d widget about mouse is released
142 /// and focus is out of this widget)
143 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
144 aLast = myWidgets.end();
145 for (; anIt != aLast; anIt++) {
146 QWidget* aWidget = *anIt;
148 aWidget->blockSignals(true);
153 myPanelPage->clearPage();
154 myActiveWidget = NULL;
155 emit propertyPanelDeactivated();
156 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
157 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
158 #ifdef DEBUG_ACTIVE_WIDGET
159 std::cout << "myActiveWidget = NULL" << std::endl;
162 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
163 setWindowTitle(tr("Property Panel"));
166 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
168 myWidgets = theWidgets;
169 if (theWidgets.empty()) return;
170 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
171 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
172 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
173 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
174 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
175 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
176 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
177 connect(aWidget, SIGNAL(enterClicked(QObject*)),
178 this, SIGNAL(enterClicked(QObject*)));
183 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
188 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
190 return static_cast<ModuleBase_PageBase*>(myPanelPage);
193 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
195 // Invalid feature case on abort of the operation
196 if (theFeature.get() == NULL)
198 if (theFeature->isAction() || !theFeature->data())
200 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
201 if (!eachWidget->feature().get())
202 eachWidget->setFeature(theFeature);
203 eachWidget->restoreValue();
205 // the repaint is used here to immediately react in GUI to the values change.
209 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
211 // Invalid feature case on abort of the operation
212 if (theFeature.get() == NULL)
214 if (theFeature->isAction() || !theFeature->data())
217 if (myWidgets.empty()) {
218 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
219 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
221 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
222 aFactory.createPanel(contentWidget(), theFeature);
223 /// Apply button should be update if the feature was modified by the panel
224 myOperationMgr->onValidateOperation();
228 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
230 // it is possible that the property panel widgets have not been visualized
231 // (e.g. on start operation), so it is strictly important to do not check visualized state
232 activateNextWidget(theWidget, false);
235 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
237 #ifdef DEBUG_ACTIVE_WIDGET
238 std::cout << "onFocusInWidget" << std::endl;
240 if (theWidget->canAcceptFocus())
241 activateWidget(theWidget);
244 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
246 // this slot happens when some widget lost focus, the next widget which accepts the focus
247 // should be shown, so the second parameter is true
248 // it is important for features where in cases the same attributes are used, isCase for this
249 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
250 // sketch multi rotation -> single/full point)
251 // it is important to check the widget visibility to do not check of the next widget visible
252 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
253 // release in the viewer, next, radius,
254 // widget should be activated but the first is not visualized)
255 bool isVisible = theWidget->isVisible();
256 activateNextWidget(theWidget, isVisible);
260 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
261 const bool isCheckVisibility)
263 // TO CHECK: Editing operation does not have automatical activation of widgets
264 if (isEditingMode()) {
265 activateWidget(NULL);
268 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
270 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
271 bool isFoundWidget = false;
272 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
273 for (; anIt != aLast; anIt++) {
274 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
275 if (isFoundWidget || !theWidget) {
277 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
278 continue; // this attribute does not participate in the current case
279 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
280 if (!aCurrentWidget->isVisible())
283 if (!aCurrentWidget->isObligatory())
284 continue; // not obligatory widgets are not activated automatically
286 if (!aCurrentWidget->canAcceptFocus())
287 continue; // do not set focus if it can not be accepted, case: optional choice
289 if (aCurrentWidget->focusTo()) {
290 aCurrentWidget->emitFocusInWidget();
294 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
296 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
297 // it should be performed before activateWidget(NULL) because it emits some signals which
298 // can be processed by moudule for example as to activate another widget with setting focus
299 QWidget* aNewFocusWidget = 0;
300 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
301 if (anOkBtn->isEnabled())
302 aNewFocusWidget = anOkBtn;
304 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
305 if (aCancelBtn->isEnabled())
306 aNewFocusWidget = aCancelBtn;
309 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
311 activateWidget(NULL);
314 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
316 QList<QWidget*> aWidgets;
319 QLayout* aLayout = theParent->layout();
321 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
322 QLayoutItem* anItem = aLayout->itemAt(i);
323 QWidget* aWidget = anItem ? anItem->widget() : 0;
325 if (aWidget->isVisible()) {
326 if (aWidget->focusPolicy() != Qt::NoFocus)
327 theWidgets.append(aWidget);
328 findDirectChildren(aWidget, theWidgets, false);
331 else if (anItem->layout()) {
332 QLayout* aLayout = anItem->layout();
333 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
334 QLayoutItem* anItem = aLayout->itemAt(i);
335 QWidget* aWidget = anItem ? anItem->widget() : 0;
337 if (aWidget->isVisible()) {
338 if (aWidget->focusPolicy() != Qt::NoFocus)
339 theWidgets.append(aWidget);
340 findDirectChildren(aWidget, theWidgets, false);
344 // TODO: improve recursive search for the case when here QLayout is used
345 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
346 // example for construction plane feature. If there are more levels,
347 // it should be implemented here
354 #ifdef DEBUG_TAB_WIDGETS
356 QStringList aWidgetTypes;
357 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
358 for (; anIt != aLast; anIt++) {
359 QWidget* aWidget = *anIt;
361 aWidgetTypes.append(aWidget->objectName());
363 QString anInfo = QString("theWidgets[%1]: %2")
364 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
365 qDebug(anInfo.toStdString().c_str());
370 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
372 return focusNextPrevChild(theIsNext);
375 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
377 // it wraps the Tabs clicking to follow in the chain:
378 // controls, last control, Apply, Cancel, first control, controls
379 bool isChangedFocus = false;
381 QWidget* aFocusWidget = focusWidget();
382 #ifdef DEBUG_TAB_WIDGETS
384 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
385 qDebug(anInfo.toStdString().c_str());
388 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
391 aFocusMWidget->setHighlighted(false);
393 QWidget* aNewFocusWidget = 0;
395 QList<QWidget*> aChildren;
396 findDirectChildren(this, aChildren, true);
397 int aChildrenCount = aChildren.count();
398 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
399 if (aFocusWidgetIndex >= 0) {
401 if (aFocusWidgetIndex == aChildrenCount-1) {
402 // after the last widget focus should be set to "Apply"
403 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
404 if (anOkBtn->isEnabled())
405 aNewFocusWidget = anOkBtn;
407 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
408 if (aCancelBtn->isEnabled())
409 aNewFocusWidget = aCancelBtn;
413 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
417 if (aFocusWidgetIndex == 0) {
418 // before the first widget, the last should accept focus
419 aNewFocusWidget = aChildren[aChildrenCount - 1];
422 // before the "Apply" button, the last should accept focus for consistency with "Next"
423 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
424 if (aFocusWidget == anOkBtn) {
425 aNewFocusWidget = aChildren[aChildrenCount - 1];
428 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
434 if (aNewFocusWidget) {
435 if (myActiveWidget) {
436 bool isFirstControl = !theIsNext;
437 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
438 if (aFocusWidget == aLastFocusControl) {
439 setActiveWidget(NULL, false);
443 // we want to have property panel as an active window to enter values in double control
444 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
446 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
448 if (aNewFocusMWidget)
449 aNewFocusMWidget->emitFocusInWidget();
450 isChangedFocus = true;
452 return isChangedFocus;
455 void XGUI_PropertyPanel::activateNextWidget()
457 activateNextWidget(myActiveWidget);
460 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
462 std::string aPreviosAttributeID;
464 aPreviosAttributeID = myActiveWidget->attributeID();
466 // Avoid activation of already actve widget. It could happen on focusIn event many times
467 setActiveWidget(theWidget, theEmitSignal);
470 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget, const bool isEmitSignal)
472 // Avoid activation of already actve widget. It could happen on focusIn event many times
473 if (theWidget == myActiveWidget) {
476 std::string aPreviosAttributeID;
478 aPreviosAttributeID = myActiveWidget->attributeID();
479 myActiveWidget->processValueState();
480 myActiveWidget->deactivate();
481 myActiveWidget->setHighlighted(false);
484 emit beforeWidgetActivated(theWidget);
485 theWidget->setHighlighted(true);
486 theWidget->activate();
488 myActiveWidget = theWidget;
490 #ifdef DEBUG_ACTIVE_WIDGET
491 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
493 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
494 Events_Loop::loop()->flush(anEvent);
497 emit widgetActivated(myActiveWidget);
498 if (!myActiveWidget && !isEditingMode()) {
499 emit noMoreWidgets(aPreviosAttributeID);
502 emit propertyPanelActivated();
503 myOperationMgr->workshop()->selectionActivate()->updateSelectionModes();
504 myOperationMgr->workshop()->selectionActivate()->updateSelectionFilters();
508 void XGUI_PropertyPanel::setFocusOnOkButton()
510 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
511 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
514 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
516 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
517 anCancelBtn->setEnabled(theEnabled);
520 bool XGUI_PropertyPanel::isCancelEnabled() const
522 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
523 return anCancelBtn->isEnabled();
526 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
528 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
529 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
530 aWgt->setEditingMode(isEditing);
534 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
536 QStringList aButtonNames;
537 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
538 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
539 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help
540 << XGUI_ActionsMgr::Preview;
541 for (int i = 0; i < aButtonNames.size(); ++i) {
542 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
543 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
544 aBtn->setDefaultAction(anAct);
548 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
550 return myPreselectionWidget;
553 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
555 myPreselectionWidget = theWidget;
559 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
561 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
563 if (myOperationMgr->canStopOperation(aOp)) {
564 myOperationMgr->abortOperation(aOp);
565 if (myOperationMgr->hasOperation())
572 ModuleBase_IPropertyPanel::closeEvent(theEvent);
575 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
577 return findChild<QToolButton*>(theInternalName);