MainWindow.cpp 4.31 KB
Newer Older
1 2 3
#include "..\Header\MainWindow.h"
#include "..\Header\OglViewerWidget.h"
#include <QSurfaceFormat>
Anakin's avatar
Anakin committed
4 5
#include <QMessageBox>
#include <QFileDialog>
6
#include <QPalette>
Anakin's avatar
Anakin committed
7 8
#include <QAction>
#include <QSignalMapper>
Anakin's avatar
Anakin committed
9 10
#include <QFile>
#include <QSizePolicy>
Anakin's avatar
Anakin committed
11
#include "..\Header\FileInterface.h"
Anakin's avatar
Anakin committed
12 13

#define WINDOW_NAME "Mesh Viewer"
14 15 16 17

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindowClass)
18
	, m_curSeverity(0)
19 20 21
{
	ui->setupUi(this);

Anakin's avatar
Anakin committed
22 23 24
	setWindowTitle(WINDOW_NAME);
	setWindowIcon(QIcon(":/images/icon.ico"));

25 26 27 28
	QSurfaceFormat format;
	format.setDepthBufferSize(24);
	QSurfaceFormat::setDefaultFormat(format);

Anakin's avatar
Anakin committed
29
	setupWidgets();
30 31

	ui->statusBar->showMessage("MeshViewer by Anakin", 0);
Anakin's avatar
Anakin committed
32 33

	m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -\n<detail>No file is open";
34 35 36 37 38
}

MainWindow::~MainWindow()
{
	delete ui;
Anakin's avatar
Anakin committed
39 40 41 42
}

void MainWindow::openFile()
{
Anakin's avatar
Anakin committed
43
	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Mesh (*.msh)");
Anakin's avatar
Anakin committed
44 45
	if(!fileName.isEmpty())
		emit loadFile(fileName);
Anakin's avatar
Anakin committed
46 47
}

Anakin's avatar
Anakin committed
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
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);

	QAction *openFile = new QAction("Open file", this);
	connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
	ui->mainToolBar->addAction(openFile);

	QSignalMapper* signalMapper = new QSignalMapper(this);

	QAction *x = new QAction("X", this);
	x->setCheckable(true);
	x->setChecked(true);
	ui->mainToolBar->addAction(x);

	QAction *y = new QAction("Y", this);
	y->setCheckable(true);
	y->setChecked(true);
	ui->mainToolBar->addAction(y);

	QAction *z = new QAction("Z", this);
	z->setCheckable(true);
	z->setChecked(true);
	ui->mainToolBar->addAction(z);

	connect(x, SIGNAL(triggered()), signalMapper, SLOT(map()));
	connect(y, SIGNAL(triggered()), signalMapper, SLOT(map()));
	connect(z, SIGNAL(triggered()), signalMapper, SLOT(map()));

	signalMapper->setMapping(x, 1);
	signalMapper->setMapping(y, 2);
	signalMapper->setMapping(z, 3);

	connect(signalMapper, SIGNAL(mapped(int)), viewer, SLOT(changeDirection(int)));


	QAction *fileInfo = new QAction("File info", this);
	connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
	ui->mainToolBar->addAction(fileInfo);

	QAction *help = new QAction("Help", this);
	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
	ui->mainToolBar->addAction(help);

	
}

Anakin's avatar
Anakin committed
96 97
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
98
	QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
99
		WINDOW_NAME,
Anakin's avatar
Anakin committed
100
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
101 102 103
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
104 105 106

	dialog->setStyleSheet("QLabel{min-width: 200px;}");
	dialog->setDetailedText(QString(m_fileInfo.right(m_fileInfo.size() - m_fileInfo.indexOf("<detail>") - 8)));
Anakin's avatar
Anakin committed
107
	dialog->exec();
Anakin's avatar
Anakin committed
108
	delete dialog;
Anakin's avatar
Anakin committed
109 110 111 112
}

void MainWindow::aboutTool()
{
113 114 115 116
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
117
		WINDOW_NAME,
118
		QString(file.readAll()),
Anakin's avatar
Anakin committed
119 120
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
121
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
122 123 124

	file.close();

Anakin's avatar
Anakin committed
125
	dialog->exec();
Anakin's avatar
Anakin committed
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

	delete dialog;
}

void MainWindow::setFileInfo(QString name, QStringList textures, int vertices, int triangle)
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
	m_fileInfo += QByteArray::number(textures.size());
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

	int count(0);
	for (auto& it : textures)
	{
		m_fileInfo += "Material ";
		m_fileInfo += QByteArray::number(count++);
		m_fileInfo += " - ";
		m_fileInfo += it;
		m_fileInfo += "\n";
	}
151 152
}

Anakin's avatar
Anakin committed
153
void MainWindow::printMessage(QString message, int severity)
154
{
Anakin's avatar
Anakin committed
155
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		return;

	m_curSeverity = severity;
	int time(0);
	QPalette palette;

	switch (severity)
	{
	case 1:
		time = 3000;
		palette.setColor(QPalette::WindowText, Qt::darkYellow);
		break;
	case 2:
		time = 3000;
		palette.setColor(QPalette::WindowText, Qt::red);
		break;
	case 0:
	default:
		time = 2000;
		palette.setColor(QPalette::WindowText, Qt::black);
		break;
	}

	ui->statusBar->setPalette(palette);
	ui->statusBar->showMessage(message, time);
181
}