/* $Id: StartupDialog.cpp,v 1.13 2003/01/12 16:19:56 rjesse Exp $
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2000-2003, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 */

#include "StartupDialog.h"
#include "Mango.h"
#include <qprogressbar.h>
#include <qtimer.h>

/*!
  \param timeout timeout in seconds until the dialog closes automatically.
         Whatever selection has focus at that time gets selected.
  \param parent the parent widget (see QWidget documentation).
  \param name name of this widget (see QWidget documentation).
*/
StartupDialog::StartupDialog( const int timeout,
                              QWidget* parent,
                              const char* name)
   : StartupForm( parent, name, false, Qt::WDestructiveClose)
{
   m_choiceMade = false;

   buttonContinue->setFocus();
   progressBar->setPercentageVisible( false);
   m_curTimeout = timeout * 10;	// one step every 0.1 sec
   progressBar->setTotalSteps( m_curTimeout);
   progressBar->setProgress( m_curTimeout);
   m_timer = new QTimer( this);
   connect( m_timer, SIGNAL( timeout()), this, SLOT( decreseTimeout()));
   m_timer->start( 100, false);
   
   Mango* mango = Mango::getInstance();
   connect( this, SIGNAL( startNew()), mango, SLOT( slotNew()));
   connect( this, SIGNAL( openExisting()), mango, SLOT( slotOpen()));
   connect( this, SIGNAL( continueFromLastTime()),
			mango, SLOT( slotContinueFromLastTime()));
}

/*!
  Don't close me without making a choice!
*/
void
StartupDialog::closeEvent( QCloseEvent* e)
{
   if (m_choiceMade) 
	  StartupForm::closeEvent( e);
}

void
StartupDialog::decreseTimeout()
{
   m_curTimeout--;
   progressBar->setProgress( m_curTimeout);
   if (m_curTimeout == 0) {
      if (buttonNew->hasFocus()) slotNew();
      if (buttonContinue->hasFocus()) slotContinue();
      if (buttonOpen->hasFocus()) slotOpen();
      // in case the whole dialog did not have focus, do this:
      slotContinue();
   }
}

void
StartupDialog::slotContinue()
{
   m_choiceMade = true;
   if (m_timer->isActive()) m_timer->stop();
   emit continueFromLastTime();
   close( true);
}

void
StartupDialog::slotNew()
{
   m_choiceMade = true;
   if (m_timer->isActive()) m_timer->stop();
   emit startNew();
   close( true);
}

void
StartupDialog::slotOpen()
{
   m_choiceMade = true;
   if (m_timer->isActive()) m_timer->stop();
   emit openExisting();
   close( true);
}
