#include "BinValueEditor.h"
#include <qlabel.h>

#include <iostream>
using namespace std ;

BinValueEditor::BinValueEditor(unsigned int *wo,unsigned int bi ,
                               const QString& tx, QWidget *parent, bool editable ) :
        QHBox(parent),
        label(tx), word(wo), bit(bi)
{
    QString nb ;
    QLabel *l =  new QLabel(nb.sprintf("%2d",bi), this) ;
    int hh = l->fontMetrics().size(SingleLine,"DD").width();
    l->setMargin(0) ;
    l->setMaximumWidth(hh) ;
    l->setMinimumWidth(hh) ;
    setStretchFactor(l,0) ;
    cb = new  QCheckBox(tx,this) ;
    cb ->setEnabled(editable);


    setStretchFactor(cb,1) ;
    setSpacing(5) ;
    connect(cb,SIGNAL(clicked()), this, SLOT(updateWord())) ;
    updateDisplay() ;
}

void  BinValueEditor::updateDisplay()
{
    // force bits if they are named 0 or 1
    if(label == "0")
        *word &= ~(1<<bit) ;
    else if( label  == "1")
        *word |= (1<<bit) ;

    cb->setChecked(*word & (1 << bit)) ;

//cout << "label = " << label << " " <<  ((*word & (1 << bit)) != 0) << endl ;
}

void  BinValueEditor::updateWord()
{
    if(cb->isChecked())
        *word |= (1 << bit)	 ;
    else
        *word &= (~(1 << bit)) ;
    emit bitChanged() ;
}
