/* Bak-Tang-Wiesenfeld sandpile animation (version 2.02 - Windows)
 *
 * Copyright (C) 2005 N.R. Moloney 
 *
 * Distributed under the Qt Non-Commercial license version 1.0
 */

#include <qapplication.h>
#include <qcombobox.h>
#include <qgroupbox.h>
#include <qpainter.h>
#include <qpushbutton.h>
#include <qvbuttongroup.h> 
#include <qwidget.h>

#include "controls.h"
#include "btw2d.h"

Controls::Controls( QWidget * parent, const char * name ) : 
  QWidget( parent, name )
{
  Sandpile * sandpile = new Sandpile( this, "sandpile" ); 
  sandpile->move( 0,offsety );
  sandpile->resize( 800,600 );

  QVButtonGroup * display = new QVButtonGroup( this, "display" );
  display->move( rightAlign, 50 );
  display->resize( buttonWidth,buttonHeight );

  QPushButton * displayButton =
    new QPushButton( "Display", display, "displayButton" );
  displayButton->setToggleButton( TRUE );
  displayButton->setOn( TRUE );
  QObject::connect( displayButton, SIGNAL( toggled( bool ) ),
                    sandpile, SLOT( slotDisplay( bool ) ) );

  QVButtonGroup * startAgain = new QVButtonGroup( this, "startAgain" );
  startAgain->move( rightAlign, 115 );
  startAgain->resize( buttonWidth,buttonHeight );

  QPushButton * startAgainButton = 
    new QPushButton( "Re-initialise", startAgain, "startAgainButton" );
  QObject::connect( startAgainButton, SIGNAL( clicked() ), 
		    sandpile, SLOT( slotStartAgain() ) );

  QVButtonGroup * burn = new QVButtonGroup( this, "burn" );
  burn->move( rightAlign, 180 );
  burn->resize( buttonWidth,buttonHeight );

  QPushButton * burnButton = 
    new QPushButton( "Burn", burn, "burnButton" );
  burnButton->setToggleButton( TRUE );
  QObject::connect( burnButton, SIGNAL( toggled( bool ) ), 
		    sandpile, SLOT( slotBurn( bool ) ) );

  QGroupBox * mode = new QGroupBox( "Mode", this, "mode" );
  mode->move( rightAlign, 245 );
  mode->resize( buttonWidth,comboBoxHeight );

  QComboBox * runningStyle = new QComboBox( mode, "read-only combobox" );
  runningStyle->move( 10,25 );  
  runningStyle->resize( 140,30 );
  runningStyle->insertItem( "run free" );
  runningStyle->insertItem( "sprinkle" );
  runningStyle->insertItem( "fractalsand 0" );
  runningStyle->insertItem( "fractalsand 1" );
  runningStyle->insertItem( "fractalsand 2" );

  QObject::connect( runningStyle, SIGNAL( activated( int ) ), 
		    sandpile, SLOT( slotRunningStyle( int ) ) );

  QGroupBox * speed = new QGroupBox( "Speed", this, "speed" );
  speed->move( rightAlign, 325 );
  speed->resize( buttonWidth,comboBoxHeight );

  QComboBox * runningSpeed = new QComboBox( speed, "read-only combobox" );
  runningSpeed->move( 10,25 );
  runningSpeed->resize( 140,30 );
  runningSpeed->insertItem( "fast" );
  runningSpeed->insertItem( "slow" );
  runningSpeed->insertItem( "pause" );

  QObject::connect( runningSpeed, SIGNAL( activated( int ) ),
                    sandpile, SLOT( slotSpeed( int ) ) );

  QPainter paint( this );
  paint.drawText( rightAlign,440, "System size L = 149" );
  paint.drawText( rightAlign+30,480, "Site with z = 0" );
  paint.drawText( rightAlign+30,500, "Site with z = 1" );
  paint.drawText( rightAlign+30,520, "Site with z = 2" );
  paint.drawText( rightAlign+30,540, "Site with z = 3" );
  paint.drawText( rightAlign+30,560, "Toppling site" );

  paint.setPen( NoPen );
  paint.setBrush( Qt::black );
  paint.drawRect( rightAlign,470, 10,10 );
  paint.setBrush( Qt::darkGray );
  paint.drawRect( rightAlign,490, 10,10 );
  paint.setBrush( Qt::lightGray );
  paint.drawRect( rightAlign,510, 10,10 );
  paint.setBrush( Qt::white );
  paint.drawRect( rightAlign,530, 10,10 );
  paint.setBrush( Qt::red );
  paint.drawRect( rightAlign,550, 10,10 );

  QVButtonGroup * exit = new QVButtonGroup( this, "exit" );
  exit->move( rightAlign,595 );
  exit->resize( buttonWidth,buttonHeight );

  QPushButton * exitButton = new QPushButton( "Quit", exit, "exitButton" );
  QObject::connect( exitButton, SIGNAL( clicked() ), qApp, SLOT( quit() ) );

}

///////////////////////////////////////////////////

void Controls::paintEvent( QPaintEvent * )
{
  QPainter paint( this );
  paint.drawText( rightAlign,440, "System size L = 149" );
  paint.drawText( rightAlign+30,480, "Site with z = 0" );
  paint.drawText( rightAlign+30,500, "Site with z = 1" );
  paint.drawText( rightAlign+30,520, "Site with z = 2" );
  paint.drawText( rightAlign+30,540, "Site with z = 3" );
  paint.drawText( rightAlign+30,560, "Toppling site" );

  paint.setPen( NoPen );
  paint.setBrush( Qt::black );
  paint.drawRect( rightAlign,470, 10,10 );
  paint.setBrush( Qt::darkGray );
  paint.drawRect( rightAlign,490, 10,10 );
  paint.setBrush( Qt::lightGray );
  paint.drawRect( rightAlign,510, 10,10 );
  paint.setBrush( Qt::white );
  paint.drawRect( rightAlign,530, 10,10 );
  paint.setBrush( Qt::red );
  paint.drawRect( rightAlign,550, 10,10 );
  
  return;
}