MainWindow.cpp 6.89 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";
Anakin's avatar
Anakin committed
38 39 40 41

	QFile styleSheet(":/files/StyleSheet.txt");
	styleSheet.open(QIODevice::ReadOnly);
	this->setStyleSheet(styleSheet.readAll());
42 43 44 45 46
}

MainWindow::~MainWindow()
{
	delete ui;
Anakin's avatar
Anakin committed
47
	delete m_output;
Anakin's avatar
Anakin committed
48
	delete m_infoWindow;
Anakin's avatar
Anakin committed
49 50 51 52
}

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

Anakin's avatar
Anakin committed
58 59 60 61
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);
Anakin's avatar
Anakin committed
62
	connect(viewer, &OglViewerWidget::sendMessage, this, &MainWindow::printMessage);
Anakin's avatar
Anakin committed
63

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

Anakin's avatar
Anakin committed
68 69 70 71
	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
72 73
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
74 75
	QSignalMapper* signalMapper = new QSignalMapper(this);

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

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

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

Anakin's avatar
Anakin committed
103 104 105 106 107 108
	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
109 110 111 112 113 114
	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
115 116
	ui->mainToolBar->addSeparator();

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

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

Anakin's avatar
Anakin committed
125 126 127 128 129
	QAction *test = new QAction(this);
	test->setCheckable(true);
	test->setObjectName("test123");
	ui->mainToolBar->addAction(test);

Anakin's avatar
Anakin committed
130 131 132 133 134
	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
135

Anakin's avatar
Anakin committed
136 137
}

Anakin's avatar
Anakin committed
138 139
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
140
	/*QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
141
		WINDOW_NAME,
Anakin's avatar
Anakin committed
142
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
143 144 145
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
146 147 148

	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
149
	dialog->exec();
Anakin's avatar
Anakin committed
150 151
	delete dialog;*/
	m_infoWindow->show();
Anakin's avatar
Anakin committed
152 153 154 155
}

void MainWindow::aboutTool()
{
156 157 158 159
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
160
		WINDOW_NAME,
161
		QString(file.readAll()),
Anakin's avatar
Anakin committed
162 163
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
164
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
165 166 167

	file.close();

Anakin's avatar
Anakin committed
168
	dialog->exec();
Anakin's avatar
Anakin committed
169 170 171 172

	delete dialog;
}

Anakin's avatar
Anakin committed
173 174 175 176 177 178 179 180 181
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
182 183 184 185 186
void MainWindow::resizeEvent(QResizeEvent * e)
{
	m_output->move(40, e->size().height() - 80);
}

187
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
188 189 190 191
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
192
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
193 194 195 196 197 198 199
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

	int count(0);
200
	//TODO: mark not opened textures
201
	//TODO: add more information
202
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
203 204 205 206
	{
		m_fileInfo += "Material ";
		m_fileInfo += QByteArray::number(count++);
		m_fileInfo += " - ";
207
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
208 209
		m_fileInfo += "\n";
	}
Anakin's avatar
Anakin committed
210 211

	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
212 213 214 215

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

216 217
}

Anakin's avatar
Anakin committed
218
void MainWindow::printMessage(QString message, int severity)
219
{
Anakin's avatar
Anakin committed
220
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		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);
246
}