1 // Copyright (C) 2010-2014 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay
21 #include "VectBoolSpreadSheet.h"
22 #include <QTableWidgetItem>
23 #include <QHeaderView>
28 VectBoolModel::VectBoolModel(int maxSize, int nbRows):_activated(maxSize,false),_nb_rows(nbRows)
33 int VectBoolModel::rowCount(const QModelIndex &) const
38 int VectBoolModel::columnCount (const QModelIndex &) const
47 QVariant VectBoolModel::headerData(int section, Qt::Orientation orientation, int role) const
49 if(role==Qt::FontRole)
51 QFont serifFont("Arial",6, QFont::Bold);
52 return QVariant(serifFont);
54 else if(role==Qt::DisplayRole)
56 return QVariant(section);
59 return QAbstractTableModel::headerData(section,orientation,role);
62 bool VectBoolModel::setData(const QModelIndex& index, const QVariant& value, int role)
64 if(role==Qt::UserRole)
66 int pos(index.column()*_nb_rows+index.row());
67 bool v(_activated[pos]);
69 emit nbOfTimeStepsOnChanged((int)getNbOfActivatedTimeSteps(),_dts.size());
73 return QAbstractTableModel::setData(index,value,role);
76 QVariant VectBoolModel::data(const QModelIndex &index, int role) const
78 if(role==Qt::FontRole)
80 QFont serifFont("Arial",8);//, QFont::Bold);
81 return QVariant(serifFont);
83 else if(role==Qt::UserRole)
85 int pos(index.column()*_nb_rows+index.row());
86 return QVariant(_activated[pos]);
88 else if(role==Qt::DisplayRole)
90 int pos(index.column()*_nb_rows+index.row());
92 return QVariant(QString::number(pos));
96 else if(role==Qt::TextAlignmentRole)
98 return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
100 else if(role==Qt::ToolTipRole)
102 QVariant v(data(index,Qt::DisplayRole));
103 QString v2(v.toString());
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));
109 else if(role==Qt::BackgroundRole)
111 int pos(index.column()*_nb_rows+index.row());
116 QBrush b(QColor(0,255,0));
121 QBrush b(QColor(255,0,0));
129 bool VectBoolModel::setCurrentItems(const QStringList& dts, const QStringList& its, const QStringList& tts)
131 int oldSize(curSize());
132 if(oldSize!=dts.size())
134 emit layoutAboutToBeChanged();
135 _dts=dts; _its=its; _tts=tts;
136 emit layoutChanged();
141 _dts=dts; _its=its; _tts=tts;
147 void VectBoolModel::setNumberOfRows(int newNbOfRows)
149 if(newNbOfRows!=_nb_rows)
151 emit beginResetModel();
152 _nb_rows=newNbOfRows;
153 emit endResetModel();
157 void VectBoolModel::selectUnselectAll()
159 int nbOn(getNbOfActivatedTimeSteps()),sz(curSize()),signalVal(0);
160 emit layoutAboutToBeChanged();
163 for(std::size_t ii=0;ii<sz;ii++)
164 _activated[ii]=false;
169 for(std::size_t ii=0;ii<sz;ii++)
173 emit layoutChanged();
174 emit nbOfTimeStepsOnChanged(signalVal,_dts.size());
177 int VectBoolModel::getNbOfActivatedTimeSteps() const
179 int sz(curSize()),nbOn(0);
180 for(std::size_t ii=0;ii<sz;ii++)
186 void VectBoolModel::setCurSize(int sz)
188 _dts.clear(); _its.clear(); _tts.clear();
189 for(int i=0;i<sz;i++)
191 _dts.push_back(QString());
192 _its.push_back(QString());
193 _tts.push_back(QString());
197 int VectBoolModel::curSize() const
204 VectBoolSpreadSheet::VectBoolSpreadSheet(QWidget *parent):QTableView(parent),_delegate(new OnOffDelegate)
208 VectBoolSpreadSheet::~VectBoolSpreadSheet()
213 void VectBoolSpreadSheet::init()
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();
228 void VectBoolSpreadSheet::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
230 QAbstractItemModel * m(model());
231 foreach(const QModelIndex& ind,selected.indexes())
233 m->setData(ind,QVariant(true),Qt::UserRole);
235 QTableView::selectionChanged(selected,deselected);
238 void VectBoolSpreadSheet::nbOfRowsHasChanged(int newNbOfRows)
240 VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
243 zeModel->setNumberOfRows(newNbOfRows);
244 this->verticalHeader()->setUpdatesEnabled(true);//please let this line. If not a refresh problem appear at EDF configuration.
247 void VectBoolSpreadSheet::selectUnselectAllFired()
249 VectBoolModel *zeModel(qobject_cast<VectBoolModel *>(model()));
252 zeModel->selectUnselectAll();
257 OnOffDelegate::OnOffDelegate(QObject *parent):QStyledItemDelegate(parent)
261 void OnOffDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex& index) const
264 const QAbstractItemModel *zeModel(index.model());
265 QString cont(zeModel->data(index,Qt::DisplayRole).toString());
266 bool checked(zeModel->data(index,Qt::UserRole).toBool());
270 QFont zeFont(zeModel->data(index,Qt::FontRole).value<QFont>());
271 zeFont.setBold(true);
276 font=QFont("Arial",7);
277 font.setItalic(true);
279 painter->setFont(font);
280 Qt::Alignment al((Qt::Alignment)zeModel->data(index,Qt::TextAlignmentRole).toInt());
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());
292 painter->setBrush(QBrush(QColor(255,255,255)));
293 painter->drawRect(option.rect);
294 painter->setPen(Qt::lightGray);
296 painter->drawText(option.rect,cont,QTextOption(al));