Salome HOME
Issue #1834: Fix length of lines
authorvsv <vitaly.smetannikov@opencascade.com>
Fri, 18 Nov 2016 10:01:17 +0000 (13:01 +0300)
committervsv <vitaly.smetannikov@opencascade.com>
Fri, 18 Nov 2016 10:01:31 +0000 (13:01 +0300)
src/CollectionPlugin/CMakeLists.txt
src/CollectionPlugin/CollectionPlugin_Field.cpp
src/CollectionPlugin/CollectionPlugin_Group.cpp
src/CollectionPlugin/CollectionPlugin_WidgetCreator.cpp
src/CollectionPlugin/CollectionPlugin_WidgetField.cpp
src/CollectionPlugin/CollectionPlugin_WidgetField.h

index a32fb8e9901d322598441427dfcdf74defce38e4..c1b22136e81a2ee841c4aa599481ed01095a0f5c 100644 (file)
@@ -40,6 +40,8 @@ INCLUDE_DIRECTORIES(
   ../GeomValidators
   ../Events
   ../ModuleBase
+  ../Config
+  ${CAS_INCLUDE_DIRS}
 )
 
 SET(PROJECT_LIBRARIES
@@ -51,7 +53,7 @@ SET(PROJECT_LIBRARIES
     ModuleBase
 )
 
-ADD_DEFINITIONS(-DCOLLECTIONPLUGIN_EXPORTS)
+ADD_DEFINITIONS(-DCOLLECTIONPLUGIN_EXPORTS ${CAS_DEFINITIONS})
 ADD_LIBRARY(CollectionPlugin MODULE ${PROJECT_SOURCES} ${PROJECT_HEADERS} ${XML_RESOURCES} ${TEXT_RESOURCES})
 TARGET_LINK_LIBRARIES(CollectionPlugin ${PROJECT_LIBRARIES})
 
index bc2daf57eb425add624205a406b8f8ccd28e7ed7..863b1c90a1527ce5a740e96f3873536c9aec0f6b 100644 (file)
@@ -22,7 +22,7 @@ CollectionPlugin_Field::CollectionPlugin_Field()
 void CollectionPlugin_Field::initAttributes()
 {
   data()->addAttribute(SELECTED_ID(), ModelAPI_AttributeSelectionList::typeId());
-  data()->addAttribute(COMPONENTS_NB_ID(), ModelAPI_AttributeInteger::typeId());
+  //data()->addAttribute(COMPONENTS_NB_ID(), ModelAPI_AttributeInteger::typeId());
   data()->addAttribute(COMPONENTS_NAMES_ID(), ModelAPI_AttributeStringArray::typeId());
   data()->addAttribute(VALUES_TYPE_ID(), ModelAPI_AttributeInteger::typeId());
   data()->addAttribute(STEPS_NB_ID(), ModelAPI_AttributeInteger::typeId());
index c62b7d0d96b0fd8455949773f6838de22d2391b7..ede838a3e4829039c78ffb4c84355df3d422df97 100644 (file)
@@ -19,8 +19,8 @@ CollectionPlugin_Group::CollectionPlugin_Group()
 
 void CollectionPlugin_Group::initAttributes()
 {
-  //data()->addAttribute(CollectionPlugin_Group::NAME_ID(), ModelAPI_AttributeString::typeId());
-  data()->addAttribute(CollectionPlugin_Group::LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
+  data()->addAttribute(CollectionPlugin_Group::LIST_ID(), 
+                       ModelAPI_AttributeSelectionList::typeId());
 }
 
 void CollectionPlugin_Group::execute()
index a07562235ca13c6133ac928ca3477f35787eb33b..a369cf36e4132313dbdf253d894d4c72b917f639 100644 (file)
@@ -24,14 +24,14 @@ ModuleBase_ModelWidget* CollectionPlugin_WidgetCreator::createWidgetByType(
                                                      const std::string& theType,
                                                      QWidget* theParent,
                                                      Config_WidgetAPI* theWidgetApi,
-                                                     ModuleBase_IWorkshop* /*theWorkshop*/)
+                                                     ModuleBase_IWorkshop* theWorkshop)
 {
   ModuleBase_ModelWidget* aWidget = 0;
   if (myPanelTypes.find(theType) == myPanelTypes.end())
     return aWidget;
 
   if (theType == "field-panel") {     
-    aWidget = new CollectionPlugin_WidgetField(theParent, theWidgetApi);
+    aWidget = new CollectionPlugin_WidgetField(theParent, theWorkshop, theWidgetApi);
   }
 
   return aWidget;
index d0500355ed05d30e5d20ad3dd500f935f787f1ff..a2867f4ee817a3ef1cd56d48688ac1e5be1b1a85 100644 (file)
@@ -6,6 +6,10 @@
 
 #include "CollectionPlugin_WidgetField.h"
 
+#include <ModuleBase_Tools.h>
+#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_ISelection.h>
+
 #include <QLayout>
 #include <QWidget>
 #include <QFormLayout>
 #include <QTableWidget>
 #include <QPushButton>
 #include <QHeaderView>
+#include <QStackedWidget>
+#include <QValidator>
+#include <QStyledItemDelegate>
+#include <QLineEdit>
+
+
+class DataTableItemDelegate : public QStyledItemDelegate
+{
+public:
+  enum DataType {
+    DoubleType,
+    IntegerType,
+    BooleanType,
+    StringType };
+
+  DataTableItemDelegate(DataType theType) : QStyledItemDelegate() { myType = theType; }
+
+  virtual QWidget* createEditor(QWidget* theParent, 
+                                const QStyleOptionViewItem & theOption, 
+                                const QModelIndex& theIndex) const;
+
+  virtual void setModelData(QWidget* theEditor, QAbstractItemModel* theModel, 
+                            const QModelIndex& theIndex) const;
+
+  DataType dataType() const { return myType; }
+
+  void setDataType(DataType theType) { myType = theType; }
+
+private:
+  DataType myType;
+};
+
+QWidget* DataTableItemDelegate::createEditor(QWidget* theParent, 
+                                             const QStyleOptionViewItem & theOption, 
+                                             const QModelIndex& theIndex ) const
+{
+  if ((theIndex.column() != 0) && (theIndex.row() != 0)) {
+    QLineEdit* aLineEdt = 0;
+    switch (myType) {
+    case DoubleType:
+      aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent, 
+                                                                            theOption, 
+                                                                            theIndex));
+      if (aLineEdt) {
+        aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
+        return aLineEdt;
+      }
+    case IntegerType:
+      aLineEdt = dynamic_cast<QLineEdit*>(QStyledItemDelegate::createEditor(theParent, 
+                                                                            theOption, 
+                                                                            theIndex));
+      if (aLineEdt) {
+        aLineEdt->setValidator(new QIntValidator(aLineEdt));
+        return aLineEdt;
+      }
+    case BooleanType: 
+      {
+        QComboBox* aBox = new QComboBox(theParent);
+        aBox->addItem("True");
+        aBox->addItem("False");
+        return aBox;
+      }
+    }
+  }
+  return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
+}
+
+
+void DataTableItemDelegate::setModelData(QWidget* theEditor, QAbstractItemModel* theModel, 
+                                         const QModelIndex& theIndex) const
+{
+  QComboBox* aBox = dynamic_cast<QComboBox*>(theEditor);
+  if (aBox) {
+    theModel->setData(theIndex, aBox->currentText());
+  } else 
+    QStyledItemDelegate::setModelData(theEditor, theModel, theIndex);
+}
+
+
 
 CollectionPlugin_WidgetField::
-  CollectionPlugin_WidgetField(QWidget* theParent, const Config_WidgetAPI* theData):
-ModuleBase_ModelWidget(theParent, theData)
+  CollectionPlugin_WidgetField(QWidget* theParent, 
+                               ModuleBase_IWorkshop* theWorkshop, 
+                               const Config_WidgetAPI* theData):
+ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
 {
   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
 
@@ -83,36 +168,10 @@ ModuleBase_ModelWidget(theParent, theData)
   aSliderLayout->addWidget(myMaxLbl);
 
   // Stamp value
-  aStepLayout->addWidget(new QLabel(tr("Stamp"), aStepFrame), 2, 0);
-  myStampSpn = new QSpinBox(aStepFrame);
-  aStepLayout->addWidget(myStampSpn, 2, 1);
-
-  // Data table
-  myDataTbl = new QTableWidget(2, 2, aStepFrame);
-  myDataTbl->verticalHeader()->hide();
-  myDataTbl->horizontalHeader()->hide();
-  myDataTbl->setRowHeight(0, 25);
-  myDataTbl->setRowHeight(1, 25);
-
-  QTableWidgetItem* aItem = new QTableWidgetItem("Shape");
-  aItem->setBackgroundColor(Qt::lightGray);
-  aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
-  myDataTbl->setItem(0, 0, aItem);
-
-  aItem = new QTableWidgetItem("Comp 1");
-  aItem->setBackgroundColor(Qt::lightGray);
-  myDataTbl->setItem(0, 1, aItem);
-
-  aItem = new QTableWidgetItem("Default value");
-  aItem->setBackgroundColor(Qt::lightGray);
-  aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
-  myDataTbl->setItem(1, 0, aItem);
-
-  aItem = new QTableWidgetItem("0");
-  aItem->setBackgroundColor(Qt::lightGray);
-  myDataTbl->setItem(1, 1, aItem);
-  
-  aStepLayout->addWidget(myDataTbl, 3, 0, 1, 2);
+  myCompNamesList << "Comp 1";
+  myStepWgt = new QStackedWidget(aStepFrame);
+  aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
+  appendStepControls();
 
   // Buttons below
   QWidget* aBtnWgt = new QWidget(this);
@@ -125,20 +184,83 @@ ModuleBase_ModelWidget(theParent, theData)
 
   aBtnLayout->addStretch(1);
 
-  QPushButton* aRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
-  aBtnLayout->addWidget(aRemoveBtn);
+  myRemoveBtn = new QPushButton(tr("Remove step"), aBtnWgt);
+  aBtnLayout->addWidget(myRemoveBtn);
+  myRemoveBtn->setEnabled(false);
 
   connect(myNbComponentsSpn, SIGNAL(valueChanged(int)), SLOT(onNbCompChanged(int)));
   connect(aAddBtn, SIGNAL(clicked(bool)), SLOT(onAddStep()));
-  connect(aRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
+  connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemoveStep()));
   connect(myStepSlider, SIGNAL(valueChanged(int)), SLOT(onStepMove(int)));
+  connect(myFieldTypeCombo, SIGNAL(currentIndexChanged(int)), SLOT(onFieldTypeChanged(int)));
+}
+
+void CollectionPlugin_WidgetField::appendStepControls()
+{
+  QWidget* aWidget = new QWidget(myStepWgt);
+  QGridLayout* aStepLayout = new QGridLayout(aWidget);
+  aStepLayout->setContentsMargins(0, 0, 0, 0);
+
+  aStepLayout->addWidget(new QLabel(tr("Stamp"), aWidget), 0, 0);
+
+  QSpinBox* aStampSpn = new QSpinBox(aWidget);
+  aStepLayout->addWidget(aStampSpn, 0, 1);
+
+  myStampSpnList.append(aStampSpn);
+
+  // Data table
+  QTableWidget* aDataTbl = new QTableWidget(2, myCompNamesList.count() + 1, aWidget);
+  aDataTbl->setItemDelegate(
+    new DataTableItemDelegate((DataTableItemDelegate::DataType) myFieldTypeCombo->currentIndex()));
+  aDataTbl->verticalHeader()->hide();
+  aDataTbl->horizontalHeader()->hide();
+  aDataTbl->setRowHeight(0, 25);
+  aDataTbl->setRowHeight(1, 25);
+
+  QTableWidgetItem* aItem = new QTableWidgetItem("Shape");
+  aItem->setBackgroundColor(Qt::lightGray);
+  aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
+  aDataTbl->setItem(0, 0, aItem);
+
+  aItem = new QTableWidgetItem("Default value");
+  aItem->setBackgroundColor(Qt::lightGray);
+  aItem->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
+  aDataTbl->setItem(1, 0, aItem);
+
+  for (int i = 0; i < myCompNamesList.count(); i++) {
+    aItem = new QTableWidgetItem(myCompNamesList[i]);
+    aItem->setBackgroundColor(Qt::lightGray);
+    aDataTbl->setItem(0, i + 1, aItem);
+
+    aItem = new QTableWidgetItem("0");
+    aItem->setBackgroundColor(Qt::lightGray);
+    aDataTbl->setItem(1, i + 1, aItem);
+  }
+  aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
+
+  QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
+  myDataTblList.append(aDataTbl);
+
+  myStepWgt->addWidget(aWidget);
+}
+
+void CollectionPlugin_WidgetField::removeStepControls()
+{
+  int aCurWgtId = myStepWgt->currentIndex();
+  myStepWgt->removeWidget(myStepWgt->currentWidget());
+
+  myStampSpnList.removeAt(aCurWgtId);
+  myDataTblList.removeAt(aCurWgtId);
 }
 
+
 QList<QWidget*> CollectionPlugin_WidgetField::getControls() const
 {
   QList<QWidget*> aControls;
   // this control will accept focus and will be highlighted in the Property Panel
-  //aControls.push_back(myComboBox);
+  //aControls.push_back(myShapeTypeCombo);
+  //aControls.push_back(myFieldTypeCombo);
+  //aControls.push_back(myNbComponentsSpn);
   return aControls;
 }
 
@@ -167,21 +289,31 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
 
 void CollectionPlugin_WidgetField::onNbCompChanged(int theVal)
 {
-  int aOldCol = myDataTbl->columnCount() - 1;
-  int aNbRows = myDataTbl->rowCount();
-  myDataTbl->setColumnCount(theVal + 1);
+  int aOldCol = myCompNamesList.count();
+  int aNbRows = myDataTblList.first()->rowCount();
   int aDif = theVal - aOldCol;
   QTableWidgetItem* aItem = 0;
-  for (int i = 0; i < aDif; i++) {
-    for (int j = 0; j < aNbRows; j++) {
-      aItem = new QTableWidgetItem();
-      if (j == 0)
-        aItem->setText(QString("Comp %1").arg(i + aOldCol + 1));
-      else
-        aItem->setText("0");
-      if (j < 3)
-        aItem->setBackgroundColor(Qt::lightGray);
-      myDataTbl->setItem(j, i + aOldCol + 1, aItem);
+
+  while (myCompNamesList.count() != theVal) {
+    if (aDif > 0)
+      myCompNamesList.append(QString("Comp %1").arg(myCompNamesList.count() + 1));
+    else
+      myCompNamesList.removeLast();
+  }
+
+  foreach(QTableWidget* aDataTbl, myDataTblList) {
+    aDataTbl->setColumnCount(theVal + 1);
+    for (int i = 0; i < myCompNamesList.count(); i++) {
+      for (int j = 0; j < aNbRows; j++) {
+        aItem = new QTableWidgetItem();
+        if (j == 0)
+          aItem->setText(myCompNamesList.at(i));
+        else
+          aItem->setText("0");
+        if (j < 3)
+          aItem->setBackgroundColor(Qt::lightGray);
+        aDataTbl->setItem(j, i + aOldCol + 1, aItem);
+      }
     }
   }
 }
@@ -192,28 +324,33 @@ void CollectionPlugin_WidgetField::onAddStep()
   aMax++;
   myStepSlider->setMaximum(aMax);
   myMaxLbl->setText(QString::number(aMax));
-  clearData();
+  appendStepControls();
   myStepSlider->setValue(aMax);
+  myRemoveBtn->setEnabled(aMax > 1);
 }
 
 void CollectionPlugin_WidgetField::onRemoveStep()
 {
   int aMax = myStepSlider->maximum();
   aMax--;
-  myStepSlider->setMaximum(aMax);
   myMaxLbl->setText(QString::number(aMax));
+  removeStepControls();
+  myStepSlider->setMaximum(aMax);
+  myRemoveBtn->setEnabled(aMax > 1);
 }
 
 void CollectionPlugin_WidgetField::clearData()
 {
-  int aNbRows = myDataTbl->rowCount();
-  int aNbCol = myDataTbl->columnCount();
-
-  QString aDefValue;
-  for (int i = 1; i < aNbCol; i++) {
-    aDefValue = myDataTbl->item(1, i)->text();
-    for (int j = 2; j < aNbRows; j++) {
-      myDataTbl->item(j, i)->setText(aDefValue);
+  int aNbRows = myDataTblList.first()->rowCount();
+  int aNbCol = myDataTblList.first()->columnCount();
+
+  foreach(QTableWidget* aDataTbl, myDataTblList) {
+    QString aDefValue;
+    for (int i = 1; i < aNbCol; i++) {
+      aDefValue = aDataTbl->item(1, i)->text();
+      for (int j = 2; j < aNbRows; j++) {
+        aDataTbl->item(j, i)->setText(aDefValue);
+      }
     }
   }
 }
@@ -221,4 +358,59 @@ void CollectionPlugin_WidgetField::clearData()
 void CollectionPlugin_WidgetField::onStepMove(int theStep)
 {
   myCurStepLbl->setText(QString::number(theStep));
+  myStepWgt->setCurrentIndex(theStep - 1);
+}
+
+QIntList CollectionPlugin_WidgetField::shapeTypes() const
+{
+  QIntList aRes;
+  switch (myShapeTypeCombo->currentIndex()) {
+  case 0: //"Vertices"
+    aRes.append(ModuleBase_Tools::shapeType("vertex"));
+    break;
+  case 1: // "Edges"
+    aRes.append(ModuleBase_Tools::shapeType("edge"));
+    break;
+  case 2: // "Faces"
+    aRes.append(ModuleBase_Tools::shapeType("face"));
+    break;
+  case 3: // "Solids"
+    aRes.append(ModuleBase_Tools::shapeType("solid"));
+    break;
+  case 4: // "Results"
+    aRes.append(ModuleBase_Tools::shapeType("object"));
+    break;
+  case 5: // "Parts"
+    // TODO: Selection mode for Parts
+    break;
+  }
+  return aRes;
+}
+
+bool CollectionPlugin_WidgetField::
+  isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
+{
+  return true;
+}
+
+void CollectionPlugin_WidgetField::onSelectionChanged()
+{
+  QList<ModuleBase_ViewerPrsPtr> aSelected = 
+    myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
+
+  clearData();
+
+  foreach(ModuleBase_ViewerPrsPtr aPrs, aSelected) {
+
+  }
+}
+
+void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
+{
+  DataTableItemDelegate* aDelegate = 0;
+  foreach(QTableWidget* aTable, myDataTblList) {
+    aDelegate = dynamic_cast<DataTableItemDelegate*>(aTable->itemDelegate());
+    if (aDelegate)
+      aDelegate->setDataType((DataTableItemDelegate::DataType)theIdx);
+  }
 }
index 0098157392171a3fecdfa9a0109700790754888a..3e3277b53a28c38219f3ccf9a999c467be4563f0 100644 (file)
 
 #include "CollectionPlugin.h"
 
-#include <ModuleBase_ModelWidget.h>
+#include <ModuleBase_WidgetSelector.h>
+#include <ModuleBase_ViewerPrs.h>
+
+#include <QList>
+#include <QStringList>
+
 
 class QWidget;
 class QComboBox;
@@ -19,16 +24,20 @@ class QSpinBox;
 class QLabel;
 class QSlider;
 class QTableWidget;
+class QStackedWidget;
+class QPushButton;
 
 /*!
  * \ingroup GUI
  * Represent a content of the property panel to show/modify parameters of a Field feature.
  */
-class CollectionPlugin_WidgetField : public ModuleBase_ModelWidget
+class CollectionPlugin_WidgetField : public ModuleBase_WidgetSelector
 {
  Q_OBJECT
 public:
-  CollectionPlugin_WidgetField(QWidget* theParent, const Config_WidgetAPI* theData);
+  CollectionPlugin_WidgetField(QWidget* theParent, 
+                               ModuleBase_IWorkshop* theWorkshop, 
+                               const Config_WidgetAPI* theData);
 
   virtual ~CollectionPlugin_WidgetField() {}
 
@@ -36,6 +45,12 @@ public:
   /// \return a control list
   virtual QList<QWidget*> getControls() const;
 
+  /// Checks the widget validity. By default, it returns true.
+  /// \param thePrs a selected presentation in the view
+  /// \return a boolean value
+  virtual bool isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
+
+
 protected:
   /// Saves the internal parameters to the given feature
   /// \return True in success
@@ -44,6 +59,15 @@ protected:
   /// Restore value from attribute data to the widget's control
   virtual bool restoreValueCustom();
 
+  /// Retunrs a list of possible shape types
+  /// \return a list of shapes
+  virtual QIntList shapeTypes() const;
+
+
+protected slots:
+  /// Slot which is called on selection event
+  virtual void onSelectionChanged();
+
 private slots:
   void onNbCompChanged(int theVal);
 
@@ -53,9 +77,14 @@ private slots:
 
   void onStepMove(int theStep);
 
+  void onFieldTypeChanged(int theIdx);
+
 private:
   void clearData();
 
+  void appendStepControls();
+  void removeStepControls();
+
   /// Types of shapes selection
   QComboBox* myShapeTypeCombo;
 
@@ -72,11 +101,19 @@ private:
   QSlider* myStepSlider;
 
   /// Stamp value
-  QSpinBox* myStampSpn;
+  QList<QSpinBox*> myStampSpnList;
 
-  QTableWidget* myDataTbl;
+  QList<QTableWidget*> myDataTblList;
 
   QLabel* myMaxLbl;
+
+  QStackedWidget* myStepWgt;
+
+  QStringList myCompNamesList;
+
+  QList<ModuleBase_ViewerPrsPtr> mySelection;
+
+  QPushButton* myRemoveBtn;
 };
 
 #endif
\ No newline at end of file