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*)));
124 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
129 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
131 return static_cast<ModuleBase_PageBase*>(myPanelPage);
134 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
136 // Invalid feature case on abort of the operation
137 if (theFeature.get() == NULL)
139 if (theFeature->isAction() || !theFeature->data())
141 foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
142 if (!eachWidget->feature().get())
143 eachWidget->setFeature(theFeature);
144 eachWidget->restoreValue();
146 // the repaint is used here to immediately react in GUI to the values change.
150 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
152 // TO CHECK: Editing operation does not have automatical activation of widgets
153 if (isEditingMode()) {
154 activateWidget(NULL);
157 QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
158 bool isFoundWidget = false;
160 for (; anIt != aLast; anIt++) {
161 if (isFoundWidget || !theWidget) {
162 if ((*anIt)->focusTo()) {
166 isFoundWidget = isFoundWidget || (*anIt) == theWidget;
168 activateWidget(NULL);
171 bool XGUI_PropertyPanel::focusNextPrevChild(bool theIsNext)
173 // it wraps the Tabs clicking to follow in the chain:
174 // controls, last control, Apply, Cancel, first control, controls
176 bool isChangedFocus = false;
177 if (theIsNext) { // forward by Tab
178 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
179 if (aCancelBtn->hasFocus()) {
180 // after cancel, the first control should be focused
181 QWidget* aFirstControl = 0;
182 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
183 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
185 aFirstControl->setFocus();
186 isChangedFocus = true;
189 // after the last control, the Apply button should be focused
190 QWidget* aLastControl = 0;
191 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
192 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
193 if (aLastControl && aLastControl->hasFocus()) {
194 setFocusOnOkButton();
195 isChangedFocus = true;
199 else { // backward by SHIFT + Tab
200 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
201 if (anOkBtn->hasFocus()) {
202 // after Apply, the last control should be focused
203 QWidget* aLastControl = 0;
204 for (int i = myWidgets.size()-1; i >= 0 && !aLastControl; i--)
205 aLastControl = myWidgets[i]->getControlAcceptingFocus(false);
207 aLastControl->setFocus();
208 isChangedFocus = true;
211 // after the first control, the Cancel button should be focused
212 QWidget* aFirstControl = 0;
213 for (int i = 0, aSize = myWidgets.size(); i < aSize && !aFirstControl; i++)
214 aFirstControl = myWidgets[i]->getControlAcceptingFocus(true);
215 if (aFirstControl && aFirstControl->hasFocus()) {
216 QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
217 aCancelBtn->setFocus();
218 isChangedFocus = true;
224 isChangedFocus = ModuleBase_IPropertyPanel::focusNextPrevChild(theIsNext);
226 return isChangedFocus;
229 void XGUI_PropertyPanel::activateNextWidget()
231 activateNextWidget(myActiveWidget);
234 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
236 std::string aPreviosAttributeID;
238 aPreviosAttributeID = myActiveWidget->attributeID();
240 // Avoid activation of already actve widget. It could happen on focusIn event many times
241 if (setActiveWidget(theWidget)) {
242 emit widgetActivated(myActiveWidget);
243 if (!myActiveWidget && !isEditingMode()) {
244 emit noMoreWidgets(aPreviosAttributeID);
249 bool XGUI_PropertyPanel::setActiveWidget(ModuleBase_ModelWidget* theWidget)
251 // Avoid activation of already actve widget. It could happen on focusIn event many times
252 if (theWidget == myActiveWidget) {
255 std::string aPreviosAttributeID;
257 aPreviosAttributeID = myActiveWidget->attributeID();
258 myActiveWidget->deactivate();
259 myActiveWidget->setHighlighted(false);
262 emit beforeWidgetActivated(theWidget);
263 theWidget->setHighlighted(true);
264 theWidget->activate();
266 myActiveWidget = theWidget;
270 void XGUI_PropertyPanel::setFocusOnOkButton()
272 QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
276 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
278 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
279 anCancelBtn->setEnabled(theEnabled);
282 bool XGUI_PropertyPanel::isCancelEnabled() const
284 QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
285 return anCancelBtn->isEnabled();
288 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
290 ModuleBase_IPropertyPanel::setEditingMode(isEditing);
291 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
292 aWgt->setEditingMode(isEditing);
296 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
298 QStringList aButtonNames;
299 aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
300 QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
301 aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
302 for (int i = 0; i < aButtonNames.size(); ++i) {
303 QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
304 QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
305 aBtn->setDefaultAction(anAct);
309 ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const
311 return myPreselectionWidget;
314 void XGUI_PropertyPanel::setPreselectionWidget(ModuleBase_ModelWidget* theWidget)
316 myPreselectionWidget = theWidget;
320 void XGUI_PropertyPanel::closeEvent(QCloseEvent* theEvent)
322 ModuleBase_Operation* aOp = myOperationMgr->currentOperation();
324 if (myOperationMgr->canStopOperation(aOp)) {
325 myOperationMgr->abortAllOperations();
330 ModuleBase_IPropertyPanel::closeEvent(theEvent);