MainWindow.cpp 8.74 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 <QToolButton>
Anakin's avatar
Anakin committed
14
#include "..\Header\FileInterface.h"
Anakin's avatar
Anakin committed
15 16

#define WINDOW_NAME "Mesh Viewer"
17 18 19 20

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

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

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

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

Anakin's avatar
Anakin committed
36
	printMessage("MeshViewer by Anakin", 0);
Anakin's avatar
Anakin committed
37

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

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

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

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

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

Anakin's avatar
Anakin committed
65 66 67 68 69
	QToolButton *openFile = new QToolButton(this);
	openFile->setObjectName("openFile");
	openFile->setToolTip("open file");
	connect(openFile, &QToolButton::pressed, this, &MainWindow::openFile);
	ui->mainToolBar->addWidget(openFile);
Anakin's avatar
Anakin committed
70

Anakin's avatar
Anakin committed
71 72 73 74 75
	QToolButton *screenshot = new QToolButton(this);
	screenshot->setObjectName("screenshot");
	screenshot->setToolTip("take screenshot");
	connect(screenshot, &QToolButton::pressed, this, &MainWindow::takeScreenShot);
	ui->mainToolBar->addWidget(screenshot);
Anakin's avatar
Anakin committed
76

Anakin's avatar
Anakin committed
77 78
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
79 80
	QSignalMapper* signalMapper = new QSignalMapper(this);

Anakin's avatar
Anakin committed
81 82 83
	QToolButton *x = new QToolButton(this);
	x->setObjectName("x");
	x->setToolTip("x-direction");
Anakin's avatar
Anakin committed
84 85
	x->setCheckable(true);
	x->setChecked(true);
Anakin's avatar
Anakin committed
86
	ui->mainToolBar->addWidget(x);
Anakin's avatar
Anakin committed
87

Anakin's avatar
Anakin committed
88 89 90
	QToolButton *y = new QToolButton(this);
	y->setObjectName("y");
	y->setToolTip("y-direction");
Anakin's avatar
Anakin committed
91 92
	y->setCheckable(true);
	y->setChecked(true);
Anakin's avatar
Anakin committed
93
	ui->mainToolBar->addWidget(y);
Anakin's avatar
Anakin committed
94

Anakin's avatar
Anakin committed
95 96 97
	QToolButton *z = new QToolButton(this);
	z->setObjectName("z");
	z->setToolTip("z-direction");
Anakin's avatar
Anakin committed
98 99
	z->setCheckable(true);
	z->setChecked(true);
Anakin's avatar
Anakin committed
100
	ui->mainToolBar->addWidget(z);
Anakin's avatar
Anakin committed
101

Anakin's avatar
Anakin committed
102 103 104
	connect(x, SIGNAL(pressed()), signalMapper, SLOT(map()));
	connect(y, SIGNAL(pressed()), signalMapper, SLOT(map()));
	connect(z, SIGNAL(pressed()), signalMapper, SLOT(map()));
Anakin's avatar
Anakin committed
105 106 107 108 109 110 111

	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
112
	ui->mainToolBar->addSeparator();
Anakin's avatar
Anakin committed
113

Anakin's avatar
Anakin committed
114 115 116
	QToolButton *wireframe = new QToolButton(this);
	wireframe->setObjectName("wireframe");
	wireframe->setToolTip("wireframe");
Anakin's avatar
Anakin committed
117 118
	wireframe->setCheckable(true);
	wireframe->setChecked(false);
Anakin's avatar
Anakin committed
119 120
	connect(wireframe, &QToolButton::pressed, viewer, &OglViewerWidget::toggleWireframe);
	ui->mainToolBar->addWidget(wireframe);
Anakin's avatar
Anakin committed
121

Anakin's avatar
Anakin committed
122 123 124
	QToolButton *light = new QToolButton(this);
	light->setObjectName("light");
	light->setToolTip("toggle light");
Anakin's avatar
Anakin committed
125 126
	light->setCheckable(true);
	light->setChecked(false);
Anakin's avatar
Anakin committed
127 128
	connect(light, &QToolButton::pressed, viewer, &OglViewerWidget::toggleLight);
	ui->mainToolBar->addWidget(light);
Anakin's avatar
Anakin committed
129

Anakin's avatar
Anakin committed
130 131
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
132 133 134 135 136 137 138 139 140 141 142
	QToolButton *fileInfo = new QToolButton(this);
	fileInfo->setObjectName("fileInfo");
	fileInfo->setToolTip("file info");
	connect(fileInfo, &QToolButton::pressed, this, &MainWindow::aboutFile);
	ui->mainToolBar->addWidget(fileInfo);

	QToolButton *help = new QToolButton(this);
	help->setObjectName("help");
	help->setToolTip("help");
	connect(help, &QToolButton::pressed, this, &MainWindow::aboutTool);
	ui->mainToolBar->addWidget(help);
Anakin's avatar
Anakin committed
143

Anakin's avatar
Anakin committed
144 145 146 147 148
	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
149

Anakin's avatar
Anakin committed
150 151
}

Anakin's avatar
Anakin committed
152 153
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
154
	/*QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
155
		WINDOW_NAME,
Anakin's avatar
Anakin committed
156
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
157 158 159
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
160 161 162

	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
163
	dialog->exec();
Anakin's avatar
Anakin committed
164 165
	delete dialog;*/
	m_infoWindow->show();
Anakin's avatar
Anakin committed
166 167 168 169
}

void MainWindow::aboutTool()
{
170 171 172 173
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
174
		WINDOW_NAME,
175
		QString(file.readAll()),
Anakin's avatar
Anakin committed
176 177
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
178
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
179 180 181

	file.close();

Anakin's avatar
Anakin committed
182
	dialog->exec();
Anakin's avatar
Anakin committed
183 184 185 186

	delete dialog;
}

Anakin's avatar
Anakin committed
187 188 189 190 191 192 193 194 195
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
196 197 198 199 200
void MainWindow::resizeEvent(QResizeEvent * e)
{
	m_output->move(40, e->size().height() - 80);
}

201
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
202 203 204 205
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
206
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
207 208 209 210 211 212
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

213
	//TODO: mark not opened textures
214
	//TODO: add more information
215
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
216
	{
217
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
218
		m_fileInfo += "\n";
Anakin's avatar
Anakin committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

		m_fileInfo += "TX0D:\t\t";
		if (it.texture == NULL)
			m_fileInfo += "-";
		m_fileInfo += it.textureName;
		m_fileInfo += "\n";

		m_fileInfo += "Flags:\t\t";
		for (int i = 0; i < 8; i++)
		{
			if (it.flags[i])
				m_fileInfo += "1";
			else
				m_fileInfo += "0";
		}
		m_fileInfo += "\n";

Anakin's avatar
Anakin committed
236 237
		m_fileInfo += "Rendertype:\t";
		m_fileInfo += QByteArray::number(it.rendertype);
Anakin's avatar
Anakin committed
238 239
		m_fileInfo += "\n";

Anakin's avatar
Anakin committed
240
		m_fileInfo += "Gloss:\t";
Anakin's avatar
Anakin committed
241
		m_fileInfo += QByteArray::number(it.shininess);
Anakin's avatar
Anakin committed
242 243 244 245
		m_fileInfo += "\tData0:\t";
		m_fileInfo += QByteArray::number(it.dataValues[0]);
		m_fileInfo += "\tData1:\t";
		m_fileInfo += QByteArray::number(it.dataValues[1]);
Anakin's avatar
Anakin committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		m_fileInfo += "\n";

		m_fileInfo += "Diffusecolor:\tR: ";
		m_fileInfo += QByteArray::number(it.diffuseColor.x());
		m_fileInfo += "\tG: ";
		m_fileInfo += QByteArray::number(it.diffuseColor.y());
		m_fileInfo += "\tB: ";
		m_fileInfo += QByteArray::number(it.diffuseColor.z());
		m_fileInfo += "\tA: ";
		m_fileInfo += QByteArray::number(it.diffuseColor.w());
		m_fileInfo += "\n";

		m_fileInfo += "Ambientcolor:\tR: ";
		m_fileInfo += QByteArray::number(it.ambientColor.x());
		m_fileInfo += "\tG: ";
		m_fileInfo += QByteArray::number(it.ambientColor.y());
		m_fileInfo += "\tB: ";
		m_fileInfo += QByteArray::number(it.ambientColor.z());
		m_fileInfo += "\tA: ";
		m_fileInfo += QByteArray::number(it.ambientColor.w());
		m_fileInfo += "\n";

		m_fileInfo += "Specularcolor:\tR: ";
		m_fileInfo += QByteArray::number(it.specularColor.x());
		m_fileInfo += "\tG: ";
		m_fileInfo += QByteArray::number(it.specularColor.y());
		m_fileInfo += " \tB: ";
		m_fileInfo += QByteArray::number(it.specularColor.z());
		m_fileInfo += " \tA: ";
		m_fileInfo += QByteArray::number(it.specularColor.w());
		m_fileInfo += "\n";

		m_fileInfo += "-----------------------------------------------------------------\n";
Anakin's avatar
Anakin committed
279
	}
Anakin's avatar
Anakin committed
280 281

	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
282 283 284 285

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

286 287
}

Anakin's avatar
Anakin committed
288
void MainWindow::printMessage(QString message, int severity)
289
{
Anakin's avatar
Anakin committed
290
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		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);
316
}