Salome HOME
Enforced verteces (continue):
[plugins/blsurfplugin.git] / src / GUI / BLSURFPluginGUI_HypothesisCreator.cxx
index 7fc5eeb9bfe13382391e53a6917cf1e526eecded..b2d7275d0ede57bbc4ba61b9c93866fb9349ab9b 100644 (file)
@@ -131,7 +131,6 @@ enum {
 
 // Enforced verteces array columns
 enum {
-//   ENF_VER_ENTRY_COLUMN = 0,
   ENF_VER_NAME_COLUMN = 0,
   ENF_VER_ENTRY_COLUMN,
   ENF_VER_X_COLUMN,
@@ -251,25 +250,28 @@ End initialization Python structures and objects
 class QDoubleValidator;
 
 //
-// BEGIN DoubleLineEditDelegate
+// BEGIN EnforcedTreeWidgetDelegate
 //
 
-DoubleLineEditDelegate::DoubleLineEditDelegate(QObject *parent)
+EnforcedTreeWidgetDelegate::EnforcedTreeWidgetDelegate(QObject *parent)
   : QItemDelegate(parent)
 {
 }
 
-QWidget *DoubleLineEditDelegate::createEditor(QWidget *parent,
-                                              const QStyleOptionViewItem &/* option */,
-                                              const QModelIndex &/* index */) const
+QWidget *EnforcedTreeWidgetDelegate::createEditor(QWidget *parent,
+                                              const QStyleOptionViewItem & option ,
+                                              const QModelIndex & index ) const
 {
   QLineEdit *editor = new QLineEdit(parent);
-  editor->setValidator(new QDoubleValidator(parent));
+  if (index.column() == ENF_VER_X_COLUMN || \
+    index.column() == ENF_VER_Y_COLUMN || \
+    index.column() == ENF_VER_Z_COLUMN)
+    editor->setValidator(new QDoubleValidator(parent));
 
   return editor;
 }
 
-void DoubleLineEditDelegate::setEditorData(QWidget *editor,
+void EnforcedTreeWidgetDelegate::setEditorData(QWidget *editor,
                                            const QModelIndex &index) const
 {
   QString value = index.model()->data(index, Qt::EditRole).toString();
@@ -278,27 +280,97 @@ void DoubleLineEditDelegate::setEditorData(QWidget *editor,
   lineEdit->setText(value);
 }
 
-void DoubleLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
+void EnforcedTreeWidgetDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                           const QModelIndex &index) const
 {
   QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
-  bool ok;
-  double value = lineEdit->text().toDouble(&ok);
 
-  if (ok) {
-    model->setData(index, value, Qt::EditRole);
-    MESSAGE("Value " << value << " was set at index(" << index.row() << "," << index.column() << ")");
+  if (index.column() == ENF_VER_X_COLUMN || \
+    index.column() == ENF_VER_Y_COLUMN || \
+    index.column() == ENF_VER_Z_COLUMN)
+  {
+    if (not vertexExists(model, index, lineEdit->text())) {
+      bool ok;
+      double value = lineEdit->text().toDouble(&ok);
+      if (ok)
+        model->setData(index, value, Qt::EditRole);
+    }
+  }
+  else if (index.column() == ENF_VER_NAME_COLUMN) {
+    QString value = lineEdit->text();
+    if (not vertexExists(model, index, value))
+      model->setData(index, value, Qt::EditRole);
+//     MESSAGE("Value " << value.toString().toStdString() << " was set at index(" << index.row() << "," << index.column() << ")");
   }
 }
 
-void DoubleLineEditDelegate::updateEditorGeometry(QWidget *editor,
+void EnforcedTreeWidgetDelegate::updateEditorGeometry(QWidget *editor,
     const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
 {
   editor->setGeometry(option.rect);
 }
 
+bool EnforcedTreeWidgetDelegate::vertexExists(QAbstractItemModel *model,
+    const QModelIndex &index, QString value) const
+{
+  bool exists = false;
+  QModelIndex parent = index.parent();
+  int row = index.row();
+  int col = index.column();
+  
+  if (parent.isValid()) {
+    if (col == ENF_VER_X_COLUMN || col == ENF_VER_Y_COLUMN || col == ENF_VER_Z_COLUMN) {
+      double x, y, z;
+      if (col == ENF_VER_X_COLUMN) {
+        x = value.toDouble();
+        y = parent.child(row, ENF_VER_Y_COLUMN).data(Qt::EditRole).toDouble();
+        z = parent.child(row, ENF_VER_Z_COLUMN).data(Qt::EditRole).toDouble();
+      }
+      if (col == ENF_VER_Y_COLUMN) {
+        y = value.toDouble();
+        x = parent.child(row, ENF_VER_X_COLUMN).data(Qt::EditRole).toDouble();
+        z = parent.child(row, ENF_VER_Z_COLUMN).data(Qt::EditRole).toDouble();
+      }
+      if (col == ENF_VER_Z_COLUMN) {
+        z = value.toDouble();
+        x = parent.child(row, ENF_VER_X_COLUMN).data(Qt::EditRole).toDouble();
+        y = parent.child(row, ENF_VER_Y_COLUMN).data(Qt::EditRole).toDouble();
+      }
+      // MESSAGE("Checking for existing vertex " << x << ", " << y << "," << z);
+      int nbChildren = model->rowCount(parent);
+      for (int i = 0 ; i < nbChildren ; i++) {
+        if (i != row) {
+          double childX = parent.child(i, ENF_VER_X_COLUMN).data(Qt::EditRole).toDouble();
+          double childY = parent.child(i, ENF_VER_Y_COLUMN).data(Qt::EditRole).toDouble();
+          double childZ = parent.child(i, ENF_VER_Z_COLUMN).data(Qt::EditRole).toDouble();
+          MESSAGE("Vertex: " << childX << ", " << childY << "," << childZ);
+          if ((childX == x) && (childY == y) && (childZ == z)) {
+            MESSAGE("Found !");
+            exists = true;
+            break;
+          }
+        }
+      }
+    }
+    else if (index.column() == ENF_VER_NAME_COLUMN) {
+      int nbChildren = model->rowCount(parent);
+      for (int i = 0 ; i < nbChildren ; i++) {
+        if (i != row) {
+          QString childName = parent.child(i, ENF_VER_NAME_COLUMN).data(Qt::EditRole).toString();
+          if (childName == value) {
+            exists = true;
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  return exists;
+}
+
 //
-// END DoubleLineEditDelegate
+// END EnforcedTreeWidgetDelegate
 //
 
 
@@ -667,6 +739,7 @@ QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
     myEnforcedTreeWidget->resizeColumnToContents(column);
   }
   myEnforcedTreeWidget->hideColumn(ENF_VER_ENTRY_COLUMN);
+  myEnforcedTreeWidget->setItemDelegate(new EnforcedTreeWidgetDelegate());
   anEnfLayout->addWidget(myEnforcedTreeWidget, 0, 0, 8, 1);
 
   QLabel* myXCoordLabel = new QLabel( tr( "BLSURF_ENF_VER_X_LABEL" ), myEnfGroup );
@@ -719,8 +792,9 @@ QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
   connect( removeButton,       SIGNAL( clicked()),                    this,         SLOT( onRemoveMap() ) );
   connect( mySizeMapTable,     SIGNAL( cellChanged ( int, int  )),    this,         SLOT( onSetSizeMap(int,int ) ) );
 
-  connect( myEnforcedTreeWidget,SIGNAL( itemClicked(QTreeWidgetItem *, int)), this, SLOT( synchronize(QTreeWidgetItem *, int) ) );
+  connect( myEnforcedTreeWidget,SIGNAL( itemClicked(QTreeWidgetItem *, int)), this, SLOT( synchronizeCoords() ) );
   connect( myEnforcedTreeWidget,SIGNAL( itemChanged(QTreeWidgetItem *, int)), this, SLOT( update(QTreeWidgetItem *, int) ) );
+  connect( myEnforcedTreeWidget,SIGNAL( itemSelectionChanged() ),     this,         SLOT( synchronizeCoords() ) );
   connect( myEnforcedTreeWidget,SIGNAL( activated(QModelIndex&)),     this,         SLOT( onEnforcedVertexActivated() ) );
   connect( addVertexButton,    SIGNAL( clicked()),                    this,         SLOT( onAddEnforcedVerteces() ) );
   connect( removeVertexButton, SIGNAL( clicked()),                    this,         SLOT( onRemoveEnforcedVertex() ) );
@@ -757,19 +831,27 @@ void BLSURFPluginGUI_HypothesisCreator::update(QTreeWidgetItem* item, int column
   }
 }
 
-/** BLSURFPluginGUI_HypothesisCreator::synchronize(item, column)
+/** BLSURFPluginGUI_HypothesisCreator::synchronizeCoords()
 This method synchronizes the QLineEdit widgets content with the coordinates
 of the enforced vertex clicked in the tree widget.
 */
-void BLSURFPluginGUI_HypothesisCreator::synchronize(QTreeWidgetItem* item, int column) {
+void BLSURFPluginGUI_HypothesisCreator::synchronizeCoords() {
 //   MESSAGE("BLSURFPluginGUI_HypothesisCreator::synchronizeCoords");
-  QVariant x = item->data(ENF_VER_X_COLUMN, Qt::EditRole);
-  if (not x.isNull()) {
-    QVariant y = item->data(ENF_VER_Y_COLUMN, Qt::EditRole);
-    QVariant z = item->data(ENF_VER_Z_COLUMN, Qt::EditRole);
-    myXCoord->setText(x.toString());
-    myYCoord->setText(y.toString());
-    myZCoord->setText(z.toString());
+  QList<QTreeWidgetItem *> items = myEnforcedTreeWidget->selectedItems();
+  if (not items.isEmpty()) {
+    QTreeWidgetItem *item;
+    for (int i=0 ; i < items.size() ; i++) {
+      item = items[i];
+      QVariant x = item->data(ENF_VER_X_COLUMN, Qt::EditRole);
+      if (not x.isNull()) {
+        QVariant y = item->data(ENF_VER_Y_COLUMN, Qt::EditRole);
+        QVariant z = item->data(ENF_VER_Z_COLUMN, Qt::EditRole);
+        myXCoord->setText(x.toString());
+        myYCoord->setText(y.toString());
+        myZCoord->setText(z.toString());
+        break;
+      }
+    }
   }
 }
 
@@ -791,11 +873,11 @@ void BLSURFPluginGUI_HypothesisCreator::addEnforcedVertex(std::string entry, std
     theItem = theItemList[0];
   }
 
-  MESSAGE("theItemName is " << theItem->text(ENF_VER_NAME_COLUMN).toStdString());
+//   MESSAGE("theItemName is " << theItem->text(ENF_VER_NAME_COLUMN).toStdString());
   bool okToCreate = true;
 
   const int nbVert = theItem->childCount();
-  MESSAGE("Number of child rows: " << nbVert);
+//   MESSAGE("Number of child rows: " << nbVert);
   if (nbVert >0) {
     double childValueX,childValueY,childValueZ;
     QTreeWidgetItem* child;
@@ -1014,21 +1096,17 @@ void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
   for ( ; evmIt != data.enfVertMap.end() ; ++evmIt) {
     std::string entry = (*evmIt).first;
     std::string shapeName = myGeomToolSelected->getNameFromEntry(entry);
-    MESSAGE("retrieveParams(): entry " << entry << ", shape: " << shapeName);
 
     std::set<std::vector<double> > evs;
     std::set<std::vector<double> >::const_iterator evsIt;
     try  {
-      MESSAGE("evs = (*evmIt)[entry];");
       evs = (*evmIt).second;
     }
     catch(...) {
-      MESSAGE("Fail");
+      MESSAGE("evs = (*evmIt)[entry]: FAIL");
       break;
     }
 
-    MESSAGE("retrieveParams(): evs.size() = " << evs.size());
-
     evsIt = evs.begin();
     for ( ; evsIt != evs.end() ; ++evsIt) {
       double x, y, z;