Salome HOME
Fix for the issue #2274: closing the study takes too much time.
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_WidgetField.cpp
index 8e0f85d42ac04a094fa4c643e977d49aec9f37f1..e7e400d37c13971ad91b57aa62f35f78fc2ea9d5 100644 (file)
@@ -1,14 +1,29 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        CollectionPlugin_WidgetField.cpp
-// Created:     16 Nov 2016
-// Author:      Vitaly SMETANNIKOV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "CollectionPlugin_WidgetField.h"
 #include "CollectionPlugin_Field.h"
 
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_IModule.h>
 #include <ModuleBase_ISelection.h>
 #include <ModuleBase_IPropertyPanel.h>
 
@@ -39,15 +54,22 @@ const char* MYFirstCol = "Shape";
 const char* MYTrue = "True";
 const char* MYFalse = "False";
 
+DataTableItemDelegate::DataTableItemDelegate(ModelAPI_AttributeTables::ValueType theType)
+  : QStyledItemDelegate(), myType(theType)
+{
+}
+
+
 QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
                                              const QStyleOptionViewItem & theOption,
                                              const QModelIndex& theIndex ) const
 {
+  QWidget* aEditor = 0;
   if ((theIndex.column() == 0) && (theIndex.row() > 0)) {
     QWidget* aWgt = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
     QLineEdit* aEdt = static_cast<QLineEdit*>(aWgt);
     aEdt->setReadOnly(true);
-    return aEdt;
+    aEditor = aEdt;
   } else {
     QLineEdit* aLineEdt = 0;
     switch (myType) {
@@ -57,7 +79,7 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
                                                                             theIndex));
       if (aLineEdt) {
         aLineEdt->setValidator(new QDoubleValidator(aLineEdt));
-        return aLineEdt;
+        aEditor = aLineEdt;
       }
       break;
     case ModelAPI_AttributeTables::INTEGER:
@@ -66,7 +88,7 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
                                                                             theIndex));
       if (aLineEdt) {
         aLineEdt->setValidator(new QIntValidator(aLineEdt));
-        return aLineEdt;
+        aEditor = aLineEdt;
       }
       break;
     case ModelAPI_AttributeTables::BOOLEAN:
@@ -74,11 +96,26 @@ QWidget* DataTableItemDelegate::createEditor(QWidget* theParent,
         QComboBox* aBox = new QComboBox(theParent);
         aBox->addItem(MYFalse);
         aBox->addItem(MYTrue);
-        return aBox;
+        aEditor = aBox;
       }
+      break;
+    default:
+      aEditor = QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
     }
   }
-  return QStyledItemDelegate::createEditor(theParent, theOption, theIndex);
+  if (myType == ModelAPI_AttributeTables::BOOLEAN)
+    connect(aEditor, SIGNAL(currentTextChanged(const QString&)),
+      SLOT(onEditItem(const QString&)));
+  else
+    connect(aEditor, SIGNAL(textEdited(const QString&)),
+      SLOT(onEditItem(const QString&)));
+  return aEditor;
+}
+
+void DataTableItemDelegate::onEditItem(const QString& theText)
+{
+  QWidget* aWgt = dynamic_cast<QWidget*>(sender());
+  commitData(aWgt);
 }
 
 
@@ -412,9 +449,14 @@ 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(myShapeTypeCombo);
-  aControls.push_back(myFieldTypeCombo);
-  aControls.push_back(myNbComponentsSpn);
+  aControls.append(myShapeTypeCombo);
+  aControls.append(myFieldTypeCombo);
+  aControls.append(myNbComponentsSpn);
+  if (myStampSpnList.size() > 0)
+    aControls.append(myStampSpnList.first());
+  if (myDataTblList.size() > 0)
+    aControls.append(myDataTblList.first());
+
   return aControls;
 }
 
@@ -471,7 +513,9 @@ bool CollectionPlugin_WidgetField::restoreValueCustom()
   std::string aTypeStr = aSelList->selectionType();
   if (aTypeStr == "")
     return false; // The attribute is not initialized
+  isBlocked = myShapeTypeCombo->blockSignals(true);
   myShapeTypeCombo->setCurrentIndex(getSelectionType(aTypeStr));
+  myShapeTypeCombo->blockSignals(isBlocked);
 
   // Get number of components
   AttributeStringArrayPtr aStringsAttr =
@@ -782,21 +826,29 @@ bool CollectionPlugin_WidgetField::
 }
 
 //**********************************************************************************
-void CollectionPlugin_WidgetField::onSelectionChanged()
+bool CollectionPlugin_WidgetField::
+  setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, const bool theToValidate)
 {
-  //if (isEditingMode())
-  //  return;
-
   if (myActivation) {
     myActivation = false;
-    return;
+    return false;
   }
   // Ignore selection for Parts mode
   if (myShapeTypeCombo->currentIndex() == 5)
-    return;
-
-  QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
-
+    return false;
+
+  QList<ModuleBase_ViewerPrsPtr> aSelected;
+  QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt;
+  for (anIt = theValues.begin(); anIt != theValues.end(); anIt++) {
+    ModuleBase_ViewerPrsPtr aValue = *anIt;
+    ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
+    if (theToValidate && aRes.get()) {
+      if (myShapeTypeCombo->currentIndex() > 3)
+        aSelected.append(aValue);
+      else if (acceptSubShape(aValue->shape(), aRes))
+        aSelected.append(aValue);
+    }
+  }
   AttributeSelectionListPtr aSelList =
     myFeature->data()->selectionList(CollectionPlugin_Field::SELECTED_ID());
   aSelList->setSelectionType(getSelectionType(myShapeTypeCombo->currentIndex()));
@@ -859,7 +911,7 @@ void CollectionPlugin_WidgetField::onSelectionChanged()
       }
     }
   }
-  emit valuesChanged();
+  return true;
 }
 
 //**********************************************************************************
@@ -876,10 +928,7 @@ void CollectionPlugin_WidgetField::onFieldTypeChanged(int theIdx)
           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");
-            }
+            aTable->item(j, i)->setText("0");
             break;
           case ModelAPI_AttributeTables::BOOLEAN:
             aTable->item(j, i)->setText(MYFalse);
@@ -944,6 +993,16 @@ bool CollectionPlugin_WidgetField::processEnter()
     myIsTabEdit = false;
     return true;
   }
+  QWidget* aCurrWgt = qApp->focusWidget();
+  int aCurWgtId = myStepWgt->currentIndex();
+  if ((aCurrWgt == myShapeTypeCombo) ||
+      (aCurrWgt == myFieldTypeCombo) ||
+      (aCurrWgt == myNbComponentsSpn) ||
+      (aCurrWgt == myStampSpnList[aCurWgtId]) ||
+      (aCurrWgt == myDataTblList[aCurWgtId])) {
+    setFocus();
+    return true;
+  }
   return false;
 }
 
@@ -972,3 +1031,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