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