Salome HOME
f6945234c70eaff528582abe1819314cc5688087
[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 #include <QDockWidget>
25
26
27 ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent, 
28                                                      ModuleBase_IWorkshop* theWorkshop, 
29                                                      const Config_WidgetAPI* theData)
30 : ModuleBase_ModelWidget(theParent), myWorkshop(theWorkshop), myActivateOnStart(false)
31 {
32   myFeatureAttributeID = theData->widgetId();
33
34   myContainer = new QWidget(theParent);
35   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
36
37   aLayout->setContentsMargins(0, 0, 0, 0);
38   QString aLabelText = QString::fromStdString(theData->widgetLabel());
39   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
40   myLabel = new QLabel(aLabelText, myContainer);
41   myLabel->setPixmap(QPixmap(aLabelIcon));
42
43   aLayout->addWidget(myLabel);
44
45   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
46   myTextLine = new QLineEdit(myContainer);
47   myTextLine->setReadOnly(true);
48   myTextLine->setToolTip(aToolTip);
49   myTextLine->installEventFilter(this);
50
51   aLayout->addWidget(myTextLine);
52
53   myActivateBtn = new QToolButton(myContainer);
54   myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
55   myActivateBtn->setCheckable(true);
56   myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
57   connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
58
59   aLayout->addWidget(myActivateBtn);
60
61   QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
62   if (!aActivateTxt.isNull()) {
63     myActivateOnStart = (aActivateTxt == "true");
64   }
65 }
66
67 //********************************************************************
68 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
69 {
70 }
71
72 //********************************************************************
73 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature) const
74 {
75   DataPtr aData = theFeature->data();
76   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
77     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
78
79   FeaturePtr aFeature = aRef->value();
80   if (!(aFeature && aFeature->isSame(mySelectedFeature))) {
81     aRef->setValue(mySelectedFeature);
82     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
83   }
84   return true;
85 }
86
87 //********************************************************************
88 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
89 {
90   DataPtr aData = theFeature->data();
91   boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(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 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
103 {
104   QList<QWidget*> aControls;
105   aControls.append(myLabel);
106   aControls.append(myTextLine);
107   aControls.append(myActivateBtn);
108   return aControls;
109 }
110
111 //********************************************************************
112 void ModuleBase_SelectorWidget::onSelectionChanged()
113 {
114   QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
115   if (aFeatures.size() > 0) {
116     FeaturePtr aFeature = aFeatures.first();
117     if ((!mySelectedFeature) && (!aFeature))
118       return;
119     if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
120       return;
121
122     // TODO: Check that the selection corresponds to selection type
123     // TODO: Use the feature kind definition like SKETCH_KIND instead of the direct text
124     if (aFeature->getKind().compare("Sketch") != 0)
125       return;
126
127     mySelectedFeature = aFeature;
128     if (mySelectedFeature) {
129       updateSelectionName();
130       activateSelection(false);
131       raisePanel();
132     } else {
133       myTextLine->setText("");
134     }
135     emit valuesChanged();
136   }
137 }
138
139 //********************************************************************
140 void ModuleBase_SelectorWidget::updateSelectionName()
141 {
142   if (mySelectedFeature) {
143     std::string aName;
144     if (mySelectedFeature->data())
145       aName = mySelectedFeature->data()->getName();
146     else 
147       aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
148  
149     myTextLine->setText(QString::fromStdString(aName));
150   } else 
151     myTextLine->setText("");
152 }
153
154 //********************************************************************
155 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
156 {
157   if (theObj == myTextLine) {
158     if (theEvent->type() == QEvent::Polish) {
159       activateSelection(myActivateOnStart);
160       onSelectionChanged();
161     }
162   }
163   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
164 }
165
166 //********************************************************************
167 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable) const
168 {
169   QWidget* aParent = myContainer->parentWidget();
170   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
171   foreach(QWidget* aWgt, aChldList) {
172     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
173       aWgt->setEnabled(toEnable);
174   }
175 }
176
177 //********************************************************************
178 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
179 {
180   enableOthersControls(!toActivate);
181   myTextLine->setEnabled(toActivate);
182
183   if (toActivate)
184     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
185   else
186     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
187
188   myActivateBtn->setDown(toActivate);
189 }
190
191 //********************************************************************
192 void ModuleBase_SelectorWidget::raisePanel() const
193 {
194   QWidget* aParent = myContainer->parentWidget();
195   QWidget* aLastPanel = 0;
196   while (!aParent->inherits("QDockWidget")) {
197     aLastPanel = aParent;
198     aParent = aParent->parentWidget();
199     if (!aParent) return;
200   }
201   if (aParent->inherits("QDockWidget")) {
202     QDockWidget* aTabWgt = (QDockWidget*) aParent;
203     aTabWgt->raise();
204   }
205 }