MainWindow.cpp 5.18 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
#include "..\Header\FileInterface.h"
Anakin's avatar
Anakin committed
12
13

#define WINDOW_NAME "Mesh Viewer"
14
15
16
17

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindowClass)
18
	, m_curSeverity(0)
19
20
21
{
	ui->setupUi(this);

Anakin's avatar
Anakin committed
22
23
24
	setWindowTitle(WINDOW_NAME);
	setWindowIcon(QIcon(":/images/icon.ico"));

25
26
27
28
	QSurfaceFormat format;
	format.setDepthBufferSize(24);
	QSurfaceFormat::setDefaultFormat(format);

Anakin's avatar
Anakin committed
29
	setupWidgets();
30
31

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

	m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -\n<detail>No file is open";
34
35
36
37
38
}

MainWindow::~MainWindow()
{
	delete ui;
Anakin's avatar
Anakin committed
39
40
41
42
}

void MainWindow::openFile()
{
Anakin's avatar
Anakin committed
43
	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Mesh (*.msh)");
Anakin's avatar
Anakin committed
44
45
	if(!fileName.isEmpty())
		emit loadFile(fileName);
Anakin's avatar
Anakin committed
46
47
}

Anakin's avatar
Anakin committed
48
49
50
51
52
void MainWindow::setupWidgets()
{
	OglViewerWidget* viewer = new OglViewerWidget(this);
	setCentralWidget(viewer);

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

Anakin's avatar
Anakin committed
57
58
59
60
	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
61
62
	ui->mainToolBar->addSeparator();

Anakin's avatar
Anakin committed
63
64
	QSignalMapper* signalMapper = new QSignalMapper(this);

Anakin's avatar
Anakin committed
65
	QAction *x = new QAction(QIcon(":/images/toolbar/X.png"), "X", this);
Anakin's avatar
Anakin committed
66
67
68
69
	x->setCheckable(true);
	x->setChecked(true);
	ui->mainToolBar->addAction(x);

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

Anakin's avatar
Anakin committed
75
	QAction *z = new QAction(QIcon(":/images/toolbar/Z.png"), "Z", this);
Anakin's avatar
Anakin committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	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
90
	ui->mainToolBar->addSeparator();
Anakin's avatar
Anakin committed
91

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

Anakin's avatar
Anakin committed
96
	QAction *help = new QAction(QIcon(":/images/toolbar/about.png"), "Help", this);
Anakin's avatar
Anakin committed
97
98
99
100
101
102
	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
	ui->mainToolBar->addAction(help);

	
}

Anakin's avatar
Anakin committed
103
104
void MainWindow::aboutFile()
{
Anakin's avatar
Anakin committed
105
	QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
Anakin's avatar
Anakin committed
106
		WINDOW_NAME,
Anakin's avatar
Anakin committed
107
		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
Anakin's avatar
Anakin committed
108
109
110
		QMessageBox::StandardButton::Close,
		this,
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
Anakin's avatar
Anakin committed
111
112
113

	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
114
	dialog->exec();
Anakin's avatar
Anakin committed
115
	delete dialog;
Anakin's avatar
Anakin committed
116
117
118
119
}

void MainWindow::aboutTool()
{
120
121
122
123
	QFile file(":/files/about.txt");
	file.open(QIODevice::ReadOnly);
	QMessageBox* dialog = new QMessageBox(
		QMessageBox::Question,
Anakin's avatar
Anakin committed
124
		WINDOW_NAME,
125
		QString(file.readAll()),
Anakin's avatar
Anakin committed
126
127
		QMessageBox::StandardButton::Close,
		this,
Anakin's avatar
Anakin committed
128
		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
129
130
131

	file.close();

Anakin's avatar
Anakin committed
132
	dialog->exec();
Anakin's avatar
Anakin committed
133
134
135
136

	delete dialog;
}

Anakin's avatar
Anakin committed
137
138
139
140
141
142
143
144
145
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);
}

146
void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
Anakin's avatar
Anakin committed
147
148
149
150
{
	m_fileInfo = QByteArray("Filename: ");
	m_fileInfo += name;
	m_fileInfo += "\nMaterials: ";
151
	m_fileInfo += QByteArray::number(materials->size());
Anakin's avatar
Anakin committed
152
153
154
155
156
157
158
	m_fileInfo += "\nVertices: ";
	m_fileInfo += QByteArray::number(vertices);
	m_fileInfo += "\nTriangle: ";
	m_fileInfo += QByteArray::number(triangle);
	m_fileInfo += "<detail>";

	int count(0);
159
160
	//TODO: mark not opened textures
	for (auto& it : *materials)
Anakin's avatar
Anakin committed
161
162
163
164
	{
		m_fileInfo += "Material ";
		m_fileInfo += QByteArray::number(count++);
		m_fileInfo += " - ";
165
		m_fileInfo += it.name;
Anakin's avatar
Anakin committed
166
167
		m_fileInfo += "\n";
	}
168
169
}

Anakin's avatar
Anakin committed
170
void MainWindow::printMessage(QString message, int severity)
171
{
Anakin's avatar
Anakin committed
172
	if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		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);
198
}