]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/CollectionPlugin/CollectionPlugin_WidgetField.cpp
Salome HOME
Issue #1953: Synchronize selection on widget activation
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_WidgetField.cpp
index 9aa3413cfc0824c032ea7f89405a2f9a37917c31..55b8066127e6f911fffde266df517c4a958f860f 100644 (file)
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_ISelection.h>
-
-#include <XGUI_Tools.h>
-#include <XGUI_Workshop.h>
-#include <XGUI_PropertyPanel.h>
+#include <ModuleBase_IPropertyPanel.h>
 
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeStringArray.h>
@@ -32,7 +29,6 @@
 #include <QHeaderView>
 #include <QStackedWidget>
 #include <QValidator>
-#include <QStyledItemDelegate>
 #include <QLineEdit>
 #include <QEvent>
 #include <QMouseEvent>
@@ -43,27 +39,6 @@ const char* MYFirstCol = "Shape";
 const char* MYTrue = "True";
 const char* MYFalse = "False";
 
-class DataTableItemDelegate : public QStyledItemDelegate
-{
-public:
-  DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType) :
-      QStyledItemDelegate() { myType = theType; }
-
-  virtual QWidget* createEditor(QWidget* theParent,
-                                const QStyleOptionViewItem & theOption,
-                                const QModelIndex& theIndex) const;
-
-  ModelAPI_AttributeTables::ValueType dataType() const { return myType; }
-
-  void setDataType(ModelAPI_AttributeTables::ValueType theType) { myType = theType; }
-
-signals:
-  void startEditing();
-
-private:
-  ModelAPI_AttributeTables::ValueType myType;
-};
-
 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
                                              const QStyleOptionViewItem & theOption,
                                              const QModelIndex& theIndex ) const
@@ -116,7 +91,7 @@ CollectionPlugin_WidgetField::
                                ModuleBase_IWorkshop* theWorkshop,
                                const Config_WidgetAPI* theData):
 ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
-  myIsEditing(false), myActivation(false)
+  myIsTabEdit(false), myActivation(false)
 {
   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
 
@@ -184,6 +159,11 @@ ModuleBase_WidgetSelector(theParent, theWorkshop, theData), myHeaderEditor(0),
   myCompNamesList << "Comp 1";
   myStepWgt = new QStackedWidget(aStepFrame);
   aStepLayout->addWidget(myStepWgt, 2, 0, 1, 2);
+
+  myDelegate =
+    new DataTableItemDelegate((ModelAPI_AttributeTables::ValueType)
+    myFieldTypeCombo->currentIndex());
+
   appendStepControls();
 
   // Buttons below
@@ -228,12 +208,7 @@ void CollectionPlugin_WidgetField::appendStepControls()
   // Data table
   QTableWidget* aDataTbl = new QTableWidget(1, myCompNamesList.count() + 1, aWidget);
   aDataTbl->installEventFilter(this);
-  DataTableItemDelegate* aDelegate = 0;
-  if (myDataTblList.isEmpty())
-    aDelegate = new DataTableItemDelegate(
-      (ModelAPI_AttributeTables::ValueType) myFieldTypeCombo->currentIndex());
-  else
-    aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
+  aDataTbl->setItemDelegate(myDelegate);
 
   QIntList aColWidth;
   if (!myDataTblList.isEmpty()) {
@@ -241,14 +216,13 @@ void CollectionPlugin_WidgetField::appendStepControls()
     for (int i = 0; i < aFirstTable->columnCount(); i++)
       aColWidth.append(aFirstTable->columnWidth(i));
   }
-  aDataTbl->setItemDelegate(aDelegate);
   myDataTblList.append(aDataTbl);
 
   aDataTbl->verticalHeader()->hide();
   aDataTbl->setRowHeight(0, 25);
   aDataTbl->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
 
-  connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)), 
+  connect(aDataTbl->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
           SLOT(onColumnResize(int, int, int)));
 
   updateHeaders(aDataTbl);
@@ -266,13 +240,14 @@ void CollectionPlugin_WidgetField::appendStepControls()
   }
 
   if (aColWidth.length() > 0) {
-    for (int i = 0; i < aDataTbl->columnCount(); i++)
+    for (int i = 0; i < aDataTbl->columnCount(); i++) {
+      if (i < aColWidth.size())
       aDataTbl->setColumnWidth(i, aColWidth.at(i));
+    }
   }
   aStepLayout->addWidget(aDataTbl, 1, 0, 1, 2);
   connect(aDataTbl, SIGNAL(cellChanged(int, int)), SLOT(onTableEdited(int, int)));
 
-  QAbstractItemDelegate* aDel = aDataTbl->itemDelegate();
   myStepWgt->addWidget(aWidget);
   aDataTbl->horizontalHeader()->viewport()->installEventFilter(this);
 }
@@ -284,6 +259,13 @@ void CollectionPlugin_WidgetField::deactivate()
   storeValueCustom();
 }
 
+//**********************************************************************************
+//void CollectionPlugin_WidgetField::showEvent(QShowEvent* theEvent)
+//{
+//  myShapeTypeCombo->setEnabled(!isEditingMode());
+//  myFieldTypeCombo->setEnabled(!isEditingMode());
+//  myNbComponentsSpn->setEnabled(!isEditingMode());
+//}
 
 //**********************************************************************************
 bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEvent)
@@ -349,8 +331,7 @@ bool CollectionPlugin_WidgetField::eventFilter(QObject* theObject, QEvent* theEv
   } else if (theEvent->type() == QEvent::FocusIn) {
     QTableWidget* aTable = dynamic_cast<QTableWidget*>(theObject);
     if (aTable) {
-      XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
-      XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
+      ModuleBase_IPropertyPanel* aPanel = myWorkshop->propertyPanel();
       if (aPanel->activeWidget() != this) {
         myActivation = true;
         aPanel->activateWidget(this, false);
@@ -372,6 +353,9 @@ QTableWidgetItem* CollectionPlugin_WidgetField::createDefaultItem() const
   case ModelAPI_AttributeTables::BOOLEAN:
     aItem->setText(MYFalse);
     break;
+  case ModelAPI_AttributeTables::STRING:
+    aItem->setText("");
+    break;
   }
   return aItem;
 }
@@ -524,6 +508,7 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
   isBlocked = myFieldTypeCombo->blockSignals(true);
   myFieldTypeCombo->setCurrentIndex(aTablesAttr->type());
   myFieldTypeCombo->blockSignals(isBlocked);
+  myDelegate->setDataType(aTablesAttr->type());
 
   AttributeIntArrayPtr aStampsAttr = aData->intArray(CollectionPlugin_Field::STAMPS_ID());
   // Fill data table
@@ -542,6 +527,8 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
     QTableWidget* aTable = myDataTblList.at(i);
     isBlocked = aTable->blockSignals(true);
     aTable->setRowCount(aRows);
+    aTable->setColumnCount(aCols + 1);
+    updateHeaders(aTable);
     for (int j = 0; j < aCols + 1; j++) {
       for (int k = 0; k < aRows; k++) {
         aItem = aTable->item(k, j);
@@ -569,8 +556,10 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
       }
     }
     // Restore columns width
-    for (int i = 0; i < aTable->columnCount(); i++)
-      aTable->setColumnWidth(i, aColWidth.at(i));
+    for (int i = 0; i < aTable->columnCount(); i++) {
+      if (i < aColWidth.size())
+        aTable->setColumnWidth(i, aColWidth.at(i));
+    }
 
     aTable->blockSignals(isBlocked);
   }
@@ -580,17 +569,19 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
 //**********************************************************************************
 int CollectionPlugin_WidgetField::getSelectionType(const std::string& theStr) const
 {
-  if (theStr == "vertex")
+  QString aType(theStr.c_str());
+  aType = aType.toLower();
+  if (aType == "vertex")
     return 0;
-  else if (theStr == "edge")
+  else if (aType == "edge")
     return 1;
-  else if (theStr == "face")
+  else if (aType == "face")
     return 2;
-  else if (theStr == "solid")
+  else if (aType == "solid")
     return 3;
-  else if (theStr == "object")
+  else if (aType == "object")
     return 4;
-  else if (theStr == "part")
+  else if (aType == "part")
     return 5;
   return -1;
 }
@@ -732,7 +723,7 @@ void CollectionPlugin_WidgetField::onAddStep()
       for(int j = 1; j < aRows; j++) {
         aItem = aTable->item(j, i);
         if (!aItem) {
-          aItem = new QTableWidgetItem();       
+          aItem = new QTableWidgetItem();
           aTable->setItem(j, i, aItem);
         }
         AttributeSelectionPtr aAttr = aSelList->value(j - 1);
@@ -751,6 +742,7 @@ void CollectionPlugin_WidgetField::onAddStep()
       }
     }
   }
+  emit valuesChanged();
 }
 
 //**********************************************************************************
@@ -762,8 +754,9 @@ void CollectionPlugin_WidgetField::onRemoveStep()
   removeStepControls();
   myStepSlider->setMaximum(aMax);
 
-  AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
-  aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
+  //AttributeTablesPtr aTablesAttr = myFeature->data()->tables(CollectionPlugin_Field::VALUES_ID());
+  //aTablesAttr->setSize(aTablesAttr->rows(), aTablesAttr->columns(), myDataTblList.size());
+  emit valuesChanged();
 }
 
 //**********************************************************************************
@@ -783,7 +776,7 @@ void CollectionPlugin_WidgetField::onStepMove(int theStep)
 
 //**********************************************************************************
 bool CollectionPlugin_WidgetField::
-  isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
+  isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
 {
   return (myShapeTypeCombo->currentIndex() == 5)? false : true;
 }
@@ -791,6 +784,9 @@ bool CollectionPlugin_WidgetField::
 //**********************************************************************************
 void CollectionPlugin_WidgetField::onSelectionChanged()
 {
+  //if (isEditingMode())
+  //  return;
+
   if (myActivation) {
     myActivation = false;
     return;
@@ -799,8 +795,7 @@ void CollectionPlugin_WidgetField::onSelectionChanged()
   if (myShapeTypeCombo->currentIndex() == 5)
     return;
 
-  QList<ModuleBase_ViewerPrsPtr> aSelected =
-    myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
+  QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
 
   AttributeSelectionListPtr aSelList =
     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
@@ -870,34 +865,30 @@ void CollectionPlugin_WidgetField::onSelectionChanged()
 //**********************************************************************************
 void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
 {
-  DataTableItemDelegate* aDelegate = 0;
-  aDelegate = dynamic_cast<DataTableItemDelegate*>(myDataTblList.first()->itemDelegate());
-  if (aDelegate) {
-    ModelAPI_AttributeTables::ValueType aOldType = aDelegate->dataType();
-    if (aOldType != theIdx) {
-      aDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
-      int aColumns = myDataTblList.first()->columnCount();
-      int aRows = myDataTblList.first()->rowCount();
-      foreach(QTableWidget* aTable, myDataTblList) {
-        for(int i = 1; i < aColumns; i++) {
-          for(int j = 0; j < aRows; j++) {
-            switch (theIdx) {
-            case ModelAPI_AttributeTables::DOUBLE:
-            case ModelAPI_AttributeTables::INTEGER:
-              if ((aOldType == ModelAPI_AttributeTables::BOOLEAN) ||
-                  (aOldType == ModelAPI_AttributeTables::STRING)) {
-                    aTable->item(j, i)->setText("0");
-              }
-              break;
-            case ModelAPI_AttributeTables::BOOLEAN:
-              aTable->item(j, i)->setText(MYFalse);
-              break;
-            }
+  ModelAPI_AttributeTables::ValueType aOldType = myDelegate->dataType();
+  if (aOldType != theIdx) {
+    myDelegate->setDataType((ModelAPI_AttributeTables::ValueType)theIdx);
+    int aColumns = myDataTblList.first()->columnCount();
+    int aRows = myDataTblList.first()->rowCount();
+    foreach(QTableWidget* aTable, myDataTblList) {
+      for(int i = 1; i < aColumns; i++) {
+        for(int j = 0; j < aRows; j++) {
+          switch (theIdx) {
+          case ModelAPI_AttributeTables::DOUBLE:
+          case ModelAPI_AttributeTables::INTEGER:
+            aTable->item(j, i)->setText("0");
+            break;
+          case ModelAPI_AttributeTables::BOOLEAN:
+            aTable->item(j, i)->setText(MYFalse);
+            break;
+          case ModelAPI_AttributeTables::STRING:
+            aTable->item(j, i)->setText("");
+            break;
           }
         }
       }
-      emit valuesChanged();
     }
+    emit valuesChanged();
   }
 }
 
@@ -946,8 +937,8 @@ void CollectionPlugin_WidgetField::onShapeTypeChanged(int theType)
 //**********************************************************************************
 bool CollectionPlugin_WidgetField::processEnter()
 {
-  if (myIsEditing) {
-    myIsEditing = false;
+  if (myIsTabEdit) {
+    myIsTabEdit = false;
     return true;
   }
   return false;
@@ -956,8 +947,8 @@ bool CollectionPlugin_WidgetField::processEnter()
 //**********************************************************************************
 void CollectionPlugin_WidgetField::onFocusChanged(QWidget* theOld, QWidget* theNew)
 {
-  if (theNew && (!myIsEditing))
-    myIsEditing = dynamic_cast<QLineEdit*>(theNew);
+  if (theNew && (!myIsTabEdit))
+    myIsTabEdit = dynamic_cast<QLineEdit*>(theNew);
 }
 
 //**********************************************************************************
@@ -978,3 +969,24 @@ void CollectionPlugin_WidgetField::onColumnResize(int theIndex, int theOld, int
       aTable->setColumnWidth(theIndex, theNew);
   }
 }
+
+//**********************************************************************************
+QList<std::shared_ptr<ModuleBase_ViewerPrs>>
+  CollectionPlugin_WidgetField::getAttributeSelection() const
+{
+  QList<std::shared_ptr<ModuleBase_ViewerPrs>> aList;
+  if(myFeature) {
+    DataPtr aData = myFeature->data();
+    AttributeSelectionListPtr aSelList =
+      aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
+    AttributeSelectionPtr aAttr;
+    ObjectPtr anObject;
+    for (int i = 0; i < aSelList->size(); i++) {
+      aAttr = aSelList->value(i);
+      ModuleBase_ViewerPrsPtr
+        aPrs(new ModuleBase_ViewerPrs(aAttr->context(), aAttr->value(), NULL));
+      aList.append(aPrs);
+    }
+  }
+  return aList;
+}
\ No newline at end of file