]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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
15 #include <QEvent>
16 #include <QFrame>
17 #include <QIcon>
18 #include <QKeyEvent>
19 #include <QLayoutItem>
20 #include <QToolButton>
21 #include <QVBoxLayout>
22 #include <QVBoxLayout>
23 #include <QWidget>
24 #include <QToolButton>
25 #include <QAction>
26
27 #ifdef _DEBUG
28 #include <iostream>
29 #endif
30
31 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
32     : ModuleBase_IPropertyPanel(theParent), 
33     myActiveWidget(NULL)
34 {
35   this->setWindowTitle(tr("Property Panel"));
36   QAction* aViewAct = this->toggleViewAction();
37   this->setObjectName(PROP_PANEL);
38   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
39
40   QWidget* aContent = new QWidget(this);
41   myMainLayout = new QVBoxLayout(aContent);
42   myMainLayout->setContentsMargins(3, 3, 3, 3);
43   this->setWidget(aContent);
44
45   QFrame* aFrm = new QFrame(aContent);
46   aFrm->setFrameStyle(QFrame::Sunken);
47   aFrm->setFrameShape(QFrame::Panel);
48   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
49   aBtnLay->setContentsMargins(0, 0, 0, 0);
50   myMainLayout->addWidget(aFrm);
51
52   QStringList aBtnNames;
53   aBtnNames << QString(PROP_PANEL_HELP)
54             << QString(PROP_PANEL_OK)
55             << QString(PROP_PANEL_CANCEL);
56   foreach(QString eachBtnName, aBtnNames) {
57     QToolButton* aBtn = new QToolButton(aFrm);
58     aBtn->setObjectName(eachBtnName);
59     aBtn->setAutoRaise(true);
60     aBtnLay->addWidget(aBtn);
61   }
62   aBtnLay->insertStretch(1, 1);
63   // aBtn->setShortcut(QKeySequence(Qt::Key_Escape));
64
65   myCustomWidget = new QWidget(aContent);
66   myMainLayout->addWidget(myCustomWidget);
67   setStretchEnabled(true);
68 }
69
70 XGUI_PropertyPanel::~XGUI_PropertyPanel()
71 {
72 }
73
74 void XGUI_PropertyPanel::cleanContent()
75 {
76   if (myActiveWidget)
77     myActiveWidget->deactivate();
78   myWidgets.clear();
79   qDeleteAll(myCustomWidget->children());
80   myActiveWidget = NULL;
81   setWindowTitle(tr("Property Panel"));
82 }
83
84 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
85 {
86   myWidgets = theWidgets;
87   if (theWidgets.empty()) return;
88   bool isEnableStretch = true;
89   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
90       theWidgets.end();
91   for (; anIt != aLast; anIt++) {
92     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
93     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
94             this,  SLOT(activateNextWidget(ModuleBase_ModelWidget*)));
95     connect(*anIt, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
96             this,  SLOT(activateWidget(ModuleBase_ModelWidget*)));
97
98     //ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
99     //if (aPointWidget)
100     //  connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
101     //          SIGNAL(storedPoint2D(ObjectPtr, const std::string&)))
102     //}
103
104     if (!isEnableStretch) continue;
105     foreach(QWidget* eachWidget, (*anIt)->getControls()) {
106       QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
107       if(aVPolicy == QSizePolicy::Expanding ||
108          aVPolicy == QSizePolicy::MinimumExpanding) {
109         isEnableStretch = false;
110       }
111     }
112   }
113   setStretchEnabled(isEnableStretch);
114   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
115   if (aLastWidget) {
116     QList<QWidget*> aControls = aLastWidget->getControls();
117     if (!aControls.empty()) {
118       QWidget* aLastControl = aControls.last();
119
120       QToolButton* anOkBtn = findChild<QToolButton*>(PROP_PANEL_OK);
121       QToolButton* aCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
122
123       setTabOrder(aLastControl, anOkBtn);
124       setTabOrder(anOkBtn, aCancelBtn);
125     }
126   }
127 }
128
129 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
130 {
131   return myWidgets;
132 }
133
134 QWidget* XGUI_PropertyPanel::contentWidget()
135 {
136   return myCustomWidget;
137 }
138
139 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
140 {
141   // Invalid feature case on abort of the operation
142   if (theFeature.get() == NULL)
143     return;
144   if (theFeature->isAction() || !theFeature->data())
145     return;
146   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
147   {
148     eachWidget->setFeature(theFeature);
149     eachWidget->restoreValue();
150   }
151   // the repaint is used here to immediately react in GUI to the values change.
152   repaint();
153 }
154
155 void XGUI_PropertyPanel::activateNextWidget(ModuleBase_ModelWidget* theWidget)
156 {
157   // TO CHECK: Editing operation does not have automatical activation of widgets
158   if (isEditingMode()) {
159     activateWidget(NULL);
160     return;
161   }
162   ModuleBase_ModelWidget* aNextWidget = 0;
163   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
164   bool isFoundWidget = false;
165   for (; anIt != aLast && !aNextWidget; anIt++) {
166     if (isFoundWidget || !theWidget) {
167       if ((*anIt)->focusTo()) {
168         aNextWidget = *anIt;
169       }
170     }
171     isFoundWidget = (*anIt) == theWidget;
172   }
173   // Normaly focusTo is enough to activate widget
174   // here is a special case on mouse click in the viewer
175   //if(aNextWidget == NULL) {
176     activateWidget(aNextWidget);
177   //}
178 }
179
180 void XGUI_PropertyPanel::setStretchEnabled(bool isEnabled)
181 {
182   if (myMainLayout->count() == 0)
183     return;
184   int aStretchIdx = myMainLayout->count() - 1;
185   bool hasStretch = myMainLayout->itemAt(aStretchIdx)->spacerItem() != NULL;
186   QLayoutItem* aChild;
187   if (isEnabled) {
188     if (!hasStretch) myMainLayout->addStretch(1);
189   } else if (hasStretch) {
190     aChild = myMainLayout->takeAt(aStretchIdx);
191     delete aChild;
192   }
193 }
194
195 void XGUI_PropertyPanel::activateNextWidget()
196 {
197   activateNextWidget(myActiveWidget);
198 }
199
200 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
201 {
202   // Avoid activation of already actve widget. It could happen on focusIn event many times
203   if (theWidget == myActiveWidget)
204     return;
205   if(myActiveWidget) {
206     myActiveWidget->deactivate();
207     myActiveWidget->setHighlighted(false);
208   }
209   if(theWidget) {
210     if (theWidget)
211       emit beforeWidgetActivated(theWidget);
212     theWidget->activate();
213     theWidget->setHighlighted(true);
214   }
215   myActiveWidget = theWidget;
216   if (myActiveWidget)
217     emit widgetActivated(theWidget);
218   else if (!isEditingMode())
219     emit noMoreWidgets();
220 }
221
222 void XGUI_PropertyPanel::setCancelEnabled(bool theEnabled)
223 {
224   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
225   anCancelBtn->setEnabled(theEnabled);
226 }
227
228 bool XGUI_PropertyPanel::isCancelEnabled() const
229 {
230   QToolButton* anCancelBtn = findChild<QToolButton*>(PROP_PANEL_CANCEL);
231   return anCancelBtn->isEnabled();
232 }
233
234 void XGUI_PropertyPanel::setEditingMode(bool isEditing)
235 {
236   ModuleBase_IPropertyPanel::setEditingMode(isEditing);
237   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
238     aWgt->setEditingMode(isEditing);
239   }
240 }
241
242 void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr)
243 {
244   QStringList aButtonNames;
245   aButtonNames << PROP_PANEL_OK << PROP_PANEL_CANCEL << PROP_PANEL_HELP;
246   QList<XGUI_ActionsMgr::OperationStateActionId> aActionIds;
247   aActionIds << XGUI_ActionsMgr::Accept << XGUI_ActionsMgr::Abort << XGUI_ActionsMgr::Help;
248   for (int i = 0; i < aButtonNames.size(); ++i) {
249     QToolButton* aBtn = findChild<QToolButton*>(aButtonNames.at(i));
250     QAction* anAct = theMgr->operationStateAction(aActionIds.at(i));
251     aBtn->setDefaultAction(anAct);
252   }
253 }