/* calling the QPainter stuff. */
void ColorMapWidget::paintEvent(QPaintEvent *) {
    QFont f(QApplication::font());
    double res;
    QPainter p;
    QString tmpDisplayedText;
    int i,h,yoff,ysub,cidx;
    int lscale;
    double scale;

    lscale = (int) floor(log10(dmax));
    scale = pow(10,lscale);
    f.setPixelSize(11);
    p.begin(this);
    p.setFont(f);
    QFontMetrics fm(p.fontMetrics());
    yoff = fm.lineSpacing();
    ysub = fm.ascent()/2;
    tmpDisplayedText.sprintf("x 1 e %i",lscale);
    p.setPen(Qt::white);
    p.drawText(10,yoff,tmpDisplayedText);
    h = height()-3*yoff;
    h = h/CBAR_STEP*CBAR_STEP;
    yoff *= 2;
    if (cmap_len != h) {
        free_colormap(colormap);
        cmap_len = h;
        if (colorState == HSV)
            colormap = colormap_hsv(cmap_len);
        else if (colorState == GRAYSCALE)
            colormap = colormap_gs(cmap_len);
        else
            fprintf(stderr, "Invalid color space specified\n");

        doHistogram(scaleState);
    }
    p.drawText(40, yoff+ysub, tmpDisplayedText.sprintf( "%.3f",dmax/scale));
    for(i=0; i<N_LABELS-1; i++) {
        res = dmax - (i+1)*(dmax - dmin)/(double)(N_LABELS-1);
        p.drawText(40, yoff+ysub+(int)((i+1)*(h/(double)(N_LABELS-1))),
                   tmpDisplayedText.sprintf( "%.3f",res/scale));
    }
    for(i=0; i<cmap_len; i++) {
        cidx = cmap_len-1-i;
        p.setPen(QColor((int)(255*colormap[cidx][0]), (int)(255*colormap[cidx][1]),
                        (int)(255*colormap[cidx][2])));
        p.drawLine(10, yoff+i, 10+histogram[cidx/CBAR_STEP], yoff+i);
    }
    p.end();
}
void ColorMapWidget::setColorMap(int newSpace) {

    if (colorState != newSpace) {
        colorState = (colorSpace)newSpace;
        free_colormap(colormap);

        if (colorState == HSV)
            colormap = colormap_hsv(cmap_len);
        else if (colorState == GRAYSCALE)
            colormap = colormap_gs(cmap_len);
        else
            fprintf(stderr, "Invalid color space specified\n");
        doHistogram(scaleState);
    }
}
// slot to change the colormap
void Error3DViewerWidget::setColorMap(int newSpace) 
{
  if (newSpace != csp) {
    csp = (ColorMapWidget::colorSpace)newSpace;
    free(colormap);
    if (csp == ColorMapWidget::HSV)
      colormap = colormap_hsv(CMAP_LENGTH);
    else if (csp == ColorMapWidget::GRAYSCALE)
      colormap = colormap_gs(CMAP_LENGTH);
    else
      fprintf(stderr, "Invalid color space specified\n");
    if (error_mode != SAMPLE_ERROR) {
      makeCurrent();
      // display wait cursor while rebuilding list (useful for n=1 only)
      QApplication::setOverrideCursor(Qt::waitCursor);
      rebuildList();
      QApplication::restoreOverrideCursor();
    } else {
      genErrorTextures();
    }
    updateGL();
  }
}