Salome HOME
Update of CheckDone
[modules/smesh.git] / src / PluginUtils / SMESH_AdvOptionsWdg.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : SMESH_AdvOptionsWdg.h
24 // Author : Open CASCADE S.A.S.
25
26 #include "SMESH_AdvOptionsWdg.h"
27
28 #include <QTableWidget>
29 #include <QPushButton>
30 #include <QGridLayout>
31 #include <QCheckBox>
32 #include <QHeaderView>
33
34 #define SPACING 6
35 #define MARGIN  11
36
37 namespace
38 {
39   const int IS_CUSTOM = Qt::UserRole;
40 }
41
42 SMESH_AdvOptionsWdg::SMESH_AdvOptionsWdg( QWidget* parent )
43   : QWidget( parent )
44 {
45   myTable = new QTableWidget( /*nbrows=*/0, /*nbcol=*/3, this );
46   QPushButton* addBtn = new QPushButton( tr("ADD_OPTION_BTN"), this );
47
48   myTable->setHorizontalHeaderLabels
49     ( QStringList() << tr("CHOICE") << tr("OPTION_NAME") << tr("OPTION_VALUE") );
50   QHeaderView * header = myTable->horizontalHeader();
51   header->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
52   header->setSectionResizeMode( 1, QHeaderView::Stretch );
53   header->setSectionResizeMode( 2, QHeaderView::ResizeToContents );
54
55   QGridLayout* lay = new QGridLayout( this );
56   lay->setMargin( MARGIN );
57   lay->setSpacing( SPACING );
58   lay->addWidget( myTable, 0,0, 1,3 );
59   lay->addWidget( addBtn,  1,0 );
60
61   connect( addBtn, SIGNAL( clicked() ), SLOT( onAdd() ));
62 }
63
64 SMESH_AdvOptionsWdg::~SMESH_AdvOptionsWdg()
65 {
66 }
67
68 void SMESH_AdvOptionsWdg::AddOption( QString name, QString value, bool isDefault, bool isCustom )
69 {
70   int row = myTable->rowCount();
71   myTable->insertRow( row );
72
73   QTableWidgetItem*  nameItem = new QTableWidgetItem( name );
74   QTableWidgetItem* valueItem = new QTableWidgetItem( value );
75   if ( !name.isEmpty() )
76     nameItem->setFlags( nameItem->flags() & ~Qt::ItemIsEditable );
77   myTable->setItem( row, 1, nameItem );
78   myTable->setItem( row, 2, valueItem );
79   nameItem->setData( IS_CUSTOM, isCustom );
80
81   QCheckBox* chkBox = new QCheckBox();
82   QWidget*      wdg = new QWidget();
83   QHBoxLayout*  lay = new QHBoxLayout( wdg );
84   lay->setContentsMargins(0,0,0,0);
85   lay->addStretch();
86   lay->addWidget(chkBox);
87   lay->addStretch();
88   myTable->setCellWidget( row, 0, wdg );
89   connect( chkBox, SIGNAL(toggled(bool)), this, SLOT(onToggle()));
90   myTable->setCurrentCell( row, 1, QItemSelectionModel::NoUpdate );
91   chkBox->setChecked( !isDefault );
92
93   if ( name.isEmpty() )
94     myTable->editItem( nameItem );
95 }
96
97 void SMESH_AdvOptionsWdg::SetCustomOptions( const QString& text )
98 {
99   QStringList nameVals = text.split(" ");
100   for ( int i = 1; i < nameVals.count(); i += 2 )
101     AddOption( nameVals[i-1], nameVals[i], false, true );
102 }
103
104 void SMESH_AdvOptionsWdg::onAdd()
105 {
106   AddOption( "", "", false, true );
107   
108 }
109 void SMESH_AdvOptionsWdg::onToggle()
110 {
111   int row = myTable->currentRow();
112   QTableWidgetItem* valueItem = myTable->item( row, 2 );
113
114   bool isActive = isChecked( row );
115   int c = isActive ? 0 : 150;
116   valueItem->setForeground( QBrush( QColor( c, c, c )));
117   if ( isActive )
118     valueItem->setFlags( valueItem->flags() | Qt::ItemIsEditable );
119   else
120     valueItem->setFlags( valueItem->flags() & ~Qt::ItemIsEditable );
121 }
122
123 void SMESH_AdvOptionsWdg::GetOption( int      row,
124                                      QString& name,
125                                      QString& value,
126                                      bool&    isDefault,
127                                      bool &   isCustom)
128 {
129   if ( row < myTable->rowCount() )
130   {
131     name      = myTable->item( row, 1 )->text();
132     value     = myTable->item( row, 2 )->text();
133     isDefault = !isChecked( row );
134     isCustom  = myTable->item( row, 1 )->data( IS_CUSTOM ).toInt();
135   }
136 }
137
138 QString SMESH_AdvOptionsWdg::GetCustomOptions()
139 {
140   QString text, value, name;
141   bool isDefault, isCustom;
142   for ( int row = 0; row < myTable->rowCount(); ++row )
143   {
144     GetOption( row, name, value, isDefault, isCustom );
145     if ( !name.isEmpty() && !value.isEmpty() && isCustom && !isDefault )
146       text += name + " " + value + " ";
147   }
148   return text;
149 }
150
151 bool SMESH_AdvOptionsWdg::isChecked( int row )
152 {
153   QCheckBox* cb = myTable->cellWidget( row, 0 )->findChild<QCheckBox *>();
154   return cb->isChecked();
155 }