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), myWorkshop(theWorkshop), myActivateOnStart(false)
32 myFeatureAttributeID = theData->widgetId();
34 myContainer = new QWidget(theParent);
35 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
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));
43 aLayout->addWidget(myLabel);
45 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
46 myTextLine = new QLineEdit(myContainer);
47 myTextLine->setReadOnly(true);
48 myTextLine->setToolTip(aToolTip);
49 myTextLine->installEventFilter(this);
51 aLayout->addWidget(myTextLine);
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)));
59 aLayout->addWidget(myActivateBtn);
61 QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
62 if (!aActivateTxt.isNull()) {
63 myActivateOnStart = (aActivateTxt == "true");
67 //********************************************************************
68 ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget()
72 //********************************************************************
73 bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature) const
75 DataPtr aData = theFeature->data();
76 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
77 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(myFeatureAttributeID));
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));
87 //********************************************************************
88 bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature)
90 DataPtr aData = theFeature->data();
91 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(myFeatureAttributeID);
93 bool isBlocked = this->blockSignals(true);
94 mySelectedFeature = aRef->value();
95 updateSelectionName();
97 this->blockSignals(isBlocked);
101 //********************************************************************
102 QList<QWidget*> ModuleBase_SelectorWidget::getControls() const
104 QList<QWidget*> aControls;
105 aControls.append(myLabel);
106 aControls.append(myTextLine);
107 aControls.append(myActivateBtn);
111 //********************************************************************
112 void ModuleBase_SelectorWidget::onSelectionChanged()
114 QList<FeaturePtr> aFeatures = myWorkshop->selectedFeatures();
115 if (aFeatures.size() > 0) {
116 FeaturePtr aFeature = aFeatures.first();
117 if ((!mySelectedFeature) && (!aFeature))
119 if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature))
122 // TODO: Check that the selection corresponds to selection type
123 if (aFeature->getKind().compare("Sketch") != 0)
126 mySelectedFeature = aFeature;
127 if (mySelectedFeature) {
128 updateSelectionName();
129 activateSelection(false);
132 myTextLine->setText("");
134 emit valuesChanged();
138 //********************************************************************
139 void ModuleBase_SelectorWidget::updateSelectionName()
141 if (mySelectedFeature) {
143 if (mySelectedFeature->data())
144 aName = mySelectedFeature->data()->getName();
146 aName = boost::dynamic_pointer_cast<ModelAPI_Object>(mySelectedFeature)->getName();
148 myTextLine->setText(QString::fromStdString(aName));
150 myTextLine->setText("");
153 //********************************************************************
154 bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent)
156 if (theObj == myTextLine) {
157 if (theEvent->type() == QEvent::Polish) {
158 activateSelection(myActivateOnStart);
159 onSelectionChanged();
162 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
165 //********************************************************************
166 void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable) const
168 QWidget* aParent = myContainer->parentWidget();
169 QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
170 foreach(QWidget* aWgt, aChldList) {
171 if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
172 aWgt->setEnabled(toEnable);
176 //********************************************************************
177 void ModuleBase_SelectorWidget::activateSelection(bool toActivate)
179 enableOthersControls(!toActivate);
180 myTextLine->setEnabled(toActivate);
183 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
185 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
187 myActivateBtn->setDown(toActivate);
190 //********************************************************************
191 void ModuleBase_SelectorWidget::raisePanel() const
193 QWidget* aParent = myContainer->parentWidget();
194 QWidget* aLastPanel = 0;
195 while (!aParent->inherits("QDockWidget")) {
196 aLastPanel = aParent;
197 aParent = aParent->parentWidget();
198 if (!aParent) return;
200 if (aParent->inherits("QDockWidget")) {
201 QDockWidget* aTabWgt = (QDockWidget*) aParent;