Salome HOME
Replace boost::shared_ptr<ModelAPI_Feature> on FeaturePtr
[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
11 #include <QWidget>
12 #include <QVBoxLayout>
13 #include <QFrame>
14 #include <QPushButton>
15 #include <QIcon>
16 #include <QVBoxLayout>
17 #include <QEvent>
18 #include <QKeyEvent>
19
20 #ifdef _DEBUG
21 #include <iostream>
22 #endif
23
24 XGUI_PropertyPanel::XGUI_PropertyPanel(QWidget* theParent)
25 {
26   this->setWindowTitle(tr("Property Panel"));
27   QAction* aViewAct = this->toggleViewAction();
28   this->setObjectName(XGUI::PROP_PANEL);
29   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
30
31   QWidget* aContent = new QWidget(this);
32   QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
33   aMainLay->setContentsMargins(3, 3, 3, 3);
34   this->setWidget(aContent);
35
36   QFrame* aFrm = new QFrame(aContent);
37   aFrm->setFrameStyle(QFrame::Sunken);
38   aFrm->setFrameShape(QFrame::Panel);
39   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
40   aBtnLay->setContentsMargins(0, 0, 0, 0);
41   aMainLay->addWidget(aFrm);
42
43   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
44   aBtn->setFlat(true);
45   aBtnLay->addWidget(aBtn);
46   aBtnLay->addStretch(1);
47   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
48   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
49   aBtn->setToolTip(tr("Ok"));
50   aBtn->setFlat(true);
51   aBtnLay->addWidget(aBtn);
52   aBtn->installEventFilter(this);
53
54   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
55   aBtn->setToolTip(tr("Cancel"));
56   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
57   aBtn->setFlat(true);
58   aBtnLay->addWidget(aBtn);
59
60   myCustomWidget = new QWidget(aContent);
61   aMainLay->addWidget(myCustomWidget);
62   aMainLay->addStretch(1);
63
64   aBtn->installEventFilter(this);
65 }
66
67 XGUI_PropertyPanel::~XGUI_PropertyPanel()
68 {
69 }
70
71 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
72 {
73   myWidgets = theWidgets;
74
75   if (!theWidgets.empty()) {
76     QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
77     for (; anIt != aLast; anIt++) {
78       connect(*anIt, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
79               this, SIGNAL(keyReleased(const std::string&, QKeyEvent*)));
80     }
81     ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
82     if (aLastWidget) {
83       QList<QWidget*> aControls = aLastWidget->getControls();
84       if (!aControls.empty()) {
85         QWidget* aLastControl = aControls.last();
86
87         QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
88         QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
89
90         setTabOrder(aLastControl, anOkBtn);
91         setTabOrder(anOkBtn, aCancelBtn);
92       }
93     }
94     ModuleBase_ModelWidget* aWidget = theWidgets.first();
95     if (aWidget)
96       aWidget->focusTo();
97   }
98 }
99
100 bool XGUI_PropertyPanel::eventFilter(QObject *theObject, QEvent *theEvent)
101 {
102   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
103   QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
104   if (theObject == anOkBtn || theObject == aCancelBtn) {
105     if (theEvent->type() == QEvent::KeyRelease) {
106       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
107       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
108         // TODO: this is enter button processing when the focus is on "Apply" or "Cancel" buttons
109         emit keyReleased("", (QKeyEvent*) theEvent);
110         return true;
111       }
112     }
113   }
114   return QDockWidget::eventFilter(theObject, theEvent);
115 }
116
117 QWidget* XGUI_PropertyPanel::contentWidget()
118 {
119   return myCustomWidget;
120 }
121
122 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
123 {
124   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
125     eachWidget->restoreValue(theFeature);
126   }
127   // the repaint is used here to immediatelly react in GUI to the values change.
128   repaint();
129 }
130
131 void XGUI_PropertyPanel::onFocusActivated(const std::string& theAttributeName)
132 {
133   if (theAttributeName == XGUI::PROP_PANEL_OK) {
134     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
135     aBtn->setFocus();
136   }
137   if (theAttributeName == XGUI::PROP_PANEL_CANCEL) {
138     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
139     aBtn->setFocus();
140   }
141   else {
142     foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
143       if (eachWidget->canFocusTo(theAttributeName)) {
144         eachWidget->focusTo();
145         break;
146       }
147     }
148   }
149 }