LoadRawDialog.h 5.02 KB
#ifndef LOADRAWDIALOG_H
#define LOADRAWDIALOG_H

#include "ui_LoadRawDialog.h"
#include "VolumeDataStruct.h"
#include "GlobalValues.h"
#include <QFileDialog>
#include <iostream>
using namespace std;



class LoadRawDialog : public QDialog
{
	Q_OBJECT

public:
	LoadRawDialog(QWidget *parent = 0, Qt::WFlags flags = 0)
	{
		ui.setupUi(this);
		file_size = 0;
		ui.cmbDataType->insertItem(0, tr("uchar"), GL_UNSIGNED_BYTE);
		ui.cmbDataType->insertItem(1, tr("float"), GL_FLOAT);
		ui.cmbDataType->insertItem(2, tr("int16"), GL_SHORT);

		ui.cmbInternalPrecision->insertItem(0, tr("8-bit"), GL_UNSIGNED_BYTE);
		ui.cmbInternalPrecision->insertItem(1, tr("32-float"), GL_FLOAT);
		ui.cmbInternalPrecision->insertItem(2, tr("16-bit"), GL_SHORT);
		newVolume.FileType = VOLUME_FILE_RAW;

		ui.cmbEndian->insertItem(0, tr("little"), LOAD_LITTLE_ENDIAN);
		ui.cmbEndian->insertItem(1, tr("big"), LOAD_BIG_ENDIAN);
	}
	~LoadRawDialog(){}

private:
	Ui::LoadRawDialogClass ui;

	//volume structure
	VolumeData newVolume;

	//loaded file size
	int file_size;

	//enables or disables all widgets
	void widgetsEnabled(bool b)
	{
		ui.spinHeaderSize->setEnabled(b);
		ui.cmbDataType->setEnabled(b);
		ui.spinNumComponents->setEnabled(b);
		ui.spinDataSizeX->setEnabled(b);
		ui.spinDataSizeY->setEnabled(b);
		ui.spinDataSizeZ->setEnabled(b);
		ui.cmbEndian->setEnabled(b);
	}
	void updateVolumeStructure()
	{
		newVolume.HeaderSize = ui.spinHeaderSize->text().toInt();
		newVolume.BitType = ui.cmbEndian->itemData(ui.cmbEndian->currentIndex()).toInt();
		newVolume.ExternalComponents = ui.spinNumComponents->text().toInt();
		newVolume.ExternalDatatype = ui.cmbDataType->itemData(ui.cmbDataType->currentIndex()).toInt();
		newVolume.InternalComponents = ui.spinNumComponents->text().toInt();
		newVolume.InternalDatatype = ui.cmbInternalPrecision->itemData(ui.cmbInternalPrecision->currentIndex()).toInt();
		newVolume.GetByteSize(newVolume.ExternalDatatype);
		newVolume.Dim.x = ui.spinDataSizeX->text().toInt();
		newVolume.Dim.y = ui.spinDataSizeY->text().toInt();
		newVolume.Dim.z = ui.spinDataSizeZ->text().toInt();
		newVolume.Normalized = (int)ui.chkNormalized->isChecked();
		//newVolume.Name = ui.txtName->text().toAscii();
	}
	void calculateSize()
	{
		int header = ui.spinHeaderSize->text().toInt();
		int components = ui.spinNumComponents->text().toInt();
		newVolume.ExternalDatatype = ui.cmbDataType->itemData(ui.cmbDataType->currentIndex()).toInt();
		int precision = newVolume.GetByteSize(newVolume.ExternalDatatype);
		int sx = ui.spinDataSizeX->text().toInt();
		int sy = ui.spinDataSizeY->text().toInt();
		int sz = ui.spinDataSizeZ->text().toInt();

		int calc_size = header + sx*sy*sz*components*precision;

		//update the calculated size on lblCalcSize
		ui.lblCalcSize->setText(QString::number(calc_size));

		//if the calculated size is > the size of the file on disk, don't allow OK
		if(calc_size > file_size)
		{
			QPalette plt;
			plt.setColor(QPalette::WindowText, Qt::red);
			ui.lblCalcSize->setPalette(plt);
			ui.okButton->setEnabled(false);
		}
		else if(calc_size < file_size)
		{
			QPalette plt;
			plt.setColor(QPalette::WindowText, Qt::black);
			ui.lblCalcSize->setPalette(plt);
			ui.okButton->setEnabled(true);
		}
		else
		{
			QPalette plt;
			plt.setColor(QPalette::WindowText, Qt::green);
			ui.lblCalcSize->setPalette(plt);
			ui.okButton->setEnabled(true);
		}
	}

public slots:
	void on_okButton_clicked()
	{
		updateVolumeStructure();
		newVolume.Name = newVolume.Filenames[0].getPrefix();
		LoadRawVolume(newVolume);
	}
	void on_spinHeaderSize_valueChanged(int x){calculateSize();}
	void on_spinDataSizeX_valueChanged(int x){calculateSize();}
	void on_spinDataSizeY_valueChanged(int x){calculateSize();}
	void on_spinDataSizeZ_valueChanged(int x){calculateSize();}
	void on_spinNumComponents_valueChanged(int x){calculateSize();}
	void on_cmbDataType_currentIndexChanged(int x){calculateSize();}
	void on_btnLoadFile_clicked()
	{
		//load a file dialog and grab the selected file name
		QStringList filenames =QFileDialog::getOpenFileNames(this,tr("Load RAW Volume"), QString(), tr("(*.*)"));

		if(!filenames.isEmpty())
		{
			//fill the newVolume structure with the filenames
			newVolume.Filenames.clear();
			for(int f=0; f<filenames.count(); f++)
				newVolume.Filenames.push_back(string(filenames[f].toAscii()));

			//update the textbox showing the first filename
			ui.txtFilename->setText(filenames[0]);

			//enable all of the widgets
			widgetsEnabled(true);
			
			//disable the z-widget if multiple files are selected
			if(filenames.count() > 1)
			{
				ui.spinDataSizeZ->setValue(1);
				ui.spinDataSizeZ->setEnabled(false);
			}

			//get the file size
			string file_string = (const char *)filenames[0].toAscii();
			cout<<file_string<<endl;
			FILE* f = fopen(file_string.c_str(), "rb");
			fseek(f, 0, SEEK_END);
			file_size = ftell(f);
			fclose(f);

			//update lblFileSize
			ui.lblFileSize->setText(QString::number(file_size));
			calculateSize();
		}
		else
		{
			//disable all of the widgets
			ui.txtFilename->setText(QString());
			widgetsEnabled(false);
		}

	}

};

#endif