MNE-CPP  beta 1.0
projectionmodel.cpp
Go to the documentation of this file.
1 //=============================================================================================================
38 //*************************************************************************************************************
39 //=============================================================================================================
40 // INCLUDES
41 //=============================================================================================================
42 
43 #include "projectionmodel.h"
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // USED NAMESPACES
49 //=============================================================================================================
50 
51 using namespace MNEBrowseRawQt;
52 
53 
54 //*************************************************************************************************************
55 //=============================================================================================================
56 // DEFINE MEMBER METHODS
57 //=============================================================================================================
58 
59 ProjectionModel::ProjectionModel(QObject *parent)
60 : QAbstractTableModel(parent)
61 , m_bFileloaded(false)
62 {
63 }
64 
65 
66 //*************************************************************************************************************
67 
68 ProjectionModel::ProjectionModel(QObject *parent, QFile& qFile)
69 : QAbstractTableModel(parent)
70 , m_bFileloaded(false)
71 {
72  //read projections from fif file
73  loadProjections(qFile);
74 }
75 
76 
77 //*************************************************************************************************************
78 
79 ProjectionModel::ProjectionModel(QObject *parent, QList<FiffProj>& dataProjs)
80 : QAbstractTableModel(parent)
81 , m_bFileloaded(false)
82 {
83  addProjections(dataProjs);
84 }
85 
86 
87 //*************************************************************************************************************
88 //virtual functions
89 int ProjectionModel::rowCount(const QModelIndex & /*parent*/) const
90 {
91  //Return number of stored evoked sets
92  if(m_dataProjs.size()!=0)
93  return m_dataProjs.size();
94  else
95  return 0;
96 }
97 
98 
99 //*************************************************************************************************************
100 
101 int ProjectionModel::columnCount(const QModelIndex & /*parent*/) const
102 {
103  return 3;
104 }
105 
106 
107 //*************************************************************************************************************
108 
109 QVariant ProjectionModel::headerData(int section, Qt::Orientation orientation, int role) const
110 {
111  if(role != Qt::DisplayRole && role != Qt::TextAlignmentRole)
112  return QVariant();
113 
114  //Return the number and description/comment of the fiff evoked data in the set as vertical header
115  if(orientation == Qt::Vertical) {
116  if(section<m_dataProjs.size())
117  return QString("%1").arg(section);
118  }
119 
120  if(role==Qt::TextAlignmentRole)
121  return Qt::AlignHCenter + Qt::AlignVCenter;
122 
123  //Return the horizontal header
124  if(orientation == Qt::Horizontal) {
125  switch(section) {
126  case 0:
127  return QString("%1").arg("Name");
128  break;
129 
130  case 1:
131  return QString("%1").arg("State");
132  break;
133 
134  case 2:
135  return QString("%1").arg("Dimension");
136  break;
137 
138  case 3:
139  return QString("%1").arg("Data");
140  break;
141  }
142  }
143 
144  return QVariant();
145 }
146 
147 
148 //*************************************************************************************************************
149 
150 QVariant ProjectionModel::data(const QModelIndex &index, int role) const
151 {
152  if(index.row() >= m_dataProjs.size())
153  return QVariant();
154 
155  if (index.isValid()) {
156  //******** first column (projection name) ********
157  if(index.column()==0) {
158  QVariant v;
159 
160  switch(role) {
161  case Qt::DisplayRole:
162  v.setValue(QString("%1").arg(m_dataProjs.at(index.row()).desc));
163  return v;
164  break;
165 
166  case ProjectionModelRoles::GetProjectionName:
167  v.setValue(m_dataProjs.at(index.row()).desc);
168  return v;
169  break;
170 
171  case Qt::TextAlignmentRole:
172  return Qt::AlignHCenter + Qt::AlignVCenter;
173  }
174  }//end column check
175 
176  //******** second column (project state - active or inactive) ********
177  if(index.column()==1) {
178  QVariant v;
179 
180  switch(role) {
181  case Qt::DisplayRole:
182  if(m_dataProjs.at(index.row()).active)
183  v.setValue(QString("%1").arg("Active"));
184  else
185  v.setValue(QString("%1").arg("Inactive"));
186  return v;
187  break;
188 
189  case ProjectionModelRoles::GetProjectionState:
190  v.setValue(m_dataProjs.at(index.row()).active);
191  return v;
192  break;
193 
194  case Qt::TextAlignmentRole:
195  return Qt::AlignHCenter + Qt::AlignVCenter;
196  }
197  }//end column check
198 
199  //******** third column (projection data size / dimensions) ********
200  if(index.column()==2) {
201  QVariant v;
202 
203  switch(role) {
204  case Qt::DisplayRole:
205  v.setValue(QString("(%1|%2)").arg(m_dataProjs.at(index.row()).data->data.rows()).arg(m_dataProjs.at(index.row()).data->data.cols()));
206  return v;
207  break;
208 
209  case ProjectionModelRoles::GetProjectionDimension:
210  v.setValue(QPair<int,int>(m_dataProjs.at(index.row()).data->data.rows(), m_dataProjs.at(index.row()).data->data.cols()));
211  return v;
212  break;
213 
214  case Qt::TextAlignmentRole:
215  return Qt::AlignHCenter + Qt::AlignVCenter;
216  }
217  }//end column check
218 
219  //******** fourth column (projection data) ********
220  if(index.column()==3) {
221  QVariant v;
222 
223  switch(role) {
224  case ProjectionModelRoles::GetProjectionData:
225  v.setValue(m_dataProjs.at(index.row()).desc);
226  return v;
227  break;
228 
229  case Qt::TextAlignmentRole:
230  return Qt::AlignHCenter + Qt::AlignVCenter;
231  }
232  }//end column check
233  } // end index.valid() check
234 
235  return QVariant();
236 }
237 
238 
239 //*************************************************************************************************************
240 
241 bool ProjectionModel::insertRows(int position, int span, const QModelIndex & parent)
242 {
243  Q_UNUSED(position);
244  Q_UNUSED(span);
245  Q_UNUSED(parent);
246 
247  return true;
248 }
249 
250 
251 //*************************************************************************************************************
252 
253 bool ProjectionModel::removeRows(int position, int span, const QModelIndex & parent)
254 {
255  Q_UNUSED(position);
256  Q_UNUSED(span);
257  Q_UNUSED(parent);
258 
259  return true;
260 }
261 
262 
263 //*************************************************************************************************************
264 
265 Qt::ItemFlags ProjectionModel::flags(const QModelIndex & index) const
266 {
267  Q_UNUSED(index);
268  return Qt::ItemIsEnabled | Qt::ItemIsSelectable /*| Qt::ItemIsEditable*/;
269 }
270 
271 
272 //*************************************************************************************************************
273 
274 bool ProjectionModel::setData(const QModelIndex & index, const QVariant & value, int role)
275 {
276  Q_UNUSED(index);
277  Q_UNUSED(value);
278  Q_UNUSED(role);
279 
280  return true;
281 }
282 
283 
284 //*************************************************************************************************************
285 
287 {
288  beginResetModel();
289  clearModel();
290 
291  //
292  // Open the file
293  //
294  FiffStream::SPtr t_pStream(new FiffStream(&qFile));
295  QString t_sFileName = t_pStream->streamName();
296 
297  qDebug()<<"Opening header data %s...\n"<<t_sFileName.toUtf8().constData();
298 
299  FiffDirTree t_Tree;
300  QList<FiffDirEntry> t_Dir;
301 
302  if(!t_pStream->open(t_Tree, t_Dir))
303  return false;
304 
305  QList<FiffProj> q_ListProj = t_pStream->read_proj(t_Tree);
306 
307  if (q_ListProj.size() == 0)
308  {
309  qDebug()<<"Could not find projectors\n";
310  return false;
311  }
312 
313  m_dataProjs.append(q_ListProj);
314 
315  //garbage collecting
316  t_pStream->device()->close();
317 
318  endResetModel();
319 
320  emit fileLoaded(true);
321  emit dataChanged(createIndex(0,0), createIndex(rowCount(),columnCount()));
322  emit headerDataChanged(Qt::Vertical, 0, rowCount());
323 
324  return true;
325 }
326 
327 
328 //*************************************************************************************************************
329 
331 {
332  Q_UNUSED(qFile);
333 
334  beginResetModel();
335  clearModel();
336 
337  //TODO: Save projections to file
338 
339  endResetModel();
340  return true;
341 }
342 
343 
344 //*************************************************************************************************************
345 
346 void ProjectionModel::addProjections(const QList<FiffProj>& dataProjs)
347 {
348  m_dataProjs.append(dataProjs);
349 
350  emit dataChanged(createIndex(0,0), createIndex(rowCount(),columnCount()));
351  emit headerDataChanged(Qt::Vertical, 0, rowCount());
352 }
353 
354 
355 //*************************************************************************************************************
356 
358 {
359  m_dataProjs.append(fiffInfo.projs);
360 
361  emit dataChanged(createIndex(0,0), createIndex(rowCount(),columnCount()));
362  emit headerDataChanged(Qt::Vertical, 0, rowCount());
363 }
364 
365 
366 //*************************************************************************************************************
367 
369 {
370  beginResetModel();
371 
372  m_dataProjs.clear();
373 
374  m_bFileloaded = false;
375 
376  endResetModel();
377 
378  qDebug("ProjectionModel cleared.");
379 }
FIFF measurement file information.
Definition: fiff_info.h:96
QList< FiffProj > projs
Definition: fiff_info.h:266
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
QSharedPointer< FiffStream > SPtr
Definition: fiff_stream.h:132
Directory tree structure.
Definition: fiff_dir_tree.h:80
void addProjections(const QList< FiffProj > &dataProjs)
This class represents the projection model of the model/view framework of mne_browse_raw_qt applicati...
FIFF File I/O routines.
Definition: fiff_stream.h:129