MainWindow.cpp 9.77 KB
Newer Older
1 2
#include "..\Header\MainWindow.h"
#include "..\Header\OglViewerWidget.h"
3
#include "..\Header\FileInterface.h"
4
#include "..\Header\OutputDevice.h"
5
#include <QSurfaceFormat>
Anakin's avatar
Anakin committed
6
#include <QSignalMapper>
7
#include <QToolButton>
Anakin's avatar
Anakin committed
8
#include <QFile>
9 10 11
#include <QFileDialog>
#include <QMessageBox>
#include <QPalette>
Anakin's avatar
Anakin committed
12
#include <QResizeEvent>
Anakin's avatar
Anakin committed
13 14

#define WINDOW_NAME "Mesh Viewer"
15

16 17 18 19

/////////////////////////////////////////////////////////////////////////
// constructor/destructor

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

Anakin's avatar
Anakin committed
30 31 32
	setWindowTitle(WINDOW_NAME);
	setWindowIcon(QIcon(":/images/icon.ico"));

33
	connect(OutputDevice::getInstance(this), &OutputDevice::sendMessage, this, &MainWindow::printMessage);
Anakin's avatar
Anakin committed
34
	connect(OutputDevice::getInstance(this), &OutputDevice::sendFileInfo, this, &MainWindow::setFileInfo);
35 36

	// setup opengl things
37 38
	QSurfaceFormat format;
	format.setDepthBufferSize(24);
Anakin's avatar
Anakin committed
39 40 41
	format.setMajorVersion(2);
	format.setMinorVersion(0);
	format.setProfile(QSurfaceFormat::NoProfile);
42 43
	QSurfaceFormat::setDefaultFormat(format);

44 45
	// set default text to file info
	m_fileInfo = "Filename: -\nMaterials: -\nVertices: -\nTriangle: -<detail>No file is open";
Anakin's avatar
Anakin committed
46

47 48
	// add widgets to the window
	setupWidgets();
Anakin's avatar
Anakin committed
49

50
	// load stylesheet
Anakin's avatar
Anakin committed
51 52 53
	QFile styleSheet(":/files/StyleSheet.txt");
	styleSheet.open(QIODevice::ReadOnly);
	this->setStyleSheet(styleSheet.readAll());
54 55

	printMessage("MeshViewer by Anakin", 0);
56 57 58 59 60
}

MainWindow::~MainWindow()
{
	delete ui;
Anakin's avatar
Anakin committed
61
	delete m_output;
Anakin's avatar
Anakin committed
62
	delete m_infoWindow;
Anakin's avatar
Anakin committed
63 64
}

65 66 67

/////////////////////////////////////////////////////////////////////////
// functions
Anakin's avatar
Anakin committed
68

Anakin's avatar
Anakin committed
69 70
void MainWindow::setupWidgets()
{
71
	// Ogl Viewer
Anakin's avatar
Anakin committed
72 73 74
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);

75
	// open file
Anakin's avatar
Anakin committed
76 77 78 79 80
	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
81

82
	// screenshot
Anakin's avatar
Anakin committed
83 84 85 86 87
	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
88

89
	//////////////////////////////////////////////////
Anakin's avatar
Anakin committed
90 91
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
92 93
	QSignalMapper* signalMapper = new QSignalMapper(this);

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

102
	// Y
Anakin's avatar
Anakin committed
103 104 105
	QToolButton *y = new QToolButton(this);
	y->setObjectName("y");
	y->setToolTip("y-direction");
Anakin's avatar
Anakin committed
106 107
	y->setCheckable(true);
	y->setChecked(true);
Anakin's avatar
Anakin committed
108
	ui->mainToolBar->addWidget(y);
Anakin's avatar
Anakin committed
109

110
	// Z
Anakin's avatar
Anakin committed
111 112 113
	QToolButton *z = new QToolButton(this);
	z->setObjectName("z");
	z->setToolTip("z-direction");
Anakin's avatar
Anakin committed
114 115
	z->setCheckable(true);
	z->setChecked(true);
Anakin's avatar
Anakin committed
116
	ui->mainToolBar->addWidget(z);
Anakin's avatar
Anakin committed
117

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

	signalMapper->setMapping(x, 1);
	signalMapper->setMapping(y, 2);
	signalMapper->setMapping(z, 3);

126
	connect(signalMapper, SIGNAL(mapped(int)), viewer, SLOT(toggleAxis(int)));
Anakin's avatar
Anakin committed
127

128
	//////////////////////////////////////////////////
Anakin's avatar
Anakin committed
129
	ui->mainToolBar->addSeparator();
Anakin's avatar
Anakin committed
130

131
	// wireframe
Anakin's avatar
Anakin committed
132 133 134
	QToolButton *wireframe = new QToolButton(this);
	wireframe->setObjectName("wireframe");
	wireframe->setToolTip("wireframe");
Anakin's avatar
Anakin committed
135 136
	wireframe->setCheckable(true);
	wireframe->setChecked(false);
Anakin's avatar
Anakin committed
137 138
	connect(wireframe, &QToolButton::pressed, viewer, &OglViewerWidget::toggleWireframe);
	ui->mainToolBar->addWidget(wireframe);
Anakin's avatar
Anakin committed
139

140
	// light
Anakin's avatar
Anakin committed
141 142 143
	QToolButton *light = new QToolButton(this);
	light->setObjectName("light");
	light->setToolTip("toggle light");
Anakin's avatar
Anakin committed
144 145
	light->setCheckable(true);
	light->setChecked(false);
Anakin's avatar
Anakin committed
146 147
	connect(light, &QToolButton::pressed, viewer, &OglViewerWidget::toggleLight);
	ui->mainToolBar->addWidget(light);
Anakin's avatar
Anakin committed
148

149
	// settings
Anakin's avatar
Anakin committed
150 151 152 153 154 155
	QToolButton *settings = new QToolButton(this);
	settings->setObjectName("settings");
	settings->setToolTip("settings");
	connect(settings, &QToolButton::pressed, viewer, &OglViewerWidget::showSettings);
	ui->mainToolBar->addWidget(settings);

156
	//////////////////////////////////////////////////
Anakin's avatar
Anakin committed
157 158
	ui->mainToolBar->addSeparator();

159
	// fileinfo
Anakin's avatar
Anakin committed
160 161 162
	QToolButton *fileInfo = new QToolButton(this);
	fileInfo->setObjectName("fileInfo");
	fileInfo->setToolTip("file info");
163
	connect(fileInfo, &QToolButton::pressed, m_infoWindow, &FileInfoWindow::show);
Anakin's avatar
Anakin committed
164 165
	ui->mainToolBar->addWidget(fileInfo);

166
	// help
Anakin's avatar
Anakin committed
167 168 169 170 171
	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
172

173
	// output on screen
Anakin's avatar
Anakin committed
174 175
	m_output->setObjectName("output");
	m_output->setAlignment(Qt::AlignTop);
176
	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
177
	m_output->raise();
Anakin's avatar
Anakin committed
178

Anakin's avatar
Anakin committed
179 180
}

181
void MainWindow::openFile()
Anakin's avatar
Anakin committed
182
{
183 184 185 186
	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Mesh (*.msh)");
	if(!fileName.isEmpty())
		emit loadFile(fileName);
}
Anakin's avatar
Anakin committed
187

188 189 190 191 192 193 194
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
195 196 197 198
}

void MainWindow::aboutTool()
{
199 200 201 202
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
203
		WINDOW_NAME,
204
		QString(file.readAll()),
Anakin's avatar
Anakin committed
205 206
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
207
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
208 209 210

	file.close();

Anakin's avatar
Anakin committed
211
	dialog->exec();
Anakin's avatar
Anakin committed
212 213 214 215

	delete dialog;
}

216
void MainWindow::resizeEvent(QResizeEvent * e)
Anakin's avatar
Anakin committed
217
{
218
	m_output->move(40, e->size().height() - 80);
Anakin's avatar
Anakin committed
219 220
}

221 222 223 224 225

/////////////////////////////////////////////////////////////////////////
// slots

void MainWindow::printMessage(QString message, int severity)
Anakin's avatar
Anakin committed
226
{
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
		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);
Anakin's avatar
Anakin committed
253 254
}

255
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
256
{
257
	// save basic file information
Anakin's avatar
Anakin committed
258 259 260
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
261
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
262 263 264 265 266 267
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

268
	// add detailed information
269
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
270
	{
271
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
272
		m_fileInfo += "\n";
Anakin's avatar
Anakin committed
273 274

		m_fileInfo += "TX0D:\t\t";
275
		if (it.tx0d.isEmpty())
Anakin's avatar
Anakin committed
276
			m_fileInfo += "-";
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		else
			m_fileInfo += it.tx0d;
		m_fileInfo += "\n";

		m_fileInfo += "TX1D:\t\t";
		if (it.tx1d.isEmpty())
			m_fileInfo += "-";
		else
			m_fileInfo += it.tx1d;
		m_fileInfo += "\n";

		m_fileInfo += "TX2D:\t\t";
		if (it.tx2d.isEmpty())
			m_fileInfo += "-";
		else
			m_fileInfo += it.tx2d;
		m_fileInfo += "\n";

		m_fileInfo += "TX3D:\t\t";
		if (it.tx3d.isEmpty())
			m_fileInfo += "-";
		else
			m_fileInfo += it.tx3d;
Anakin's avatar
Anakin committed
300 301 302 303 304 305 306 307 308 309 310 311
		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
312 313
		m_fileInfo += "Rendertype:\t";
		m_fileInfo += QByteArray::number(it.rendertype);
Anakin's avatar
Anakin committed
314 315
		m_fileInfo += "\n";

Anakin's avatar
Anakin committed
316
		m_fileInfo += "Gloss:\t";
Anakin's avatar
Anakin committed
317
		m_fileInfo += QByteArray::number(it.shininess);
Anakin's avatar
Anakin committed
318 319 320 321
		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
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		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
355
	}
Anakin's avatar
Anakin committed
356

357
	// print basic information on screen
Anakin's avatar
Anakin committed
358
	m_output->setText(m_fileInfo.left(m_fileInfo.indexOf("<detail>")));
Anakin's avatar
Anakin committed
359

360
	// print basic and detailed information on info window
Anakin's avatar
Anakin committed
361 362 363
	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)));

364
}