Salome HOME
Copyright update 2020
[plugins/ghs3dplugin.git] / src / GUI / GHS3DPluginGUI_AdvWidget.cxx
index 18a79bf3af73dbc843b4a4b8ac381d521471fdaf..14010785d24f30c0b6667b04466eecdfaee785ec 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2020  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
 #include <QFileDialog>
 
 #include <iostream>
+#include <GHS3DPlugin_Hypothesis.hxx>
 
+namespace
+{
+  enum { EDITABLE_ROLE = Qt::UserRole + 1, PARAM_NAME,
+         NAME_COL = 0, VALUE_COL };
+
+  class ItemDelegate: public QItemDelegate {
+  public:
+    ItemDelegate(QObject* parent=0): QItemDelegate(parent) {}
+    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
+    {
+      bool editable = index.data( EDITABLE_ROLE ).toInt();
+      return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
+    }
+  };
+}
 
 //////////////////////////////////////////
 // GHS3DPluginGUI_AdvWidget
@@ -38,9 +54,84 @@ GHS3DPluginGUI_AdvWidget::GHS3DPluginGUI_AdvWidget( QWidget* parent, Qt::WindowF
 : QWidget( parent, f )
 {
   setupUi( this );
-  advOptionTable->layout()->setMargin( 0 );
+  //myOptionTable->layout()->setMargin( 0 );
+  myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
+  myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
+
+  connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
 }
 
 GHS3DPluginGUI_AdvWidget::~GHS3DPluginGUI_AdvWidget()
 {
 }
+
+void GHS3DPluginGUI_AdvWidget::AddOption( const char* option, bool isCustom )
+{
+  QTreeWidget * table = myOptionTable;
+  //table->setExpanded( true );
+
+  QTreeWidgetItem * row = new QTreeWidgetItem( table );
+  row->setData( NAME_COL, EDITABLE_ROLE, int( isCustom && !option ));
+  row->setFlags( row->flags() | Qt::ItemIsEditable );
+
+  QString name, value;
+  bool isDefault = false;
+  if ( option )
+  {
+    QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
+    if ( name_value_type.size() > 0 )
+      name = name_value_type[0];
+    if ( name_value_type.size() > 1 )
+      value = name_value_type[1];
+    if ( name_value_type.size() > 2 )
+      isDefault = !name_value_type[2].toInt();
+
+    // if ( value == GHS3DPlugin_Hypothesis::NoValue() )
+    //   value.clear();
+  }
+  row->setText( 0, tr( name.toLatin1().constData() ));
+  row->setText( 1, tr( value.toLatin1().constData() ));
+  row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
+  row->setData( NAME_COL, PARAM_NAME, name );
+
+  if ( isCustom )
+  {
+    myOptionTable->scrollToItem( row );
+    myOptionTable->setCurrentItem( row );
+    myOptionTable->editItem( row, NAME_COL );
+  }
+}
+
+void GHS3DPluginGUI_AdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
+                                                  QString&          option,
+                                                  QString&          value,
+                                                  bool&             isDefault)
+{
+  option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
+  value     = tblRow->text( VALUE_COL );
+  isDefault = ! tblRow->checkState( NAME_COL );
+
+  // if ( value.isEmpty() )
+  //   value = GHS3DPlugin_Hypothesis::NoValue();
+}
+
+
+void GHS3DPluginGUI_AdvWidget::itemChanged(QTreeWidgetItem * tblRow, int column)
+{
+  if ( tblRow )
+  {
+    myOptionTable->blockSignals( true );
+
+    tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
+
+    int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
+    tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
+
+    if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
+    {
+      tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
+    }
+
+    myOptionTable->blockSignals( false );
+  }
+}