Salome HOME
Bugfixes for focus processing in the property panel
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 /*
4  * XGUI_PropertyPanel.cpp
5  *
6  *  Created on: Apr 29, 2014
7  *      Author: sbh
8  */
9
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ActionsMgr.h>
12 //#include <AppElements_Constants.h>
13 #include <ModuleBase_WidgetMultiSelector.h>
14 #include <ModuleBase_Tools.h>
15 #include <ModuleBase_PageBase.h>
16 #include <ModuleBase_PageWidget.h>
17
18 #include <QEvent>
19 #include <QFrame>
20 #include <QIcon>
21 #include <QKeyEvent>
22 #include <QLayoutItem>
23 #include <QToolButton>
24 #include <QVBoxLayout>
25 #include <QGridLayout>
26 #include <QWidget>
27 #include <QToolButton>
28 #include <QAction>
29
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
35     : ModuleBase_IPropertyPanel(theParent), 
36     myActiveWidget(NULL),
37     myPanelPage(NULL)
38 {
39   this->setWindowTitle(tr("Property Panel"));
40   QAction* aViewAct = this->toggleViewAction();
41   this->setObjectName(PROP_PANEL);
42   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
43
44   QWidget* aContent = new QWidget(this);
45   QGridLayout* aMainLayout = new QGridLayout(aContent);
46   const int kPanelColumn = 0;
47   int aPanelRow = 0;
48   aMainLayout->setContentsMargins(3, 3, 3, 3);
49   this->setWidget(aContent);
50
51   QFrame* aFrm = new QFrame(aContent);
52   aFrm->setFrameStyle(QFrame::Raised);
53   aFrm->setFrameShape(QFrame::Panel);
54   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
55   ModuleBase_Tools::zeroMargins(aBtnLay);
56   aMainLayout->addWidget(aFrm, aPanelRow++, kPanelColumn);
57
58   QStringList aBtnNames;
59   aBtnNames << QString(PROP_PANEL_HELP)
60             << QString(PROP_PANEL_OK)
61             << QString(PROP_PANEL_CANCEL);
62   foreach(QString eachBtnName, aBtnNames) {
63     QToolButton* aBtn = new QToolButton(aFrm);
64     aBtn->setObjectName(eachBtnName);
65     aBtn->setAutoRaise(true);
66     aBtnLay->addWidget(aBtn);
67   }
68   aBtnLay->insertStretch(1, 1);
69
70   myPanelPage = new ModuleBase_PageWidget(aContent);
71   myPanelPage->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
72   aMainLayout->addWidget(myPanelPage, aPanelRow, kPanelColumn);
73 }
74
75 XGUI_PropertyPanel::~XGUI_PropertyPanel()
76 {
77 }
78
79 void XGUI_PropertyPanel::cleanContent()
80 {
81   if (myActiveWidget)
82     myActiveWidget->deactivate();
83   myWidgets.clear();
84   myPanelPage->clearPage();
85   myActiveWidget = NULL;
86   setWindowTitle(tr("Property Panel"));
87 }
88
89 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
90 {
91   myWidgets = theWidgets;
92   if (theWidgets.empty()) return;
93   foreach (ModuleBase_ModelWidget* aWidget, theWidgets) {
94     connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
95             this,    SLOT(activateWidget(ModuleBase_ModelWidget*)));
96     connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
97             this,    SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
98     connect(aWidget, SIGNAL(keyReleased(QKeyEvent*)),
99             this,    SIGNAL(keyReleased(QKeyEvent*)));
100   }
101   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
102   if (aLastWidget) {
103     QList<QWidget*> aControls = aLastWidget->getControls();
104     if (!aControls.empty()) {
105       QWidget* aLastControl = aControls.last();
106
107       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
108       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
109
110       setTabOrder(aLastControl, anOkBtn);
111       setTabOrder(anOkBtn, aCancelBtn);
112     }
113   }
114 }
115
116 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
117 {
118   return myWidgets;
119 }
120
121 ModuleBase_PageBase* XGUI_PropertyPanel::contentWidget()
122 {
123
124   return static_cast<ModuleBase_PageBase*>(myPanelPage);
125 }
126
127 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
128 {
129   // Invalid feature case on abort of the operation
130   if (theFeature.get() == NULL)
131     return;
132   if (theFeature->isAction() || !theFeature->data())
133     return;
134   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
135     eachWidget->setFeature(theFeature);
136     eachWidget->restoreValue();
137   }
138   // the repaint is used here to immediately react in GUI to the values change.
139   repaint();
140 }
141
142 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
143 {
144   // TO CHECK: Editing operation does not have automatical activation of widgets
145   if (isEditingMode()) {
146     activateWidget(NULL);
147     return;
148   }
149   ModuleBase_ModelWidget* aNextWidget = 0;
150   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
151   bool isFoundWidget = false;
152   for (; anIt != aLast && !aNextWidget; anIt++) {
153     if (isFoundWidget || !theWidget) {
154       if ((*anIt)->focusTo()) {
155         aNextWidget = *anIt;
156       }
157     }
158     isFoundWidget = (*anIt) == theWidget;
159   }
160   // Normaly focusTo is enough to activate widget
161   // here is a special case on mouse click in the viewer
162   if(aNextWidget == NULL) {
163     activateWidget(aNextWidget);
164   }
165 }
166
167 void XGUI_PropertyPanel::activateNextWidget()
168 {
169   activateNextWidget(myActiveWidget);
170 }
171
172 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
173 {
174   // Avoid activation of already actve widget. It could happen on focusIn event many times
175   if (theWidget == myActiveWidget) {
176     return;
177   }
178   if(myActiveWidget) {
179     myActiveWidget->deactivate();
180     myActiveWidget->setHighlighted(false);
181   }
182   if(theWidget) {
183     emit beforeWidgetActivated(theWidget);
184     theWidget->setHighlighted(true);
185     theWidget->activate();
186   }
187   myActiveWidget = theWidget;
188   if (myActiveWidget) {
189     emit widgetActivated(theWidget);
190   } else if (!isEditingMode()) {
191     emit noMoreWidgets();
192   }
193 }
194
195 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
196 {
197   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
198   anCancelBtn->setEnabled(theEnabled);
199 }
200
201 bool XGUI_PropertyPanel::isCancelEnabled() const
202 {
203   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
204   return anCancelBtn->isEnabled();
205 }
206
207 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
208 {
209   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
210   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
211     aWgt->setEditingMode(isEditing);
212   }
213 }
214
215 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
216 {
217   QStringList aButtonNames;
218   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
219   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
220   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
221   for (int i = 0; i < aButtonNames.size(); ++i) {
222     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
223     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
224     aBtn->setDefaultAction(anAct);
225   }
226 }