]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / XGUI / XGUI_PropertyPanel.cpp
1 /*
2  * XGUI_PropertyPanel.cpp
3  *
4  *  Created on: Apr 29, 2014
5  *      Author: sbh
6  */
7
8 #include <XGUI_Constants.h>
9 #include <XGUI_PropertyPanel.h>
10 #include <ModuleBase_WidgetPoint2D.h>
11
12 #include <QWidget>
13 #include <QVBoxLayout>
14 #include <QFrame>
15 #include <QPushButton>
16 #include <QIcon>
17 #include <QVBoxLayout>
18 #include <QEvent>
19 #include <QKeyEvent>
20
21 #ifdef _DEBUG
22 #include <iostream>
23 #endif
24
25 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
26 {
27   this->setWindowTitle(tr("Property Panel"));
28   QAction* aViewAct = this->toggleViewAction();
29   this->setObjectName(XGUI::PROP_PANEL);
30   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
31
32   QWidget* aContent = new QWidget(this);
33   myMainLayout = new QVBoxLayout(aContent);
34   myMainLayout->setContentsMargins(3, 3, 3, 3);
35   this->setWidget(aContent);
36
37   QFrame* aFrm = new QFrame(aContent);
38   aFrm->setFrameStyle(QFrame::Sunken);
39   aFrm->setFrameShape(QFrame::Panel);
40   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
41   aBtnLay->setContentsMargins(0, 0, 0, 0);
42   myMainLayout->addWidget(aFrm);
43
44   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
45   aBtn->setFlat(true);
46   aBtnLay->addWidget(aBtn);
47   aBtnLay->addStretch(1);
48   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
49   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
50   aBtn->setToolTip(tr("Ok"));
51   aBtn->setFlat(true);
52   aBtnLay->addWidget(aBtn);
53   aBtn->installEventFilter(this);
54
55   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
56   aBtn->setToolTip(tr("Cancel"));
57   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
58   aBtn->setFlat(true);
59   aBtnLay->addWidget(aBtn);
60
61   myCustomWidget = new QWidget(aContent);
62   myMainLayout->addWidget(myCustomWidget);
63   myMainLayout->addStretch(1);
64
65   aBtn->installEventFilter(this);
66 }
67
68 XGUI_PropertyPanel::~XGUI_PropertyPanel()
69 {
70 }
71
72 void XGUI_PropertyPanel::cleanContent()
73 {
74   myWidgets.clear();
75   
76   QLayoutItem* aItem = myMainLayout->takeAt(myMainLayout->count() - 1);
77   delete aItem;
78
79   myMainLayout->removeWidget(myCustomWidget);
80   delete myCustomWidget;
81
82   myCustomWidget = new QWidget(widget());
83   myMainLayout->addWidget(myCustomWidget);
84   myMainLayout->addStretch(1);
85 }
86
87 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
88 {
89   myWidgets = theWidgets;
90
91   if (!theWidgets.empty()) {
92     QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
93     for (; anIt != aLast; anIt++) {
94       connect(*anIt, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
95               this, SIGNAL(keyReleased(const std::string&, QKeyEvent*)));
96
97       connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
98               this, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
99
100       ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
101       if (aPointWidget)
102         connect(aPointWidget, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
103                 this, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)));
104     }
105     ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
106     if (aLastWidget) {
107       QList<QWidget*> aControls = aLastWidget->getControls();
108       if (!aControls.empty()) {
109         QWidget* aLastControl = aControls.last();
110
111         QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
112         QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
113
114         setTabOrder(aLastControl, anOkBtn);
115         setTabOrder(anOkBtn, aCancelBtn);
116       }
117     }
118     ModuleBase_ModelWidget* aWidget = theWidgets.first();
119     if (aWidget) {
120       activateWidget(aWidget);
121     }
122   }
123 }
124
125 bool XGUI_PropertyPanel::eventFilter(QObject *theObject, QEvent *theEvent)
126 {
127   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
128   QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
129   if (theObject == anOkBtn || theObject == aCancelBtn) {
130     if (theEvent->type() == QEvent::KeyRelease) {
131       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
132       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
133         // TODO: this is enter button processing when the focus is on "Apply" or "Cancel" buttons
134         emit keyReleased("", (QKeyEvent*) theEvent);
135         return true;
136       }
137     }
138   }
139   return QDockWidget::eventFilter(theObject, theEvent);
140 }
141
142 QWidget* XGUI_PropertyPanel::contentWidget()
143 {
144   return myCustomWidget;
145 }
146
147 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
148 {
149   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
150     eachWidget->restoreValue(theFeature);
151   }
152   // the repaint is used here to immediatelly react in GUI to the values change.
153   repaint();
154 }
155
156 void XGUI_PropertyPanel::onFocusActivated(const std::string& theAttributeName)
157 {
158   if (theAttributeName == XGUI::PROP_PANEL_OK) {
159     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
160     aBtn->setFocus();
161   }
162   if (theAttributeName == XGUI::PROP_PANEL_CANCEL) {
163     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
164     aBtn->setFocus();
165   }
166   else {
167     foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
168       if (eachWidget->canFocusTo(theAttributeName)) {
169         eachWidget->focusTo();
170         break;
171       }
172     }
173   }
174 }
175
176 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
177 {
178   ModuleBase_ModelWidget* aNextWidget = 0;
179
180   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
181                                                  aLast = myWidgets.end();
182   for (;anIt != aLast; anIt++)
183   {
184     if ((*anIt) == theWidget) {
185       anIt++;
186       if (anIt != aLast)
187         aNextWidget = *anIt;
188       break;
189     }
190   }
191   activateWidget(aNextWidget);
192 }
193
194 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
195 {
196   if (theWidget)
197     theWidget->focusTo();
198   emit widgetActivated(theWidget);
199 }