Salome HOME
2e29fce922cf42171d96cbd1782e68d2befa5bfc
[modules/paravis.git] / src / Plugins / MEDReader / ParaViewPlugin / VectBoolSpreadSheet.cxx
1 // Copyright (C) 2010-2015  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 "VectBoolSpreadSheet.h"
22 #include <QTableWidgetItem>
23 #include <QHeaderView>
24 #include <QTimeEdit>
25 #include <QPainter>
26 #include <iostream>
27
28 VectBoolModel::VectBoolModel(int maxSize, int nbRows):_activated(maxSize,false),_nb_rows(nbRows)
29 {
30   setCurSize(maxSize);
31 }
32
33 int VectBoolModel::rowCount(const QModelIndex &) const
34 {
35   return _nb_rows;
36 }
37
38 int VectBoolModel::columnCount (const QModelIndex &) const
39 {
40   int sz(curSize());
41   if(sz%_nb_rows==0)
42     return sz/_nb_rows;
43   else
44     return sz/_nb_rows+1;
45 }
46
47 QVariant VectBoolModel::headerData(int section, Qt::Orientation orientation, int role) const
48 {
49   if(role==Qt::FontRole)
50     {
51       QFont serifFont("Arial",6, QFont::Bold);
52       return QVariant(serifFont);
53     }
54   else if(role==Qt::DisplayRole)
55     {
56       return QVariant(section);
57     }
58   else
59     return QAbstractTableModel::headerData(section,orientation,role);
60 }
61
62 bool VectBoolModel::setData(const QModelIndex& index, const QVariant& value, int role)
63 {
64   if(role==Qt::UserRole)
65     {
66       int pos(index.column()*_nb_rows+index.row());
67       bool v(_activated[pos]);
68       _activated[pos]=!v;
69       emit nbOfTimeStepsOnChanged((int)getNbOfActivatedTimeSteps(),_dts.size());
70       return true;
71     }
72   else
73     return QAbstractTableModel::setData(index,value,role);
74 }
75
76 QVariant VectBoolModel::data(const QModelIndex &index, int role) const
77 {
78   if(role==Qt::FontRole)
79     {
80       QFont serifFont("Arial",8);//, QFont::Bold);
81       return QVariant(serifFont);
82     }
83   else if(role==Qt::UserRole)
84     {
85       int pos(index.column()*_nb_rows+index.row());
86       return QVariant(_activated[pos]);
87     }
88   else if(role==Qt::DisplayRole)
89     {
90       int pos(index.column()*_nb_rows+index.row());
91       if(pos<curSize())
92         return QVariant(QString::number(pos));
93       else
94         return QVariant();
95     }
96   else if(role==Qt::TextAlignmentRole)
97     {
98       return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
99     }
100   else if(role==Qt::ToolTipRole)
101     {
102       QVariant v(data(index,Qt::DisplayRole));
103       QString v2(v.toString());
104       int pos(v2.toInt());
105       QString v3(_activated[pos]?QString("ON"):QString("OFF"));
106       QString v4(QString("time #%1 (it=%2 order=%3 t=%4) (%5)").arg(v2).arg(_dts[pos]).arg(_its[pos]).arg(_tts[pos]).arg(v3));
107       return QVariant(v4);
108     }
109   else if(role==Qt::BackgroundRole)
110     {
111       int pos(index.column()*_nb_rows+index.row());
112       if(pos>=curSize())
113         return QVariant();
114       if(_activated[pos])
115         {
116           QBrush b(QColor(0,255,0));
117           return QVariant(b);
118         }
119       else
120         {
121           QBrush b(QColor(255,0,0));
122           return QVariant(b);
123         }
124     }
125   else
126     return QVariant();
127 }
128
129 bool VectBoolModel::setCurrentItems(const QStringList& dts, const QStringList& its, const QStringList& tts)
130 {
131   int oldSize(curSize());
132   if(oldSize!=dts.size())
133     {
134       emit layoutAboutToBeChanged();
135       _dts=dts; _its=its; _tts=tts;
136       emit layoutChanged();
137       return true;
138     }
139   else
140     {
141       _dts=dts; _its=its; _tts=tts;
142       return false; 
143     }
144   
145 }
146
147 void VectBoolModel::setNumberOfRows(int newNbOfRows)
148 {
149   if(newNbOfRows!=_nb_rows)
150     {
151       emit beginResetModel();
152       _nb_rows=newNbOfRows;
153       emit endResetModel();
154     }
155 }
156
157 void VectBoolModel::selectUnselectAll()
158 {
159   int nbOn(getNbOfActivatedTimeSteps()),sz(curSize()),signalVal(0);
160   emit layoutAboutToBeChanged();
161   if(nbOn>sz/2)
162     {
163       for(std::size_t ii=0;ii<sz;ii++)
164         _activated[ii]=false;
165       signalVal=0;
166     }
167   else
168     {
169       for(std::size_t ii=0;ii<sz;ii++)
170         _activated[ii]=true;
171       signalVal=(int)sz;
172     }
173   emit layoutChanged();
174   emit nbOfTimeStepsOnChanged(signalVal,_dts.size());
175 }
176
177 int VectBoolModel::getNbOfActivatedTimeSteps() const
178 {
179   int sz(curSize()),nbOn(0);
180   for(std::size_t ii=0;ii<sz;ii++)
181     if(_activated[ii])
182       nbOn++;
183   return nbOn;
184 }
185
186 void VectBoolModel::setCurSize(int sz)
187 {
188   _dts.clear(); _its.clear(); _tts.clear();
189   for(int i=0;i<sz;i++)
190     {
191       _dts.push_back(QString());
192       _its.push_back(QString());
193       _tts.push_back(QString());
194     }
195 }
196
197 int VectBoolModel::curSize() const
198 {
199   return _dts.size();
200 }
201
202 ///////////////
203
204 VectBoolSpreadSheet::VectBoolSpreadSheet(QWidget *parent):QTableView(parent),_delegate(new OnOffDelegate)
205 {
206 }
207
208 VectBoolSpreadSheet::~VectBoolSpreadSheet()
209 {
210   delete _delegate;
211 }
212
213 void VectBoolSpreadSheet::init()
214 {
215   this->horizontalHeader()->setMinimumSectionSize(2);
216   this->horizontalHeader()->setDefaultSectionSize(2);
217   this->verticalHeader()->setMinimumSectionSize(2);
218   this->verticalHeader()->setDefaultSectionSize(2);
219   this->setItemDelegate(_delegate);
220   this->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
221   this->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
222   this->resizeColumnsToContents();
223   this->resizeRowsToContents();
224   //this->verticalHeader()->hide();
225   //this->horizontalHeader()->hide();
226 }
227
228 void VectBoolSpreadSheet::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
229 {
230   QAbstractItemModel * m(model());
231   foreach(const QModelIndex& ind,selected.indexes())
232     {
233       m->setData(ind,QVariant(true),Qt::UserRole);
234     }
235   QTableView::selectionChanged(selected,deselected);
236 }
237
238 void VectBoolSpreadSheet::nbOfRowsHasChanged(int newNbOfRows)
239 {
240   VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
241   if(!zeModel)
242     return ;
243   zeModel->setNumberOfRows(newNbOfRows);
244   this->verticalHeader()->setUpdatesEnabled(true);//please let this line. If not a refresh problem appear at EDF configuration.
245 }
246
247 void VectBoolSpreadSheet::selectUnselectAllFired()
248 {
249   VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
250   if(!zeModel)
251     return ;
252   zeModel->selectUnselectAll();
253 }
254
255 ///////////////
256
257 OnOffDelegate::OnOffDelegate(QObject *parent):QStyledItemDelegate(parent)
258 {
259 }
260
261 void OnOffDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex& index) const
262 {
263   painter->save();
264   const QAbstractItemModel *zeModel(index.model());
265   QString cont(zeModel->data(index,Qt::DisplayRole).toString());
266   bool checked(zeModel->data(index,Qt::UserRole).toBool());
267   QFont font;
268   if(checked)
269     {
270       QFont zeFont(zeModel->data(index,Qt::FontRole).value<QFont>());
271       zeFont.setBold(true);
272       font=zeFont;
273     }
274   else
275     {
276       font=QFont("Arial",7);
277       font.setItalic(true);
278     }
279   painter->setFont(font);
280   Qt::Alignment al((Qt::Alignment)zeModel->data(index,Qt::TextAlignmentRole).toInt());
281   if(checked)
282     {//sizeHint
283       //painter->drawEllipse(option.rect);
284       //painter->setBrush(QBrush(Qt::lightGray,Qt::Dense6Pattern));
285       painter->setBrush(QBrush(QColor(230,230,255)));
286       painter->drawRect(option.rect);
287       //painter->drawLine(option.rect.topLeft(),option.rect.bottomRight());
288       //painter->drawLine(option.rect.topRight(),option.rect.bottomLeft());
289     }
290   else
291     {
292       painter->setBrush(QBrush(QColor(255,255,255)));
293       painter->drawRect(option.rect);
294       painter->setPen(Qt::lightGray);
295     }
296   painter->drawText(option.rect,cont,QTextOption(al));
297   painter->restore();
298 }