]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Pass focus to children widgets (pages)
[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     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   if(myActiveWidget) {
178     myActiveWidget->deactivate();
179     myActiveWidget->setHighlighted(false);
180   }
181   if(theWidget) {
182     if (theWidget)
183       emit beforeWidgetActivated(theWidget);
184     theWidget->activate();
185     theWidget->setHighlighted(true);
186   }
187   myActiveWidget = theWidget;
188   if (myActiveWidget)
189     emit widgetActivated(theWidget);
190   else if (!isEditingMode())
191     emit noMoreWidgets();
192 }
193
194 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
195 {
196   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
197   anCancelBtn->setEnabled(theEnabled);
198 }
199
200 bool XGUI_PropertyPanel::isCancelEnabled() const
201 {
202   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
203   return anCancelBtn->isEnabled();
204 }
205
206 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
207 {
208   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
209   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
210     aWgt->setEditingMode(isEditing);
211   }
212 }
213
214 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
215 {
216   QStringList aButtonNames;
217   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
218   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
219   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
220   for (int i = 0; i < aButtonNames.size(); ++i) {
221     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
222     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
223     aBtn->setDefaultAction(anAct);
224   }
225 }