Salome HOME
Merge abn/configuration branch into master branch
[plugins/blsurfplugin.git] / src / GUI / BLSURFPluginGUI_AdvWidget.cxx
1 // Copyright (C) 2007-2016  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   std::cout << "fileName: " << fileName.toStdString() << std::endl;
71   if (!fileName.endsWith(".mesh") && !fileName.endsWith(".meshb"))
72     fileName.append(".mesh");
73   myGMFFileName->setText(fileName);
74 }
75
76 void BLSURFPluginGUI_AdvWidget::AddOption( int iTable, const char* option )
77 {
78   if ( iTable < 0 || iTable > 2 ) return;
79
80   QTreeWidgetItem * table = myOptionTable->topLevelItem( iTable );
81   table->setExpanded( true );
82
83   QTreeWidgetItem * row = new QTreeWidgetItem( table );
84   row->setData( NAME_COL, EDITABLE_ROLE, int( iTable == 2 && !option ));
85   row->setFlags( row->flags() | Qt::ItemIsEditable );
86
87   QString name, value;
88   bool isDefault = false;
89   if ( option )
90   {
91     QStringList name_value_type = QString(option).split( ":", QString::KeepEmptyParts );
92     if ( name_value_type.size() > 0 )
93       name = name_value_type[0];
94     if ( name_value_type.size() > 1 )
95       value = name_value_type[1];
96     if ( name_value_type.size() > 2 )
97       isDefault = !name_value_type[2].toInt();
98   }
99   row->setText( 0, tr( name.toLatin1().constData() ));
100   row->setText( 1, tr( value.toLatin1().constData() ));
101   row->setCheckState( 0, isDefault ? Qt::Unchecked : Qt::Checked);
102   row->setData( NAME_COL, PARAM_NAME, name );
103
104   if ( iTable == 2 )
105   {
106     table->setHidden( false );
107     if ( !option )
108     {
109       myOptionTable->scrollToItem( row );
110       myOptionTable->editItem( row, NAME_COL );
111     }
112   }
113 }
114
115 void BLSURFPluginGUI_AdvWidget::GetOptionAndValue( QTreeWidgetItem * tblRow,
116                                                    QString&          option,
117                                                    QString&          value,
118                                                    bool&             isDefault)
119 {
120   option    = tblRow->data( NAME_COL, PARAM_NAME ).toString();
121   value     = tblRow->text( VALUE_COL );
122   isDefault = ! tblRow->checkState( NAME_COL );
123 }
124
125
126 void BLSURFPluginGUI_AdvWidget::itemChanged(QTreeWidgetItem * tblRow, int column)
127 {
128   if ( tblRow )
129   {
130     myOptionTable->blockSignals( true );
131
132     tblRow->setData( VALUE_COL, EDITABLE_ROLE, int( tblRow->checkState( NAME_COL )));
133
134     int c = tblRow->checkState( NAME_COL ) ? 0 : 150;
135     tblRow->setForeground( VALUE_COL, QBrush( QColor( c, c, c )));
136
137     if ( column == NAME_COL && tblRow->data( NAME_COL, EDITABLE_ROLE ).toInt() ) // custom table
138     {
139       tblRow->setData( NAME_COL, PARAM_NAME, tblRow->text( NAME_COL ));
140     }
141
142     myOptionTable->blockSignals( false );
143   }
144 }