/*
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 "DialogReferences.h"
#include "DebuggerCoreInterface.h"
#include "Debugger.h"
#include "MemoryRegions.h"

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


//------------------------------------------------------------------------------
// Name: showEvent(QShowEvent *)
// Desc: 
//------------------------------------------------------------------------------
void DialogReferences::showEvent(QShowEvent *) {
	ui.listWidget->clear();
	ui.progressBar->setValue(0);
}

//------------------------------------------------------------------------------
// Name: on_btnFind_clicked()
// Desc: find button event handler
//------------------------------------------------------------------------------
void DialogReferences::on_btnFind_clicked() {
	bool ok;
	const BaseTypes::address_t address = ui.txtAddress->text().toUInt(&ok, 16);
	
	ui.listWidget->clear();
	ui.progressBar->setValue(0);
	if(ok) {
		if(Debugger::memoryRegions != 0) {
			Debugger::memoryRegions->sync();
			QVector<MemoryRegions::Region> r = Debugger::memoryRegions->regions();
			const float regionPercentStep = 1.0 / static_cast<double>(r.size()) * 100;
			float regionBase = 0.0;
			
			
			for(QVector<MemoryRegions::Region>::const_iterator it = r.begin(); it != r.end(); ++it) {
				BaseTypes::address_t potentialReference;

				const BaseTypes::address_t howMany = it->end - it->start;

				for(BaseTypes::address_t addr = it->start; addr != it->end; addr += sizeof(BaseTypes::address_t)) {
					if(Debugger::debuggerBase->readBytes(addr, &potentialReference, sizeof(potentialReference))) {
						if(potentialReference == address) {
							ui.listWidget->addItem(QString().sprintf("%08x", addr));
						}
					}
					const BaseTypes::address_t howManyDone = addr - it->start;
					const int percent = static_cast<int>(regionBase + (static_cast<double>(howManyDone) / static_cast<double>(howMany) * regionPercentStep));
					ui.progressBar->setValue(percent);
				}
				regionBase += regionPercentStep;
			}
		}
		ui.progressBar->setValue(100);
	}
}

//------------------------------------------------------------------------------
// Name: on_listWidget_itemDoubleClicked(QListWidgetItem *item)
// Desc: follows the foudn item in the data view
//------------------------------------------------------------------------------
void DialogReferences::on_listWidget_itemDoubleClicked(QListWidgetItem *item) {
	bool ok;
	const QString s = item->text();
	const BaseTypes::address_t addr = s.toUInt(&ok, 16);
	if(ok) {
		Debugger::dumpData(addr);
	}
}
