52 #include <QPainterPath>
58 using namespace DISPLIB;
59 using namespace Eigen;
66 : QAbstractItemDelegate(parent)
75 m_tableview = m_pTableView;
76 m_tableview->setMouseTracking(
true);
83 m_iScaleType = ScaleType;
90 float t_fPlotHeight = option.rect.height();
91 switch(index.column()) {
96 painter->drawText(QRectF(-option.rect.y()-t_fPlotHeight,0,t_fPlotHeight,20),Qt::AlignCenter,index.model()->data(index,Qt::DisplayRole).toString());
122 QVariant variant = index.model()->data(index,Qt::DisplayRole);
123 RowVectorXd data = variant.value< RowVectorXd >();
129 QPainterPath path(QPointF(option.rect.x(),option.rect.y()));
132 painter->setRenderHint(QPainter::Antialiasing,
false);
133 createGridPath(index, option, path, data);
134 createGridTick(index, option, painter);
137 capturePoint(index, option, path, data, painter);
141 pen.setStyle(Qt::DotLine);
143 painter->setPen(pen);
144 painter->drawPath(path);
148 path = QPainterPath(QPointF(option.rect.x(),option.rect.y()));
150 createPlotPath(index, option, path, data);
153 painter->translate(0,t_fPlotHeight/2);
154 painter->setRenderHint(QPainter::Antialiasing,
true);
156 if(option.state & QStyle::State_Selected)
157 painter->setPen(QPen(t_pModel->isFreezed() ? Qt::darkRed : Qt::red, 1, Qt::SolidLine));
159 painter->setPen(QPen(t_pModel->isFreezed() ? Qt::darkGray : Qt::darkBlue, 1, Qt::SolidLine));
161 painter->drawPath(path);
176 switch(index.column()) {
178 size = QSize(20,option.rect.height());
181 RowVectorXd data = index.model()->data(index).value< RowVectorXd >();
197 if(mousex != m_mousex){
199 m_tableview_row = tableview_row;
204 m_x_rate = (float)m_mousex/(
float)m_visRect.width();
206 m_tableview->viewport()->repaint();
212 void FrequencySpectrumDelegate::capturePoint(
const QModelIndex &index,
const QStyleOptionViewItem &option, QPainterPath& path, RowVectorXd& data, QPainter *painter)
const
217 if (m_tableview_row == index.row()){
222 RowVectorXd org_vecFreqScale = t_pModel->
getFreqScale();
234 for(i = lowerIdx+1; i <= upperIdx; ++i) {
238 float tmp_rate = (vecFreqScale[i] - vecFreqScale[lowerIdx])/(vecFreqScale[upperIdx]-vecFreqScale[lowerIdx]);
240 if (tmp_rate > m_x_rate) {
break;}
249 unsigned short usPosY = m_visRect.bottom();
250 unsigned short usPosX = m_visRect.left();
251 unsigned short usHeight = m_visRect.height();
252 unsigned short usWidth = m_visRect.width();
254 int iPosX = m_mousex;
255 int iPosY = m_mousey;
257 if(iPosX>usPosX && iPosX < usPosX+usWidth && iPosY > (usPosY - usHeight) && iPosY < usPosY )
261 painter->setPen(QPen(Qt::gray, 1, Qt::DashLine));
263 QPoint start(iPosX - 25, iPosY);
264 QPoint end(iPosX + 25, iPosY);
269 start.setX(iPosX); start.setY(usPosY -usHeight);
270 end.setX(iPosX); end.setY(usPosY);
271 painter->drawLine(start, end);
273 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine));
276 double fs = t_pModel->
getInfo()->sfreq/2;
283 double max = log10(fs+1);
284 org_vecFreqScale *= max;
286 freq = pow(10,org_vecFreqScale[i]) - 1;
290 org_vecFreqScale *= fs;
292 freq = org_vecFreqScale[i];
296 QString tx = QString(
"%1 [DB], %2 [Hz]").arg(data[i]).arg(freq);
298 if (iPosX > usPosX + usWidth - tx.size()*8 )
299 painter->drawText(iPosX-tx.size()*8, iPosY-8, tx);
301 painter->drawText(iPosX+8, iPosY-8, tx);
308 void FrequencySpectrumDelegate::createPlotPath(
const QModelIndex &index,
const QStyleOptionViewItem &option, QPainterPath& path, RowVectorXd& data)
const
312 float fMaxValue = data.maxCoeff();
315 float fScaleY = option.rect.height()/(fMaxValue*0.5);
317 float y_base = path.currentPosition().y();
318 QPointF qSamplePosition;
327 fValue = val*fScaleY;
329 float newY = y_base+fValue;
331 qSamplePosition.setY(newY);
332 qSamplePosition.setX((
double)option.rect.width()*t_pModel->
getFreqScaleBound()[lowerIdx]);
334 path.moveTo(qSamplePosition);
339 for(i = lowerIdx+1; i <= upperIdx; ++i) {
340 float val = data[i]-data[0];
341 fValue = val*fScaleY;
343 float newY = y_base+fValue;
345 qSamplePosition.setY(newY);
346 qSamplePosition.setX((
double)option.rect.width()*t_pModel->
getFreqScaleBound()[i]);
348 path.lineTo(qSamplePosition);
354 void FrequencySpectrumDelegate::createGridPath(
const QModelIndex &index,
const QStyleOptionViewItem &option, QPainterPath& path, RowVectorXd& data)
const
362 double nf = t_pModel->
getInfo()->sfreq/2;
364 qint32 numLines = (m_iScaleType)? (qint32)ceil(log10(nf)) : 5 ;
366 QList<qint32> qListLineSamples;
368 qListLineSamples << 0;
372 for(qint32 lineIdx = 0; lineIdx < numLines; ++lineIdx)
374 double val = pow(10,lineIdx);
375 qint32 idx = (qint32)floor(val / ((
float)nf/(
float)t_pModel->
getNumStems()));
376 qListLineSamples.append(idx);
380 for(qint32 lineIdx = 1; lineIdx < numLines; ++lineIdx)
382 double val = lineIdx*(nf/numLines);
383 qint32 idx = (qint32)floor(val / ((
float)nf/(float)t_pModel->
getNumStems()));
384 qListLineSamples.append(idx);
389 float yStart = option.rect.topLeft().y();
391 float yEnd = option.rect.bottomRight().y();
393 for(qint32 i = 0; i < qListLineSamples.size(); ++i) {
396 float x = (t_pModel->
getFreqScaleBound()[qListLineSamples[i]])*option.rect.width();
397 path.moveTo(x,yStart);
407 void FrequencySpectrumDelegate::createGridTick(
const QModelIndex &index,
const QStyleOptionViewItem &option, QPainter *painter)
const
413 double nf = t_pModel->
getInfo()->sfreq/2;
415 qint32 numLines = (m_iScaleType)? (qint32)ceil(log10(nf)) : 5 ;
417 QList<qint32> qListLineSamples;
419 qListLineSamples << 0;
422 for(qint32 lineIdx = 0; lineIdx < numLines; ++lineIdx)
424 double val = pow(10,lineIdx);
425 qint32 idx = (qint32)floor(val / ((
float)nf/(
float)t_pModel->
getNumStems()));
426 qListLineSamples.append(idx);
431 for(qint32 lineIdx = 1; lineIdx < numLines; ++lineIdx)
433 double val = lineIdx*(nf/numLines);
434 qint32 idx = (qint32)floor(val / ((
float)nf/(float)t_pModel->
getNumStems()));
435 qListLineSamples.append(idx);
441 float yStart = 1.0*option.rect.topLeft().y();
446 float x = (t_pModel->
getFreqScaleBound()[qListLineSamples[0]])*option.rect.width();
447 painter->drawText(x,yStart,QString(
"%1Hz").arg(val));
450 for(qint32 i = 1; i < qListLineSamples.size(); ++i) {
453 double val = (m_iScaleType)? pow(10,i-1) : t_pModel->
getFreqScaleBound()[qListLineSamples[i]]*nf;
454 float x = (t_pModel->
getFreqScaleBound()[qListLineSamples[i]])*option.rect.width();
455 painter->drawText(x,yStart,QString(
"%1Hz").arg(val));