Salome HOME
Merge from agy/feedbackads140304
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / VectBoolWidget.cxx
1 // Copyright (C) 2010-2014  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 }
67
68 void VectBoolItem::theModelHasChanged()
69 {
70   bool val(isActivated());
71   if(val!=_old_status)
72     {
73       _old_status=val;
74       emit changed();
75     }
76 }
77
78 VectBoolWidget::VectBoolWidget(QWidget *parent, int nbOfItems):QFrame(parent)
79 {
80   QSizePolicy sizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
81   sizePolicy.setHorizontalStretch(0);
82   sizePolicy.setVerticalStretch(0);
83   sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
84   this->setSizePolicy(sizePolicy);
85   this->setFrameShape(QFrame::StyledPanel);
86   this->setFrameShadow(QFrame::Raised);
87   _horizontal_layout=new QHBoxLayout(this);
88   _horizontal_layout->setSpacing(6);
89   _horizontal_layout->setContentsMargins(11,11,11,11);
90   _spreadsheet_container=new QFrame(this);
91   QSizePolicy sizePolicy1(QSizePolicy::Expanding,QSizePolicy::Expanding);
92   sizePolicy1.setHorizontalStretch(0);
93   sizePolicy1.setVerticalStretch(0);
94   sizePolicy1.setHeightForWidth(_spreadsheet_container->sizePolicy().hasHeightForWidth());
95   _spreadsheet_container->setSizePolicy(sizePolicy1);
96   _grid_layout=new QGridLayout(_spreadsheet_container);
97   _grid_layout->setSpacing(6);
98   _grid_layout->setContentsMargins(11,11,11,11);
99   _horizontal_layout->addWidget(_spreadsheet_container);
100   _group_box=new QGroupBox(this);
101   QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Expanding);
102   sizePolicy2.setHorizontalStretch(0);
103   sizePolicy2.setVerticalStretch(0);
104   sizePolicy2.setHeightForWidth(_group_box->sizePolicy().hasHeightForWidth());
105   _group_box->setSizePolicy(sizePolicy2);
106   //
107   _vertical_layout=new QVBoxLayout(_group_box);
108   _vertical_layout->setSpacing(6);
109   _vertical_layout->setContentsMargins(11,11,11,11);
110   _nb_of_time_steps_on=new NbOfActiveTSDisplay(_group_box);
111   _nb_of_time_steps_on->setMaximumSize(QSize(40,16777215));
112   _vertical_layout->addWidget(_nb_of_time_steps_on);
113   _nb_of_rows_selecter=new QSlider(_group_box);
114   QSizePolicy sizePolicy3(QSizePolicy::Expanding,QSizePolicy::Expanding);
115   sizePolicy3.setHorizontalStretch(0);
116   sizePolicy3.setVerticalStretch(0);
117   sizePolicy3.setHeightForWidth(_nb_of_rows_selecter->sizePolicy().hasHeightForWidth());
118   _nb_of_rows_selecter->setSizePolicy(sizePolicy3);
119   _nb_of_rows_selecter->setOrientation(Qt::Vertical);
120   _vertical_layout->addWidget(_nb_of_rows_selecter);
121   _sel_unsel_all=new QPushButton(_group_box);
122   _sel_unsel_all->setMaximumSize(QSize(40,16777215));
123   _sel_unsel_all->setText(QString("Sel All"));
124   QFont font("Andale Mono");
125   font.setPointSize(5);
126   _sel_unsel_all->setFont(font);
127   _vertical_layout->addWidget(_sel_unsel_all);
128   //
129   _horizontal_layout->addWidget(_group_box);
130   //
131   _vbw=new VectBoolSpreadSheet(_spreadsheet_container);
132   QSizePolicy sizePolicy4(QSizePolicy::Expanding,QSizePolicy::Expanding);
133   sizePolicy4.setHorizontalStretch(0);
134   sizePolicy4.setVerticalStretch(0);
135   sizePolicy4.setHeightForWidth(_vbw->sizePolicy().hasHeightForWidth());
136   _vbw->setSizePolicy(sizePolicy4);
137   _nb_of_rows_selecter->setSizePolicy(sizePolicy3);
138   _nb_of_rows_selecter->setMinimum(1);
139   _nb_of_rows_selecter->setMaximum(GetMaxTickValueOfSlider(nbOfItems));
140   _nb_of_rows_selecter->setTickInterval(1);
141   _nb_of_rows_selecter->setTickPosition(QSlider::TicksRight);
142   connect(_nb_of_rows_selecter,SIGNAL(valueChanged(int)),_vbw,SLOT(nbOfRowsHasChanged(int)));
143   connect(_sel_unsel_all,SIGNAL(clicked()),_vbw,SLOT(selectUnselectAllFired()));
144   _grid_layout->addWidget(_vbw);
145   _vbw->init();
146   _vbm=new VectBoolModel(nbOfItems,1);
147   _vbw->setModel(_vbm);
148   connect(this->_vbm,SIGNAL(nbOfTimeStepsOnChanged(int,int)),_nb_of_time_steps_on,SLOT(display(int,int)));
149   _vbm->selectUnselectAll();
150   _nb_of_rows_selecter->setValue(_nb_of_rows_selecter->maximum());
151   //
152   _items.resize(nbOfItems);
153   for(int i=0;i<nbOfItems;i++)
154     {
155       _items[i]=new VectBoolItem(this,i,_vbm);
156       connect(_vbm,SIGNAL(nbOfTimeStepsOnChanged(int,int)),_items[i],SLOT(theModelHasChanged()));
157     }
158 }
159
160 void VectBoolWidget::setItems(const QStringList& dts, const QStringList& its, const QStringList& tts)
161 {
162   int curNbOfItems(dts.size());
163   if(curNbOfItems>getNumberOfBoolItems())
164     {
165       std::cerr << "ERROR ! Internal Error in VectBoolWidget::setItems ! Current number of items is bigger than the current one !" << std::endl;
166       return ;
167     }
168   if(_vbm->setCurrentItems(dts,its,tts))
169     {
170       _nb_of_rows_selecter->setMaximum(GetMaxTickValueOfSlider(curNbOfItems));
171       _nb_of_rows_selecter->setValue(_nb_of_rows_selecter->maximum());
172       _nb_of_time_steps_on->display(_vbm->getNbOfActivatedTimeSteps(),dts.size());
173     }
174 }
175
176 int VectBoolWidget::getNumberOfBoolItems() const
177 {
178   return (int)_vbm->getSize();
179 }
180
181 int VectBoolWidget::GetMaxTickValueOfSlider(int val)
182 {
183   double valf((double)val);
184   double ret(sqrt(valf));
185   int reti((int)ret);
186   if(reti*reti==val)
187     return reti;
188   else
189     return reti+1;
190 }
191
192 VectBoolWidget::~VectBoolWidget()
193 {
194   delete _vbm;
195 }