MainWindow.cpp 4.58 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
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);

Anakin's avatar
Anakin committed
53
	QAction *openFile = new QAction(QIcon(":/images/toolbar/open.png"), "Open file", this);
Anakin's avatar
Anakin committed
54 55 56
	connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
	ui->mainToolBar->addAction(openFile);

Anakin's avatar
Anakin committed
57 58
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
59 60
	QSignalMapper* signalMapper = new QSignalMapper(this);

Anakin's avatar
Anakin committed
61
	QAction *x = new QAction(QIcon(":/images/toolbar/X.png"), "X", this);
Anakin's avatar
Anakin committed
62 63 64 65
	x->setCheckable(true);
	x->setChecked(true);
	ui->mainToolBar->addAction(x);

Anakin's avatar
Anakin committed
66
	QAction *y = new QAction(QIcon(":/images/toolbar/Y.png"), "Y", this);
Anakin's avatar
Anakin committed
67 68 69 70
	y->setCheckable(true);
	y->setChecked(true);
	ui->mainToolBar->addAction(y);

Anakin's avatar
Anakin committed
71
	QAction *z = new QAction(QIcon(":/images/toolbar/Z.png"), "Z", this);
Anakin's avatar
Anakin committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
	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)));

Anakin's avatar
Anakin committed
86
	ui->mainToolBar->addSeparator();
Anakin's avatar
Anakin committed
87

Anakin's avatar
Anakin committed
88
	QAction *fileInfo = new QAction(QIcon(":/images/toolbar/info.png"), "File info", this);
Anakin's avatar
Anakin committed
89 90 91
	connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
	ui->mainToolBar->addAction(fileInfo);

Anakin's avatar
Anakin committed
92
	QAction *help = new QAction(QIcon(":/images/toolbar/about.png"), "Help", this);
Anakin's avatar
Anakin committed
93 94 95 96 97 98
	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
	ui->mainToolBar->addAction(help);

	
}

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

	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
110
	dialog->exec();
Anakin's avatar
Anakin committed
111
	delete dialog;
Anakin's avatar
Anakin committed
112 113 114 115
}

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

	file.close();

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

	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";
	}
154 155
}

Anakin's avatar
Anakin committed
156
void MainWindow::printMessage(QString message, int severity)
157
{
Anakin's avatar
Anakin committed
158
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		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);
184
}