1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESH_AdvOptionsWdg.h
24 // Author : Open CASCADE S.A.S.
26 #include "SMESH_AdvOptionsWdg.h"
28 #include <QTableWidget>
29 #include <QPushButton>
30 #include <QGridLayout>
32 #include <QHeaderView>
39 const int IS_CUSTOM = Qt::UserRole;
42 SMESH_AdvOptionsWdg::SMESH_AdvOptionsWdg( QWidget* parent )
45 myTable = new QTableWidget( /*nbrows=*/0, /*nbcol=*/3, this );
46 QPushButton* addBtn = new QPushButton( tr("ADD_OPTION_BTN"), this );
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 );
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 );
61 connect( addBtn, SIGNAL( clicked() ), SLOT( onAdd() ));
64 SMESH_AdvOptionsWdg::~SMESH_AdvOptionsWdg()
68 void SMESH_AdvOptionsWdg::AddOption( QString name, QString value, bool isDefault, bool isCustom )
70 int row = myTable->rowCount();
71 myTable->insertRow( row );
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 );
81 QCheckBox* chkBox = new QCheckBox();
82 QWidget* wdg = new QWidget();
83 QHBoxLayout* lay = new QHBoxLayout( wdg );
84 lay->setContentsMargins(0,0,0,0);
86 lay->addWidget(chkBox);
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 );
94 myTable->editItem( nameItem );
97 void SMESH_AdvOptionsWdg::SetCustomOptions( const QString& text )
99 QStringList nameVals = text.split(" ");
100 for ( int i = 1; i < nameVals.count(); i += 2 )
101 AddOption( nameVals[i-1], nameVals[i], false, true );
104 void SMESH_AdvOptionsWdg::onAdd()
106 AddOption( "", "", false, true );
109 void SMESH_AdvOptionsWdg::onToggle()
111 int row = myTable->currentRow();
112 QTableWidgetItem* valueItem = myTable->item( row, 2 );
114 bool isActive = isChecked( row );
115 int c = isActive ? 0 : 150;
116 valueItem->setForeground( QBrush( QColor( c, c, c )));
118 valueItem->setFlags( valueItem->flags() | Qt::ItemIsEditable );
120 valueItem->setFlags( valueItem->flags() & ~Qt::ItemIsEditable );
123 void SMESH_AdvOptionsWdg::GetOption( int row,
129 if ( row < myTable->rowCount() )
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();
138 QString SMESH_AdvOptionsWdg::GetCustomOptions()
140 QString text, value, name;
141 bool isDefault, isCustom;
142 for ( int row = 0; row < myTable->rowCount(); ++row )
144 GetOption( row, name, value, isDefault, isCustom );
145 if ( !name.isEmpty() && !value.isEmpty() && isCustom && !isDefault )
146 text += name + " " + value + " ";
151 bool SMESH_AdvOptionsWdg::isChecked( int row )
153 QCheckBox* cb = myTable->cellWidget( row, 0 )->findChild<QCheckBox *>();
154 return cb->isChecked();