]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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       connect(*anIt, SIGNAL(activated(ModuleBase_ModelWidget*)),
101               this, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)));
102
103       ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
104       if (aPointWidget)
105         connect(aPointWidget, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
106                 this, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)));
107     }
108     ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
109     if (aLastWidget) {
110       QList<QWidget*> aControls = aLastWidget->getControls();
111       if (!aControls.empty()) {
112         QWidget* aLastControl = aControls.last();
113
114         QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
115         QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
116
117         setTabOrder(aLastControl, anOkBtn);
118         setTabOrder(anOkBtn, aCancelBtn);
119       }
120     }
121     onActivateNextWidget(0);
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::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
162 {
163   ModuleBase_ModelWidget* aNextWidget = 0;
164   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
165                                                  aLast = myWidgets.end();
166   bool isFoundWidget = false;
167   for (;anIt != aLast && !aNextWidget; anIt++)
168   {
169     if (isFoundWidget || !theWidget) {
170       if ((*anIt)->focusTo()) {
171         aNextWidget = *anIt;
172       }
173     }
174     isFoundWidget = (*anIt) == theWidget;
175   }
176   emit widgetActivated(aNextWidget);
177 }