//
// C++ Implementation: colorslider
//
// Description: 
//
//
// Author: Andreas Krumnow <andreas@krumnow.de>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

/* * TODO:

    DONE:
    
    - ColorSlider direkt von QSlider abgeleitet
*/

#include "colorslider.h"

ColorSlider::ColorSlider(Qt::Orientation orientation, QWidget *parent)
    : QSlider(orientation, parent)
{
    max_col = 408;
    curIntCol = 0;
    setRange(0, max_col);
    setMinimumHeight ( 14 );

    connect( this, SIGNAL(valueChanged( int)),
                this, SLOT(setValue( int )));
}

void ColorSlider::paintEvent(QPaintEvent *event)
{
    stift = new QPainter(this);
    int x, xold, y, y1;
    x = xold = 0; 
    y = 0; 
    y1 = height();
    double scale = (double)(width()) / max_col;
    QRgb rgb = intSldrToRgb(curIntCol);
    for(int i=0; i < max_col; i++)
    {
        rgb = intSldrToRgb(i);
        stift->setPen(rgb);
        stift->setBrush(QColor(rgb));
        x = int(double(i) * scale) +10;
        if(0 < (x-xold)) stift->drawRect(xold,y,(x-xold),y1);
        xold = x;
    }
    delete stift;
    QSlider::paintEvent(event);
}

void ColorSlider::setValue(int value)
{
    curIntCol = value;
    emit valueChanged( intSldrToRgb(value) );
    update();
}

QRgb ColorSlider::intSldrToRgb(const int i)
{
    int r, g, b, c;
    r = g = b = 0;
    c = i;
    // Translate Sliders Value to RGB
    if(c < 0) c = 0;
    if(c > max_col) c = max_col;
    if((  0 < c)&&(c <  52)){ r = c * 5;            g = 0;               b = 0; }
    if(( 51 < c)&&(c < 103)){ r = 255;              g = (c-52)*5;        b = 0; }
    if((102 < c)&&(c < 154)){ r = 255-((c-103)*5);  g = 255;             b = 0; }
    if((153 < c)&&(c < 205)){ r = 0;                g = 255;             b = (c-154)*5; }
    if((204 < c)&&(c < 256)){ r = 0;                g = 255-((c-205)*5); b = 255; }
    if((255 < c)&&(c < 307)){ r = ((c-256)*5);      g = 0;               b = 255; }
    if((306 < c)&&(c < 358)){ r = 255;              g = (c-307)*5;       b = 255; }
    if((357 < c)&&(c < 409)){ r = 255-((c-358)*5);  g = 255-((c-358)*5); b = 255-((c-358)*5); }
    // save the values ...
    QColor tr_rgb;
    tr_rgb.setRgb(r,g,b);
    return tr_rgb.rgb();
}