]> 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 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
126 {
127   return myWidgets;
128 }
129
130 bool XGUI_PropertyPanel::eventFilter(QObject *theObject, QEvent *theEvent)
131 {
132   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
133   QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
134   if (theObject == anOkBtn || theObject == aCancelBtn) {
135     if (theEvent->type() == QEvent::KeyRelease) {
136       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
137       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
138         // TODO: this is enter button processing when the focus is on "Apply" or "Cancel" buttons
139         emit keyReleased("", (QKeyEvent*) theEvent);
140         return true;
141       }
142     }
143   }
144   return QDockWidget::eventFilter(theObject, theEvent);
145 }
146
147 QWidget* XGUI_PropertyPanel::contentWidget()
148 {
149   return myCustomWidget;
150 }
151
152 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
153 {
154   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
155     eachWidget->restoreValue(theFeature);
156   }
157   // the repaint is used here to immediatelly react in GUI to the values change.
158   repaint();
159 }
160
161 void XGUI_PropertyPanel::onFocusActivated(const std::string& theAttributeName)
162 {
163   if (theAttributeName == XGUI::PROP_PANEL_OK) {
164     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
165     aBtn->setFocus();
166   }
167   if (theAttributeName == XGUI::PROP_PANEL_CANCEL) {
168     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
169     aBtn->setFocus();
170   }
171   else {
172     foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
173       if (eachWidget->canFocusTo(theAttributeName)) {
174         eachWidget->focusTo();
175         break;
176       }
177     }
178   }
179 }
180
181 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
182 {
183   ModuleBase_ModelWidget* aNextWidget = 0;
184
185   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
186                                                  aLast = myWidgets.end();
187   for (;anIt != aLast; anIt++)
188   {
189     if ((*anIt) == theWidget) {
190       anIt++;
191       if (anIt != aLast)
192         aNextWidget = *anIt;
193       break;
194     }
195   }
196   activateWidget(aNextWidget);
197 }
198
199 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
200 {
201   if (theWidget)
202     theWidget->focusTo();
203   emit widgetActivated(theWidget);
204 }