Salome HOME
Merge branch 'V9_2_2_BR'
[plugins/blsurfplugin.git] / src / GUI / BLSURFPluginGUI_AdvWidget.cxx
1 // Copyright (C) 2007-2019  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    : BLSURFPluginGUI_Dlg.cxx
22 // Authors : Gilles DAVID (OCC)
23 // ---
24 //
25
26 #include "BLSURFPluginGUI_Dlg.h"
27
28 #include <QFileDialog>
29
30 #include <iostream>
31
32 namespace
33 {
34   enum { EDITABLE_ROLE = Qt::UserRole + 1, PARAM_NAME,
35          NAME_COL = 0, VALUE_COL };
36
37   class ItemDelegate: public QItemDelegate {
38   public:
39     ItemDelegate(QObject* parent=0): QItemDelegate(parent) {}
40     QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
41     {
42       bool editable = index.data( EDITABLE_ROLE ).toInt();
43       return editable ? QItemDelegate::createEditor( parent, o, index ) : 0;
44     }
45   };
46 }
47
48 //////////////////////////////////////////
49 // BLSURFPluginGUI_AdvWidget
50 //////////////////////////////////////////
51
52 BLSURFPluginGUI_AdvWidget::BLSURFPluginGUI_AdvWidget( QWidget* parent, Qt::WindowFlags f )
53   : QWidget( parent, f )
54 {
55   setupUi( this );
56   myOptionTable->topLevelItem( 2 )->setHidden( true ); // hide CUSTOM options
57   myOptionTable->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
58   myOptionTable->setItemDelegate( new ItemDelegate( myOptionTable ) );
59
60   connect( myOptionTable, SIGNAL( itemChanged(QTreeWidgetItem *, int)), SLOT( itemChanged(QTreeWidgetItem *, int )));
61 }
62
63 BLSURFPluginGUI_AdvWidget::~BLSURFPluginGUI_AdvWidget()
64 {
65 }
66
67 void BLSURFPluginGUI_AdvWidget::onChooseGMFFile()
68 {
69   QString fileName = QFileDialog::getSaveFileName(0, tr("BLSURF_GMF_FILE_DIALOG"), myGMFFileName->text(), tr("BLSURF_GMF_FILE_FORMAT"));
70   if ( !fileName.isEmpty() ) {
71     if (!fileName.endsWith(".mesh") && !fileName.endsWith(".meshb"))
72       fileName.append(".mesh");
73     myGMFFileName->setText(fileName);
74   }
75 }
76
77 void BLSURFPluginGUI_AdvWidget::AddOption( int iTable, const char* option )
78 {
79   if ( iTable < 0 || iTable > 2 ) return;
80
81   QTreeWidgetItem * table = myOptionTable->topLevelItem( iTable );
82   table->setExpanded( true );
83
84   QTreeWidgetItem * row = new QTreeWidgetItem( table );
85   row->setData( NAME_COL, EDITABLE_ROLE, int( iTable == 2 && !option ));
86   row->setFlags( row->flags() | Qt::ItemIsEditable );
87
88   QString name, value;
89   bool isDefault = false;
90   if ( option )
91   {
92     QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
93     if ( name_value_type.size() > 0 )
94       name = name_value_type[0];
95     if ( name_value_type.size() > 1 )
96       value = name_value_type[1];
97     if ( name_value_type.size() > 2 )
98       isDefault = !name_value_type[2].toInt();
99   }
100   row->setText( 0, tr( name.toLatin1().constData() ));
101   row->setText( 1, tr( value.toLatin1().constData() ));
102   row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
103   row->setData( NAME_COL, PARAM_NAME, name );
104
105   if ( iTable == 2 )
106   {
107     table->setHidden( false );
108     if ( !option )
109     {
110       myOptionTable->scrollToItem( row );
111       myOptionTable->setCurrentItem( row );
112       myOptionTable->editItem( row, NAME_COL );
113     }
114   }
115 }
116
117 void BLSURFPluginGUI_AdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
118                                                    QString&          option,
119                                                    QString&          value,
120                                                    bool&             isDefault)
121 {
122   option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
123   value     = tblRow->text( VALUE_COL );
124   isDefault = ! tblRow->checkState( NAME_COL );
125 }
126
127
128 void BLSURFPluginGUI_AdvWidget::itemChanged(QTreeWidgetItem * tblRow, int column)
129 {
130   if ( tblRow )
131   {
132     myOptionTable->blockSignals( true );
133
134     tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
135
136     int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
137     tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
138
139     if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
140     {
141       tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
142     }
143
144     myOptionTable->blockSignals( false );
145   }
146 }