]> 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
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   myMainLayout = new QVBoxLayout(aContent);
33   myMainLayout->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   myMainLayout->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   myMainLayout->addWidget(myCustomWidget);
62   myMainLayout->addStretch(1);
63
64   aBtn->installEventFilter(this);
65 }
66
67 XGUI_PropertyPanel::~XGUI_PropertyPanel()
68 {
69 }
70
71 void XGUI_PropertyPanel::cleanContent()
72 {
73   myWidgets.clear();
74   
75   QLayoutItem* aItem = myMainLayout->takeAt(myMainLayout->count() - 1);
76   delete aItem;
77
78   myMainLayout->removeWidget(myCustomWidget);
79   delete myCustomWidget;
80
81   myCustomWidget = new QWidget(widget());
82   myMainLayout->addWidget(myCustomWidget);
83   myMainLayout->addStretch(1);
84 }
85
86 void XGUI_PropertyPanel::setModelWidgets(const QList<ModuleBase_ModelWidget*>& theWidgets)
87 {
88   myWidgets = theWidgets;
89
90   if (!theWidgets.empty()) {
91     QList<ModuleBase_ModelWidget*>::const_iterator anIt = theWidgets.begin(), aLast = theWidgets.end();
92     for (; anIt != aLast; anIt++) {
93       connect(*anIt, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
94               this, SIGNAL(keyReleased(const std::string&, QKeyEvent*)));
95     }
96     ModuleBase_ModelWidget* aLastWidget = theWidgets.last();
97     if (aLastWidget) {
98       QList<QWidget*> aControls = aLastWidget->getControls();
99       if (!aControls.empty()) {
100         QWidget* aLastControl = aControls.last();
101
102         QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
103         QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
104
105         setTabOrder(aLastControl, anOkBtn);
106         setTabOrder(anOkBtn, aCancelBtn);
107       }
108     }
109     ModuleBase_ModelWidget* aWidget = theWidgets.first();
110     if (aWidget) {
111       activateWidget(aWidget);
112     }
113   }
114 }
115
116 bool XGUI_PropertyPanel::eventFilter(QObject *theObject, QEvent *theEvent)
117 {
118   QPushButton* anOkBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
119   QPushButton* aCancelBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
120   if (theObject == anOkBtn || theObject == aCancelBtn) {
121     if (theEvent->type() == QEvent::KeyRelease) {
122       QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
123       if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
124         // TODO: this is enter button processing when the focus is on "Apply" or "Cancel" buttons
125         emit keyReleased("", (QKeyEvent*) theEvent);
126         return true;
127       }
128     }
129   }
130   return QDockWidget::eventFilter(theObject, theEvent);
131 }
132
133 QWidget* XGUI_PropertyPanel::contentWidget()
134 {
135   return myCustomWidget;
136 }
137
138 void XGUI_PropertyPanel::updateContentWidget(FeaturePtr theFeature)
139 {
140   foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
141     eachWidget->restoreValue(theFeature);
142   }
143   // the repaint is used here to immediatelly react in GUI to the values change.
144   repaint();
145 }
146
147 void XGUI_PropertyPanel::onFocusActivated(const std::string& theAttributeName)
148 {
149   if (theAttributeName == XGUI::PROP_PANEL_OK) {
150     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
151     aBtn->setFocus();
152   }
153   if (theAttributeName == XGUI::PROP_PANEL_CANCEL) {
154     QPushButton* aBtn = findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
155     aBtn->setFocus();
156   }
157   else {
158     foreach(ModuleBase_ModelWidget* eachWidget, myWidgets) {
159       if (eachWidget->canFocusTo(theAttributeName)) {
160         eachWidget->focusTo();
161         break;
162       }
163     }
164   }
165 }
166
167 void XGUI_PropertyPanel::onActivateNextWidget(ModuleBase_ModelWidget* theWidget)
168 {
169   ModuleBase_ModelWidget* aNextWidget = 0;
170
171   QList<ModuleBase_ModelWidget*>::const_iterator anIt = myWidgets.begin(),
172                                                  aLast = myWidgets.end();
173   for (;anIt != aLast; anIt++)
174   {
175     if ((*anIt) == theWidget) {
176       anIt++;
177       if (anIt != aLast)
178         aNextWidget = *anIt;
179       break;
180     }
181   }
182   activateWidget(aNextWidget);
183 }
184
185 void XGUI_PropertyPanel::activateWidget(ModuleBase_ModelWidget* theWidget)
186 {
187   if (theWidget)
188     theWidget->focusTo();
189   emit widgetActivated(theWidget);
190 }