#include "radiobuttonselection.h"

RadioButtonSelection::RadioButtonSelection( QWidget *parent ) : QWidget( parent )
{
  QVBoxLayout *vLayout = new QVBoxLayout( this );

  connect( this, SIGNAL( somethingChanged() ), this, SLOT( somethingChangedSlot() ) );
}

void RadioButtonSelection::setValues( QList< int > ids, QList< QString > btnTexts )
{
  this->ids = ids;
  QVBoxLayout *vLayout = static_cast< QVBoxLayout* >( this->layout() );

  if( ids.length() <= 0 )
  {
    QLabel *label = new QLabel( tr("There is nothing to show!") );
    vLayout->addWidget( label );
    return;
  }

  for( int i = 0; i < btnTexts.length(); i++ )
  {
    rb << new QRadioButton( btnTexts[ i ] );
    vLayout->addWidget( rb[ i ] );
    connect( rb[ i ], SIGNAL( clicked(bool) ), this, SIGNAL( somethingChanged() ) );
  }
}

int RadioButtonSelection::getCheckedId( void )
{
  int checkedId = -1;
  for( int i = 0; i < rb.length(); i++ )
    if( rb[ i ]->isChecked() )
      checkedId = ids[ i ];
  return checkedId;
}

void RadioButtonSelection::somethingChangedSlot( void )
{
  emit somethingChanged( getCheckedId() );
}
