Salome HOME
Property pannel refactoring: focus processing in paged widgets
[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   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin();
94   for (; anIt != theWidgets.end(); anIt++) {
95     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
96     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
97             this,  SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
98     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
99             this,  SLOT(activateWidget(ModuleBase_ModelWidget*)));
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   {
136     eachWidget->setFeature(theFeature);
137     eachWidget->restoreValue();
138   }
139   // the repaint is used here to immediately react in GUI to the values change.
140   repaint();
141 }
142
143 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
144 {
145   // TO CHECK: Editing operation does not have automatical activation of widgets
146   if (isEditingMode()) {
147     activateWidget(NULL);
148     return;
149   }
150   ModuleBase_ModelWidget* aNextWidget = 0;
151   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
152   bool isFoundWidget = false;
153   for (; anIt != aLast && !aNextWidget; anIt++) {
154     if (isFoundWidget || !theWidget) {
155       if ((*anIt)->focusTo()) {
156         aNextWidget = *anIt;
157       }
158     }
159     isFoundWidget = (*anIt) == theWidget;
160   }
161   // Normaly focusTo is enough to activate widget
162   // here is a special case on mouse click in the viewer
163   //if(aNextWidget == NULL) {
164     activateWidget(aNextWidget);
165   //}
166 }
167
168 void XGUI_PropertyPanel::activateNextWidget()
169 {
170   activateNextWidget(myActiveWidget);
171 }
172
173 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
174 {
175   // Avoid activation of already actve widget. It could happen on focusIn event many times
176   if (theWidget == myActiveWidget)
177     return;
178   if(myActiveWidget) {
179     myActiveWidget->deactivate();
180     myActiveWidget->setHighlighted(false);
181   }
182   if(theWidget) {
183     if (theWidget)
184       emit beforeWidgetActivated(theWidget);
185     theWidget->activate();
186     theWidget->setHighlighted(true);
187   }
188   myActiveWidget = theWidget;
189   if (myActiveWidget)
190     emit widgetActivated(theWidget);
191   else if (!isEditingMode())
192     emit noMoreWidgets();
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 }