SWBF2-Classic-Msh-Viewer/MeshViewerQt/Source/MainWindow.cpp

143 lines
3.3 KiB
C++

#include "..\Header\MainWindow.h"
#include "..\Header\OpenGlViewer.h"
#include "..\Header\MshFile.h"
#include "..\Header\defines.h"
#include <QKeyEvent>
#include <QMessageBox>
#include <QFileDialog>
/////////////////////////////////////////////////////////////////////////
// constructor/destructor
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui (new Ui::MainWindowClass)
{
setupWindow();
}
MainWindow::~MainWindow()
{
delete ui;
}
/////////////////////////////////////////////////////////////////////////
// private functions
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));
ui->mainToolBar->addAction("Open File", this, &MainWindow::openFile);
ui->mainToolBar->addAction("File Info", this, &MainWindow::aboutFile);
ui->statusBar->showMessage(DEFAULT_STATUS_MESSAGE);
}
void MainWindow::import(const char * path)
{
try
{
MshFile file(path);
// Models
std::vector<Model*>* tmp_models = file.getModels();
// Textures
std::vector<std::string> tmp_texNames = file.getTextureNames();
std::vector<QImage*>* tmp_textures = new std::vector<QImage*>;
std::string tmp_path = path;
while (tmp_path.back() != '/' && tmp_path.back() != '\\')
tmp_path.pop_back();
for (auto& it : tmp_texNames)
{
QImage* tmp_image = new QImage;
std::string test = tmp_path + it;
QString test2 = "D:\\workspaces\\Visual Studio 2015\\Projects\\OpenGL\\Release\\Msh\\texture32R.tga";
if (tmp_image->load(test2))
tmp_textures->push_back(tmp_image);
else
{
delete tmp_image;
tmp_image = new QImage(1, 1, QImage::Format_RGB32);
tmp_image->fill(Qt::red);
tmp_textures->push_back(tmp_image);
}
}
// add a solid default color at the end (maybe there is an invalid index later)
QImage* tmp_image = new QImage(1, 1, QImage::Format_RGB16);
tmp_image->fill(Qt::red);
tmp_textures->push_back(tmp_image);
tmp_texNames.clear();
// Bounding Box
BoundingBox tmp_bbox = file.getBoundingBox();
OpenGlViewer* tmp_viewer = dynamic_cast<OpenGlViewer*>(centralWidget());
tmp_viewer->setData(tmp_models, tmp_textures, tmp_bbox);
}
catch (std::invalid_argument e)
{
//TODO:
QMessageBox msg(this);
msg.addButton(QMessageBox::Ok);
msg.setText(QString::fromStdString(e.what()));
msg.setIcon(QMessageBox::Critical);
msg.setWindowTitle("Open File Error");
msg.exec();
}
}
/////////////////////////////////////////////////////////////////////////
// private slots
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "../Release/Msh", "Mesh (*.msh)");
import(fileName.toStdString().c_str());
}
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();
}
/////////////////////////////////////////////////////////////////////////
// events
void MainWindow::keyPressEvent(QKeyEvent * keyEvent)
{
switch (keyEvent->key())
{
case Qt::Key::Key_Escape:
close();
break;
}
QMainWindow::keyPressEvent(keyEvent);
}