1 // File: ModuleBase_SelectorWidget.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
6 #include "ModuleBase_SelectorWidget.h"
7 #include "ModuleBase_IWorkshop.h"
9 #include <Events_Loop.h>
10 #include <Model_Events.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <Config_WidgetAPI.h>
21 #include <QToolButton>
24 #include <QDockWidget>
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)
32 myContainer = new QWidget(theParent);
33 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
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));
41 aLayout->addWidget(myLabel);
43 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
44 myTextLine = new QLineEdit(myContainer);
45 myTextLine->setReadOnly(true);
46 myTextLine->setToolTip(aToolTip);
47 myTextLine->installEventFilter(this);
49 aLayout->addWidget(myTextLine);
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)));
57 aLayout->addWidget(myActivateBtn);
59 QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
60 if (!aActivateTxt.isNull()) {
61 myActivateOnStart = (aActivateTxt == "true");
65 //********************************************************************
66 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
70 //********************************************************************
71 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature) const
73 DataPtr aData = theFeature->data();
74 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
75 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
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));
85 //********************************************************************
86 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
88 DataPtr aData = theFeature->data();
89 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
91 bool isBlocked = this->blockSignals(true);
92 mySelectedFeature = aRef->value();
93 updateSelectionName();
95 this->blockSignals(isBlocked);
99 //********************************************************************
100 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
102 QList<QWidget*> aControls;
103 aControls.append(myLabel);
104 aControls.append(myTextLine);
105 aControls.append(myActivateBtn);
109 //********************************************************************
110 void ModuleBase_SelectorWidget::onSelectionChanged()
112 QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
113 if (aFeatures.size() > 0) {
114 FeaturePtr aFeature = aFeatures.first();
115 if ((!mySelectedFeature) && (!aFeature))
117 if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
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)
125 mySelectedFeature = aFeature;
126 if (mySelectedFeature) {
127 updateSelectionName();
128 activateSelection(false);
131 myTextLine->setText("");
133 emit valuesChanged();
137 //********************************************************************
138 void ModuleBase_SelectorWidget::updateSelectionName()
140 if (mySelectedFeature) {
142 if (mySelectedFeature->data())
143 aName = mySelectedFeature->data()->getName();
145 aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
147 myTextLine->setText(QString::fromStdString(aName));
149 myTextLine->setText("");
152 //********************************************************************
153 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
155 if (theObj == myTextLine) {
156 if (theEvent->type() == QEvent::Polish) {
157 activateSelection(myActivateOnStart);
158 onSelectionChanged();
161 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
164 //********************************************************************
165 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable) const
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);
175 //********************************************************************
176 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
178 enableOthersControls(!toActivate);
179 myTextLine->setEnabled(toActivate);
182 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
184 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
186 myActivateBtn->setDown(toActivate);
189 //********************************************************************
190 void ModuleBase_SelectorWidget::raisePanel() const
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;
199 if (aParent->inherits("QDockWidget")) {
200 QDockWidget* aTabWgt = (QDockWidget*) aParent;