MNE-CPP  beta 1.0
imagesc.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "imagesc.h"
43 #include "colormap.h"
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // Qt INCLUDES
49 //=============================================================================================================
50 
51 #include <QPainter>
52 
53 
54 //*************************************************************************************************************
55 //=============================================================================================================
56 // USED NAMESPACES
57 //=============================================================================================================
58 
59 using namespace DISPLIB;
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // DEFINE MEMBER METHODS
65 //=============================================================================================================
66 
67 ImageSc::ImageSc(QWidget *parent)
68 : Graph(parent)
69 , m_pPixmapData(NULL)
70 , m_pPixmapColorbar(NULL)
71 {
72  init();
73 }
74 
75 
76 //*************************************************************************************************************
77 
78 ImageSc::ImageSc(MatrixXd &p_dMat, QWidget *parent)
79 : Graph(parent)
80 , m_pPixmapData(NULL)
81 , m_pPixmapColorbar(NULL)
82 {
83  init();
84  updateData(p_dMat);
85 }
86 
87 
88 //*************************************************************************************************************
89 
90 ImageSc::ImageSc(MatrixXf &p_fMat, QWidget *parent)
91 : Graph(parent)
92 , m_pPixmapData(NULL)
93 , m_pPixmapColorbar(NULL)
94 {
95  init();
96  updateData(p_fMat);
97 }
98 
99 
100 //*************************************************************************************************************
101 
102 ImageSc::ImageSc(MatrixXi &p_iMat, QWidget *parent)
103 : Graph(parent)
104 , m_pPixmapData(NULL)
105 , m_pPixmapColorbar(NULL)
106 {
107  init();
108  updateData(p_iMat);
109 }
110 
111 
112 //*************************************************************************************************************
113 
115 {
116  if(m_pPixmapData)
117  delete m_pPixmapData;
119  delete m_pPixmapColorbar;
120 }
121 
122 
123 //*************************************************************************************************************
124 
126 {
127  //Parent init
128  Graph::init();
129 
130  //Colormap
132 
133  //Colorbar
134  m_bColorbar = true;
135  m_qFontColorbar.setPixelSize(10);
136  m_qPenColorbar = QPen(Qt::black);
137  m_iColorbarWidth = 12;
138  m_iColorbarSteps = 7;//>= 2!!
139  m_iColorbarGradSteps = 200;
140 
141 }
142 
143 
144 //*************************************************************************************************************
145 
146 void ImageSc::updateData(MatrixXd &p_dMat)
147 {
148  if(p_dMat.rows() > 0 && p_dMat.cols() > 0)
149  {
150  m_dMinValue = p_dMat.minCoeff();
151  m_dMaxValue = p_dMat.maxCoeff();
152 
153  // -- data --
154  m_matCentNormData = p_dMat;
155  double minValue = m_matCentNormData.minCoeff();
156  m_matCentNormData.array() -= minValue;
157  double maxValue = m_matCentNormData.maxCoeff();
158  m_matCentNormData.array() /= maxValue;
159 
160  updateMaps();
161  }
162 }
163 
164 
165 //*************************************************************************************************************
166 
167 void ImageSc::updateData(MatrixXf &p_fMat)
168 {
169  MatrixXd t_dMat = p_fMat.cast<double>();
170  updateData(t_dMat);
171 }
172 
173 
174 //*************************************************************************************************************
175 
176 void ImageSc::updateData(MatrixXi &p_iMat)
177 {
178  MatrixXd t_dMat = p_iMat.cast<double>();
179  updateData(t_dMat);
180 }
181 
182 
183 //*************************************************************************************************************
184 
186 {
187  if(m_pPixmapData)
188  {
189  delete m_pPixmapData;
190  m_pPixmapData = NULL;
191  }
193  {
194  delete m_pPixmapColorbar;
195  m_pPixmapColorbar = NULL;
196  }
197 
198  if(m_matCentNormData.rows() > 0 && m_matCentNormData.cols() > 0)
199  {
200  // --Data--
201  qint32 x = m_matCentNormData.cols();
202  qint32 y = m_matCentNormData.rows();
203  qint32 i, j;
204  QImage t_qImageData(x, y, QImage::Format_RGB32);
205  for(i = 0; i < x; ++i)
206  for(j = 0; j < y; ++j)
207  t_qImageData.setPixel(i, j, pColorMapper(m_matCentNormData(j,i)));
208 
209  m_pPixmapData = new QPixmap(QPixmap::fromImage(t_qImageData));
210 
211  // --Colorbar--
212  QImage t_qImageColorbar(1, m_iColorbarGradSteps, QImage::Format_RGB32);
213 
214  double t_dQuantile = 1.0/((double)m_iColorbarGradSteps-1);
215  for(j = 0; j < m_iColorbarGradSteps; ++j)
216  {
217  QRgb t_qRgb = pColorMapper(t_dQuantile*((double)(m_iColorbarGradSteps-1-j))*1.0);
218  t_qImageColorbar.setPixel(0, j, t_qRgb);
219  }
220  m_pPixmapColorbar = new QPixmap(QPixmap::fromImage(t_qImageColorbar));
221 
222 
223  // --Scale Values--
224  m_qVecScaleValues.clear();
225 
226  double scale = pow(10, floor(log(m_dMaxValue-m_dMinValue)/log(10.0)));
227 
228  //Zero Based Scale?
229  if(m_dMaxValue > 0 && m_dMinValue < 0)
230  {
231  double quantum = floor((((m_dMaxValue-m_dMinValue)/scale)/(m_iColorbarSteps-1))*10.0)*(scale/10.0);
232  double start = 0;
233  while(m_dMinValue < (start - quantum))
234  start -= quantum;
235  //Create Steps
236  m_qVecScaleValues.push_back(start);
237  for(qint32 i = 1; i < m_iColorbarSteps-1; ++i)
238  m_qVecScaleValues.push_back(m_qVecScaleValues[i-1]+quantum);
239  }
240  else
241  {
242  double quantum = floor((((m_dMaxValue-m_dMinValue)/scale)/(m_iColorbarSteps-1))*10.0)*(scale/10.0);
243  double start = floor(((m_dMaxValue-m_dMinValue)/2.0 + m_dMinValue)/scale)*scale;
244  while(m_dMinValue < (start - quantum))
245  start -= quantum;
246  //Create Steps
247  m_qVecScaleValues.push_back(start);
248  for(qint32 i = 1; i < m_iColorbarSteps-1; ++i)
249  m_qVecScaleValues.push_back(m_qVecScaleValues[i-1]+quantum);
250  }
251  update();
252  }
253 }
254 
255 
256 //*************************************************************************************************************
257 
258 void ImageSc::setColorMap(const QString &p_sColorMap)
259 {
260  if(p_sColorMap == "Jet")
262  else if(p_sColorMap == "Hot")
264  else if(p_sColorMap == "HotNeg1")
266  else if(p_sColorMap == "HotNeg2")
268  else if(p_sColorMap == "Bone")
270  else if(p_sColorMap == "RedBlue")
272  else
274 
275  updateMaps();
276 }
277 
278 
279 //*************************************************************************************************************
280 
281 void ImageSc::paintEvent(QPaintEvent *)
282 {
283  QPainter painter(this);
284  if (m_pPixmapData)
285  {
286  QPoint t_qPointTopLeft(0,0);
287 
288  // -- Data --
289  QSize t_qSizePixmapData = m_qSizeWidget;
290 
291  t_qSizePixmapData.setHeight(t_qSizePixmapData.height()-m_iBorderTopBottom*2);
292  t_qSizePixmapData.setWidth(t_qSizePixmapData.width()-m_iBorderLeftRight*2);
293  // Scale data
294  QPixmap t_qPixmapScaledData = m_pPixmapData->scaled(t_qSizePixmapData, Qt::IgnoreAspectRatio);//Qt::KeepAspectRatio);
295  // Calculate data position
296  t_qPointTopLeft.setX((m_qSizeWidget.width()-t_qPixmapScaledData.width())/2);
297  t_qPointTopLeft.setY((m_qSizeWidget.height()-t_qPixmapScaledData.height())/2);
298  //Draw data
299  painter.drawPixmap(t_qPointTopLeft,t_qPixmapScaledData);
300  //Draw border
301  painter.drawRect(t_qPointTopLeft.x()-1, t_qPointTopLeft.y()-1, t_qPixmapScaledData.width()+1, t_qPixmapScaledData.height()+1);
302 
303  // -- Colorbar --
304  if(m_bColorbar && m_pPixmapColorbar && m_qVecScaleValues.size() >= 2)
305  {
306  QSize t_qSizePixmapColorbar = m_qSizeWidget;
307 
308  t_qSizePixmapColorbar.setWidth(m_iColorbarWidth);
309  t_qSizePixmapColorbar.setHeight(t_qPixmapScaledData.height());
310 
311  // Scale colorbar
312  QPixmap t_qPixmapScaledColorbar = m_pPixmapColorbar->scaled(t_qSizePixmapColorbar, Qt::IgnoreAspectRatio);
313  // Calculate colorbar position
314  t_qPointTopLeft.setY(t_qPointTopLeft.y());
315  t_qPointTopLeft.setX(t_qPointTopLeft.x() + t_qPixmapScaledData.width() + m_iBorderLeftRight/3);
316  //Draw colorbar
317  painter.drawPixmap(t_qPointTopLeft,t_qPixmapScaledColorbar);
318  //Draw border
319  painter.drawRect(t_qPointTopLeft.x()-1, t_qPointTopLeft.y()-1, m_iColorbarWidth+1, t_qPixmapScaledData.height()+1);
320 
321  // -- Scale --
322  painter.setPen(m_qPenColorbar);
323  painter.setFont(m_qFontColorbar);
324 
325  qint32 x = t_qPointTopLeft.x()+ m_iColorbarWidth + m_qFontColorbar.pixelSize()/2;
326  qint32 x_markLeft = x - m_iColorbarWidth - m_qFontColorbar.pixelSize()/2;
327  qint32 x_markRight = x_markLeft + m_iColorbarWidth;
328 
329  // max
330  painter.save();
331  qint32 y_max = t_qPointTopLeft.y() - m_qFontColorbar.pixelSize()/2;
332  painter.translate(x, y_max-1);
333  painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMaxValue));
334  painter.restore();
335  //draw max marks
336  qint32 y_max_mark = y_max + m_qFontColorbar.pixelSize()/2;
337  painter.drawLine(x_markLeft,y_max_mark,x_markLeft+2,y_max_mark);
338  painter.drawLine(x_markRight-3,y_max_mark,x_markRight-1,y_max_mark);
339 
340  // min
341  painter.save();
342  qint32 y_min = t_qPointTopLeft.y() + t_qSizePixmapColorbar.height()-1 - m_qFontColorbar.pixelSize()/2;
343  painter.translate(x, y_min-1);
344  painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_dMinValue));
345  painter.restore();
346  //draw min marks
347  qint32 y_min_mark = y_min + m_qFontColorbar.pixelSize()/2;
348  painter.drawLine(x_markLeft,y_min_mark,x_markLeft+2,y_min_mark);
349  painter.drawLine(x_markRight-3,y_min_mark,x_markRight-1,y_min_mark);
350 
351  //Scale values
352  qint32 y_dist = y_min - y_max;
353  double minPercent = (m_qVecScaleValues[0]- m_dMinValue)/(m_dMaxValue-m_dMinValue);
354  double distPercent = (m_qVecScaleValues[1]-m_qVecScaleValues[0])/(m_dMaxValue-m_dMinValue);
355  qint32 y_current = y_min - (minPercent*y_dist);
356  qint32 y_current_mark;
357  //draw scale
358  for(qint32 i = 0; i < m_qVecScaleValues.size(); ++i)
359  {
360  painter.save();
361  painter.translate(x, y_current-1);
362  painter.drawText(QRect(0, 0, 100, 12), Qt::AlignLeft, QString::number(m_qVecScaleValues[i]));
363  painter.restore();
364  //draw marks
365  y_current_mark = y_current + m_qFontColorbar.pixelSize()/2;
366  painter.drawLine(x_markLeft,y_current_mark,x_markLeft+2,y_current_mark);
367  painter.drawLine(x_markRight-3,y_current_mark,x_markRight-1,y_current_mark);
368  //update y_current
369  y_current -= distPercent*y_dist;
370  }
371  }
372 
373  //Draw title & axes
374  Graph::drawLabels(t_qPixmapScaledData.width(), t_qPixmapScaledData.height());
375  }
376 }
ColorMap class declaration.
QPen m_qPenColorbar
Definition: imagesc.h:205
double m_dMinValue
Definition: imagesc.h:194
QPixmap * m_pPixmapData
Definition: imagesc.h:189
ImageSc(QWidget *parent=0)
Definition: imagesc.cpp:67
ImageSc class declaration.
double m_dMaxValue
Definition: imagesc.h:195
MatrixXd m_matCentNormData
Definition: imagesc.h:192
qint32 m_iColorbarWidth
Definition: imagesc.h:201
QVector< double > m_qVecScaleValues
Definition: imagesc.h:200
qint32 m_iBorderLeftRight
Definition: graph.h:138
static QRgb valueToHot(double v)
Definition: colormap.h:359
QPixmap * m_pPixmapColorbar
Definition: imagesc.h:190
static QRgb valueToHotNegative2(double v)
Definition: colormap.h:377
static QRgb valueToJet(double v)
Definition: colormap.h:350
static QRgb valueToHotNegative1(double v)
Definition: colormap.h:368
qint32 m_iBorderTopBottom
Definition: graph.h:137
QFont m_qFontColorbar
Definition: imagesc.h:204
qint32 m_iColorbarGradSteps
Definition: imagesc.h:203
static QRgb valueToBone(double v)
Definition: colormap.h:386
Base class for graphs.
Definition: graph.h:95
void updateMaps()
Definition: imagesc.cpp:185
bool m_bColorbar
Definition: imagesc.h:199
void updateData(MatrixXd &p_dMat)
Definition: imagesc.cpp:146
static QRgb valueToRedBlue(double v)
Definition: colormap.h:395
QSize m_qSizeWidget
Definition: graph.h:131
void setColorMap(const QString &p_sColorMap)
Definition: imagesc.cpp:258
QRgb(* pColorMapper)(double)
Definition: imagesc.h:197
qint32 m_iColorbarSteps
Definition: imagesc.h:202