MNE-CPP  beta 1.0
frequencyspectrummodel.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 #include "frequencyspectrummodel.h"
38 
39 #include <QDebug>
40 #include <QBrush>
41 #include <QThread>
42 
43 
44 //*************************************************************************************************************
45 //=============================================================================================================
46 // USED NAMESPACES
47 //=============================================================================================================
48 
49 using namespace XDISPLIB;
50 
51 
52 //*************************************************************************************************************
53 //=============================================================================================================
54 // DEFINE MEMBER METHODS
55 //=============================================================================================================
56 
58 : QAbstractTableModel(parent)
59 , m_fSps(1024.0f)
60 , m_iT(10)
61 , m_bIsFreezed(false)
62 , m_bInitialized(false)
63 , m_iLowerFrqIdx(0)
64 , m_iUpperFrqIdx(0)
65 , m_iScaleType(0)
66 {
67 }
68 
69 
70 //*************************************************************************************************************
71 //virtual functions
72 int FrequencySpectrumModel::rowCount(const QModelIndex & /*parent*/) const
73 {
74  if(!m_qMapIdxRowSelection.empty())
75  return m_qMapIdxRowSelection.size();
76  else
77  return 0;
78 }
79 
80 
81 //*************************************************************************************************************
82 
83 int FrequencySpectrumModel::columnCount(const QModelIndex & /*parent*/) const
84 {
85  return 2;
86 }
87 
88 
89 //*************************************************************************************************************
90 
91 QVariant FrequencySpectrumModel::data(const QModelIndex &index, int role) const
92 {
93  if(role != Qt::DisplayRole && role != Qt::BackgroundRole)
94  return QVariant();
95 
96  if (index.isValid()) {
97  qint32 r = m_qMapIdxRowSelection[index.row()];
98 
99  //******** first column (chname) ********
100  if(index.column() == 0 && role == Qt::DisplayRole)
101  if(m_pFiffInfo)
102  return QVariant(m_pFiffInfo->chs[r].ch_name);
103 
104 
105  //******** second column (data plot) ********
106  if(index.column()==1) {
107  QVariant v;
108 
109  switch(role) {
110  case Qt::DisplayRole: {
111  //pack all adjacent (after reload) RowVectorPairs into a QList
112  RowVectorXd vec;
113 
114  if(m_bIsFreezed)
115  {
116  // data freeze
117  vec = m_dataCurrentFreeze.row(r);
118  v.setValue(vec);
119  }
120  else
121  {
122  // data
123  vec = m_dataCurrent.row(r);
124  v.setValue(vec);
125  }
126  return v;
127  break;
128  }
129  case Qt::BackgroundRole: {
130 // if(m_fiffInfo.bads.contains(m_chInfolist[row].ch_name)) {
131 // QBrush brush;
132 // brush.setStyle(Qt::SolidPattern);
133 // // qDebug() << m_chInfolist[row].ch_name << "is marked as bad, index:" << row;
134 // brush.setColor(Qt::red);
135 // return QVariant(brush);
136 // }
137 // else
138  return QVariant();
139 
140  break;
141  }
142  } // end role switch
143  } // end column check
144 
145  } // end index.valid() check
146 
147  return QVariant();
148 }
149 
150 
151 //*************************************************************************************************************
152 
153 QVariant FrequencySpectrumModel::headerData(int section, Qt::Orientation orientation, int role) const
154 {
155  if(role != Qt::DisplayRole && role != Qt::TextAlignmentRole)
156  return QVariant();
157 
158  if(orientation == Qt::Horizontal) {
159  switch(section) {
160  case 0: //chname column
161  return QVariant();
162  case 1: //data plot column
163  return QVariant("data plot");
164  switch(role) {
165  case Qt::DisplayRole:
166  return QVariant("data plot");
167  case Qt::TextAlignmentRole:
168  return QVariant(Qt::AlignLeft);
169  }
170  }
171  }
172  else if(orientation == Qt::Vertical) {
173  QModelIndex chname = createIndex(section,0);
174  switch(role) {
175  case Qt::DisplayRole:
176  return QVariant(data(chname).toString());
177  }
178  }
179 
180  return QVariant();
181 }
182 
183 
184 //*************************************************************************************************************
185 
187 {
188  beginResetModel();
189  m_pFiffInfo = info;
190  endResetModel();
191 
192  resetSelection();
193 }
194 
195 
196 //*************************************************************************************************************
197 
199 {
200  m_iScaleType = ScaleType;
201 }
202 
203 //*************************************************************************************************************
204 
205 void FrequencySpectrumModel::addData(const MatrixXd &data)
206 {
207  m_dataCurrent = data;
208 
209  if(m_vecFreqScale.size() != m_dataCurrent.cols() && m_pFiffInfo)
210  {
211  double freqRes = (m_pFiffInfo->sfreq/2) / m_dataCurrent.cols();
212  double k = 1.0;
213  m_vecFreqScale.resize(1,m_dataCurrent.cols());
214 
215  double currFreq = 0;
216  for(qint32 i = 0; i < m_dataCurrent.cols(); ++i)
217  {
218  if (m_iScaleType) //log
219  m_vecFreqScale[i] = log10(currFreq+k);
220  else // normal
221  m_vecFreqScale[i] = currFreq;
222 
223  currFreq += freqRes;
224  }
225 
226  double max = m_vecFreqScale.maxCoeff();
227  m_vecFreqScale /= max;
228 
229  m_vecFreqScaleBound = m_vecFreqScale;
230  m_iLowerFrqIdx = 0;
231  m_iUpperFrqIdx = m_vecFreqScale.size()-1;
232 
233 
234  m_bInitialized = true;
235  }
236 
237  //Update data content
238  QModelIndex topLeft = this->index(0,1);
239  QModelIndex bottomRight = this->index(m_dataCurrent.rows()-1,1);
240  QVector<int> roles; roles << Qt::DisplayRole;
241  emit dataChanged(topLeft, bottomRight, roles);
242 }
243 
244 
245 //*************************************************************************************************************
246 
247 void FrequencySpectrumModel::selectRows(const QList<qint32> &selection)
248 {
249  beginResetModel();
250 
251  m_qMapIdxRowSelection.clear();
252 
253  qint32 count = 0;
254  for(qint32 i = 0; i < selection.size(); ++i)
255  {
256  if(selection[i] < m_pFiffInfo->chs.size())
257  {
258  m_qMapIdxRowSelection.insert(count,selection[i]);
259  ++count;
260  }
261  }
262 
263  emit newSelection(selection);
264 
265  endResetModel();
266 }
267 
268 
269 //*************************************************************************************************************
270 
272 {
273  beginResetModel();
274 
275  m_qMapIdxRowSelection.clear();
276 
277  for(qint32 i = 0; i < m_pFiffInfo->chs.size(); ++i)
278  m_qMapIdxRowSelection.insert(i,i);
279 
280  endResetModel();
281 }
282 
283 
284 //*************************************************************************************************************
285 
286 void FrequencySpectrumModel::toggleFreeze(const QModelIndex &)
287 {
288  m_bIsFreezed = !m_bIsFreezed;
289 
290  if(m_bIsFreezed)
291  m_dataCurrentFreeze = m_dataCurrent;
292 
293  //Update data content
294  QModelIndex topLeft = this->index(0,1);
295  QModelIndex bottomRight = this->index(m_dataCurrent.rows()-1,1);
296  QVector<int> roles; roles << Qt::DisplayRole;
297  emit dataChanged(topLeft, bottomRight, roles);
298 }
299 
300 
301 //*************************************************************************************************************
302 
303 void FrequencySpectrumModel::setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
304 {
305  if(!m_bInitialized)
306  return;
307 
308  beginResetModel();
309 
310  double nf = m_pFiffInfo->sfreq/2;
311 
312  m_iLowerFrqIdx = 0;
313  m_iUpperFrqIdx = m_vecFreqScale.size()-1;
314 
315  //find boundaries
316  for(qint32 i = 0; i < m_vecFreqScale.size(); ++i)
317  {
318  float val = m_vecFreqScale[i]*nf;
319  if( val < fLowerFrqBound)
320  m_iLowerFrqIdx = i;
321 
322  if( val > fUpperFrqBound)
323  {
324  m_iUpperFrqIdx = i;
325  break;
326  }
327  }
328 
329  // scale it new
330  m_vecFreqScaleBound = m_vecFreqScale;
331  for(qint32 i = 0; i < m_vecFreqScaleBound.size(); ++i)
332  m_vecFreqScaleBound[i] = (m_vecFreqScaleBound[i] - m_vecFreqScale[m_iLowerFrqIdx]) / (m_vecFreqScale[m_iUpperFrqIdx] - m_vecFreqScale[m_iLowerFrqIdx]);
333 
334  endResetModel();
335 }
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
void newSelection(QList< qint32 > selection)
void selectRows(const QList< qint32 > &selection)
Declaration of the FrequencySpectrumModel Class.
QSharedPointer< FiffInfo > SPtr
Definition: fiff_info.h:99
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
void setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
void toggleFreeze(const QModelIndex &index)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const