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

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

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

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

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

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

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

Anakin's avatar
Anakin committed
98 99 100 101 102 103 104 105
	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
106
	QAction *fileInfo = new QAction(QIcon(":/images/toolbar/info.png"), "File info", this);
Anakin's avatar
Anakin committed
107 108 109
	connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
	ui->mainToolBar->addAction(fileInfo);

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

Anakin's avatar
Anakin committed
114 115 116 117 118
	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
119

Anakin's avatar
Anakin committed
120 121
}

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

	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
133
	dialog->exec();
Anakin's avatar
Anakin committed
134 135
	delete dialog;*/
	m_infoWindow->show();
Anakin's avatar
Anakin committed
136 137 138 139
}

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

	file.close();

Anakin's avatar
Anakin committed
152
	dialog->exec();
Anakin's avatar
Anakin committed
153 154 155 156

	delete dialog;
}

Anakin's avatar
Anakin committed
157 158 159 160 161 162 163 164 165
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
166 167 168 169 170
void MainWindow::resizeEvent(QResizeEvent * e)
{
	m_output->move(40, e->size().height() - 80);
}

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

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

	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
195 196 197 198

	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)));

199 200
}

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