From: vsv Date: Tue, 10 Jun 2014 06:01:57 +0000 (+0400) Subject: Define selection type for selection control X-Git-Tag: V_0.4.4~296^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d2cb1dde43baaa219e72a0de8c60ae8f166445af;p=modules%2Fshaper.git Define selection type for selection control --- diff --git a/src/ConstructionPlugin/extrusion_widget.xml b/src/ConstructionPlugin/extrusion_widget.xml index f368a68cd..c0069ca67 100644 --- a/src/ConstructionPlugin/extrusion_widget.xml +++ b/src/ConstructionPlugin/extrusion_widget.xml @@ -3,7 +3,8 @@ label="Select a face" icon=":icons/sketch.png" tooltip="Select a face for extrusion" - activate="true" + activate="true" + shape_types="face" /> diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 4593a68fe..b56c21791 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -12,7 +12,7 @@ SET(PROJECT_HEADERS ModuleBase_WidgetFactory.h ModuleBase_WidgetPoint2D.h ModuleBase_WidgetSwitch.h - ModuleBase_SelectorWidget.h + ModuleBase_WidgetSelector.h ModuleBase_IWorkshop.h ) @@ -26,7 +26,7 @@ SET(PROJECT_SOURCES ModuleBase_WidgetFactory.cpp ModuleBase_WidgetPoint2D.cpp ModuleBase_WidgetSwitch.cpp - ModuleBase_SelectorWidget.cpp + ModuleBase_WidgetSelector.cpp ) SET(PROJECT_LIBRARIES @@ -54,6 +54,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src/Model ${CMAKE_SOURCE_DIR}/src/ModelAPI ${CMAKE_SOURCE_DIR}/src/GeomDataAPI + ${CMAKE_SOURCE_DIR}/src/GeomAPI ) ADD_DEFINITIONS(-DMODULEBASE_EXPORTS ${CAS_DEFINITIONS}) diff --git a/src/ModuleBase/ModuleBase_SelectorWidget.cpp b/src/ModuleBase/ModuleBase_SelectorWidget.cpp deleted file mode 100644 index f3f19b152..000000000 --- a/src/ModuleBase/ModuleBase_SelectorWidget.cpp +++ /dev/null @@ -1,203 +0,0 @@ -// File: ModuleBase_SelectorWidget.h -// Created: 2 June 2014 -// Author: Vitaly Smetannikov - - -#include "ModuleBase_SelectorWidget.h" -#include "ModuleBase_IWorkshop.h" - -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - - -ModuleBase_SelectorWidget::ModuleBase_SelectorWidget(QWidget* theParent, - ModuleBase_IWorkshop* theWorkshop, - const Config_WidgetAPI* theData) -: ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop), myActivateOnStart(false) -{ - myContainer = new QWidget(theParent); - QHBoxLayout* aLayout = new QHBoxLayout(myContainer); - - aLayout->setContentsMargins(0, 0, 0, 0); - QString aLabelText = QString::fromStdString(theData->widgetLabel()); - QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); - myLabel = new QLabel(aLabelText, myContainer); - myLabel->setPixmap(QPixmap(aLabelIcon)); - - aLayout->addWidget(myLabel); - - QString aToolTip = QString::fromStdString(theData->widgetTooltip()); - myTextLine = new QLineEdit(myContainer); - myTextLine->setReadOnly(true); - myTextLine->setToolTip(aToolTip); - myTextLine->installEventFilter(this); - - aLayout->addWidget(myTextLine); - - myActivateBtn = new QToolButton(myContainer); - myActivateBtn->setIcon(QIcon(":icons/hand_point.png")); - myActivateBtn->setCheckable(true); - myActivateBtn->setToolTip(tr("Activate/Deactivate selection")); - connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool))); - - aLayout->addWidget(myActivateBtn); - - QString aActivateTxt = QString::fromStdString(theData->getProperty("activate")); - if (!aActivateTxt.isNull()) { - myActivateOnStart = (aActivateTxt == "true"); - } -} - -//******************************************************************** -ModuleBase_SelectorWidget::~ModuleBase_SelectorWidget() -{ -} - -//******************************************************************** -bool ModuleBase_SelectorWidget::storeValue(FeaturePtr theFeature) const -{ - DataPtr aData = theFeature->data(); - boost::shared_ptr aRef = - boost::dynamic_pointer_cast(aData->attribute(attributeID())); - - FeaturePtr aFeature = aRef->value(); - if (!(aFeature && aFeature->isSame(mySelectedFeature))) { - aRef->setValue(mySelectedFeature); - Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED)); - } - return true; -} - -//******************************************************************** -bool ModuleBase_SelectorWidget::restoreValue(FeaturePtr theFeature) -{ - DataPtr aData = theFeature->data(); - boost::shared_ptr aRef = aData->reference(attributeID()); - - bool isBlocked = this->blockSignals(true); - mySelectedFeature = aRef->value(); - updateSelectionName(); - - this->blockSignals(isBlocked); - return true; -} - -//******************************************************************** -QList ModuleBase_SelectorWidget::getControls() const -{ - QList aControls; - aControls.append(myLabel); - aControls.append(myTextLine); - aControls.append(myActivateBtn); - return aControls; -} - -//******************************************************************** -void ModuleBase_SelectorWidget::onSelectionChanged() -{ - QList aFeatures = myWorkshop->selectedFeatures(); - if (aFeatures.size() > 0) { - FeaturePtr aFeature = aFeatures.first(); - if ((!mySelectedFeature) && (!aFeature)) - return; - if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature)) - return; - - // TODO: Check that the selection corresponds to selection type - // TODO: Use the feature kind definition like SKETCH_KIND instead of the direct text - if (aFeature->getKind().compare("Sketch") != 0) - return; - - mySelectedFeature = aFeature; - if (mySelectedFeature) { - updateSelectionName(); - activateSelection(false); - raisePanel(); - } else { - myTextLine->setText(""); - } - emit valuesChanged(); - } -} - -//******************************************************************** -void ModuleBase_SelectorWidget::updateSelectionName() -{ - if (mySelectedFeature) { - std::string aName; - if (mySelectedFeature->data()) - aName = mySelectedFeature->data()->getName(); - else - aName = boost::dynamic_pointer_cast(mySelectedFeature)->getName(); - - myTextLine->setText(QString::fromStdString(aName)); - } else - myTextLine->setText(""); -} - -//******************************************************************** -bool ModuleBase_SelectorWidget::eventFilter(QObject* theObj, QEvent* theEvent) -{ - if (theObj == myTextLine) { - if (theEvent->type() == QEvent::Polish) { - activateSelection(myActivateOnStart); - onSelectionChanged(); - } - } - return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); -} - -//******************************************************************** -void ModuleBase_SelectorWidget::enableOthersControls(bool toEnable) const -{ - QWidget* aParent = myContainer->parentWidget(); - QList aChldList = aParent->findChildren(); - foreach(QWidget* aWgt, aChldList) { - if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer)) - aWgt->setEnabled(toEnable); - } -} - -//******************************************************************** -void ModuleBase_SelectorWidget::activateSelection(bool toActivate) -{ - enableOthersControls(!toActivate); - myTextLine->setEnabled(toActivate); - - if (toActivate) - connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - else - disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - - myActivateBtn->setDown(toActivate); -} - -//******************************************************************** -void ModuleBase_SelectorWidget::raisePanel() const -{ - QWidget* aParent = myContainer->parentWidget(); - QWidget* aLastPanel = 0; - while (!aParent->inherits("QDockWidget")) { - aLastPanel = aParent; - aParent = aParent->parentWidget(); - if (!aParent) return; - } - if (aParent->inherits("QDockWidget")) { - QDockWidget* aTabWgt = (QDockWidget*) aParent; - aTabWgt->raise(); - } -} \ No newline at end of file diff --git a/src/ModuleBase/ModuleBase_SelectorWidget.h b/src/ModuleBase/ModuleBase_SelectorWidget.h deleted file mode 100644 index 48e3984ff..000000000 --- a/src/ModuleBase/ModuleBase_SelectorWidget.h +++ /dev/null @@ -1,78 +0,0 @@ -// File: ModuleBase_SelectorWidget.h -// Created: 2 June 2014 -// Author: Vitaly Smetannikov - -#ifndef ModuleBase_SelectorWidget_H -#define ModuleBase_SelectorWidget_H - -#include "ModuleBase.h" -#include "ModuleBase_ModelWidget.h" - -#include - - -class Config_WidgetAPI; -class QWidget; -class QLabel; -class QLineEdit; -class QToolButton; -class ModuleBase_IWorkshop; - -class MODULEBASE_EXPORT ModuleBase_SelectorWidget: public ModuleBase_ModelWidget -{ - Q_OBJECT -public: - ModuleBase_SelectorWidget(QWidget* theParent, - ModuleBase_IWorkshop* theWorkshop, - const Config_WidgetAPI* theData); - - virtual ~ModuleBase_SelectorWidget(); - - /// Saves the internal parameters to the given feature - /// \param theFeature a model feature to be changed - virtual bool storeValue(FeaturePtr theFeature) const; - - virtual bool restoreValue(FeaturePtr theFeature); - - /// Returns the internal parent wiget control, that can be shown anywhere - /// \returns the widget - QWidget* getControl() const { return myContainer; } - - /// Returns list of widget controls - /// \return a control list - virtual QList getControls() const; - - void setActivationOnStart(bool toActivate) { myActivateOnStart = toActivate; } - bool activateOnStart() const { return myActivateOnStart; } - - FeaturePtr selectedFeature() const { return mySelectedFeature; } - -public slots: - - /// Activate or deactivate selection - void activateSelection(bool toActivate); - -protected: - bool eventFilter(QObject* theObj, QEvent* theEvent); - -private slots: - void onSelectionChanged(); - -private: - void enableOthersControls(bool toEnable) const; - void updateSelectionName(); - void raisePanel() const; - - QWidget* myContainer; - QLabel* myLabel; - QLineEdit* myTextLine; - QToolButton* myActivateBtn; - - ModuleBase_IWorkshop* myWorkshop; - - bool myActivateOnStart; - - FeaturePtr mySelectedFeature; -}; - -#endif \ No newline at end of file diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index 4c011a454..e52d5b2d4 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -191,7 +191,7 @@ QString ModuleBase_WidgetFactory::qs(const std::string& theStdString) const QWidget* ModuleBase_WidgetFactory::selectorControl(QWidget* theParent) { - ModuleBase_SelectorWidget* aSelector = new ModuleBase_SelectorWidget(theParent, myWorkshop, myWidgetApi); + ModuleBase_WidgetSelector* aSelector = new ModuleBase_WidgetSelector(theParent, myWorkshop, myWidgetApi); QObject::connect(aSelector, SIGNAL(valuesChanged()), myOperation, SLOT(storeCustomValue())); diff --git a/src/ModuleBase/ModuleBase_WidgetSelector.cpp b/src/ModuleBase/ModuleBase_WidgetSelector.cpp new file mode 100644 index 000000000..df7b431fb --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetSelector.cpp @@ -0,0 +1,254 @@ +// File: ModuleBase_WidgetSelector.h +// Created: 2 June 2014 +// Author: Vitaly Smetannikov + + +#include "ModuleBase_WidgetSelector.h" +#include "ModuleBase_IWorkshop.h" + +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +typedef QMap ShapeTypes; +static ShapeTypes MyShapeTypes; + +TopAbs_ShapeEnum ModuleBase_WidgetSelector::shapeType(const QString& theType) +{ + if (MyShapeTypes.count() == 0) { + MyShapeTypes["face"] = TopAbs_FACE; + MyShapeTypes["vertex"] = TopAbs_VERTEX; + MyShapeTypes["wire"] = TopAbs_WIRE; + MyShapeTypes["edge"] = TopAbs_EDGE; + MyShapeTypes["shell"] = TopAbs_SHELL; + MyShapeTypes["solid"] = TopAbs_SOLID; + } + if (MyShapeTypes.contains(theType)) + return MyShapeTypes[theType]; + throw std::invalid_argument("Shape type defined in XML is not implemented!"); +} + + + + +ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent, + ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData) +: ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop), myActivateOnStart(false) +{ + myContainer = new QWidget(theParent); + QHBoxLayout* aLayout = new QHBoxLayout(myContainer); + + aLayout->setContentsMargins(0, 0, 0, 0); + QString aLabelText = QString::fromStdString(theData->widgetLabel()); + QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); + myLabel = new QLabel(aLabelText, myContainer); + myLabel->setPixmap(QPixmap(aLabelIcon)); + + aLayout->addWidget(myLabel); + + QString aToolTip = QString::fromStdString(theData->widgetTooltip()); + myTextLine = new QLineEdit(myContainer); + myTextLine->setReadOnly(true); + myTextLine->setToolTip(aToolTip); + myTextLine->installEventFilter(this); + + aLayout->addWidget(myTextLine); + + myActivateBtn = new QToolButton(myContainer); + myActivateBtn->setIcon(QIcon(":icons/hand_point.png")); + myActivateBtn->setCheckable(true); + myActivateBtn->setToolTip(tr("Activate/Deactivate selection")); + connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool))); + + aLayout->addWidget(myActivateBtn); + + QString aActivateTxt = QString::fromStdString(theData->getProperty("activate")); + if (!aActivateTxt.isNull()) { + myActivateOnStart = (aActivateTxt == "true"); + } + + std::string aTypes = theData->getProperty("shape_types"); + myShapeTypes = QString(aTypes.c_str()).split(','); +} + +//******************************************************************** +ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector() +{ +} + +//******************************************************************** +bool ModuleBase_WidgetSelector::storeValue(FeaturePtr theFeature) const +{ + DataPtr aData = theFeature->data(); + boost::shared_ptr aRef = + boost::dynamic_pointer_cast(aData->attribute(attributeID())); + + FeaturePtr aFeature = aRef->value(); + if (!(aFeature && aFeature->isSame(mySelectedFeature))) { + aRef->setValue(mySelectedFeature); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED)); + } + return true; +} + +//******************************************************************** +bool ModuleBase_WidgetSelector::restoreValue(FeaturePtr theFeature) +{ + DataPtr aData = theFeature->data(); + boost::shared_ptr aRef = aData->reference(attributeID()); + + bool isBlocked = this->blockSignals(true); + mySelectedFeature = aRef->value(); + updateSelectionName(); + + this->blockSignals(isBlocked); + return true; +} + +//******************************************************************** +QList ModuleBase_WidgetSelector::getControls() const +{ + QList aControls; + aControls.append(myLabel); + aControls.append(myTextLine); + aControls.append(myActivateBtn); + return aControls; +} + +//******************************************************************** +void ModuleBase_WidgetSelector::onSelectionChanged() +{ + QList aFeatures = myWorkshop->selectedFeatures(); + if (aFeatures.size() > 0) { + FeaturePtr aFeature = aFeatures.first(); + if ((!mySelectedFeature) && (!aFeature)) + return; + if (mySelectedFeature && aFeature && mySelectedFeature->isSame(aFeature)) + return; + + // Check that the selection corresponds to selection type + if (!isAccepted(aFeature)) return; + + mySelectedFeature = aFeature; + if (mySelectedFeature) { + updateSelectionName(); + activateSelection(false); + raisePanel(); + } else { + myTextLine->setText(""); + } + emit valuesChanged(); + } +} + +//******************************************************************** +bool ModuleBase_WidgetSelector::isAccepted(const FeaturePtr theFeature) const +{ + boost::shared_ptr aShapePtr = theFeature->data()->shape(); + if (!aShapePtr) return false; + TopoDS_Shape aShape = aShapePtr->impl(); + if (aShape.IsNull()) return false; + + TopAbs_ShapeEnum aShapeType = aShape.ShapeType(); + if (aShapeType == TopAbs_COMPOUND) { + foreach (QString aType, myShapeTypes) { + TopExp_Explorer aEx(aShape, shapeType(aType)); + if (aEx.More()) + return true; + } + } else { + foreach (QString aType, myShapeTypes) { + if (shapeType(aType) == aShapeType) + return true; + } + } + return false; +} + +//******************************************************************** +void ModuleBase_WidgetSelector::updateSelectionName() +{ + if (mySelectedFeature) { + std::string aName; + if (mySelectedFeature->data()) + aName = mySelectedFeature->data()->getName(); + else + aName = boost::dynamic_pointer_cast(mySelectedFeature)->getName(); + + myTextLine->setText(QString::fromStdString(aName)); + } else + myTextLine->setText(""); +} + +//******************************************************************** +bool ModuleBase_WidgetSelector::eventFilter(QObject* theObj, QEvent* theEvent) +{ + if (theObj == myTextLine) { + if (theEvent->type() == QEvent::Polish) { + activateSelection(myActivateOnStart); + onSelectionChanged(); + } + } + return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); +} + +//******************************************************************** +void ModuleBase_WidgetSelector::enableOthersControls(bool toEnable) const +{ + QWidget* aParent = myContainer->parentWidget(); + QList aChldList = aParent->findChildren(); + foreach(QWidget* aWgt, aChldList) { + if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer)) + aWgt->setEnabled(toEnable); + } +} + +//******************************************************************** +void ModuleBase_WidgetSelector::activateSelection(bool toActivate) +{ + enableOthersControls(!toActivate); + myTextLine->setEnabled(toActivate); + + if (toActivate) + connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); + else + disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); + + myActivateBtn->setDown(toActivate); +} + +//******************************************************************** +void ModuleBase_WidgetSelector::raisePanel() const +{ + QWidget* aParent = myContainer->parentWidget(); + QWidget* aLastPanel = 0; + while (!aParent->inherits("QDockWidget")) { + aLastPanel = aParent; + aParent = aParent->parentWidget(); + if (!aParent) return; + } + if (aParent->inherits("QDockWidget")) { + QDockWidget* aTabWgt = (QDockWidget*) aParent; + aTabWgt->raise(); + } +} \ No newline at end of file diff --git a/src/ModuleBase/ModuleBase_WidgetSelector.h b/src/ModuleBase/ModuleBase_WidgetSelector.h new file mode 100644 index 000000000..0d411f3b7 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetSelector.h @@ -0,0 +1,86 @@ +// File: ModuleBase_WidgetSelector.h +// Created: 2 June 2014 +// Author: Vitaly Smetannikov + +#ifndef ModuleBase_WidgetSelector_H +#define ModuleBase_WidgetSelector_H + +#include "ModuleBase.h" +#include "ModuleBase_ModelWidget.h" + +#include + +#include + +#include + + +class Config_WidgetAPI; +class QWidget; +class QLabel; +class QLineEdit; +class QToolButton; +class ModuleBase_IWorkshop; + +class MODULEBASE_EXPORT ModuleBase_WidgetSelector: public ModuleBase_ModelWidget +{ + Q_OBJECT +public: + ModuleBase_WidgetSelector(QWidget* theParent, + ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData); + + virtual ~ModuleBase_WidgetSelector(); + + /// Saves the internal parameters to the given feature + /// \param theFeature a model feature to be changed + virtual bool storeValue(FeaturePtr theFeature) const; + + virtual bool restoreValue(FeaturePtr theFeature); + + /// Returns the internal parent wiget control, that can be shown anywhere + /// \returns the widget + QWidget* getControl() const { return myContainer; } + + /// Returns list of widget controls + /// \return a control list + virtual QList getControls() const; + + void setActivationOnStart(bool toActivate) { myActivateOnStart = toActivate; } + bool activateOnStart() const { return myActivateOnStart; } + + FeaturePtr selectedFeature() const { return mySelectedFeature; } + +public slots: + + /// Activate or deactivate selection + void activateSelection(bool toActivate); + +protected: + bool eventFilter(QObject* theObj, QEvent* theEvent); + +private slots: + void onSelectionChanged(); + +private: + void enableOthersControls(bool toEnable) const; + void updateSelectionName(); + void raisePanel() const; + bool isAccepted(const FeaturePtr theFeature) const; + + static TopAbs_ShapeEnum shapeType(const QString& theType); + + QWidget* myContainer; + QLabel* myLabel; + QLineEdit* myTextLine; + QToolButton* myActivateBtn; + + ModuleBase_IWorkshop* myWorkshop; + + bool myActivateOnStart; + + FeaturePtr mySelectedFeature; + QStringList myShapeTypes; +}; + +#endif \ No newline at end of file diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index f038d3133..3ff0a0046 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -269,8 +269,12 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage) std::set::const_iterator aIt; for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) { FeaturePtr aFeature = (*aIt); - if (aFeature->getKind() != PARTSET_PART_KIND) - myDisplayer->redisplay(aFeature, false); + if (aFeature->getKind() != PARTSET_PART_KIND) { + if (myDisplayer->isVisible(aFeature)) + myDisplayer->redisplay(aFeature, false); + else + myDisplayer->display(aFeature, false); + } } myDisplayer->updateViewer(); }