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