MainWindow.cpp 6 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 12
#include <QFont>
#include <QResizeEvent>
Anakin's avatar
Anakin committed
13
#include "..\Header\FileInterface.h"
Anakin's avatar
Anakin committed
14 15

#define WINDOW_NAME "Mesh Viewer"
16 17 18 19

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindowClass)
20
	, m_curSeverity(0)
Anakin's avatar
Anakin committed
21
	, m_output(new QLabel(this))
22 23 24
{
	ui->setupUi(this);

Anakin's avatar
Anakin committed
25 26 27
	setWindowTitle(WINDOW_NAME);
	setWindowIcon(QIcon(":/images/icon.ico"));

28 29 30 31
	QSurfaceFormat format;
	format.setDepthBufferSize(24);
	QSurfaceFormat::setDefaultFormat(format);

Anakin's avatar
Anakin committed
32
	setupWidgets();
33 34

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

	m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -\n<detail>No file is open";
37 38 39 40 41
}

MainWindow::~MainWindow()
{
	delete ui;
Anakin's avatar
Anakin committed
42
	delete m_output;
Anakin's avatar
Anakin committed
43 44 45 46
}

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

Anakin's avatar
Anakin committed
52 53 54 55 56
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);

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

Anakin's avatar
Anakin committed
61 62 63 64
	QAction *screenShot = new QAction(QIcon(":/images/toolbar/screenshot.png"), "Screenshot", this);
	connect(screenShot, &QAction::triggered, this, &MainWindow::takeScreenShot);
	ui->mainToolBar->addAction(screenShot);

Anakin's avatar
Anakin committed
65 66
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
67 68
	QSignalMapper* signalMapper = new QSignalMapper(this);

Anakin's avatar
Anakin committed
69
	QAction *x = new QAction(QIcon(":/images/toolbar/X.png"), "X", this);
Anakin's avatar
Anakin committed
70 71 72 73
	x->setCheckable(true);
	x->setChecked(true);
	ui->mainToolBar->addAction(x);

Anakin's avatar
Anakin committed
74
	QAction *y = new QAction(QIcon(":/images/toolbar/Y.png"), "Y", this);
Anakin's avatar
Anakin committed
75 76 77 78
	y->setCheckable(true);
	y->setChecked(true);
	ui->mainToolBar->addAction(y);

Anakin's avatar
Anakin committed
79
	QAction *z = new QAction(QIcon(":/images/toolbar/Z.png"), "Z", this);
Anakin's avatar
Anakin committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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
94
	ui->mainToolBar->addSeparator();
Anakin's avatar
Anakin committed
95

Anakin's avatar
Anakin committed
96 97 98 99 100 101 102 103
	QAction *wireframe = new QAction(QIcon(":/images/toolbar/wireframe.png"), "Wireframe", this);
	wireframe->setCheckable(true);
	wireframe->setChecked(false);
	connect(wireframe, &QAction::triggered, viewer, &OglViewerWidget::toggleWireframe);
	ui->mainToolBar->addAction(wireframe);

	ui->mainToolBar->addSeparator();

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

Anakin's avatar
Anakin committed
108
	QAction *help = new QAction(QIcon(":/images/toolbar/about.png"), "Help", this);
Anakin's avatar
Anakin committed
109 110 111
	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
	ui->mainToolBar->addAction(help);

Anakin's avatar
Anakin committed
112 113 114 115 116
	m_output->setObjectName("output");
	m_output->setStyleSheet("QLabel#output{color : white; min-width: 400px; min-height: 50px;}");
	m_output->setAlignment(Qt::AlignTop);
	m_output->setText("Name: -\nMaterials: -\nVertice: -\nTriangle: -");
	m_output->raise();
Anakin's avatar
Anakin committed
117 118 119
	
}

Anakin's avatar
Anakin committed
120 121
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
122
	QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
123
		WINDOW_NAME,
Anakin's avatar
Anakin committed
124
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
125 126 127
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
128 129 130

	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
131
	dialog->exec();
Anakin's avatar
Anakin committed
132
	delete dialog;
Anakin's avatar
Anakin committed
133 134 135 136
}

void MainWindow::aboutTool()
{
137 138 139 140
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
141
		WINDOW_NAME,
142
		QString(file.readAll()),
Anakin's avatar
Anakin committed
143 144
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
145
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
146 147 148

	file.close();

Anakin's avatar
Anakin committed
149
	dialog->exec();
Anakin's avatar
Anakin committed
150 151 152 153

	delete dialog;
}

Anakin's avatar
Anakin committed
154 155 156 157 158 159 160 161 162
void MainWindow::takeScreenShot()
{
	QString destination = QFileDialog::getSaveFileName(this, "Save as...", "", "PNG (*.png);; BMP (*.bmp);;TIFF (*.tiff, *.tif);;JPEG (*.jpg *jpeg)");

	OglViewerWidget* viewer = dynamic_cast<OglViewerWidget*>(centralWidget());
	if (!destination.isEmpty() && viewer != NULL)
		viewer->grab().save(destination);
}

Anakin's avatar
Anakin committed
163 164 165 166 167
void MainWindow::resizeEvent(QResizeEvent * e)
{
	m_output->move(40, e->size().height() - 80);
}

168
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
169 170 171 172
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
173
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
174 175 176 177 178 179 180
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

	int count(0);
181 182
	//TODO: mark not opened textures
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
183 184 185 186
	{
		m_fileInfo += "Material ";
		m_fileInfo += QByteArray::number(count++);
		m_fileInfo += " - ";
187
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
188 189
		m_fileInfo += "\n";
	}
Anakin's avatar
Anakin committed
190 191

	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
192 193
}

Anakin's avatar
Anakin committed
194
void MainWindow::printMessage(QString message, int severity)
195
{
Anakin's avatar
Anakin committed
196
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		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);
222
}