1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
4 * XGUI_PropertyPanel.cpp
6 * Created on: Apr 29, 2014
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>
23 #include <QLayoutItem>
24 #include <QToolButton>
25 #include <QVBoxLayout>
26 #include <QGridLayout>
28 #include <QToolButton>
35 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
36 : ModuleBase_IPropertyPanel(theParent),
38 myPreselectionWidget(NULL),
40 myOperationMgr(theMgr)
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 }");
47 QWidget* aContent = new QWidget(this);
48 QGridLayout* aMainLayout = new QGridLayout(aContent);
49 const int kPanelColumn = 0;
51 aMainLayout->setContentsMargins(3, 3, 3, 3);
52 this->setWidget(aContent);
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);
61 myHeaderWidget = aFrm;
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);
73 aBtnLay->insertStretch(1, 1);
75 myPanelPage = new ModuleBase_PageWidget(aContent);
76 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
77 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
80 XGUI_PropertyPanel::~XGUI_PropertyPanel()
84 void XGUI_PropertyPanel::cleanContent()
87 myActiveWidget->deactivate();
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;
100 aWidget->blockSignals(true);
105 myPanelPage->clearPage();
106 myActiveWidget = NULL;
107 setWindowTitle(tr("Property Panel"));
110 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
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 connect(aWidget, SIGNAL(focusNextPrev()),
122 this, SLOT(onFocusNextPrev()));
126 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
131 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
133 return static_cast<ModuleBase_PageBase*>(myPanelPage);
136 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
138 // Invalid feature case on abort of the operation
139 if (theFeature.get() == NULL)
141 if (theFeature->isAction() || !theFeature->data())
143 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
144 if (!eachWidget->feature().get())
145 eachWidget->setFeature(theFeature);
146 eachWidget->restoreValue();
148 // the repaint is used here to immediately react in GUI to the values change.
152 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
154 // TO CHECK: Editing operation does not have automatical activation of widgets
155 if (isEditingMode()) {
156 activateWidget(NULL);
159 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
160 bool isFoundWidget = false;
162 for (; anIt != aLast; anIt++) {
163 if (isFoundWidget || !theWidget) {
164 if ((*anIt)->focusTo()) {
168 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
170 activateWidget(NULL);
173 void XGUI_PropertyPanel::onFocusNextPrev()
175 setActiveWidget(NULL);
178 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
180 // it wraps the Tabs clicking to follow in the chain:
181 // controls, last control, Apply, Cancel, first control, controls
183 bool isChangedFocus = false;
184 if (theIsNext) { // forward by Tab
185 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
186 if (aCancelBtn->hasFocus()) {
187 // after cancel, the first control should be focused
188 QWidget* aFirstControl = 0;
189 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
190 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
192 aFirstControl->setFocus();
193 isChangedFocus = true;
196 // after the last control, the Apply button should be focused
197 QWidget* aLastControl = 0;
198 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
199 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
200 if (aLastControl && aLastControl->hasFocus()) {
201 setFocusOnOkButton();
202 isChangedFocus = true;
206 else { // backward by SHIFT + Tab
207 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
208 if (anOkBtn->hasFocus()) {
209 // after Apply, the last control should be focused
210 QWidget* aLastControl = 0;
211 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
212 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
214 aLastControl->setFocus();
215 isChangedFocus = true;
218 // after the first control, the Cancel button should be focused
219 QWidget* aFirstControl = 0;
220 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
221 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
222 if (aFirstControl && aFirstControl->hasFocus()) {
223 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
224 aCancelBtn->setFocus();
225 isChangedFocus = true;
231 isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
233 return isChangedFocus;
236 void XGUI_PropertyPanel::activateNextWidget()
238 activateNextWidget(myActiveWidget);
241 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
243 // Avoid activation of already actve widget. It could happen on focusIn event many times
244 if (setActiveWidget(theWidget)) {
245 if (myActiveWidget) {
246 emit widgetActivated(myActiveWidget);
247 } else if (!isEditingMode()) {
248 emit noMoreWidgets();
249 setFocusOnOkButton();
254 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
256 // Avoid activation of already actve widget. It could happen on focusIn event many times
257 if (theWidget == myActiveWidget) {
261 myActiveWidget->deactivate();
262 myActiveWidget->setHighlighted(false);
265 emit beforeWidgetActivated(theWidget);
266 theWidget->setHighlighted(true);
267 theWidget->activate();
269 myActiveWidget = theWidget;
273 void XGUI_PropertyPanel::setFocusOnOkButton()
275 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
279 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
281 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
282 anCancelBtn->setEnabled(theEnabled);
285 bool XGUI_PropertyPanel::isCancelEnabled() const
287 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
288 return anCancelBtn->isEnabled();
291 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
293 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
294 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
295 aWgt->setEditingMode(isEditing);
299 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
301 QStringList aButtonNames;
302 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
303 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
304 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
305 for (int i = 0; i < aButtonNames.size(); ++i) {
306 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
307 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
308 aBtn->setDefaultAction(anAct);
312 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
314 return myPreselectionWidget;
317 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
319 myPreselectionWidget = theWidget;
323 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
325 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
327 if (myOperationMgr->canStopOperation(aOp)) {
328 myOperationMgr->abortAllOperations();
333 ModuleBase_IPropertyPanel::closeEvent(theEvent);