Blame view

LoadRawDialog.h 5.02 KB
ee96a02c   David Mayerich   first commit from...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  #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