MainWindow.cpp 6.63 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))
Anakin's avatar
Anakin committed
22
	, m_infoWindow(new FileInfoWindow())
23 24 25
{
	ui->setupUi(this);

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

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

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

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

Anakin's avatar
Anakin committed
37
	m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -<detail>No file is open";
38 39 40 41 42
}

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

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

Anakin's avatar
Anakin committed
54 55 56 57
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);
Anakin's avatar
Anakin committed
58
	connect(viewer, &OglViewerWidget::sendMessage, this, &MainWindow::printMessage);
Anakin's avatar
Anakin committed
59

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

Anakin's avatar
Anakin committed
64 65 66 67
	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
68 69
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
70 71
	QSignalMapper* signalMapper = new QSignalMapper(this);

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

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

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

Anakin's avatar
Anakin committed
99 100 101 102 103 104
	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);

Anakin's avatar
Anakin committed
105 106 107 108 109 110
	QAction *light = new QAction(QIcon(":/images/toolbar/light.png"), "Light", this);
	light->setCheckable(true);
	light->setChecked(false);
	connect(light, &QAction::triggered, viewer, &OglViewerWidget::toggleLight);
	ui->mainToolBar->addAction(light);

Anakin's avatar
Anakin committed
111 112
	ui->mainToolBar->addSeparator();

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

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

Anakin's avatar
Anakin committed
121 122 123 124 125
	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
126

Anakin's avatar
Anakin committed
127 128
}

Anakin's avatar
Anakin committed
129 130
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
131
	/*QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
132
		WINDOW_NAME,
Anakin's avatar
Anakin committed
133
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
134 135 136
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
137 138 139

	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
140
	dialog->exec();
Anakin's avatar
Anakin committed
141 142
	delete dialog;*/
	m_infoWindow->show();
Anakin's avatar
Anakin committed
143 144 145 146
}

void MainWindow::aboutTool()
{
147 148 149 150
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
151
		WINDOW_NAME,
152
		QString(file.readAll()),
Anakin's avatar
Anakin committed
153 154
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
155
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
156 157 158

	file.close();

Anakin's avatar
Anakin committed
159
	dialog->exec();
Anakin's avatar
Anakin committed
160 161 162 163

	delete dialog;
}

Anakin's avatar
Anakin committed
164 165 166 167 168 169 170 171 172
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
173 174 175 176 177
void MainWindow::resizeEvent(QResizeEvent * e)
{
	m_output->move(40, e->size().height() - 80);
}

178
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
179 180 181 182
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
183
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
184 185 186 187 188 189 190
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

	int count(0);
191
	//TODO: mark not opened textures
192
	//TODO: add more information
193
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
194 195 196 197
	{
		m_fileInfo += "Material ";
		m_fileInfo += QByteArray::number(count++);
		m_fileInfo += " - ";
198
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
199 200
		m_fileInfo += "\n";
	}
Anakin's avatar
Anakin committed
201 202

	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
203 204 205 206

	m_infoWindow->setBasicText(QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))));
	m_infoWindow->setDetailText(QString(m_fileInfo.right(m_fileInfo.size() - m_fileInfo.indexOf("<detail>") - 8)));

207 208
}

Anakin's avatar
Anakin committed
209
void MainWindow::printMessage(QString message, int severity)
210
{
Anakin's avatar
Anakin committed
211
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		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);
237
}