Skip to content
Snippets Groups Projects
Select Git revision
  • master
  • stable
2 results

instruction_emulator.rs

Blame
    • Timo Kreuzer's avatar
      4a6c4428
      Change how callbacks work · 4a6c4428
      Timo Kreuzer authored
      The emulator had a mutable reference to the callbacks structure,
      which made it extremely difficult to use, due to the inability to
      hold any other references on it and the inability to make this
      part of a structure.
      
      To get rid of the reference, the callback is now passed as a
      parameter to the try_io_emulation method, replacing the PVOID
      context (helping to avoid unsafe casts).
      4a6c4428
      History
      Change how callbacks work
      Timo Kreuzer authored
      The emulator had a mutable reference to the callbacks structure,
      which made it extremely difficult to use, due to the inability to
      hold any other references on it and the inability to make this
      part of a structure.
      
      To get rid of the reference, the callback is now passed as a
      parameter to the try_io_emulation method, replacing the PVOID
      context (helping to avoid unsafe casts).
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MainWindow.cpp 5.18 KiB
    #include "..\Header\MainWindow.h"
    #include "..\Header\OglViewerWidget.h"
    #include <QSurfaceFormat>
    #include <QMessageBox>
    #include <QFileDialog>
    #include <QPalette>
    #include <QAction>
    #include <QSignalMapper>
    #include <QFile>
    #include <QSizePolicy>
    #include "..\Header\FileInterface.h"
    
    #define WINDOW_NAME "Mesh Viewer"
    
    MainWindow::MainWindow(QWidget *parent)
    	: QMainWindow(parent)
    	, ui(new Ui::MainWindowClass)
    	, m_curSeverity(0)
    {
    	ui->setupUi(this);
    
    	setWindowTitle(WINDOW_NAME);
    	setWindowIcon(QIcon(":/images/icon.ico"));
    
    	QSurfaceFormat format;
    	format.setDepthBufferSize(24);
    	QSurfaceFormat::setDefaultFormat(format);
    
    	setupWidgets();
    
    	ui->statusBar->showMessage("MeshViewer by Anakin", 0);
    
    	m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -\n<detail>No file is open";
    }
    
    MainWindow::~MainWindow()
    {
    	delete ui;
    }
    
    void MainWindow::openFile()
    {
    	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Mesh (*.msh)");
    	if(!fileName.isEmpty())
    		emit loadFile(fileName);
    }
    
    void MainWindow::setupWidgets()
    {
    	OglViewerWidget* viewer = new OglViewerWidget(this);
    	setCentralWidget(viewer);
    
    	QAction *openFile = new QAction(QIcon(":/images/toolbar/open.png"), "Open file", this);
    	connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
    	ui->mainToolBar->addAction(openFile);
    
    	QAction *screenShot = new QAction(QIcon(":/images/toolbar/screenshot.png"), "Screenshot", this);
    	connect(screenShot, &QAction::triggered, this, &MainWindow::takeScreenShot);
    	ui->mainToolBar->addAction(screenShot);
    
    	ui->mainToolBar->addSeparator();
    
    	QSignalMapper* signalMapper = new QSignalMapper(this);
    
    	QAction *x = new QAction(QIcon(":/images/toolbar/X.png"), "X", this);
    	x->setCheckable(true);
    	x->setChecked(true);
    	ui->mainToolBar->addAction(x);
    
    	QAction *y = new QAction(QIcon(":/images/toolbar/Y.png"), "Y", this);
    	y->setCheckable(true);
    	y->setChecked(true);
    	ui->mainToolBar->addAction(y);
    
    	QAction *z = new QAction(QIcon(":/images/toolbar/Z.png"), "Z", this);
    	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)));
    
    	ui->mainToolBar->addSeparator();
    
    	QAction *fileInfo = new QAction(QIcon(":/images/toolbar/info.png"), "File info", this);
    	connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
    	ui->mainToolBar->addAction(fileInfo);
    
    	QAction *help = new QAction(QIcon(":/images/toolbar/about.png"), "Help", this);
    	connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
    	ui->mainToolBar->addAction(help);
    
    	
    }
    
    void MainWindow::aboutFile()
    {
    	QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
    		WINDOW_NAME,
    		QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
    		QMessageBox::StandardButton::Close,
    		this,
    		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    
    	dialog->setStyleSheet("QLabel{min-width: 200px;}");
    	dialog->setDetailedText(QString(m_fileInfo.right(m_fileInfo.size() - m_fileInfo.indexOf("<detail>") - 8)));
    	dialog->exec();
    	delete dialog;
    }
    
    void MainWindow::aboutTool()
    {
    	QFile file(":/files/about.txt");
    	file.open(QIODevice::ReadOnly);
    	QMessageBox* dialog = new QMessageBox(
    		QMessageBox::Question,
    		WINDOW_NAME,
    		QString(file.readAll()),
    		QMessageBox::StandardButton::Close,
    		this,
    		Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    
    	file.close();
    
    	dialog->exec();
    
    	delete dialog;
    }
    
    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);
    }
    
    void MainWindow::setFileInfo(QString name, QVector<Material>* materials, int vertices, int triangle)
    {
    	m_fileInfo = QByteArray("Filename: ");
    	m_fileInfo += name;
    	m_fileInfo += "\nMaterials: ";
    	m_fileInfo += QByteArray::number(materials->size());
    	m_fileInfo += "\nVertices: ";
    	m_fileInfo += QByteArray::number(vertices);
    	m_fileInfo += "\nTriangle: ";
    	m_fileInfo += QByteArray::number(triangle);
    	m_fileInfo += "<detail>";
    
    	int count(0);
    	//TODO: mark not opened textures
    	for (auto& it : *materials)
    	{
    		m_fileInfo += "Material ";
    		m_fileInfo += QByteArray::number(count++);
    		m_fileInfo += " - ";
    		m_fileInfo += it.name;
    		m_fileInfo += "\n";
    	}
    }
    
    void MainWindow::printMessage(QString message, int severity)
    {
    	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);
    }