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