/***************************************************************************
 *   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.             *
 ***************************************************************************/
#ifndef CELL_MANAGER_H
#define CELL_MANAGER_H

#include <qframe.h>
#include <qpoint.h>
#include <qpixmap.h>

#include <vector>

class Cell;
class CellRow;

class CellManager : public QFrame
{
    Q_OBJECT
public:
    CellManager( uint r, uint c, QWidget* parent=0, const char* name=0 );
    virtual ~CellManager();

    void setReadMode( bool rd ) { read_only = rd; setAcceptDrops( !read_only ); }
    bool getReadMode() const    { return read_only; }

    void showReady();
    void resetAll();
    void nextRow();
    void updateMe();

    void showLabels( bool b, bool link=FALSE );
    void showHLabel( bool b );
    void showVLabel( bool b );

    virtual void createRows() = 0;

    virtual void appendCellRow( uint ap ) = 0;
    virtual void removeCellRow( uint rm );
    void appendCells( uint ap );
    void removeCells( uint rm );

    Cell*    getCellAt( uint r, uint c );
    CellRow* getCellRowAt( uint idx );
    CellRow* getCellRowAt( int x, int y );
    CellRow* getCellRowAt( QPoint p );

    CellRow* getActiveRow() { return activeRow; }

    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;
    virtual bool installActiveRow( CellRow* ) = 0;

signals:
    void ready(bool);

protected:
    virtual void refreshPixmap();
    virtual void paintEvent( QPaintEvent* );
    virtual void drawCellContent( QPainter* );
    virtual void paintCell( Cell* cell, QPainter* p=0 ) = 0;
    virtual void paintCellRow( CellRow* cellrow, QPainter* p=0 );
    virtual void resizeEvent( QResizeEvent* );
    virtual void drawReady( Cell* ) {}

    virtual void drawLabels( QPainter* );
    virtual void drawHLabel( QPainter* );
    virtual void drawVLabel( QPainter* );

    void eraseCell( Cell* cell );
    void eraseCellRow( CellRow* cellrow );

    void placeRows();
    void computePosition( CellRow* cellrow, uint r );
    void addItem();
    void delItem();

protected:
    uint vLabel;
    uint hLabel;
    uint margin;
    uint row;
    uint col;
    bool dirty;
    bool dragging;
    bool delivered;
    bool read_only;
    uint active_ctr;
    uint intern_ctr;
    QPixmap  my_pixmap;
    CellRow* activeRow;
    std::vector<CellRow*> *cellrow_list;
    typedef std::vector<CellRow*>::iterator cellrow_iterator;

    bool m_showLabels;
    bool m_showHLabel;
    bool m_showVLabel;

private:
    CellManager( const CellManager& );
    CellManager& operator=( const CellManager& );
};

#endif // CELL_MANAGER_H
