/***************************************************************************
 *   Copyright (C) 2005 by Tavarez Arnaud Bakoula                          *
 *   tbakoula@yahoo.fr                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "cellrow.h"
#include "cell.h"

#include <qpainter.h>

CellRow::CellRow( uint idx, uint sz )
    :  index( idx ), dim( sz )
{
    uint w = dim*Cell::getSize();
    uint l = Cell::getSize();
    bound = QRect( 0, 0, w, l );
    cell_list = new std::vector<Cell*>();
}

/**
   The destructor. Restores the allocated ressources
 */
CellRow::~CellRow()
{
    cell_iterator it = cell_list->begin();
    for ( ; it != cell_list->end() ; it++ )
      delete *it;

    delete cell_list;
    cell_list = 0;
}

/**
   Returns the cell whose index is \a idx.
 */
Cell*
CellRow::getCellAt( uint idx )
{
    if ( cell_list->empty() )
      return 0;
    return cell_list->at( idx );
}

/**
   Returns the cell located at the specified coordinates
 */
Cell*
CellRow::getCellAt( int x, int y )
{
    if ( cell_list->empty() )
      return 0;

    cell_iterator it = cell_list->begin();
    for ( ; it != cell_list->end(); it++ )
    {
      if ( (*it)->rect().contains( x, y ) )
        return *it;
    }
    return 0;
}

/**
   This function removes the specified number of cells from the row
 */
void CellRow::removeCells( uint rm )
{
    if ( cell_list->empty() )
      return;

    for ( uint i=0; i!=rm; i++ )
      cell_list->pop_back();
    dim -= rm;
}

/**
   This function moves the cells, i.e it recomputes the cells'
   positions. So after that they can be redrawn at the good location.
 */
void CellRow::placeCells()
{
    if ( !cell_list->empty() )
    {
      uint i=0;
      cell_iterator it = cell_list->begin();
      for ( ; it != cell_list->end(); it++, i++ )
      {
        computePosition( *it, i );
      }
    }
}

/**
   This function computes the position of the specified cell.
 */
void CellRow::computePosition( Cell* cell, uint c )
{
    QPoint point;
    int wr = rect().width();
    int wc = Cell::getSize();
    int space = int((wr - dim*wc) / (dim+1));

    point.setX( bound.x() + space + c*(space + wc) );
    point.setY( bound.y() );
    cell->rect().moveTopLeft( point );
}

/**
   Draws the current cellrow and its contents in the paintdevice
   relative to the painter \a painter.
   You must notice that the point \a p must be the top left
   corner of the cellrow bounding rect.
 */
void CellRow::draw( QPainter* painter )
{
    cell_iterator it = cell_list->begin();
    for ( ; it != cell_list->end(); it++ )
      (*it)->draw( painter, (*it)->rect().topLeft() );
}

void CellRow::updateMe()
{
    int sz = Cell::getSize();
    QSize new_size( sz, sz );
    cell_iterator it = cell_list->begin();
    for ( ; it != cell_list->end(); it++ )
      (*it)->rect().setSize( new_size );
    rect().setHeight( sz );
}
