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_PropertyPanel.h>
22 #include <XGUI_ActionsMgr.h>
23 #include <XGUI_OperationMgr.h>
24 //#include <AppElements_Constants.h>
25 #include <ModuleBase_WidgetMultiSelector.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_PageBase.h>
28 #include <ModuleBase_PageWidget.h>
29 #include <ModuleBase_WidgetFactory.h>
30 #include <ModuleBase_OperationDescription.h>
31 #include <ModuleBase_Events.h>
32 #include <ModuleBase_IWorkshop.h>
34 #include <Events_Loop.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
43 #include <QLayoutItem>
44 #include <QToolButton>
45 #include <QVBoxLayout>
46 #include <QGridLayout>
54 //#define DEBUG_TAB_WIDGETS
56 //#define DEBUG_ACTIVE_WIDGET
58 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
59 : ModuleBase_IPropertyPanel(theParent),
61 myPreselectionWidget(NULL),
63 myOperationMgr(theMgr)
65 setWindowTitle(tr("Property Panel"));
66 QAction* aViewAct = toggleViewAction();
67 setObjectName(PROP_PANEL);
68 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
70 QWidget* aContent = new QWidget(this);
71 QGridLayout* aMainLayout = new QGridLayout(aContent);
72 const int kPanelColumn = 0;
74 aMainLayout->setContentsMargins(3, 3, 3, 3);
77 QFrame* aFrm = new QFrame(aContent);
78 aFrm->setFrameStyle(QFrame::Raised);
79 aFrm->setFrameShape(QFrame::Panel);
80 QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
81 ModuleBase_Tools::zeroMargins(aBtnLay);
82 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
84 myHeaderWidget = aFrm;
86 QStringList aBtnNames;
87 aBtnNames << QString(PROP_PANEL_HELP)
88 << QString(PROP_PANEL_OK)
89 << QString(PROP_PANEL_OK_PLUS)
90 << QString(PROP_PANEL_CANCEL);
91 foreach(QString eachBtnName, aBtnNames) {
92 QToolButton* aBtn = new QToolButton(aFrm);
93 aBtn->setObjectName(eachBtnName);
94 aBtn->setAutoRaise(true);
95 aBtnLay->addWidget(aBtn);
97 aBtnLay->insertStretch(1, 1);
99 myPanelPage = new ModuleBase_PageWidget(aContent);
100 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
101 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
103 // spit to make the preview button on the bottom of the panel
104 aMainLayout->setRowStretch(aPanelRow++, 1);
106 // preview button on the bottom of panel
107 aFrm = new QFrame(aContent);
108 aBtnLay = new QHBoxLayout(aFrm);
109 aBtnLay->addStretch(1);
110 ModuleBase_Tools::zeroMargins(aBtnLay);
111 aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
113 QToolButton* aBtn = new QToolButton(aFrm);
114 aBtn->setObjectName(PROP_PANEL_PREVIEW);
115 aBtnLay->addWidget(aBtn);
118 XGUI_PropertyPanel::~XGUI_PropertyPanel()
122 void XGUI_PropertyPanel::cleanContent()
125 myActiveWidget->deactivate();
127 /// as the widgets are deleted later, it is important that the signals
128 /// of these widgets are not processed. An example of the error is issue 986.
129 /// In the given case, the property panel is firstly filled by new widgets
130 /// of restarted operation and after that the mouse release signal come from
131 /// the widget of the previous operation (Point2d widget about mouse is released
132 /// and focus is out of this widget)
133 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
134 aLast = myWidgets.end();
135 for (; anIt != aLast; anIt++) {
136 QWidget* aWidget = *anIt;
138 aWidget->blockSignals(true);
143 myPanelPage->clearPage();
144 myActiveWidget = NULL;
145 #ifdef DEBUG_ACTIVE_WIDGET
146 std::cout << "myActiveWidget = NULL" << std::endl;
149 findButton(PROP_PANEL_PREVIEW)->setVisible(false); /// by default it is hidden
150 setWindowTitle(tr("Property Panel"));
153 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
155 myWidgets = theWidgets;
156 if (theWidgets.empty()) return;
157 foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
158 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
159 this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*)));
160 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
161 this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
162 connect(aWidget, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
163 this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
164 connect(aWidget, SIGNAL(enterClicked(QObject*)),
165 this, SIGNAL(enterClicked(QObject*)));
170 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
175 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
177 return static_cast<ModuleBase_PageBase*>(myPanelPage);
180 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
182 // Invalid feature case on abort of the operation
183 if (theFeature.get() == NULL)
185 if (theFeature->isAction() || !theFeature->data())
187 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
188 if (!eachWidget->feature().get())
189 eachWidget->setFeature(theFeature);
190 eachWidget->restoreValue();
192 // the repaint is used here to immediately react in GUI to the values change.
196 void XGUI_PropertyPanel::createContentPanel(FeaturePtr theFeature)
198 // Invalid feature case on abort of the operation
199 if (theFeature.get() == NULL)
201 if (theFeature->isAction() || !theFeature->data())
204 if (myWidgets.empty()) {
205 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
206 QString aXmlRepr = anOperation->getDescription()->xmlRepresentation();
208 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myOperationMgr->workshop());
209 aFactory.createPanel(contentWidget(), theFeature);
210 /// Apply button should be update if the feature was modified by the panel
211 myOperationMgr->onValidateOperation();
213 std::shared_ptr<Config_FeatureMessage> aFeatureInfo =
214 myOperationMgr->workshop()->featureInfo(theFeature->getKind().c_str());
215 findButton(PROP_PANEL_OK_PLUS)->setVisible(aFeatureInfo->isApplyContinue());
218 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
220 // it is possible that the property panel widgets have not been visualized
221 // (e.g. on start operation), so it is strictly important to do not check visualized state
222 activateNextWidget(theWidget, false);
225 void XGUI_PropertyPanel::onFocusInWidget(ModuleBase_ModelWidget* theWidget)
227 #ifdef DEBUG_ACTIVE_WIDGET
228 std::cout << "onFocusInWidget" << std::endl;
230 if (theWidget->canAcceptFocus())
231 activateWidget(theWidget);
234 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
236 // this slot happens when some widget lost focus, the next widget which accepts the focus
237 // should be shown, so the second parameter is true
238 // it is important for features where in cases the same attributes are used, isCase for this
239 // attribute returns true, however it can be placed in hidden stack widget(extrusion: elements,
240 // sketch multi rotation -> single/full point)
241 // it is important to check the widget visibility to do not check of the next widget visible
242 // state if the current is not shown. (example: sketch circle re-entrant operation after mouse
243 // release in the viewer, next, radius,
244 // widget should be activated but the first is not visualized)
245 bool isVisible = theWidget->isVisible();
246 activateNextWidget(theWidget, isVisible);
250 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget,
251 const bool isCheckVisibility)
253 // TO CHECK: Editing operation does not have automatical activation of widgets
254 if (isEditingMode()) {
255 activateWidget(NULL);
258 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
260 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
261 bool isFoundWidget = false;
262 ModuleBase_Tools::activateWindow(this, "XGUI_PropertyPanel::activateNextWidget()");
263 for (; anIt != aLast; anIt++) {
264 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
265 if (isFoundWidget || !theWidget) {
267 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
268 continue; // this attribute does not participate in the current case
269 if (isCheckVisibility && !aCurrentWidget->isInternal()) {
270 if (!aCurrentWidget->isVisible())
273 if (!aCurrentWidget->isObligatory())
274 continue; // not obligatory widgets are not activated automatically
276 if (!aCurrentWidget->canAcceptFocus())
277 continue; // do not set focus if it can not be accepted, case: optional choice
279 if (aCurrentWidget->focusTo()) {
280 aCurrentWidget->emitFocusInWidget();
284 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
286 // set focus to Ok/Cancel button in Property panel if there are no more active widgets
287 // it should be performed before activateWidget(NULL) because it emits some signals which
288 // can be processed by moudule for example as to activate another widget with setting focus
289 QWidget* aNewFocusWidget = 0;
290 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
291 if (anOkBtn->isEnabled())
292 aNewFocusWidget = anOkBtn;
294 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
295 if (aCancelBtn->isEnabled())
296 aNewFocusWidget = aCancelBtn;
299 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::activateNextWidget");
301 activateWidget(NULL);
304 void findDirectChildren(QWidget* theParent, QList<QWidget*>& theWidgets, const bool theDebug)
306 QList<QWidget*> aWidgets;
309 QLayout* aLayout = theParent->layout();
311 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
312 QLayoutItem* anItem = aLayout->itemAt(i);
313 QWidget* aWidget = anItem ? anItem->widget() : 0;
315 if (aWidget->isVisible()) {
316 if (aWidget->focusPolicy() != Qt::NoFocus)
317 theWidgets.append(aWidget);
318 findDirectChildren(aWidget, theWidgets, false);
321 else if (anItem->layout()) {
322 QLayout* aLayout = anItem->layout();
323 for (int i = 0, aCount = aLayout->count(); i < aCount; i++) {
324 QLayoutItem* anItem = aLayout->itemAt(i);
325 QWidget* aWidget = anItem ? anItem->widget() : 0;
327 if (aWidget->isVisible()) {
328 if (aWidget->focusPolicy() != Qt::NoFocus)
329 theWidgets.append(aWidget);
330 findDirectChildren(aWidget, theWidgets, false);
334 // TODO: improve recursive search for the case when here QLayout is used
335 // currently, the switch widget uses only 1 level of qlayout in qlayout using for
336 // example for construction plane feature. If there are more levels,
337 // it should be implemented here
344 #ifdef DEBUG_TAB_WIDGETS
346 QStringList aWidgetTypes;
347 QList<QWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
348 for (; anIt != aLast; anIt++) {
349 QWidget* aWidget = *anIt;
351 aWidgetTypes.append(aWidget->objectName());
353 QString anInfo = QString("theWidgets[%1]: %2")
354 .arg(theWidgets.count()).arg(aWidgetTypes.join(","));
355 qDebug(anInfo.toStdString().c_str());
360 bool XGUI_PropertyPanel::setFocusNextPrevChild(bool theIsNext)
362 return focusNextPrevChild(theIsNext);
365 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
367 // it wraps the Tabs clicking to follow in the chain:
368 // controls, last control, Apply, Cancel, first control, controls
369 bool isChangedFocus = false;
371 QWidget* aFocusWidget = focusWidget();
372 #ifdef DEBUG_TAB_WIDGETS
374 QString anInfo = QString("focus Widget: %1").arg(aFocusWidget->objectName());
375 qDebug(anInfo.toStdString().c_str());
378 ModuleBase_ModelWidget* aFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
381 aFocusMWidget->setHighlighted(false);
383 QWidget* aNewFocusWidget = 0;
385 QList<QWidget*> aChildren;
386 findDirectChildren(this, aChildren, true);
387 int aChildrenCount = aChildren.count();
388 int aFocusWidgetIndex = aChildren.indexOf(aFocusWidget);
389 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
390 if (aFocusWidgetIndex >= 0) {
392 if (aFocusWidgetIndex == aChildrenCount-1) {
393 // after the last widget focus should be set to "Apply"
394 if (anOkBtn->isEnabled())
395 aNewFocusWidget = anOkBtn;
397 QToolButton* aCancelBtn = findButton(PROP_PANEL_CANCEL);
398 if (aCancelBtn->isEnabled())
399 aNewFocusWidget = aCancelBtn;
403 aNewFocusWidget = aChildren[aFocusWidgetIndex+1];
407 if (aFocusWidgetIndex == 0) {
408 // before the first widget, the last should accept focus
409 aNewFocusWidget = aChildren[aChildrenCount - 1];
412 // before the "Apply" button, the last should accept focus for consistency with "Next"
413 if (aFocusWidget == anOkBtn) {
414 aNewFocusWidget = aChildren[aChildrenCount - 1];
417 aNewFocusWidget = aChildren[aFocusWidgetIndex-1];
423 if (aNewFocusWidget) {
424 if (myActiveWidget) {
425 bool isFirstControl = !theIsNext;
426 QWidget* aLastFocusControl = myActiveWidget->getControlAcceptingFocus(isFirstControl);
427 if (aFocusWidget == aLastFocusControl) {
428 setActiveWidget(NULL);
432 // we want to have property panel as an active window to enter values in double control
433 ModuleBase_Tools::setFocus(aNewFocusWidget, "XGUI_PropertyPanel::focusNextPrevChild()");
435 ModuleBase_ModelWidget* aNewFocusMWidget = ModuleBase_ModelWidget::findModelWidget(this,
437 if (aNewFocusMWidget)
438 aNewFocusMWidget->emitFocusInWidget();
439 isChangedFocus = true;
441 return isChangedFocus;
444 void XGUI_PropertyPanel::activateNextWidget()
446 activateNextWidget(myActiveWidget);
449 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget, const bool theEmitSignal)
451 std::string aPreviosAttributeID;
453 aPreviosAttributeID = myActiveWidget->attributeID();
455 // Avoid activation of already actve widget. It could happen on focusIn event many times
456 if (setActiveWidget(theWidget) && theEmitSignal) {
457 emit widgetActivated(myActiveWidget);
458 if (!myActiveWidget && !isEditingMode()) {
459 emit noMoreWidgets(aPreviosAttributeID);
464 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
466 // Avoid activation of already actve widget. It could happen on focusIn event many times
467 if (theWidget == myActiveWidget) {
470 std::string aPreviosAttributeID;
472 aPreviosAttributeID = myActiveWidget->attributeID();
473 myActiveWidget->processValueState();
474 myActiveWidget->deactivate();
475 myActiveWidget->setHighlighted(false);
478 emit beforeWidgetActivated(theWidget);
479 theWidget->setHighlighted(true);
480 theWidget->activate();
482 myActiveWidget = theWidget;
483 #ifdef DEBUG_ACTIVE_WIDGET
484 std::cout << "myActiveWidget = " << (theWidget ? theWidget->context().c_str() : "") << std::endl;
486 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
487 Events_Loop::loop()->flush(anEvent);
492 void XGUI_PropertyPanel::setFocusOnOkButton()
494 QToolButton* anOkBtn = findButton(PROP_PANEL_OK);
495 ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::setFocusOnOkButton()");
498 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
500 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
501 anCancelBtn->setEnabled(theEnabled);
504 bool XGUI_PropertyPanel::isCancelEnabled() const
506 QToolButton* anCancelBtn = findButton(PROP_PANEL_CANCEL);
507 return anCancelBtn->isEnabled();
510 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
512 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
513 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
514 aWgt->setEditingMode(isEditing);
518 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
520 QStringList aButtonNames;
521 aButtonNames << PROP_PANEL_OK<< PROP_PANEL_OK_PLUS << PROP_PANEL_CANCEL
522 << PROP_PANEL_HELP << PROP_PANEL_PREVIEW;
523 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
524 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::AcceptPlus << XGUI_ActionsMgr::Abort
525 << XGUI_ActionsMgr::Help << XGUI_ActionsMgr::Preview;
526 for (int i = 0; i < aButtonNames.size(); ++i) {
527 QToolButton* aBtn = findButton(aButtonNames.at(i).toStdString().c_str());
528 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
529 aBtn->setDefaultAction(anAct);
533 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
535 return myPreselectionWidget;
538 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
540 myPreselectionWidget = theWidget;
544 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
546 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
548 if (myOperationMgr->canStopOperation(aOp)) {
549 myOperationMgr->abortOperation(aOp);
550 if (myOperationMgr->hasOperation())
557 ModuleBase_IPropertyPanel::closeEvent(theEvent);
560 QToolButton* XGUI_PropertyPanel::findButton(const char* theInternalName) const
562 return findChild<QToolButton*>(theInternalName);