]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PropertyPanel.cpp
Salome HOME
The save returns the list of saved files
[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_PropertyPanel.h>
9 #include <XGUI_Constants.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     : QDockWidget(theParent)
27 {
28   this->setWindowTitle(tr("Property Panel"));
29   QAction* aViewAct = this->toggleViewAction();
30   this->setObjectName(XGUI::PROP_PANEL);
31   setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
32
33   QWidget* aContent = new QWidget(this);
34   myMainLayout = new QVBoxLayout(aContent);
35   myMainLayout->setContentsMargins(3, 3, 3, 3);
36   this->setWidget(aContent);
37
38   QFrame* aFrm = new QFrame(aContent);
39   aFrm->setFrameStyle(QFrame::Sunken);
40   aFrm->setFrameShape(QFrame::Panel);
41   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
42   aBtnLay->setContentsMargins(0, 0, 0, 0);
43   myMainLayout->addWidget(aFrm);
44
45   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
46   aBtn->setFlat(true);
47   aBtnLay->addWidget(aBtn);
48   aBtnLay->addStretch(1);
49   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
50   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
51   aBtn->setToolTip(tr("Ok"));
52   aBtn->setFlat(true);
53   aBtnLay->addWidget(aBtn);
54   aBtn->installEventFilter(this);
55
56   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
57   aBtn->setToolTip(tr("Cancel"));
58   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
59   aBtn->setFlat(true);
60   aBtnLay->addWidget(aBtn);
61
62   myCustomWidget = new QWidget(aContent);
63   myMainLayout->addWidget(myCustomWidget);
64   myMainLayout->addStretch(1);
65
66   aBtn->installEventFilter(this);
67 }
68
69 XGUI_PropertyPanel::~XGUI_PropertyPanel()
70 {
71 }
72
73 void XGUI_PropertyPanel::cleanContent()
74 {
75   myWidgets.clear();
76   qDeleteAll(myCustomWidget->children());
77 }
78
79 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
80 {
81   myWidgets = theWidgets;
82   if (theWidgets.empty()) return;
83
84   QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast =
85       theWidgets.end();
86   for (; anIt != aLast; anIt++) {
87     //TODO(sbh): Think how to connect prop panle hotkeys and operations mgr
88     connect(*anIt, SIGNAL(keyReleased(QKeyEvent*)), this,
89             SIGNAL(keyReleased(QKeyEvent*)));
90
91     connect(*anIt, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)), this,
92             SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
93
94     //connect(*anIt, SIGNAL(activated(ModuleBase_ModelWidget*)),
95     //        this, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)));
96
97     ModuleBase_WidgetPoint2D* aPointWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(*anIt);
98     if (aPointWidget)
99       connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
100               SIGNAL(storedPoint2D(ObjectPtr, const std::string&)));
101   }
102   ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
103   if (aLastWidget) {
104     QList<QWidget*> aControls = aLastWidget->getControls();
105     if (!aControls.empty()) {
106       QWidget* aLastControl = aControls.last();
107
108       QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
109       QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
110
111       setTabOrder(aLastControl, anOkBtn);
112       setTabOrder(anOkBtn, aCancelBtn);
113     }
114   }
115   onActivateNextWidget(NULL);
116 }
117
118 const QList<ModuleBase_ModelWidget*>& XGUI_PropertyPanel::modelWidgets() const
119 {
120   return myWidgets;
121 }
122
123 bool XGUI_PropertyPanel::eventFilter(QObject *theObject, QEvent *theEvent)
124 {
125   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
126   QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
127   if (theObject == anOkBtn || theObject == aCancelBtn) {
128     if (theEvent->type() == QEvent::KeyRelease) {
129       QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
130       if (aKeyEvent && (aKeyEvent->key() == Qt::Key_Return ||
131                         aKeyEvent->key() == Qt::Key_Enter)) {
132         // TODO: this is enter button processing when the focus is on "Apply" or "Cancel" buttons
133         //emit keyReleased("", (QKeyEvent*) theEvent);
134         return true;
135       }
136     }
137   }
138   return QDockWidget::eventFilter(theObject, theEvent);
139 }
140
141 QWidget* XGUI_PropertyPanel::contentWidget()
142 {
143   return myCustomWidget;
144 }
145
146 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
147 {
148   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets)
149   {
150     eachWidget->setFeature(theFeature);
151     eachWidget->restoreValue();
152   }
153   // the repaint is used here to immediatelly react in GUI to the values change.
154   repaint();
155 }
156
157 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
158 {
159   ModuleBase_ModelWidget* aNextWidget = 0;
160   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(), aLast = myWidgets.end();
161   bool isFoundWidget = false;
162   for (; anIt != aLast && !aNextWidget; anIt++) {
163     if (isFoundWidget || !theWidget) {
164       if ((*anIt)->focusTo()) {
165         aNextWidget = *anIt;
166       }
167     }
168     isFoundWidget = (*anIt) == theWidget;
169   }
170   emit widgetActivated(aNextWidget);
171 }
172
173 void XGUI_PropertyPanel::setAcceptEnabled(bool isEnabled)
174 {
175   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
176   anOkBtn->setEnabled(isEnabled);
177 }