Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / ParaViewPlugin / VectBoolWidget.cxx
1 // Copyright (C) 2010-2022  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 // Author : Anthony Geay
20
21 #include "VectBoolWidget.h"
22 #include "VectBoolSpreadSheet.h"
23
24 #include <QHBoxLayout>
25 #include <QVBoxLayout>
26 #include <QGridLayout>
27 #include <QPushButton>
28 #include <QLabel>
29 #include <QGroupBox>
30 #include <QSlider>
31
32 #include <cmath>
33 #include <iostream>
34
35 NbOfActiveTSDisplay::NbOfActiveTSDisplay(QWidget *parent):QLabel(parent),_nb_of_items_on(0),_total_nb_of_items(0)
36 {
37   setFont(QFont("Arial",8,QFont::Bold));
38   setTextFormat(Qt::AutoText);
39 }
40
41   void NbOfActiveTSDisplay::display(int nbOfItemsOn, int totalNbOfItems)
42 {
43   _nb_of_items_on=nbOfItemsOn;
44   _total_nb_of_items=totalNbOfItems;
45   setText(QString("%1/%2").arg(_nb_of_items_on).arg(_total_nb_of_items));
46 }
47
48 void NbOfActiveTSDisplay::totalNbOfItemsChanged(int totalNbOfItems)
49 {
50   _total_nb_of_items=totalNbOfItems;
51   setText(QString("%1/%2").arg(_nb_of_items_on).arg(_total_nb_of_items));
52 }
53
54 VectBoolItem::VectBoolItem(QObject *parent, int pos, VectBoolModel *model):QObject(parent),_pos(pos),_old_status(true),_model(model)
55 {
56 }
57
58 bool VectBoolItem::isActivated() const
59 {
60   return _model->getStatusAt(_pos);
61 }
62
63 void VectBoolItem::activated(bool val)
64 {
65   _model->setStatusAt(_pos,val);
66   _old_status=val;
67 }
68
69 void VectBoolItem::theModelHasChanged()
70 {
71   bool val(isActivated());
72   if(val!=_old_status)
73     {
74       _old_status=val;
75       emit changed();
76     }
77 }
78
79 VectBoolWidget::VectBoolWidget(QWidget *parent, int nbOfItems):QFrame(parent)
80 {
81   QSizePolicy sizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
82   sizePolicy.setHorizontalStretch(0);
83   sizePolicy.setVerticalStretch(0);
84   sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
85   this->setSizePolicy(sizePolicy);
86   this->setFrameShape(QFrame::StyledPanel);
87   this->setFrameShadow(QFrame::Raised);
88   _horizontal_layout=new QHBoxLayout(this);
89   _horizontal_layout->setSpacing(6);
90   _horizontal_layout->setContentsMargins(11,11,11,11);
91   _spreadsheet_container=new QFrame(this);
92   QSizePolicy sizePolicy1(QSizePolicy::Expanding,QSizePolicy::Expanding);
93   sizePolicy1.setHorizontalStretch(0);
94   sizePolicy1.setVerticalStretch(0);
95   sizePolicy1.setHeightForWidth(_spreadsheet_container->sizePolicy().hasHeightForWidth());
96   _spreadsheet_container->setSizePolicy(sizePolicy1);
97   _grid_layout=new QGridLayout(_spreadsheet_container);
98   _grid_layout->setSpacing(6);
99   _grid_layout->setContentsMargins(11,11,11,11);
100   _horizontal_layout->addWidget(_spreadsheet_container);
101   _group_box=new QGroupBox(this);
102   QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Expanding);
103   sizePolicy2.setHorizontalStretch(0);
104   sizePolicy2.setVerticalStretch(0);
105   sizePolicy2.setHeightForWidth(_group_box->sizePolicy().hasHeightForWidth());
106   _group_box->setSizePolicy(sizePolicy2);
107   //
108   _vertical_layout=new QVBoxLayout(_group_box);
109   _vertical_layout->setSpacing(6);
110   _vertical_layout->setContentsMargins(11,11,11,11);
111   _nb_of_time_steps_on=new NbOfActiveTSDisplay(_group_box);
112   _nb_of_time_steps_on->setMaximumSize(QSize(40,16777215));
113   _vertical_layout->addWidget(_nb_of_time_steps_on);
114   _nb_of_rows_selecter=new QSlider(_group_box);
115   QSizePolicy sizePolicy3(QSizePolicy::Expanding,QSizePolicy::Expanding);
116   sizePolicy3.setHorizontalStretch(0);
117   sizePolicy3.setVerticalStretch(0);
118   sizePolicy3.setHeightForWidth(_nb_of_rows_selecter->sizePolicy().hasHeightForWidth());
119   _nb_of_rows_selecter->setSizePolicy(sizePolicy3);
120   _nb_of_rows_selecter->setOrientation(Qt::Vertical);
121   _vertical_layout->addWidget(_nb_of_rows_selecter);
122   _sel_unsel_all=new QPushButton(_group_box);
123   _sel_unsel_all->setMaximumSize(QSize(40,16777215));
124   _sel_unsel_all->setText(QString("Sel All"));
125   QFont font("Andale Mono");
126   font.setPointSize(5);
127   _sel_unsel_all->setFont(font);
128   _vertical_layout->addWidget(_sel_unsel_all);
129   //
130   _horizontal_layout->addWidget(_group_box);
131   //
132   _vbw=new VectBoolSpreadSheet(_spreadsheet_container);
133   QSizePolicy sizePolicy4(QSizePolicy::Expanding,QSizePolicy::Expanding);
134   sizePolicy4.setHorizontalStretch(0);
135   sizePolicy4.setVerticalStretch(0);
136   sizePolicy4.setHeightForWidth(_vbw->sizePolicy().hasHeightForWidth());
137   _vbw->setSizePolicy(sizePolicy4);
138   _nb_of_rows_selecter->setSizePolicy(sizePolicy3);
139   _nb_of_rows_selecter->setMinimum(1);
140   _nb_of_rows_selecter->setMaximum(GetMaxTickValueOfSlider(nbOfItems));
141   _nb_of_rows_selecter->setTickInterval(1);
142   _nb_of_rows_selecter->setTickPosition(QSlider::TicksRight);
143   connect(_nb_of_rows_selecter,SIGNAL(valueChanged(int)),_vbw,SLOT(nbOfRowsHasChanged(int)));
144   connect(_sel_unsel_all,SIGNAL(clicked()),_vbw,SLOT(selectUnselectAllFired()));
145   _grid_layout->addWidget(_vbw);
146   _vbw->init();
147   _vbm=new VectBoolModel(nbOfItems,1);
148   _vbw->setModel(_vbm);
149   connect(this->_vbm,SIGNAL(nbOfTimeStepsOnChanged(int,int)),_nb_of_time_steps_on,SLOT(display(int,int)));
150   _vbm->selectUnselectAll();
151   _nb_of_rows_selecter->setValue(_nb_of_rows_selecter->maximum());
152   //
153   for(int i=0;i<nbOfItems;i++)
154     {
155       VectBoolItem* item = new VectBoolItem(this,i,_vbm);
156       _items["000"+QString::number(i)]=item;
157       connect(_vbm, SIGNAL(nbOfTimeStepsOnChanged(int,int)), item, SLOT(theModelHasChanged()));
158     }
159 }
160
161 void VectBoolWidget::setItems(const QStringList& dts, const QStringList& its, const QStringList& tts)
162 {
163   int curNbOfItems(dts.size());
164   if(curNbOfItems>getNumberOfBoolItems())
165     {
166       std::cerr << "ERROR ! Internal Error in VectBoolWidget::setItems ! Current number of items is bigger than the current one !" << std::endl;
167       std::cerr << curNbOfItems << " "<<getNumberOfBoolItems()<<std::endl;
168       return ;
169     }
170   if(_vbm->setCurrentItems(dts,its,tts))
171     {
172       _nb_of_rows_selecter->setMaximum(GetMaxTickValueOfSlider(curNbOfItems));
173       _nb_of_rows_selecter->setValue(_nb_of_rows_selecter->maximum());
174       _nb_of_time_steps_on->display(_vbm->getNbOfActivatedTimeSteps(),dts.size());
175     }
176 }
177
178 int VectBoolWidget::getNumberOfBoolItems() const
179 {
180   return (int)_vbm->getSize();
181 }
182
183 int VectBoolWidget::GetMaxTickValueOfSlider(int val)
184 {
185   double valf((double)val);
186   double ret(sqrt(valf));
187   int reti((int)ret);
188   if(reti*reti==val)
189     return reti;
190   else
191     return reti+1;
192 }
193
194 VectBoolWidget::~VectBoolWidget()
195 {
196   delete _vbm;
197 }