/* This file is part of the KDE project
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "utils.h"
#include "utils_p.h"

#include <tqheader.h>
#include <tqlayout.h>

#include <ktabwidget.h>
#include <tdelistview.h>
#include <kiconloader.h>
#include <kdialogbase.h>
#include <kpushbutton.h>
#include <kguiitem.h>

#ifdef KEXI_DEBUG_GUI

static DebugWindowDialog* debugWindow = 0;
static KTabWidget* debugWindowTab = 0;
static TDEListView* kexiDBDebugPage = 0;
static TDEListView* kexiAlterTableActionDebugPage = 0;

TQWidget *KexiUtils::createDebugWindow(TQWidget *parent)
{
	// (this is internal code - do not use i18n() here)
	debugWindow = new DebugWindowDialog(parent);
	debugWindow->setSizeGripEnabled( true );
	TQBoxLayout *lyr = new TQVBoxLayout(debugWindow, KDialogBase::marginHint());
	debugWindowTab = new KTabWidget(debugWindow, "debugWindowTab");
	lyr->addWidget( debugWindowTab );
	debugWindow->resize(900, 600);
	debugWindow->setIcon( DesktopIcon("application-vnd.tde.info") );
	debugWindow->setCaption("Kexi Internal Debugger");
	debugWindow->show();
	return debugWindow;
}

void KexiUtils::addKexiDBDebug(const TQString& text)
{
	// (this is internal code - do not use i18n() here)
	if (!debugWindowTab)
		return;
	if (!kexiDBDebugPage) {
		TQWidget *page = new TQWidget(debugWindowTab);
		TQVBoxLayout *vbox = new TQVBoxLayout(page);
		TQHBoxLayout *hbox = new TQHBoxLayout(page);
		vbox->addLayout(hbox);
		hbox->addStretch(1);
		KPushButton *btn_clear = new KPushButton(KGuiItem("Clear", "clear_left"), page);
		hbox->addWidget(btn_clear);

		kexiDBDebugPage = new TDEListView(page, "kexiDbDebugPage");
		TQObject::connect(btn_clear, TQ_SIGNAL(clicked()), kexiDBDebugPage, TQ_SLOT(clear()));
		vbox->addWidget(kexiDBDebugPage);
		kexiDBDebugPage->addColumn("");
		kexiDBDebugPage->header()->hide();
		kexiDBDebugPage->setSorting(-1);
		kexiDBDebugPage->setAllColumnsShowFocus ( true );
		kexiDBDebugPage->setColumnWidthMode( 0, TQListView::Maximum );
		kexiDBDebugPage->setRootIsDecorated( true );
		debugWindowTab->addTab( page, "KexiDB" );
		debugWindowTab->showPage(page);
		kexiDBDebugPage->show();
	}
	//add \n after (about) every 30 characters
//TODO	TQString realText

	TDEListViewItem * li = new TDEListViewItem( kexiDBDebugPage, kexiDBDebugPage->lastItem(), text );
	li->setMultiLinesEnabled( true );
}

void KexiUtils::addAlterTableActionDebug(const TQString& text, int nestingLevel)
{
	// (this is internal code - do not use i18n() here)
	if (!debugWindowTab)
		return;
	if (!kexiAlterTableActionDebugPage) {
		TQWidget *page = new TQWidget(debugWindowTab);
		TQVBoxLayout *vbox = new TQVBoxLayout(page);
		TQHBoxLayout *hbox = new TQHBoxLayout(page);
		vbox->addLayout(hbox);
		hbox->addStretch(1);
		KPushButton *btn_exec = new KPushButton(KGuiItem("Real Alter Table", "document-save"), page);
		btn_exec->setName("executeRealAlterTable");
		hbox->addWidget(btn_exec);
		KPushButton *btn_clear = new KPushButton(KGuiItem("Clear", "clear_left"), page);
		hbox->addWidget(btn_clear);
		KPushButton *btn_sim = new KPushButton(KGuiItem("Simulate Execution", "application-x-executable"), page);
		btn_sim->setName("simulateAlterTableExecution");
		hbox->addWidget(btn_sim);

		kexiAlterTableActionDebugPage = new TDEListView(page, "kexiAlterTableActionDebugPage");
		TQObject::connect(btn_clear, TQ_SIGNAL(clicked()), kexiAlterTableActionDebugPage, TQ_SLOT(clear()));
		vbox->addWidget(kexiAlterTableActionDebugPage);
		kexiAlterTableActionDebugPage->addColumn("");
		kexiAlterTableActionDebugPage->header()->hide();
		kexiAlterTableActionDebugPage->setSorting(-1);
		kexiAlterTableActionDebugPage->setAllColumnsShowFocus ( true );
		kexiAlterTableActionDebugPage->setColumnWidthMode( 0, TQListView::Maximum );
		kexiAlterTableActionDebugPage->setRootIsDecorated( true );
		debugWindowTab->addTab( page, "AlterTable Actions" );
		debugWindowTab->showPage(page);
		page->show();
	}
	if (text.isEmpty()) //don't move up!
		return;
	TDEListViewItem * li;
	int availableNestingLevels = 0;
	// compute availableNestingLevels
	TQListViewItem * lastItem = kexiAlterTableActionDebugPage->lastItem();
	//kdDebug() << "lastItem: " << (lastItem ? lastItem->text(0) : TQString()) << endl;
	while (lastItem) {
		lastItem = lastItem->parent();
		availableNestingLevels++;
	}
	//kdDebug() << "availableNestingLevels: " << availableNestingLevels << endl;
	//go up (availableNestingLevels-levelsToGoUp) levels
	lastItem = kexiAlterTableActionDebugPage->lastItem();
	int levelsToGoUp = availableNestingLevels - nestingLevel;
	while (levelsToGoUp > 0 && lastItem) {
		lastItem = lastItem->parent();
		levelsToGoUp--;
	}
	//kdDebug() << "lastItem2: " << (lastItem ? lastItem->text(0) : TQString()) << endl;
	if (lastItem) {
		TQListViewItem *after = lastItem->firstChild(); //find last child so we can insert a new item after it
		while (after && after->nextSibling())
			after = after->nextSibling();
		if (after)
			li = new TDEListViewItem( lastItem, after, text ); //child, after
		else
			li = new TDEListViewItem( lastItem, text ); //1st child
	}
	else {
		lastItem = kexiAlterTableActionDebugPage->lastItem();
		while (lastItem && lastItem->parent())
			lastItem = lastItem->parent();
		//kdDebug() << "lastItem2: " << (lastItem ? lastItem->text(0) : TQString()) << endl;
		li = new TDEListViewItem( kexiAlterTableActionDebugPage, lastItem, text ); //after
	}
	li->setOpen(true);
	li->setMultiLinesEnabled( true );
}

void KexiUtils::connectPushButtonActionForDebugWindow(const char* actionName, 
	const TQObject *receiver, const char* slot)
{
	if (debugWindow) {
		KPushButton* btn = findFirstChild<KPushButton>(debugWindow, "KPushButton", actionName);
		if (btn)
			TQObject::connect(btn, TQ_SIGNAL(clicked()), receiver, slot);
	}
}

#endif //KEXI_DEBUG_GUI
