]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_SelectorWidget.cpp
Salome HOME
Merge branch 'SketchSolver'
[modules/shaper.git] / src / ModuleBase / ModuleBase_SelectorWidget.cpp
1 // File:        ModuleBase_SelectorWidget.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5
6 #include "ModuleBase_SelectorWidget.h"
7 #include "ModuleBase_IWorkshop.h"
8
9 #include <Events_Loop.h>
10 #include <Model_Events.h>
11
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <Config_WidgetAPI.h>
16
17 #include <QWidget>
18 #include <QLayout>
19 #include <QLabel>
20 #include <QLineEdit>
21 #include <QToolButton>
22 #include <QString>
23 #include <QEvent>
24
25
26 ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent, 
27                                                      ModuleBase_IWorkshop* theWorkshop, 
28                                                      const Config_WidgetAPI* theData)
29 : ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
30 {
31   myFeatureAttributeID = theData->widgetId();
32
33   myContainer = new QWidget(theParent);
34   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
35
36   aLayout->setContentsMargins(0, 0, 0, 0);
37   QString aLabelText = QString::fromStdString(theData->widgetLabel());
38   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
39   myLabel = new QLabel(aLabelText, myContainer);
40   myLabel->setPixmap(QPixmap(aLabelIcon));
41
42   aLayout->addWidget(myLabel);
43
44   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
45   myTextLine = new QLineEdit(myContainer);
46   myTextLine->setReadOnly(true);
47   myTextLine->setToolTip(aToolTip);
48   myTextLine->installEventFilter(this);
49
50   aLayout->addWidget(myTextLine);
51
52   myActivateBtn = new QToolButton(myContainer);
53   myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
54   myActivateBtn->setCheckable(true);
55   myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
56   connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
57
58   aLayout->addWidget(myActivateBtn);
59
60   QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
61   if (!aActivateTxt.isNull()) {
62     myActivateOnStart = (aActivateTxt == "true");
63   }
64 }
65
66 //********************************************************************
67 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
68 {
69 }
70
71 //********************************************************************
72 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature)
73 {
74   DataPtr aData = theFeature->data();
75   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
76     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
77
78   bool isBlocked = this->blockSignals(true);
79   aRef->setValue(mySelectedFeature);
80   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
81
82   this->blockSignals(isBlocked);
83   return true;
84 }
85
86 //********************************************************************
87 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
88 {
89   DataPtr aData = theFeature->data();
90   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
91     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
92
93   bool isBlocked = this->blockSignals(true);
94   mySelectedFeature = aRef->value();
95   updateSelectionName(); 
96
97   this->blockSignals(isBlocked);
98   return true;
99 }
100
101 //********************************************************************
102 bool ModuleBase_SelectorWidget::canFocusTo(const std::string& theAttributeName)
103 {
104   return false;
105 }
106
107 //********************************************************************
108 void ModuleBase_SelectorWidget::focusTo()
109 {
110 }
111
112 //********************************************************************
113 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
114 {
115   QList<QWidget*> aControls;
116   aControls.append(myLabel);
117   aControls.append(myTextLine);
118   aControls.append(myActivateBtn);
119   return aControls;
120 }
121
122 //********************************************************************
123 void ModuleBase_SelectorWidget::onSelectionChanged()
124 {
125   QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
126   if (aFeatures.size() > 0) {
127     FeaturePtr aFeature = aFeatures.first();
128     if ((!mySelectedFeature) && (!aFeature))
129       return;
130     if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
131       return;
132
133     // TODO: Check that the selection corresponds to selection type
134     if (aFeature->getKind().compare("Sketch") != 0)
135       return;
136
137     mySelectedFeature = aFeature;
138     if (mySelectedFeature) {
139       updateSelectionName();
140       activateSelection(false);
141     } else {
142       myTextLine->setText("");
143     }
144     emit valuesChanged();
145   }
146 }
147
148 //********************************************************************
149 void ModuleBase_SelectorWidget::updateSelectionName()
150 {
151   if (mySelectedFeature) {
152     std::string aName;
153     if (mySelectedFeature->data())
154       aName = mySelectedFeature->data()->getName();
155     else 
156       aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
157  
158     myTextLine->setText(QString::fromStdString(aName));
159   } else 
160     myTextLine->setText("");
161 }
162
163 //********************************************************************
164 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
165 {
166   if (theObj == myTextLine) {
167     if (theEvent->type() == QEvent::Polish) {
168       activateSelection(myActivateOnStart);
169       onSelectionChanged();
170     }
171   }
172   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
173 }
174
175 //********************************************************************
176 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable)
177 {
178   QWidget* aParent = myContainer->parentWidget();
179   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
180   foreach(QWidget* aWgt, aChldList) {
181     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
182       aWgt->setEnabled(toEnable);
183   }
184 }
185
186 //********************************************************************
187 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
188 {
189   enableOthersControls(!toActivate);
190   myTextLine->setEnabled(toActivate);
191
192   if (toActivate)
193     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
194   else
195     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
196
197   myActivateBtn->setDown(toActivate);
198 }