#!/usr/bin/env kjscmd

StdDirs.addResourceType("buttonmaker", StdDirs.kde_default("data") + "/buttonmaker");

var imgfx = new ImageFX();
var win = new KMainWindow(this);

try {
//var view = Factory.loadui(StdDirs.findResource("buttonmaker", "ButtonMakerUI.ui"), this, win);
  var view = Factory.loadui("ButtonMakerUI.ui", this, win);
}
catch(err)
{
	alert( err );
	exit();
}

var quitAction = StdAction.quit( 0, 0, win.actionCollection() );
var saveAction = StdAction.save( 0, 0, win.actionCollection() );
var newAction = StdAction.fileNew( 0, 0, win.actionCollection() );

win.connect( quitAction, 'activated()', this, 'exit');
win.connect( saveAction, 'activated()', this, 'saveImage');
win.connect( newAction, 'activated()', this, 'resetView');


win.connect(view.TextBox.Font, 'fontSelected(const QFont&)', this, 'updateUI' );
win.connect(view.TextBox.TextColors, 'fgChanged(const QColor&)', this, 'updateUI' );
win.connect(view.TextBox.TextColors, 'bgChanged(const QColor&)', this, 'updateUI' );
win.connect(view.TextBox.Offset, 'valueChanged(int)', this, 'updateUI' );
win.connect(view.TextBox.YOffset, 'valueChanged(int)', this, 'updateUI' );
win.connect(view.TextBox.XOffset, 'valueChanged(int)', this, 'updateUI' );

win.connect(view.Text, 'returnPressed(const QString&)', this, 'updateUI');

win.connect(view.Mode, 'released(int)', this, 'changeMode');
win.connect(view.Mode.Xsize, 'valueChanged(int)', this, 'updatePillBox' );
win.connect(view.Mode.Ysize, 'valueChanged(int)', this, 'updatePillBox' );
win.connect(view.Mode.BaseColor, 'changed(const QColor&)', this, 'updatePillBox' );
win.connect(view.Mode.BaseImage, 'urlSelected(const QString&)', this, 'loadImage');

win.connect(view.Mode.Xscale, 'valueChanged(int)', this, 'updateUI' );
win.connect(view.Mode.Yscale, 'valueChanged(int)', this, 'updateUI' );

var bgimage = new Pixmap();
bgimage.resize(16,32);
bgimage.fill("blue");

resetView();

win.createGUI("buttonmaker.rc");
win.setCentralWidget(view);
win.setCaption("ButtonMaker");
win.show();
application.exec();

function CustomPillBox()
{
	this.a = new Image();
	this.b = new Image();
	this.c = new Image();
	this.d = new Image();
	this.e = new Image();
	this.f = new Image();
	this.g = new Image();
	this.h = new Image();
	this.i = new Image();

	this.height = function() {
		return this.a.height() + this.d.height() + this.g.height();
	}

	this.width = function() {
		return this.a.width() + this.b.width() + this.c.width();
	}

	this.loadImages = function() {
		return (
		this.a.load(StdDirs.findResource("buttonmaker", "01.png")) &&
		this.b.load(StdDirs.findResource("buttonmaker", "02.png")) &&
		this.c.load(StdDirs.findResource("buttonmaker", "03.png")) &&
		this.d.load(StdDirs.findResource("buttonmaker", "04.png")) &&
		this.e.load(StdDirs.findResource("buttonmaker", "05.png")) &&
		this.f.load(StdDirs.findResource("buttonmaker", "06.png")) &&
		this.g.load(StdDirs.findResource("buttonmaker", "07.png")) &&
		this.h.load(StdDirs.findResource("buttonmaker", "08.png")) &&
		this.i.load(StdDirs.findResource("buttonmaker", "09.png")));
	}

	this.colorize = function( color ) {
		var percent = 0.55;
		this.a = imgfx.blendColor( color, this.a, percent);
		this.b = imgfx.blendColor( color, this.b, percent);
		this.c = imgfx.blendColor( color, this.c, percent);
		this.d = imgfx.blendColor( color, this.d, percent);
		this.e = imgfx.blendColor( color, this.e, percent);
		this.f = imgfx.blendColor( color, this.f, percent);
		this.g = imgfx.blendColor( color, this.g, percent);
		this.h = imgfx.blendColor( color, this.h, percent);
		this.i = imgfx.blendColor( color, this.i, percent);
	}

	this.resize = function( newW, newH ) {

		this.b.smoothScale( newW - ( this.a.width() + this.c.width() ), this.b.height() );
		this.h.smoothScale( newW - ( this.g.width() + this.i.width() ), this.h.height() );

		this.d.smoothScale( this.d.width(), newH - ( this.a.height() + this.g.height() ) );
		this.f.smoothScale( this.f.width(), newH - ( this.c.height() + this.i.height() ) );

		this.e.smoothScale( newW - ( this.a.width() + this.c.width() ),
			 newH - ( this.c.height() + this.i.height() ) );
	}

	this.pixmap = function(width, height){
		this.resize(width, height);
		var pix = new Pixmap();
		pix.resize(width, height);
		pix.fill("white");
		  
		var painter = new Painter();
		try {
			if( painter.begin(pix) )
			{
				//painter.drawRect(0,0,this.width(), this.height());
				painter.drawImage(0, 0, this.a, 0, 0, -1, -1, 0);
				painter.drawImage(0, this.a.height(), this.d, 0, 0, -1, -1, 0);
				painter.drawImage(0, ( this.height() - this.g.height() ) ,this.g, 0, 0, -1, -1, 0);
		
				painter.drawImage(this.a.width(), 0, this.b, 0, 0, -1, -1, 0);
				painter.drawImage(this.d.width(), this.b.height(), this.e, 0, 0, -1, -1, 0);
				painter.drawImage(this.g.width(), ( this.height() - this.h.height() ) ,this.h, 0, 0, -1, -1, 0);
		
				painter.drawImage(this.a.width() + this.b.width(), 0, this.c, 0, 0, -1, -1, 0);
				painter.drawImage(this.d.width() + this.e.width(), this.c.height(), this.f, 0, 0, -1, -1, 0);
				painter.drawImage(this.g.width() + this.h.width(), ( this.height() - this.i.height() ) ,this.i, 0, 0, -1, -1, 0);
				if( painter.end() )
					pix = painter.pixmap();
			}
			else
			  alert("Could not paint to the pixmap.");
		} catch (err) { alert(err); }
		return pix;
	}

}

function render(txt, bgimg, font, offset, fgColor, bgColor, xOffset, yOffset)
{
	var h = bgimg.height();
	var w = bgimg.width();
	var fg = new Pen();
	var bg = new Pen();
	fg.setColor(fgColor);
	bg.setColor(bgColor);

	var painter = new Painter();
	try {
		if (painter.begin(bgimg))
		{
			painter.scale( view.Mode.Xscale.value, view.Mode.Yscale.value );
			painter.setPen(bg);
			painter.setFont(font);
			var box = painter.textBox(txt);
			var txtX = (w/2) - (box.width()/2) + xOffset;
			var txtY = (h/2) + (box.height()/2) + yOffset;
			painter.drawText(txtX+offset,txtY+offset,txt);
			painter.setPen(fg);
			painter.drawText(txtX,txtY,txt);
			painter.end()
			return painter.pixmap();
		}
		else
		  alert("Could not paint to the pixmap.");
	} catch (err) { alert(err); }
	return new Pixmap();
}

function changeMode(mode)
{
	if( mode == 0)
	{
		loadImage(view.Mode.BaseImage.url);
	}
	else
	{
		var pb = new CustomPillBox();
		if ( !pb.loadImages() )
			alert("Error Loading Resources!");
		else
		{
			pb.colorize(view.Mode.BaseColor.color);
			bgimage = pb.pixmap(view.Mode.Xsize.value,view.Mode.Ysize.value);
		}
		updateUI();
	}
}

function updatePillBox()
{
	var pb = new CustomPillBox();
	if ( !pb.loadImages() )
		alert("Error Loading Resources!");
	else
	{
		pb.colorize(view.Mode.BaseColor.color);
		bgimage = pb.pixmap(view.Mode.Xsize.value,view.Mode.Ysize.value);
	}
	updateUI();
}

function updateUI()
{
	view.PreviewBox.Preview.pixmap = render(view.Text.text, 
						bgimage, 
						view.TextBox.Font.font, 
						view.TextBox.Offset.value, 
						view.TextBox.TextColors.foreground, 
						view.TextBox.TextColors.background,
						view.TextBox.XOffset.value,
						view.TextBox.YOffset.value);
	win.adjustSize();
}

function saveImage()
{
	var img = new Image(this);
	var fileName = StdDialog.getSaveFileName();
	if( fileName != "")
	{
		var pixmap = render(view.Text.text, 
					bgimage, 
					view.TextBox.Font.font, 
					view.TextBox.Offset.value, 
					view.TextBox.TextColors.foreground, 
					view.TextBox.TextColors.background,
					view.TextBox.XOffset.value,
					view.TextBox.YOffset.value);
		img.setPixmap(pixmap);
		img.save(fileName);
	}
}

function resetView()
{
	view.TextBox.Offset.value = 1;
	if ( application.args.length > 0 )
		view.Text.text = application.args[0];
	else
		view.Text.text = "";
	view.TextBox.Font.font = "";
	view.TextBox.TextColors.foreground = "black";
	view.TextBox.TextColors.background = "white";
	view.Mode.BaseImage.url = StdDirs.findResource("buttonmaker", "default.png");
	loadImage(view.Mode.BaseImage.url);
}

function loadImage(file)
{
	var img = new Image(this);
	if (img.load(file) )
	{
		bgimage = img.pixmap();
		updateUI();
	}
}
