MNE-CPP  beta 1.0
sensormodel.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "sensormodel.h"
42 #include "sensoritem.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // Qt INCLUDES
48 //=============================================================================================================
49 
50 #include <QDebug>
51 
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // USED NAMESPACES
56 //=============================================================================================================
57 
58 using namespace XDISPLIB;
59 
60 
61 //*************************************************************************************************************
62 //=============================================================================================================
63 // DEFINE MEMBER METHODS
64 //=============================================================================================================
65 
66 SensorModel::SensorModel(QObject *parent)
67 : QAbstractTableModel(parent)
68 , m_iCurrentLayoutId(0)
69 {
70 }
71 
72 
73 //*************************************************************************************************************
74 
75 SensorModel::SensorModel(QIODevice* device, QObject *parent)
76 : QAbstractTableModel(parent)
77 , m_iCurrentLayoutId(0)
78 {
79  if(!this->read(device))
80  qWarning() << "Not able to read sensor layout.";
81 }
82 
83 
84 //*************************************************************************************************************
85 
86 void SensorModel::createSelection()
87 {
88  QList<qint32> listSelection = m_qMapSelection.keys(true);
89  emit newSelection(listSelection);
90 }
91 
92 
93 //*************************************************************************************************************
94 
95 int SensorModel::rowCount(const QModelIndex &parent) const
96 {
97  Q_UNUSED(parent)
98  if(m_qListSensorLayouts.size() > 0 && m_iCurrentLayoutId < m_qListSensorLayouts.size())
99  return m_qListSensorLayouts[m_iCurrentLayoutId].numChannels();
100  else
101  return 0;
102 }
103 
104 
105 //*************************************************************************************************************
106 
107 int SensorModel::columnCount(const QModelIndex &parent) const
108 {
109  Q_UNUSED(parent)
110  return 4;
111 }
112 
113 
114 //*************************************************************************************************************
115 
116 QVariant SensorModel::data(const QModelIndex &index, int role) const
117 {
118  Q_UNUSED(role)
119  if (index.isValid()) {
120  qint32 row = index.row();//m_qMapIdxRowSelection[index.row()];
121 
122  //******** first column (short ChName) ********
123  if(index.column() == 0)// && role == Qt::DisplayRole)
124  return QVariant(m_qListSensorLayouts[m_iCurrentLayoutId].shortChNames()[row]);
125 
126  //******** second column (full ChName) ********
127  if(index.column() == 1)// && role == Qt::DisplayRole)
128  return QVariant(m_qListSensorLayouts[m_iCurrentLayoutId].fullChNames()[row]);
129 
130  //******** third column (Position) ********
131  if(index.column() == 2)
132  return QVariant(m_qListSensorLayouts[m_iCurrentLayoutId].loc()[row]);
133 
134 // switch(role) {
135 // case Qt::DisplayRole: {
136 
137  //******** fourth column (selected) ********
138  if(index.column() == 3)
139  {
140  QString fullName = m_qListSensorLayouts[m_iCurrentLayoutId].fullChNames()[row];
141  qint32 chNum = m_qMapNameId[fullName];
142  return QVariant(m_qMapSelection[chNum]);
143  }
144  }
145 
146  return QVariant();
147 }
148 
149 
150 //*************************************************************************************************************
151 
152 bool SensorModel::read(QIODevice* device)
153 {
154  QDomDocument domDocument;
155 
156  QString errorStr;
157  int errorLine;
158  int errorColumn;
159 
160  if (!domDocument.setContent(device, true, &errorStr, &errorLine, &errorColumn))
161  {
162  qCritical() << QString("Parse error at line %1, column %2:\n%3").arg(errorLine).arg(errorColumn).arg(errorStr);
163  return false;
164  }
165 
166  QDomElement root = domDocument.documentElement();
167  if (root.tagName() != "MNEXSensorLayout")
168  {
169  qCritical() << QString("The file is not a MNE-X Sensor Layout file.");
170  return false;
171  }
172 
173  QString t_sDevice = root.attribute("Device", "No Device");
174 
175  //
176  // Layouts
177  //
178  QDomElement childSensorsLayouts = root.firstChildElement("SensorLayouts");
179  QDomElement childSensors = childSensorsLayouts.firstChildElement("Sensors");
180  while (!childSensors.isNull())
181  {
182  m_qListSensorLayouts.append(SensorLayout::parseSensorLayout(childSensors));
183  childSensors = childSensors.nextSiblingElement("Sensors");
184  }
185 
186  //
187  // Groups
188  //
189  QDomElement childSensorGroups = root.firstChildElement("SensorGroups");
190  QDomElement childGroup = childSensorGroups.firstChildElement("Group");
191  while (!childGroup.isNull())
192  {
193  m_qListSensorGroups.append(SensorGroup::parseSensorGroup(childGroup));
194  childGroup = childGroup.nextSiblingElement("Group");
195  }
196 
197 // qDebug() << root.tagName();//domDocument.toString();
198 // qDebug() << t_sDevice;
199 
200  return true;
201 }
202 
203 
204 //*************************************************************************************************************
205 
207 {
208  QList<qint32> selection;
209 
210  for(qint32 i = 0; i < m_qListSensorGroups[id].getChannelNames().size(); ++i)
211  selection.append(m_qMapNameId[m_qListSensorGroups[id].getChannelNames()[i]]);
212 
213  emit newSelection(selection);
214  silentUpdateSelection(selection);
215 }
216 
217 
218 //*************************************************************************************************************
219 
221 {
222  beginResetModel();
223 
224  int oldLayout = m_iCurrentLayoutId;
225 
226  if(id >= 0 && id < m_qListSensorLayouts.size())
227  m_iCurrentLayoutId = id;
228 
229  endResetModel();
230 
231  if(oldLayout != m_iCurrentLayoutId)
232  emit newLayout();
233 }
234 
235 
236 //*************************************************************************************************************
237 
238 void SensorModel::mapChannelInfo(const QList<XMEASLIB::RealTimeSampleArrayChInfo>& chInfoList)
239 {
240  m_qMapSelection.clear();
241  m_qMapNameId.clear();
242  for(qint32 i = 0; i < chInfoList.size(); ++i)
243  {
244  m_qMapSelection.insert(i,true);
245  m_qMapNameId.insert(chInfoList.at(i).getChannelName(), i);
246  }
247 }
248 
249 
250 //*************************************************************************************************************
251 
253 {
254  m_qMapSelection[item->getChNumber()] = item->isChoosen();
255  createSelection();
256 }
257 
258 
259 //*************************************************************************************************************
260 
261 void SensorModel::silentUpdateSelection(const QList<qint32>& selection)
262 {
263  QMap<qint32, bool>::iterator it;
264  for (it = m_qMapSelection.begin(); it != m_qMapSelection.end(); ++it)
265  it.value() = false;
266 
267  for(qint32 i = 0; i < selection.size(); ++i)
268  m_qMapSelection[selection[i]] = true;
269 
270  //Update data content
271  QModelIndex topLeft = this->index(0,3);
272 
273  QModelIndex bottomRight = this->index(m_qMapSelection.size()-1,3);
274 
275  QVector<int> roles; roles << Qt::DisplayRole;
276 
277  emit dataChanged(topLeft, bottomRight, roles);
278 }
void applySensorGroup(int id)
QVariant data(int row, int column, int role=Qt::DisplayRole) const
Definition: sensormodel.h:225
void newSelection(QList< qint32 > selection)
bool isChoosen() const
Definition: sensoritem.h:277
qint32 getChNumber() const
Definition: sensoritem.h:236
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: sensormodel.cpp:95
Declaration of the SensorItem Class.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
void mapChannelInfo(const QList< XMEASLIB::RealTimeSampleArrayChInfo > &chInfoList)
void silentUpdateSelection(const QList< qint32 > &selection)
void setCurrentLayout(int id)
SensorModel(QObject *parent=0)
Definition: sensormodel.cpp:66
static SensorGroup parseSensorGroup(const QDomElement &sensorGroupElement)
Definition: sensorgroup.cpp:72
The SensorItem class represents a channel item, plottet at the graphics scene.
Definition: sensoritem.h:64
static SensorLayout parseSensorLayout(const QDomElement &sensorLayoutElement)
void updateChannelState(SensorItem *item)