Salome HOME
Copyright update 2020
[modules/med.git] / src / MEDGUI / MEDGUISelectComponents.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
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 //  MED MEDGUI : MED component GUI implementation
21 //  File   : MEDGUISelectComponents.cxx
22 //  Module : MED
23 //
24 #include "MEDGUISelectComponents.h"
25 #include "ui_MEDGUISelectComponents.h"
26
27 #include "MEDCalculatorBrowserLiteStruct.hxx"
28 #include "MEDCalculatorBrowserField.hxx"
29
30 #include <sstream>
31 #include <string>
32 #include <iostream>
33
34 #include <QLabel>
35 #include <QCheckBox>
36 #include <QLineEdit>
37
38 using namespace MEDCoupling;
39
40 //  Constructor
41 //  Initialize qt part and private var part
42 //  l is the MEDGUILiteStruct that will be modified, fi is a field id, for the case where one field only is selected
43 //  First, connect button ok and cancel
44 //  Then, set min and max for the loop according to fieldInd, this will made the QDialog show juste one field (with id fieldInd) or all fields
45 //  Then, fill the QDialog with the file name
46 //    For each field concerned, add the field name
47 //      For each component, add a QCheckBox and the component name as a modifiable QLineEdit
48 MEDGUISelectComponents::MEDGUISelectComponents(ParaMEDMEM::MEDCalculatorBrowserLiteStruct& l,QWidget *parent, int fi/*=-1*/) :
49   QDialog(parent),
50   ui(new Ui::MEDGUISelectComponents),
51   ls(&l),
52   fieldInd(fi)
53 {
54   ui->setupUi(this);
55
56   connect(ui->buttonBox,SIGNAL(rejected()),this,SLOT(close()));// close without applying
57   connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(fillSelection()));//  apply
58   connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(close()));//  close
59
60   int min,max;
61   fieldInd!=-1?min=fieldInd:min=0;
62   fieldInd!=-1?max=fieldInd:max=(int)ls->getNumberOfFields()-1;
63
64   QLabel *filename = new QLabel(ls->getName().c_str(),this);
65   ui->verticalLayout_2->insertWidget(0,filename);
66
67   for (int i = max; i >= min; i -= 1)
68     {
69
70       QGridLayout *fieldLayout = new QGridLayout(this);
71
72       QLabel *fieldname = new QLabel(ls->getField(i).getName().c_str(),this);
73       fieldLayout->addWidget(fieldname,0,0,1,0);
74
75
76       for (int j = ls->getField(i).getComponentsSize()-1; j >= 0; j -= 1)
77         {
78           QCheckBox *cb = new QCheckBox(this);
79           if(ls->getField(i).getSelectedComponents()[j]){
80             cb->setCheckState(Qt::Checked);
81           }
82
83           std::string compname ="";
84           if(ls->getField(i).getComponents()[j] != std::string("-noname-")) compname = ls->getField(i).getComponents()[j];
85
86           QLineEdit *le = new QLineEdit(compname.c_str(),this);
87
88           std::ostringstream chName, leName;
89           chName << ls->getName().c_str() << ls->getField(i).getName().c_str() << j << "chkbox";
90           leName << ls->getName().c_str() << ls->getField(i).getName().c_str() << j << "namebox";
91
92           cb->setObjectName(QString(chName.str().c_str()));
93           le->setObjectName(QString(leName.str().c_str()));
94           cb->setAccessibleName(QString(chName.str().c_str()));
95           le->setAccessibleName(QString(leName.str().c_str()));
96
97           fieldLayout->addWidget(cb,j+1,0);
98           fieldLayout->addWidget(le,j+1,1);
99         }
100
101       ui->verticalLayout_2->insertLayout(1,fieldLayout);
102
103     }
104 }
105
106 //  Destructor
107 MEDGUISelectComponents::~MEDGUISelectComponents()
108 {
109   delete ui;
110 }
111
112 //  Qt specific class
113 void MEDGUISelectComponents::changeEvent(QEvent *e)
114 {
115   QDialog::changeEvent(e);
116   switch (e->type()) {
117   case QEvent::LanguageChange:
118     ui->retranslateUi(this);
119     break;
120   default:
121     break;
122   }
123 }
124
125 // Fill the MEDGUILiteStruct according to the selection and modification made by user
126 // First init the min,max for the loop : if equal, just one field will be modified, else all field
127 // Then for each field, for each component, if there is modification, they are applied (selection, unselection or rename)
128 void MEDGUISelectComponents::fillSelection()
129 {
130   int min,max;
131   fieldInd!=-1?min=fieldInd:min=0;
132   fieldInd!=-1?max=fieldInd:max=(int)ls->getNumberOfFields()-1;
133
134   for (int i = max; i >= min; i -= 1) {
135
136     for (unsigned int j = 0; j < ls->getField(i).getComponentsSize(); j += 1)
137       {
138         std::ostringstream chName, leName;
139         chName << ls->getName().c_str() << ls->getField(i).getName().c_str() << j << "chkbox";
140         leName << ls->getName().c_str() << ls->getField(i).getName().c_str() << j << "namebox";
141
142         QCheckBox *cb = this->findChild<QCheckBox*>(QString(chName.str().c_str()));
143         QLineEdit *le = this->findChild<QLineEdit*>(QString(leName.str().c_str()));
144
145         if (cb->isChecked())
146           {
147             ls->getField(i).selectComponent(j);
148           }else{
149           ls->getField(i).unselectComponent(j);
150         }
151         ls->getField(i).setComponentName(j,le->text().toStdString());
152       }
153   }
154 }