Salome HOME
Update viewer after selection clear for OCCT 7.4.0.
[modules/shaper.git] / src / PartSet / PartSet_BSplineWidget.cpp
index 7093c28fd5fe069d42a7558fb7c766c4ca05087f..9ca97448d277b246ee80c4e46416c6441214cee0 100644 (file)
@@ -33,6 +33,8 @@
 #include <QGroupBox>
 #include <QLabel>
 #include <QVBoxLayout>
+#include <QScrollArea>
+#include <QToolButton>
 
 
 PartSet_BSplineWidget::PartSet_BSplineWidget(
@@ -41,18 +43,35 @@ PartSet_BSplineWidget::PartSet_BSplineWidget(
   : ModuleBase_ModelWidget(theParent, theData)
 {
   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
-  ModuleBase_Tools::adjustMargins(aMainLayout);
+  aMainLayout->setContentsMargins(0, 0, 0, 0);
 
   // GroupBox to keep widgets for B-spline poles and weights
-  myPolesGroupBox = new QGroupBox(tr("Poles and weights"), theParent);
+  myPolesGroupBox = new QGroupBox(tr("Poles and weights"), this);
   aMainLayout->addWidget(myPolesGroupBox);
+
+  QVBoxLayout* aLayout = new QVBoxLayout(myPolesGroupBox);
+  ModuleBase_Tools::adjustMargins(aLayout);
+
+  myScrollArea = new QScrollArea(myPolesGroupBox);
+  myScrollArea->setWidgetResizable(true);
+  myScrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+  myScrollArea->setFrameStyle(QFrame::NoFrame);
+  aLayout->addWidget(myScrollArea);
+
+  QWidget* aContainer = new QWidget(myScrollArea);
+  QVBoxLayout* aBoxLay = new QVBoxLayout(aContainer);
+  aBoxLay->setContentsMargins(0, 0, 0, 0);
+
   // layout of GroupBox
-  QGridLayout* aGroupLayout = new QGridLayout(myPolesGroupBox);
-  aGroupLayout->setSpacing(4);
+  myPolesWgt = new QWidget(aContainer);
+  QGridLayout* aGroupLayout = new QGridLayout(myPolesWgt);
   aGroupLayout->setColumnStretch(1, 1);
   ModuleBase_Tools::adjustMargins(aGroupLayout);
 
   restoreValueCustom();
+  aBoxLay->addWidget(myPolesWgt);
+  aBoxLay->addStretch(1);
+  myScrollArea->setWidget(aContainer);
 }
 
 void PartSet_BSplineWidget::setFeature(const FeaturePtr& theFeature,
@@ -73,26 +92,17 @@ void PartSet_BSplineWidget::deactivate()
 QList<QWidget*> PartSet_BSplineWidget::getControls() const
 {
   QList<QWidget*> aControls;
-  std::list<BSplinePoleWidgets>::const_iterator anIt = myPoles.begin();
-  for (; anIt != myPoles.end(); ++anIt) {
-    aControls.append(anIt->myWeight);
-  }
+  aControls.append(myScrollArea);
   return aControls;
 }
 
 void PartSet_BSplineWidget::storePolesAndWeights() const
 {
   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
-  //AttributePoint2DArrayPtr aPointArray = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
-  //    aData->attribute(SketchPlugin_BSpline::POLES_ID()));
   AttributeDoubleArrayPtr aWeightsArray = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
 
-  //aPointArray->setSize((int)myPoles.size());
-  //aWeightsArray->setSize((int)myPoles.size());
-
   std::list<BSplinePoleWidgets>::const_iterator anIt = myPoles.begin();
   for (int anIndex = 0; anIt != myPoles.end(); ++anIndex, ++anIt) {
-    //aPointArray->setPnt(anIndex, anIt->myX->value(), anIt->myY->value());
     aWeightsArray->setValue(anIndex, anIt->myWeight->value());
   }
 }
@@ -103,17 +113,11 @@ bool PartSet_BSplineWidget::storeValueCustom()
   if (!aData || !aData->isValid()) // can be on abort of sketcher element
     return false;
 
-  //AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
-  //    aData->attribute(SketchPlugin_BSpline::POLES_ID()));
   AttributeDoubleArrayPtr aWeights = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
 
   bool isBlocked = blockSignals(true);
-  //bool isImmutable = aPoles->setImmutable(true);
-
   storePolesAndWeights();
   ModuleBase_Tools::flushUpdated(myFeature);
-
-  //aPoles->setImmutable(isImmutable);
   blockSignals(isBlocked);
 
   updateObject(myFeature);
@@ -149,33 +153,65 @@ bool PartSet_BSplineWidget::restoreValueCustom()
 
 void PartSet_BSplineWidget::addPoleWidget()
 {
-  QGridLayout* aGroupLay = dynamic_cast<QGridLayout*>(myPolesGroupBox->layout());
-  ModuleBase_Tools::adjustMargins(aGroupLay);
-
+  QGridLayout* aGroupLay = dynamic_cast<QGridLayout*>(myPolesWgt->layout());
   int aNbPoles = (int)myPoles.size();
+  QString aPoleStr = tr("Pole %1").arg(aNbPoles + 1);
 
-  QString aPoleStr = tr("Pole %1");
-  aPoleStr = aPoleStr.arg(aNbPoles + 1);
+  myPoles.push_back(BSplinePoleWidgets());
+  BSplinePoleWidgets& aPole = myPoles.back();
+  aGroupLay->addWidget(createPoleWidget(aPole, aPoleStr, myPolesWgt), aNbPoles, 1);
+}
 
-  QGroupBox* aPoleGroupBox = new QGroupBox(aPoleStr, myPolesGroupBox);
-  QFormLayout* aPoleLay = new QFormLayout(aPoleGroupBox);
-  ModuleBase_Tools::adjustMargins(aPoleLay);
-  aPoleLay->setSpacing(2);
+QGroupBox* PartSet_BSplineWidget::createPoleWidget(BSplinePoleWidgets& thePole,
+  const QString& theName, QWidget* theParent)
+{
+  QGroupBox* aPoleGroupBox = new QGroupBox(theName, theParent);
+  QGridLayout* aPoleLay = new QGridLayout(aPoleGroupBox);
+  aPoleLay->setSpacing(0);
+  ModuleBase_Tools::zeroMargins(aPoleLay);
+
+  thePole.myX = new ModuleBase_LabelValue(aPoleGroupBox, tr("X"));
+  aPoleLay->addWidget(thePole.myX, 0, 0, 1, 3);
+  thePole.myY = new ModuleBase_LabelValue(aPoleGroupBox, tr("Y"));
+  aPoleLay->addWidget(thePole.myY, 1, 0, 1, 3);
+  thePole.myWeight = new ModuleBase_ParamSpinBox(aPoleGroupBox);
+  thePole.myWeight->setMinimum(0.0);
+
+  aPoleLay->addWidget(new QLabel(tr("Weight :"), aPoleGroupBox), 2, 0);
+  aPoleLay->addWidget(thePole.myWeight, 2, 1);
+  // we should listen textChanged signal as valueChanged do not send when text is modified
+  connect(thePole.myWeight, SIGNAL(textChanged(const QString&)),
+    this, SIGNAL(valuesChanged()));
 
-  myPoles.push_back(BSplinePoleWidgets());
-  BSplinePoleWidgets& aPoleWidgets = myPoles.back();
+  thePole.myAddBtn = new QToolButton(aPoleGroupBox);
+  thePole.myAddBtn->setIcon(QIcon(":pictures/add.png"));
+  thePole.myAddBtn->setToolTip(tr("Add a new pole after the current"));
+  aPoleLay->addWidget(thePole.myAddBtn, 2, 2);
+  connect(thePole.myAddBtn, SIGNAL(clicked(bool)), SLOT(onAddPole()));
 
-  aPoleWidgets.myX = new ModuleBase_LabelValue(aPoleGroupBox, tr("X"));
-  aPoleLay->addRow(aPoleWidgets.myX);
-  aPoleWidgets.myY = new ModuleBase_LabelValue(aPoleGroupBox, tr("Y"));
-  aPoleLay->addRow(aPoleWidgets.myY);
-  aPoleWidgets.myWeight = new ModuleBase_ParamSpinBox(aPoleGroupBox);
-  aPoleWidgets.myWeight->setMinimum(0.0);
-  aPoleLay->addRow(tr("Weight") + " : ", aPoleWidgets.myWeight);
+  return aPoleGroupBox;
+}
 
-  aGroupLay->addWidget(aPoleGroupBox, aNbPoles, 1);
 
-  // we should listen textChanged signal as valueChanged do not send when text is modified
-  connect(aPoleWidgets.myWeight, SIGNAL(textChanged(const QString&)),
-    this, SIGNAL(valuesChanged()));
+void PartSet_BSplineWidget::onAddPole()
+{
+  QObject* aObj = sender();
+  std::list<BSplinePoleWidgets>::const_iterator aIt;
+  int aId = 0;
+  bool aFound = false;
+  for (aIt = myPoles.cbegin(); aIt != myPoles.cend(); aIt++, aId++) {
+    if ((*aIt).myAddBtn == aObj) {
+      aFound = true;
+      break;
+    }
+  }
+  if (aFound) {
+    // add a new pole after found Id
+    std::ostringstream anActionName;
+    anActionName << SketchPlugin_BSplineBase::ADD_POLE_ACTION_ID() << "#" << aId;
+    if (feature()->customAction(anActionName.str()))
+      updateObject(feature());
+
+    restoreValueCustom();
+  }
 }