/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "DialogOptions.h"
#include <QFontDialog>
#include <QFileDialog>
#include <QFont>
#include "Debugger.h"

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
DialogOptions::DialogOptions(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::on_btnStackFont_clicked() {
	QFont oldFont;
	oldFont.fromString(ui.txtStackFont->text());
	ui.txtStackFont->setText(QFontDialog::getFont(0, oldFont, this).toString());
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::on_btnMemoryFont_clicked() {
	QFont oldFont;
	oldFont.fromString(ui.txtMemoryFont->text());
	ui.txtMemoryFont->setText(QFontDialog::getFont(0, oldFont, this).toString());
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::on_btnSymbolDir_clicked() {
	QString s = QFileDialog::getExistingDirectory(
                	this,
                	"Choose a directory",
                	"",
                	QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	if(s.size() != 0) {
		ui.txtSymbolDir->setText(s);
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::on_btnPluginDir_clicked() {
	QString s = QFileDialog::getExistingDirectory(
                	this,
                	"Choose a directory",
                	"",
                	QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	if(s.size() != 0) {
		ui.txtPluginDir->setText(s);
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::showEvent(QShowEvent *event) {
	QDialog::showEvent(event);
	
	if(syntax == ATT) {
		ui.rdoSytntaxIntel->setChecked(false);
		ui.rdoSytntaxATT->setChecked(true);
	} else if(syntax == Intel) {
		ui.rdoSytntaxIntel->setChecked(true);
		ui.rdoSytntaxATT->setChecked(false);
	}
	
	if(closeBehaviour == Detach) {
		ui.rdoDetach->setChecked(true);
		ui.rdoKill->setChecked(false);
	} else {
		ui.rdoDetach->setChecked(false);
		ui.rdoKill->setChecked(true);
	}
	
	if(initialBreakPoint == EntryPoint) {
		ui.rdoBPEntry->setChecked(true);
		ui.rdoBPMain->setChecked(false);
	} else {
		ui.rdoBPEntry->setChecked(false);
		ui.rdoBPMain->setChecked(true);	
	}
	
	ui.chkWarnDataBreakpoint->setChecked(warnOnNoExecBP);
	
	ui.txtStackFont->setText(StackViewFont);
	ui.txtMemoryFont->setText(MemoryDumpViewFont);
	ui.txtSymbolDir->setText(symbolPath);
	ui.txtPluginDir->setText(pluginPath);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogOptions::on_okButton_clicked() {

	if(ui.rdoSytntaxIntel->isChecked()) {
		syntax = Intel;
	} else if(ui.rdoSytntaxATT->isChecked()) {
		syntax = ATT;
	}
	
	if(ui.rdoDetach->isChecked()) {
		closeBehaviour = Detach;
	} else if(ui.rdoKill->isChecked()) {
		closeBehaviour = Terminate;
	}
	
	StackViewFont		= ui.txtStackFont->text();
	MemoryDumpViewFont	= ui.txtMemoryFont->text();
	symbolPath			= ui.txtSymbolDir->text();
	pluginPath			= ui.txtPluginDir->text();
	
	if(ui.rdoBPMain->isChecked()) {
		initialBreakPoint = MainSymbol;
	} else if(ui.rdoBPEntry->isChecked()) {
		initialBreakPoint = EntryPoint;
	}
	
	warnOnNoExecBP = ui.chkWarnDataBreakpoint->isChecked();
}
