From 98302664ca365c838c9c585f87d0d4229780976e Mon Sep 17 00:00:00 2001 From: Anakin Date: Sun, 29 Jan 2017 11:35:43 +0100 Subject: [PATCH] add OuputDevice as singleton to manage output to statusbar, --- QtMeshViewer/Header/FileInterface.h | 16 +++------------ QtMeshViewer/Header/GeometryEngine.h | 1 - QtMeshViewer/Header/MshFile.h | 2 +- QtMeshViewer/Header/OglViewerWidget.h | 1 - QtMeshViewer/Header/OutputDevice.h | 27 +++++++++++++++++++++++++ QtMeshViewer/Source/GeometryEngine.cpp | 9 +++++---- QtMeshViewer/Source/MainWindow.cpp | 6 ++++-- QtMeshViewer/Source/MshFile.cpp | 17 ++++++++-------- QtMeshViewer/Source/OglViewerWidget.cpp | 7 +++---- 9 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 QtMeshViewer/Header/OutputDevice.h diff --git a/QtMeshViewer/Header/FileInterface.h b/QtMeshViewer/Header/FileInterface.h index 4dbed75..e3b1995 100644 --- a/QtMeshViewer/Header/FileInterface.h +++ b/QtMeshViewer/Header/FileInterface.h @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -7,7 +6,6 @@ #include #include #include -#include "MainWindow.h" struct BoundingBox { QQuaternion rotation; @@ -54,14 +52,12 @@ struct Material { std::uint8_t dataValues[2] = { 0 }; }; -class FileInterface : public QObject +class FileInterface { - Q_OBJECT public: - explicit FileInterface(QString path, QObject *parent) - : QObject(parent) - , m_models(new QVector) + explicit FileInterface(QString path) + : m_models(new QVector) , m_materials(new QVector) { //open file @@ -70,10 +66,6 @@ public: if (!m_file.is_open()) throw std::invalid_argument(std::string("ERROR: file not found: ") += path.toStdString()); - MainWindow* tmp = dynamic_cast(parent->parent()->parent()); - if(tmp != NULL) - connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(printMessage(QString, int))); - m_filepath = path.left(path.lastIndexOf(QRegExp("/|\\\\"))); }; @@ -138,6 +130,4 @@ public: return defMaterial; }; -signals: - void sendMessage(QString msg, int severity); }; \ No newline at end of file diff --git a/QtMeshViewer/Header/GeometryEngine.h b/QtMeshViewer/Header/GeometryEngine.h index 8542415..235ae9f 100644 --- a/QtMeshViewer/Header/GeometryEngine.h +++ b/QtMeshViewer/Header/GeometryEngine.h @@ -46,7 +46,6 @@ public slots: // signals signals: void requestResetView(); - void sendMessage(QString message, int severity); void requestUpdate(); void sendFileInfo(QString name, QVector* materials, int vertices, int triangle); }; diff --git a/QtMeshViewer/Header/MshFile.h b/QtMeshViewer/Header/MshFile.h index 39a9596..f5c3fed 100644 --- a/QtMeshViewer/Header/MshFile.h +++ b/QtMeshViewer/Header/MshFile.h @@ -20,7 +20,7 @@ enum ModelTyp { class MshFile : public FileInterface { public: - explicit MshFile(QString path, QObject *parent = Q_NULLPTR); + explicit MshFile(QString path); virtual ~MshFile(); private: diff --git a/QtMeshViewer/Header/OglViewerWidget.h b/QtMeshViewer/Header/OglViewerWidget.h index ed77ed1..4050f7c 100644 --- a/QtMeshViewer/Header/OglViewerWidget.h +++ b/QtMeshViewer/Header/OglViewerWidget.h @@ -91,7 +91,6 @@ public slots: // signals signals: - void sendMessage(QString message, int severity); void loadFile(QString); }; diff --git a/QtMeshViewer/Header/OutputDevice.h b/QtMeshViewer/Header/OutputDevice.h new file mode 100644 index 0000000..3f4ee63 --- /dev/null +++ b/QtMeshViewer/Header/OutputDevice.h @@ -0,0 +1,27 @@ +#pragma once +#include + +class OutputDevice : public QObject +{ + Q_OBJECT + +private: + OutputDevice(QObject *parent = Q_NULLPTR) : QObject(parent) {}; + +public: + OutputDevice(OutputDevice const&) = delete; + void operator=(OutputDevice const&) = delete; + + ~OutputDevice() {}; + + static OutputDevice* getInstance(QObject *parent = Q_NULLPTR) { + static OutputDevice* instance = new OutputDevice(parent); + return instance; + }; + + void print(QString message, int severity) { emit sendMessage(message, severity); }; + +signals: + void sendMessage(QString message, int severity); + +}; \ No newline at end of file diff --git a/QtMeshViewer/Source/GeometryEngine.cpp b/QtMeshViewer/Source/GeometryEngine.cpp index 80549ba..002817e 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -2,6 +2,7 @@ #include "..\Header\MshFile.h" #include "..\Header\OglViewerWidget.h" #include "..\Header\MainWindow.h" +#include "..\Header\OutputDevice.h" #include @@ -154,7 +155,7 @@ void GeometryEngine::loadFile(QString filePath) //reset view emit requestResetView(); - emit sendMessage("loading file..", 0); + OutputDevice::getInstance()->print("loading file..", 0); try { @@ -163,7 +164,7 @@ void GeometryEngine::loadFile(QString filePath) QVector indexData; // open file and get the information - MshFile file(filePath, this); + MshFile file(filePath); models = file.getModels(); m_materials = file.getMaterials(); @@ -213,13 +214,13 @@ void GeometryEngine::loadFile(QString filePath) m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint)); emit requestUpdate(); - emit sendMessage("done..", 0); + OutputDevice::getInstance()->print("done..", 0); emit sendFileInfo(filePath.right(filePath.size() - filePath.lastIndexOf(QRegExp("/|\\\\")) - 1), m_materials, vertexData.size(), indexData.size() / 3); } catch (std::invalid_argument e) { clearData(); - emit sendMessage(QString(e.what()), 2); + OutputDevice::getInstance()->print(QString(e.what()), 2); } } diff --git a/QtMeshViewer/Source/MainWindow.cpp b/QtMeshViewer/Source/MainWindow.cpp index d09568e..ffa2daa 100644 --- a/QtMeshViewer/Source/MainWindow.cpp +++ b/QtMeshViewer/Source/MainWindow.cpp @@ -1,6 +1,7 @@ #include "..\Header\MainWindow.h" #include "..\Header\OglViewerWidget.h" #include "..\Header\FileInterface.h" +#include "..\Header\OutputDevice.h" #include #include #include @@ -29,7 +30,7 @@ MainWindow::MainWindow(QWidget *parent) setWindowTitle(WINDOW_NAME); setWindowIcon(QIcon(":/images/icon.ico")); - printMessage("MeshViewer by Anakin", 0); + connect(OutputDevice::getInstance(this), &OutputDevice::sendMessage, this, &MainWindow::printMessage); // setup opengl things QSurfaceFormat format; @@ -49,6 +50,8 @@ MainWindow::MainWindow(QWidget *parent) QFile styleSheet(":/files/StyleSheet.txt"); styleSheet.open(QIODevice::ReadOnly); this->setStyleSheet(styleSheet.readAll()); + + printMessage("MeshViewer by Anakin", 0); } MainWindow::~MainWindow() @@ -67,7 +70,6 @@ void MainWindow::setupWidgets() // Ogl Viewer OglViewerWidget* viewer = new OglViewerWidget(this); setCentralWidget(viewer); - connect(viewer, &OglViewerWidget::sendMessage, this, &MainWindow::printMessage); // open file QToolButton *openFile = new QToolButton(this); diff --git a/QtMeshViewer/Source/MshFile.cpp b/QtMeshViewer/Source/MshFile.cpp index 970d1cd..3e13bfb 100644 --- a/QtMeshViewer/Source/MshFile.cpp +++ b/QtMeshViewer/Source/MshFile.cpp @@ -1,5 +1,6 @@ #include "..\Header\MshFile.h" #include "..\Header\tga.h" +#include "..\Header\OutputDevice.h" #include // helper function to save data from file to any variable type @@ -9,8 +10,8 @@ ///////////////////////////////////////////////////////////////////////// // public constructor/destructor -MshFile::MshFile(QString path, QObject * parent) - : FileInterface(path, parent) +MshFile::MshFile(QString path) + : FileInterface(path) { import(); } @@ -89,7 +90,7 @@ void MshFile::loadChunks(std::list& destination, std::streampos st // out of file. Maybe a size information is corrupted if (!m_file.good()) { - emit sendMessage("WARNING: corrupted file. Trying to continue..", 1); + OutputDevice::getInstance()->print("WARNING: corrupted file. Trying to continue..", 1); m_file.clear(); break; } @@ -506,7 +507,7 @@ void MshFile::analyseSegmChunks(Model * dataDestination, std::list if (tmp_size < (unsigned) new_segment->vertices.size()) { - emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1); + OutputDevice::getInstance()->print("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1); for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--) for (unsigned int j = 0; j < 3; j++) @@ -514,7 +515,7 @@ void MshFile::analyseSegmChunks(Model * dataDestination, std::list } else if (tmp_size > (unsigned) new_segment->vertices.size()) { - emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1); + OutputDevice::getInstance()->print("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1); tmp_size = new_segment->vertices.size(); } @@ -698,7 +699,7 @@ void MshFile::readUV(Segment * dataDestination, std::streampos position) if (tmp_size < (unsigned) dataDestination->vertices.size()) { - emit sendMessage("WARNING: too less UVs " + QString::number(tmp_size) + " < " + QString::number(dataDestination->vertices.size()),1); + OutputDevice::getInstance()->print("WARNING: too less UVs " + QString::number(tmp_size) + " < " + QString::number(dataDestination->vertices.size()),1); for (unsigned int i = dataDestination->vertices.size(); i != tmp_size; i--) for (unsigned int j = 0; j < 2; j++) @@ -706,7 +707,7 @@ void MshFile::readUV(Segment * dataDestination, std::streampos position) } else if (tmp_size > (unsigned) dataDestination->vertices.size()) { - emit sendMessage("WARNING: too many UVs " + QString::number(tmp_size) + " > " + QString::number(dataDestination->vertices.size()), 1); + OutputDevice::getInstance()->print("WARNING: too many UVs " + QString::number(tmp_size) + " > " + QString::number(dataDestination->vertices.size()), 1); tmp_size = dataDestination->vertices.size(); } @@ -722,7 +723,7 @@ void MshFile::loadTexture(QOpenGLTexture *& destination, QString filepath, QStri if (!loadSuccess) { - emit sendMessage("WARNING: texture not found or corrupted: " + filename, 1); + OutputDevice::getInstance()->print("WARNING: texture not found or corrupted: " + filename, 1); img = QImage(1, 1, QImage::Format_RGB32); img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255)); diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp index 705d7be..588066d 100644 --- a/QtMeshViewer/Source/OglViewerWidget.cpp +++ b/QtMeshViewer/Source/OglViewerWidget.cpp @@ -1,5 +1,5 @@ #include "..\Header\OglViewerWidget.h" - +#include "..\Header\OutputDevice.h" #include #include #include @@ -65,7 +65,6 @@ void OglViewerWidget::setConnections() connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView); connect(parentWidget(), SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString))); connect(this, SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString))); - connect(m_dataEngine, SIGNAL(sendMessage(QString, int)), parentWidget(), SLOT(printMessage(QString, int))); connect(m_dataEngine, SIGNAL(requestUpdate()), this, SLOT(update())); connect(m_dataEngine, SIGNAL(sendFileInfo(QString, QVector*, int, int)), parentWidget(), SLOT(setFileInfo(QString, QVector*, int, int))); } @@ -308,12 +307,12 @@ void OglViewerWidget::keyPressEvent(QKeyEvent *e) { m_zSpeed -= 0.1; m_zSpeed < 0.09 ? m_zSpeed = 0 : NULL; - emit sendMessage(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0); + OutputDevice::getInstance()->print(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0); } else if (e->key() == Qt::Key_Plus) { m_zSpeed += 0.1; - emit sendMessage(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0); + OutputDevice::getInstance()->print(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0); } }