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>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
26 #include <QLayoutItem>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QGridLayout>
31 #include <QToolButton>
38 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent, XGUI_OperationMgr* theMgr)
39 : ModuleBase_IPropertyPanel(theParent),
41 myPreselectionWidget(NULL),
43 myOperationMgr(theMgr)
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 }");
50 QWidget* aContent = new QWidget(this);
51 QGridLayout* aMainLayout = new QGridLayout(aContent);
52 const int kPanelColumn = 0;
54 aMainLayout->setContentsMargins(3, 3, 3, 3);
55 this->setWidget(aContent);
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);
64 myHeaderWidget = aFrm;
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);
76 aBtnLay->insertStretch(1, 1);
78 myPanelPage = new ModuleBase_PageWidget(aContent);
79 myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
80 aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
83 XGUI_PropertyPanel::~XGUI_PropertyPanel()
87 void XGUI_PropertyPanel::cleanContent()
90 myActiveWidget->deactivate();
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;
103 aWidget->blockSignals(true);
108 myPanelPage->clearPage();
109 myActiveWidget = NULL;
110 setWindowTitle(tr("Property Panel"));
113 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
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*)));
127 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
132 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
134 return static_cast<ModuleBase_PageBase*>(myPanelPage);
137 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
139 // Invalid feature case on abort of the operation
140 if (theFeature.get() == NULL)
142 if (theFeature->isAction() || !theFeature->data())
144 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
145 if (!eachWidget->feature().get())
146 eachWidget->setFeature(theFeature);
147 eachWidget->restoreValue();
149 // the repaint is used here to immediately react in GUI to the values change.
153 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
155 // TO CHECK: Editing operation does not have automatical activation of widgets
156 if (isEditingMode()) {
157 activateWidget(NULL);
160 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
162 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
163 bool isFoundWidget = false;
165 for (; anIt != aLast; anIt++) {
166 ModuleBase_ModelWidget* aCurrentWidget = *anIt;
167 if (isFoundWidget || !theWidget) {
169 if (!aValidators->isCase(aCurrentWidget->feature(), aCurrentWidget->attributeID()))
170 continue; // this attribute is not participated in the current case
172 if (aCurrentWidget->focusTo()) {
176 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
178 activateWidget(NULL);
181 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
183 // it wraps the Tabs clicking to follow in the chain:
184 // controls, last control, Apply, Cancel, first control, controls
186 bool isChangedFocus = false;
187 if (theIsNext) { // forward by Tab
188 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
189 if (aCancelBtn->hasFocus()) {
190 // after cancel, the first control should be focused
191 QWidget* aFirstControl = 0;
192 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
193 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
195 aFirstControl->setFocus();
196 isChangedFocus = true;
199 // after the last control, the Apply button should be focused
200 QWidget* aLastControl = 0;
201 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
202 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
203 if (aLastControl && aLastControl->hasFocus()) {
204 setFocusOnOkButton();
205 isChangedFocus = true;
209 else { // backward by SHIFT + Tab
210 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
211 if (anOkBtn->hasFocus()) {
212 // after Apply, the last control should be focused
213 QWidget* aLastControl = 0;
214 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
215 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
217 aLastControl->setFocus();
218 isChangedFocus = true;
221 // after the first control, the Cancel button should be focused
222 QWidget* aFirstControl = 0;
223 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
224 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
225 if (aFirstControl && aFirstControl->hasFocus()) {
226 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
227 aCancelBtn->setFocus();
228 isChangedFocus = true;
234 isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
236 return isChangedFocus;
239 void XGUI_PropertyPanel::activateNextWidget()
241 activateNextWidget(myActiveWidget);
244 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
246 std::string aPreviosAttributeID;
248 aPreviosAttributeID = myActiveWidget->attributeID();
250 // Avoid activation of already actve widget. It could happen on focusIn event many times
251 if (setActiveWidget(theWidget)) {
252 emit widgetActivated(myActiveWidget);
253 if (!myActiveWidget && !isEditingMode()) {
254 emit noMoreWidgets(aPreviosAttributeID);
259 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
261 // Avoid activation of already actve widget. It could happen on focusIn event many times
262 if (theWidget == myActiveWidget) {
265 std::string aPreviosAttributeID;
267 aPreviosAttributeID = myActiveWidget->attributeID();
268 myActiveWidget->deactivate();
269 myActiveWidget->setHighlighted(false);
272 emit beforeWidgetActivated(theWidget);
273 theWidget->setHighlighted(true);
274 theWidget->activate();
276 myActiveWidget = theWidget;
280 void XGUI_PropertyPanel::setFocusOnOkButton()
282 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
286 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
288 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
289 anCancelBtn->setEnabled(theEnabled);
292 bool XGUI_PropertyPanel::isCancelEnabled() const
294 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
295 return anCancelBtn->isEnabled();
298 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
300 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
301 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
302 aWgt->setEditingMode(isEditing);
306 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
308 QStringList aButtonNames;
309 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
310 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
311 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
312 for (int i = 0; i < aButtonNames.size(); ++i) {
313 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
314 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
315 aBtn->setDefaultAction(anAct);
319 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
321 return myPreselectionWidget;
324 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
326 myPreselectionWidget = theWidget;
330 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
332 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
334 if (myOperationMgr->canStopOperation(aOp)) {
335 myOperationMgr->abortAllOperations();
340 ModuleBase_IPropertyPanel::closeEvent(theEvent);