Salome HOME
Copyright update 2022
[plugins/hybridplugin.git] / src / GUI / HYBRIDPluginGUI_AdvWidget.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // ---
21 // File    : HYBRIDPluginGUI_Dlg.cxx
22 // Authors : Renaud NEDELEC (OCC)
23 // ---
24 //
25
26 #include "HYBRIDPluginGUI_Dlg.h"
27
28 #include <QFileDialog>
29
30 #include <iostream>
31 #include <HYBRIDPlugin_Hypothesis.hxx>
32
33 namespace
34 {
35   enum { EDITABLE_ROLE = Qt::UserRole + 1, PARAM_NAME,
36          NAME_COL = 0, VALUE_COL };
37
38   class ItemDelegate: public QItemDelegate {
39   public:
40     ItemDelegate(QObject* parent=0): QItemDelegate(parent) {}
41     QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
42     {
43       bool editable = index.data( EDITABLE_ROLE ).toInt();
44       return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
45     }
46   };
47 }
48
49 //////////////////////////////////////////
50 // HYBRIDPluginGUI_AdvWidget
51 //////////////////////////////////////////
52
53 HYBRIDPluginGUI_AdvWidget::HYBRIDPluginGUI_AdvWidget( QWidget* parent, Qt::WindowFlags f )
54 : QWidget( parent, f )
55 {
56   setupUi( this );
57   //myOptionTable->layout()->setMargin( 0 );
58   myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
59   myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
60
61   connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
62 }
63
64 HYBRIDPluginGUI_AdvWidget::~HYBRIDPluginGUI_AdvWidget()
65 {
66 }
67
68 void HYBRIDPluginGUI_AdvWidget::AddOption( const char* option, bool isCustom )
69 {
70   QTreeWidget * table = myOptionTable;
71   //table->setExpanded( true );
72
73   QTreeWidgetItem * row = new QTreeWidgetItem( table );
74   row->setData( NAME_COL, EDITABLE_ROLE, int( isCustom && !option ));
75   row->setFlags( row->flags() | Qt::ItemIsEditable );
76
77   QString name, value;
78   bool isDefault = false;
79   if ( option )
80   {
81     QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
82     if ( name_value_type.size() > 0 )
83       name = name_value_type[0];
84     if ( name_value_type.size() > 1 )
85       value = name_value_type[1];
86     if ( name_value_type.size() > 2 )
87       isDefault = !name_value_type[2].toInt();
88
89     // if ( value == HYBRIDPlugin_Hypothesis::NoValue() )
90     //   value.clear();
91   }
92   row->setText( 0, tr( name.toLatin1().constData() ));
93   row->setText( 1, tr( value.toLatin1().constData() ));
94   row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
95   row->setData( NAME_COL, PARAM_NAME, name );
96
97   if ( isCustom )
98   {
99     myOptionTable->scrollToItem( row );
100     myOptionTable->setCurrentItem( row );
101     myOptionTable->editItem( row, NAME_COL );
102   }
103 }
104
105 void HYBRIDPluginGUI_AdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
106                                                   QString&          option,
107                                                   QString&          value,
108                                                   bool&             isDefault)
109 {
110   option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
111   value     = tblRow->text( VALUE_COL );
112   isDefault = ! tblRow->checkState( NAME_COL );
113
114   // if ( value.isEmpty() )
115   //   value = HYBRIDPlugin_Hypothesis::NoValue();
116 }
117
118
119 void HYBRIDPluginGUI_AdvWidget::itemChanged(QTreeWidgetItem * tblRow, int column)
120 {
121   if ( tblRow )
122   {
123     myOptionTable->blockSignals( true );
124
125     tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
126
127     int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
128     tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
129
130     if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
131     {
132       tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
133     }
134
135     myOptionTable->blockSignals( false );
136   }
137 }