#include "checkboxselection.h"

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

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

void CheckBoxSelection::setValues( QList< int > ids, QList< QString > btnTexts, QList< int > checkedIds )
{
  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++ )
  {
    cb << new CheckBox(  );
    cb.last()->setText(btnTexts[ i ]);

    for( int c = 0; c < checkedIds.length(); c++ )
      if( ids[ i ] == checkedIds[ c ] )
        cb[ i ]->setChecked( true );
    vLayout->addWidget( cb[ i ] );
    connect( cb[ i ], SIGNAL( stateChanged( int ) ), this, SIGNAL( somethingChanged() ) );
  }
}

QList< int > CheckBoxSelection::getCheckedIds( void )
{
  QList< int > checkedIds;
  for( int i = 0; i < cb.length(); i++ )
    if( cb[ i ]->isChecked() )
      checkedIds << ids[ i ];
  return checkedIds;
}

void CheckBoxSelection::somethingChangedSlot( void )
{
  emit somethingChanged( getCheckedIds() );
}
