/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  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
 *
 *  qtdemo004.spl: An example for Qt and co-routines: Hanoi Assistent
 */

load "qt";
load "task";

var qapp = new qt.QApplication();
var win = qt_ui("qtdemo004.ui");

function about()
{
	var edit = qt_child(win, "edit", "QTextEdit", 1);
	edit.setBold(1);
	edit.append(<:>
		: --------------------------------------------------------------
		: ** Hanoi Assistent **
		: A little demo for using SPL co-routines in SPL Qt applications.
		: Written by Clifford Wolf <clifford@clifford.at>.
		: --------------------------------------------------------------
	</>);
}

var hanoi_counter;
function hanoi_worker(from, to, temp, level)
{
	if (level > 1)
		hanoi_worker(from, temp, to, level-1);

	var bar = qt_child(win, "progress", "QProgressBar", 1);
	bar.setProgress(++hanoi_counter);

	task_co_return("#$hanoi_counter: Move slide from stack $from to stack $to.");

	if (level > 1)
		hanoi_worker(temp, to, from, level-1);
}

function hanoi_start()
{
	hanoi_counter = 0;
	task_kill("hanoi");
	task_create("hanoi", "hanoi_worker(1, 3, 2, 5);");

	var butt = qt_child(win, "button", "QPushButton", 1);
	butt.setEnabled(1);

	var edit = qt_child(win, "edit", "QTextEdit", 1);
	edit.clear();

	var bar = qt_child(win, "progress", "QProgressBar", 1);
	bar.setPercentageVisible(1);
	bar.setProgress(0, 31);
}

function hanoi_next()
{
	var next_move_desc = task_co_call("hanoi");

	var edit = qt_child(win, "edit", "QTextEdit", 1);
	edit.setBold(0);
	edit.append(next_move_desc ~ "\n");

	if (hanoi_counter == 31) {
		edit.setBold(1);
		edit.append("-- FINISHED --\n");
		var butt = qt_child(win, "button", "QPushButton", 1);
		butt.setEnabled(0);
	}
}

qt_signal_callback(win.child("helpAboutAction", undef, 1), "activated()", about);
qt_signal_callback(win.child("fileNewAction", undef, 1), "activated()", hanoi_start);
qt_signal_callback(win.child("button", undef, 1), "clicked()", hanoi_next);

qapp.setMainWidget(win);
win.show();

about();
qapp.exec();

