Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / ParaViewPlugin / VectBoolSpreadSheet.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 "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(int ii=0;ii<sz;ii++)
164         _activated[ii]=false;
165       signalVal=0;
166     }
167   else
168     {
169       for(int 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(int 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 (int)_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()->setSectionResizeMode(QHeaderView::ResizeToContents);
221   this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
222
223   this->resizeColumnsToContents();
224   this->resizeRowsToContents();
225   //this->verticalHeader()->hide();
226   //this->horizontalHeader()->hide();
227 }
228
229 void VectBoolSpreadSheet::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
230 {
231   QAbstractItemModel * m(model());
232   foreach(const QModelIndex& ind,selected.indexes())
233     {
234       m->setData(ind,QVariant(true),Qt::UserRole);
235     }
236   QTableView::selectionChanged(selected,deselected);
237   this->clearSelection();
238 }
239
240 void VectBoolSpreadSheet::nbOfRowsHasChanged(int newNbOfRows)
241 {
242   VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
243   if(!zeModel)
244     return ;
245   zeModel->setNumberOfRows(newNbOfRows);
246   this->verticalHeader()->setUpdatesEnabled(true);//please let this line. If not a refresh problem appear at EDF configuration.
247 }
248
249 void VectBoolSpreadSheet::selectUnselectAllFired()
250 {
251   VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
252   if(!zeModel)
253     return ;
254   zeModel->selectUnselectAll();
255 }
256
257 ///////////////
258
259 OnOffDelegate::OnOffDelegate(QObject *parent):QStyledItemDelegate(parent)
260 {
261 }
262
263 void OnOffDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex& index) const
264 {
265   painter->save();
266   const QAbstractItemModel *zeModel(index.model());
267   QString cont(zeModel->data(index,Qt::DisplayRole).toString());
268   bool checked(zeModel->data(index,Qt::UserRole).toBool());
269   QFont font;
270   if(checked)
271     {
272       QFont zeFont(zeModel->data(index,Qt::FontRole).value<QFont>());
273       zeFont.setBold(true);
274       font=zeFont;
275     }
276   else
277     {
278       font=QFont("Arial",7);
279       font.setItalic(true);
280     }
281   painter->setFont(font);
282   Qt::Alignment al((Qt::Alignment)zeModel->data(index,Qt::TextAlignmentRole).toInt());
283   if(checked)
284     {//sizeHint
285       //painter->drawEllipse(option.rect);
286       //painter->setBrush(QBrush(Qt::lightGray,Qt::Dense6Pattern));
287       painter->setBrush(QBrush(QColor(230,230,255)));
288       painter->drawRect(option.rect);
289       //painter->drawLine(option.rect.topLeft(),option.rect.bottomRight());
290       //painter->drawLine(option.rect.topRight(),option.rect.bottomLeft());
291     }
292   else
293     {
294       painter->setBrush(QBrush(QColor(255,255,255)));
295       painter->drawRect(option.rect);
296       painter->setPen(Qt::lightGray);
297     }
298   painter->drawText(option.rect,cont,QTextOption(al));
299   painter->restore();
300 }