MainWindow.cpp 3.68 KB
Newer Older
Anakin's avatar
Anakin committed
1 2
#include "..\Header\MainWindow.h"
#include "..\Header\OpenGlViewer.h"
3
#include "..\Header\MshFile.h"
Anakin's avatar
Anakin committed
4
#include "..\Header\defines.h"
Anakin's avatar
Anakin committed
5
#include <QKeyEvent>
Anakin's avatar
Anakin committed
6
#include <QMessageBox>
7
#include <QFileDialog>
Anakin's avatar
Anakin committed
8 9


Anakin's avatar
Anakin committed
10 11 12
/////////////////////////////////////////////////////////////////////////
// constructor/destructor

Anakin's avatar
Anakin committed
13 14 15 16 17 18 19 20 21 22 23 24
MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
	, ui (new Ui::MainWindowClass)
{
	setupWindow();
}

MainWindow::~MainWindow()
{
	delete ui;
}

Anakin's avatar
Anakin committed
25 26 27 28

/////////////////////////////////////////////////////////////////////////
// private functions

Anakin's avatar
Anakin committed
29 30 31 32 33 34 35 36 37 38
void MainWindow::setupWindow()
{
	ui->setupUi(this);

	this->setWindowTitle(WINDOW_NAME);
	this->setWindowIcon(QIcon(":/MainWindow/icon.ico"));
	this->resize(WINDOW_WIDTH, WINDOW_HEIGHT);

	this->setCentralWidget(new OpenGlViewer(this));

39
	ui->mainToolBar->addAction("Open File", this, &MainWindow::openFile);
Anakin's avatar
Anakin committed
40 41
	ui->mainToolBar->addAction("File Info", this, &MainWindow::aboutFile);

Anakin's avatar
Anakin committed
42
	ui->statusBar->showMessage(DEFAULT_STATUS_MESSAGE);
Anakin's avatar
Anakin committed
43 44
}

45 46
void MainWindow::import(const char * path)
{
Anakin's avatar
Anakin committed
47 48 49 50 51 52 53
	// variables
	std::vector<Model*>* tmp_models = nullptr;
	std::vector<TextureData*>* tmp_textures = new std::vector<TextureData*>;
	std::vector<std::string> tmp_texNames;
	BoundingBox tmp_bbox;

	// model file
54 55 56
	try
	{
		MshFile file(path);
Anakin's avatar
Anakin committed
57 58 59
		tmp_models = file.getModels();
		tmp_texNames = file.getTextureNames();
		tmp_bbox = file.getBoundingBox();
60 61 62 63 64 65 66 67 68 69
	}
	catch (std::invalid_argument e)
	{
		QMessageBox msg(this);
		msg.addButton(QMessageBox::Ok);
		msg.setText(QString::fromStdString(e.what()));
		msg.setIcon(QMessageBox::Critical);
		msg.setWindowTitle("Open File Error");

		msg.exec();
Anakin's avatar
Anakin committed
70
		return;
71
	}
Anakin's avatar
Anakin committed
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

	// parth to texture
	std::string tmp_path = path;
	while (tmp_path.back() != '/' && tmp_path.back() != '\\')
		tmp_path.pop_back();

	// load all textures
	for (auto& texIt : tmp_texNames)
	{
		TextureData* new_data = new TextureData;
		try
		{
			TextureTGA tmp_texFile(std::string(tmp_path + texIt).c_str());

			new_data->alpha = tmp_texFile.hasAlpha();
			new_data->width = tmp_texFile.getWidth();
			new_data->height = tmp_texFile.getHeight();
			new_data->data = tmp_texFile.getData();
		}
		catch (std::invalid_argument e)
		{
			new_data->alpha = true;
			new_data->width = 1;
			new_data->height = 1;
			new_data->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
		}

		tmp_textures->push_back(new_data);
	}

	// add a solid default color at the end (maybe there is an invalid index later)
	TextureData* new_data = new TextureData;
	new_data->alpha = true;
	new_data->width = 1;
	new_data->height = 1;
	new_data->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
	tmp_textures->push_back(new_data);

	// clean up texture name list
	tmp_texNames.clear();

	// give the data to the viewer
	OpenGlViewer* tmp_viewer = dynamic_cast<OpenGlViewer*>(centralWidget());
	tmp_viewer->setData(tmp_models, tmp_textures, tmp_bbox);
116 117
}

Anakin's avatar
Anakin committed
118

Anakin's avatar
Anakin committed
119 120
/////////////////////////////////////////////////////////////////////////
// private slots
Anakin's avatar
Anakin committed
121

Anakin's avatar
Anakin committed
122 123
void MainWindow::openFile()
{
124 125 126
	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "../Release/Msh", "Mesh (*.msh)");

	import(fileName.toStdString().c_str());
Anakin's avatar
Anakin committed
127 128 129 130 131 132 133 134
}

void MainWindow::aboutFile()
{
	//TODO: Open Window with file information
	QMessageBox* dialog = new QMessageBox(QMessageBox::Information, WINDOW_NAME, "File Info", QMessageBox::StandardButton::Close, this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
	dialog->setDetailedText("This is the cool detailed text\n");
	dialog->exec();
Anakin's avatar
Anakin committed
135 136
}

Anakin's avatar
Anakin committed
137 138 139 140

/////////////////////////////////////////////////////////////////////////
// events

Anakin's avatar
Anakin committed
141 142 143 144 145 146 147 148 149 150 151
void MainWindow::keyPressEvent(QKeyEvent * keyEvent)
{
	switch (keyEvent->key())
	{
	case Qt::Key::Key_Escape:
		close();
		break;
	}

	QMainWindow::keyPressEvent(keyEvent);
}