v2.0.0
Loading...
Searching...
No Matches
frequencyspectrummodel.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
20
21#include <fiff/fiff_info.h>
22
23//=============================================================================================================
24// QT INCLUDES
25//=============================================================================================================
26
27//=============================================================================================================
28// EIGEN INCLUDES
29//=============================================================================================================
30
31//=============================================================================================================
32// USED NAMESPACES
33//=============================================================================================================
34
35using namespace DISPLIB;
36using namespace FIFFLIB;
37using namespace Eigen;
38
39//=============================================================================================================
40// DEFINE MEMBER METHODS
41//=============================================================================================================
42
44: QAbstractTableModel(parent)
45, m_fSps(1024.0f)
46, m_iT(10)
47, m_bIsFreezed(false)
48, m_bInitialized(false)
49, m_iLowerFrqIdx(0)
50, m_iUpperFrqIdx(0)
51, m_iScaleType(0)
52{
53}
54
55//=============================================================================================================
56
57int FrequencySpectrumModel::rowCount(const QModelIndex & /*parent*/) const
58{
59 if(!m_qMapIdxRowSelection.empty())
60 return m_qMapIdxRowSelection.size();
61 else
62 return 0;
63}
64
65//=============================================================================================================
66
67int FrequencySpectrumModel::columnCount(const QModelIndex & /*parent*/) const
68{
69 return 2;
70}
71
72//=============================================================================================================
73
74QVariant FrequencySpectrumModel::data(const QModelIndex &index, int role) const
75{
76 if(role != Qt::DisplayRole && role != Qt::BackgroundRole)
77 return QVariant();
78
79 if (index.isValid()) {
80 qint32 r = m_qMapIdxRowSelection[index.row()];
81
82 //******** first column (chname) ********
83 if(index.column() == 0 && role == Qt::DisplayRole)
84 if(m_pFiffInfo)
85 return QVariant(m_pFiffInfo->chs[r].ch_name);
86
87 //******** second column (data plot) ********
88 if(index.column()==1) {
89 QVariant v;
90
91 switch(role) {
92 case Qt::DisplayRole: {
93 //pack all adjacent (after reload) RowVectorPairs into a QList
94 RowVectorXd vec;
95
96 if(m_bIsFreezed)
97 {
98 // data freeze
99 vec = m_dataCurrentFreeze.row(r);
100 v.setValue(vec);
101 }
102 else
103 {
104 // data
105 vec = m_dataCurrent.row(r);
106 v.setValue(vec);
107 }
108 return v;
109 break;
110 }
111 case Qt::BackgroundRole: {
112// if(m_fiffInfo.bads.contains(m_chInfolist[row].ch_name)) {
113// QBrush brush;
114// brush.setStyle(Qt::SolidPattern);
115// // qDebug() << m_chInfolist[row].ch_name << "is marked as bad, index:" << row;
116// brush.setColor(Qt::red);
117// return QVariant(brush);
118// }
119// else
120 return QVariant();
121
122 break;
123 }
124 } // end role switch
125 } // end column check
126
127 } // end index.valid() check
128
129 return QVariant();
130}
131
132//=============================================================================================================
133
134QVariant FrequencySpectrumModel::headerData(int section, Qt::Orientation orientation, int role) const
135{
136 if(role != Qt::DisplayRole && role != Qt::TextAlignmentRole)
137 return QVariant();
138
139 if(orientation == Qt::Horizontal) {
140 switch(section) {
141 case 0: //chname column
142 return QVariant();
143 case 1: //data plot column
144 switch(role) {
145 case Qt::DisplayRole:
146 return QVariant("data plot");
147 case Qt::TextAlignmentRole:
148 return QVariant(Qt::AlignLeft);
149 }
150 return QVariant("data plot");
151 }
152 }
153 else if(orientation == Qt::Vertical) {
154 QModelIndex chname = createIndex(section,0);
155 switch(role) {
156 case Qt::DisplayRole:
157 return QVariant(data(chname).toString());
158 }
159 }
160
161 return QVariant();
162}
163
164//=============================================================================================================
165
167{
168 beginResetModel();
169 m_pFiffInfo = info;
170 endResetModel();
171
173}
174
175//=============================================================================================================
176
178{
179 m_iScaleType = ScaleType;
180}
181
182//=============================================================================================================
183
185{
186 m_dataCurrent = data;
187
188 if(m_vecFreqScale.size() != m_dataCurrent.cols() && m_pFiffInfo)
189 {
190 double freqRes = (m_pFiffInfo->sfreq/2) / m_dataCurrent.cols();
191 double k = 1.0;
192 m_vecFreqScale.resize(1,m_dataCurrent.cols());
193
194 double currFreq = 0;
195 for(qint32 i = 0; i < m_dataCurrent.cols(); ++i)
196 {
197 if (m_iScaleType) //log
198 m_vecFreqScale[i] = log10(currFreq+k);
199 else // normal
200 m_vecFreqScale[i] = currFreq;
201
202 currFreq += freqRes;
203 }
204
205 double max = m_vecFreqScale.maxCoeff();
206 m_vecFreqScale /= max;
207
208 m_vecFreqScaleBound = m_vecFreqScale;
209 m_iLowerFrqIdx = 0;
210 m_iUpperFrqIdx = m_vecFreqScale.size()-1;
211
212 m_bInitialized = true;
213 }
214
215 //Update data content
216 QModelIndex topLeft = this->index(0,1);
217 QModelIndex bottomRight = this->index(m_dataCurrent.rows()-1,1);
218 QVector<int> roles; roles << Qt::DisplayRole;
219 emit dataChanged(topLeft, bottomRight, roles);
220}
221
222//=============================================================================================================
223
224void FrequencySpectrumModel::selectRows(const QList<qint32> &selection)
225{
226 beginResetModel();
227
228 m_qMapIdxRowSelection.clear();
229
230 qint32 count = 0;
231 for(qint32 i = 0; i < selection.size(); ++i)
232 {
233 if(selection[i] < m_pFiffInfo->chs.size())
234 {
235 m_qMapIdxRowSelection.insert(count,selection[i]);
236 ++count;
237 }
238 }
239
240 emit newSelection(selection);
241
242 endResetModel();
243}
244
245//=============================================================================================================
246
248{
249 beginResetModel();
250
251 m_qMapIdxRowSelection.clear();
252
253 for(qint32 i = 0; i < m_pFiffInfo->chs.size(); ++i)
254 m_qMapIdxRowSelection.insert(i,i);
255
256 endResetModel();
257}
258
259//=============================================================================================================
260
261void FrequencySpectrumModel::toggleFreeze(const QModelIndex & index)
262{
263 Q_UNUSED(index);
264
265 m_bIsFreezed = !m_bIsFreezed;
266
267 if(m_bIsFreezed)
268 m_dataCurrentFreeze = m_dataCurrent;
269
270 //Update data content
271 QModelIndex topLeft = this->index(0,1);
272 QModelIndex bottomRight = this->index(m_dataCurrent.rows()-1,1);
273 QVector<int> roles; roles << Qt::DisplayRole;
274 emit dataChanged(topLeft, bottomRight, roles);
275}
276
277//=============================================================================================================
278
279void FrequencySpectrumModel::setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
280{
281 if(!m_bInitialized) {
282 return;
283 }
284
285 beginResetModel();
286
287 double nf = m_pFiffInfo->sfreq/2;
288
289 m_iLowerFrqIdx = 0;
290 m_iUpperFrqIdx = m_vecFreqScale.size()-1;
291
292 //find boundaries
293 for(qint32 i = 0; i < m_vecFreqScale.size(); ++i) {
294 float val = m_vecFreqScale[i]*nf;
295 if(val < fLowerFrqBound) {
296 m_iLowerFrqIdx = i;
297 }
298
299 if( val > fUpperFrqBound) {
300 m_iUpperFrqIdx = i;
301 break;
302 }
303 }
304
305 // scale it new
306 m_vecFreqScaleBound = m_vecFreqScale;
307 for(qint32 i = 0; i < m_vecFreqScaleBound.size(); ++i) {
308 m_vecFreqScaleBound[i] = (m_vecFreqScaleBound[i] - m_vecFreqScale[m_iLowerFrqIdx]) / (m_vecFreqScale[m_iUpperFrqIdx] - m_vecFreqScale[m_iLowerFrqIdx]);
309 }
310
311 endResetModel();
312}
QAbstractTableModel storing per-channel FFT magnitudes for SpectrumView.
Full FIFF measurement metadata: everything from FIFFB_MEAS / FIFFB_MEAS_INFO needed to interpret a re...
FIFF file I/O, in-memory data structures and high-level readers/writers.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
void newSelection(QList< qint32 > selection)
void setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
void selectRows(const QList< qint32 > &selection)
void addData(const Eigen::MatrixXd &data)
void toggleFreeze(const QModelIndex &index)
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
void setInfo(QSharedPointer< FIFFLIB::FiffInfo > &info)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
QSharedPointer< FiffInfo > SPtr
Definition fiff_info.h:90