From beed9c8936992b63f10473656f4bd5df1adb176c Mon Sep 17 00:00:00 2001 From: edouard Date: Thu, 9 Sep 2010 21:18:43 +0000 Subject: [PATCH] Solved (please confirm) modelview crashing on non-existing files Made configuration portable for: dials, bargraph dials, pfd, modelview and system heatlh THis means: if your config points to resources in the share directory, then the config will be portable git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1574 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/openpilotgcs.pri | 6 ++- ground/src/libs/utils/pathutils.cpp | 38 ++++++++++++++++--- ground/src/libs/utils/pathutils.h | 2 + .../plugins/dial/dialgadgetconfiguration.cpp | 6 ++- .../lineardialgadgetconfiguration.cpp | 10 +++-- .../modelviewgadgetconfiguration.cpp | 13 +++++-- .../modelview/modelviewgadgetwidget.cpp | 13 +++++-- .../plugins/modelview/modelviewgadgetwidget.h | 1 + .../plugins/pfd/pfdgadgetconfiguration.cpp | 6 ++- .../systemhealthgadgetconfiguration.cpp | 8 +++- 10 files changed, 79 insertions(+), 24 deletions(-) diff --git a/ground/openpilotgcs.pri b/ground/openpilotgcs.pri index 61e8c0121..f74cec962 100644 --- a/ground/openpilotgcs.pri +++ b/ground/openpilotgcs.pri @@ -63,6 +63,7 @@ macx { GCS_PLUGIN_PATH = $$GCS_LIBRARY_PATH GCS_LIBEXEC_PATH = $$GCS_APP_PATH/$${GCS_APP_TARGET}.app/Contents/Resources GCS_DATA_PATH = $$GCS_APP_PATH/$${GCS_APP_TARGET}.app/Contents/Resources + GCS_DATA_BASENAME = $${GCS_APP_TARGET}.app/Contents/Resources GCS_DOC_PATH = $$GCS_DATA_PATH/doc copydata = 1 } else { @@ -76,13 +77,14 @@ macx { GCS_LIBRARY_PATH = $$GCS_BUILD_TREE/$$GCS_LIBRARY_BASENAME/openpilotgcs GCS_PLUGIN_PATH = $$GCS_LIBRARY_PATH/plugins GCS_LIBEXEC_PATH = $$GCS_APP_PATH # FIXME - GCS_DATA_PATH = $$GCS_BUILD_TREE/share + GCS_DATA_PATH = $$GCS_BUILD_TREE/share/openpilotgcs + GCS_DATA_BASENAME = share/openpilotgcs GCS_DOC_PATH = $$GCS_BUILD_TREE/share/doc !isEqual(GCS_SOURCE_TREE, $$GCS_BUILD_TREE):copydata = 1 } -DEFINES += GCS_DATA_PATH=\\\"$$GCS_DATA_PATH\\\" +DEFINES += GCS_DATA_BASENAME=\\\"$$GCS_DATA_BASENAME\\\" INCLUDEPATH += \ diff --git a/ground/src/libs/utils/pathutils.cpp b/ground/src/libs/utils/pathutils.cpp index 0e8c5e131..d788a3a7e 100644 --- a/ground/src/libs/utils/pathutils.cpp +++ b/ground/src/libs/utils/pathutils.cpp @@ -43,15 +43,41 @@ namespace Utils { QString PathUtils::GetDataPath() { // Figure out root: Up one from 'bin' - //QDir rootDir = QApplication::applicationDirPath(); - //rootDir.cdUp(); - //const QString rootDirPath = rootDir.canonicalPath(); - //QString pluginPath = rootDirPath; - //pluginPath += QLatin1Char('/'); - QString pluginPath = QLatin1String(GCS_DATA_PATH); + QDir rootDir = QApplication::applicationDirPath(); + rootDir.cdUp(); + const QString rootDirPath = rootDir.canonicalPath(); + QString pluginPath = rootDirPath; + pluginPath += QLatin1Char('/'); + pluginPath += QLatin1String(GCS_DATA_BASENAME); pluginPath += QLatin1Char('/'); return pluginPath; } +/** + Cuts the standard data path from the 'path' argument. If path does not start +with the standard data path, then do nothing. + */ +QString PathUtils::RemoveDataPath(QString path) +{ + if (path.startsWith(GetDataPath())) { + int i = path.length()- GetDataPath().length(); + return QString("%%DATAPATH%%") + path.right(i); + } else + return path; +} + +/** + Inserts the data path (only if the path starts with %%DATAPATH%%) + */ +QString PathUtils::InsertDataPath(QString path) +{ + if (path.startsWith(QString("%%DATAPATH%%"))) + { + QString newPath = GetDataPath(); + newPath += path.right(path.length()-12); + return newPath; + } + return path; +} } diff --git a/ground/src/libs/utils/pathutils.h b/ground/src/libs/utils/pathutils.h index cb04c5ac7..c4646f970 100644 --- a/ground/src/libs/utils/pathutils.h +++ b/ground/src/libs/utils/pathutils.h @@ -41,6 +41,8 @@ class QTCREATOR_UTILS_EXPORT PathUtils public: PathUtils(); QString GetDataPath(); + QString RemoveDataPath(QString path); + QString InsertDataPath(QString path); }; diff --git a/ground/src/plugins/dial/dialgadgetconfiguration.cpp b/ground/src/plugins/dial/dialgadgetconfiguration.cpp index 2abec04eb..fab159d4e 100644 --- a/ground/src/plugins/dial/dialgadgetconfiguration.cpp +++ b/ground/src/plugins/dial/dialgadgetconfiguration.cpp @@ -27,6 +27,7 @@ */ #include "dialgadgetconfiguration.h" +#include "utils/pathutils.h" #include /** @@ -59,7 +60,7 @@ DialGadgetConfiguration::DialGadgetConfiguration(QString classId, const QByteArr QDataStream stream(state); QString dialFile; stream >> dialFile; - m_defaultDial=dialFile; + m_defaultDial=Utils::PathUtils().InsertDataPath(dialFile); stream >> dialBackgroundID; stream >> dialForegroundID; stream >> dialNeedleID1; @@ -130,7 +131,8 @@ QByteArray DialGadgetConfiguration::saveState() const { QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); - stream << m_defaultDial; + QString dialFile = Utils::PathUtils().RemoveDataPath(m_defaultDial); + stream << dialFile; stream << dialBackgroundID; stream << dialForegroundID; stream << dialNeedleID1; diff --git a/ground/src/plugins/lineardial/lineardialgadgetconfiguration.cpp b/ground/src/plugins/lineardial/lineardialgadgetconfiguration.cpp index f75da203a..ef7d1e3ff 100644 --- a/ground/src/plugins/lineardial/lineardialgadgetconfiguration.cpp +++ b/ground/src/plugins/lineardial/lineardialgadgetconfiguration.cpp @@ -7,7 +7,7 @@ * @{ * @addtogroup LinearDialPlugin Linear Dial Plugin * @{ - * @brief Impliments a gadget that displays linear gauges + * @brief Implements a gadget that displays linear gauges *****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify @@ -26,6 +26,7 @@ */ #include "lineardialgadgetconfiguration.h" +#include "utils/pathutils.h" #include /** @@ -51,7 +52,9 @@ LineardialGadgetConfiguration::LineardialGadgetConfiguration(QString classId, co //if a saved configuration exists load it if (state.count() > 0) { QDataStream stream(state); - stream >> dialFile; + QString dFile; + stream >> dFile; + dialFile = Utils::PathUtils().InsertDataPath(dFile); stream >> sourceDataObject; stream >> sourceObjectField; stream >> minValue; @@ -99,7 +102,8 @@ QByteArray LineardialGadgetConfiguration::saveState() const { QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); - stream << dialFile; + QString dFile = Utils::PathUtils().RemoveDataPath(dialFile); + stream << dFile; stream << sourceDataObject; stream << sourceObjectField; stream << minValue; diff --git a/ground/src/plugins/modelview/modelviewgadgetconfiguration.cpp b/ground/src/plugins/modelview/modelviewgadgetconfiguration.cpp index 2a13d64ce..4fdb312d5 100644 --- a/ground/src/plugins/modelview/modelviewgadgetconfiguration.cpp +++ b/ground/src/plugins/modelview/modelviewgadgetconfiguration.cpp @@ -26,6 +26,7 @@ */ #include "modelviewgadgetconfiguration.h" +#include "utils/pathutils.h" #include ModelViewGadgetConfiguration::ModelViewGadgetConfiguration(QString classId, const QByteArray &state, QObject *parent) : @@ -36,9 +37,13 @@ ModelViewGadgetConfiguration::ModelViewGadgetConfiguration(QString classId, cons { if (state.count() > 0) { QDataStream stream(state); - stream >> m_acFilename; - stream >> m_bgFilename; + QString modelFile; + QString bgFile; + stream >> modelFile; + stream >> bgFile; stream >> m_enableVbo; + m_acFilename = Utils::PathUtils().InsertDataPath(modelFile); + m_bgFilename = Utils::PathUtils().InsertDataPath(bgFile); } } @@ -56,8 +61,8 @@ QByteArray ModelViewGadgetConfiguration::saveState() const QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); - stream << m_acFilename; - stream << m_bgFilename; + stream << Utils::PathUtils().RemoveDataPath(m_acFilename); + stream << Utils::PathUtils().RemoveDataPath(m_bgFilename); stream << m_enableVbo; return bytes; } diff --git a/ground/src/plugins/modelview/modelviewgadgetwidget.cpp b/ground/src/plugins/modelview/modelviewgadgetwidget.cpp index 26132e01d..e2a4c5285 100644 --- a/ground/src/plugins/modelview/modelviewgadgetwidget.cpp +++ b/ground/src/plugins/modelview/modelviewgadgetwidget.cpp @@ -77,6 +77,8 @@ void ModelViewGadgetWidget::reloadScene() //// Private functions //// void ModelViewGadgetWidget::initializeGL() { + if (loadError) + return; // OpenGL initialization m_GlView.initGl(); if (!vboEnable) @@ -97,6 +99,8 @@ void ModelViewGadgetWidget::initializeGL() void ModelViewGadgetWidget::paintGL() { + if (loadError) + return; // Clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Load identity matrix @@ -142,15 +146,18 @@ void ModelViewGadgetWidget::CreateScene() if(QFile::exists(acFilename)) { QFile aircraft(acFilename); - m_World= GLC_Factory::instance()->createWorldFromFile(aircraft); - m_ModelBoundingBox= m_World.boundingBox(); - } + loadError = false; + initializeGL(); + } else { + loadError = true; + } } catch(GLC_Exception e) { qDebug("ModelView aircraft texture file loading failed."); + loadError = true; } } diff --git a/ground/src/plugins/modelview/modelviewgadgetwidget.h b/ground/src/plugins/modelview/modelviewgadgetwidget.h index 20d18e3d0..25639d206 100644 --- a/ground/src/plugins/modelview/modelviewgadgetwidget.h +++ b/ground/src/plugins/modelview/modelviewgadgetwidget.h @@ -89,6 +89,7 @@ private: QString acFilename; QString bgFilename; bool vboEnable; + bool loadError; AttitudeActual* attActual; }; diff --git a/ground/src/plugins/pfd/pfdgadgetconfiguration.cpp b/ground/src/plugins/pfd/pfdgadgetconfiguration.cpp index 003da88e6..1b3b03ecd 100644 --- a/ground/src/plugins/pfd/pfdgadgetconfiguration.cpp +++ b/ground/src/plugins/pfd/pfdgadgetconfiguration.cpp @@ -26,6 +26,7 @@ */ #include "pfdgadgetconfiguration.h" +#include "utils/pathutils.h" #include /** @@ -43,7 +44,7 @@ PFDGadgetConfiguration::PFDGadgetConfiguration(QString classId, const QByteArray stream >> dialFile; stream >> useOpenGLFlag; stream >> hqFonts; - m_defaultDial=dialFile; + m_defaultDial=Utils::PathUtils().InsertDataPath(dialFile); } } /** @@ -66,7 +67,8 @@ QByteArray PFDGadgetConfiguration::saveState() const { QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); - stream << m_defaultDial; + QString dialFile = Utils::PathUtils().RemoveDataPath(m_defaultDial); + stream << dialFile; stream << useOpenGLFlag; stream << hqFonts; diff --git a/ground/src/plugins/systemhealth/systemhealthgadgetconfiguration.cpp b/ground/src/plugins/systemhealth/systemhealthgadgetconfiguration.cpp index f7044c962..71d572ccb 100644 --- a/ground/src/plugins/systemhealth/systemhealthgadgetconfiguration.cpp +++ b/ground/src/plugins/systemhealth/systemhealthgadgetconfiguration.cpp @@ -26,6 +26,7 @@ */ #include "systemhealthgadgetconfiguration.h" +#include "utils/pathutils.h" #include /** @@ -39,7 +40,9 @@ SystemHealthGadgetConfiguration::SystemHealthGadgetConfiguration(QString classId //if a saved configuration exists load it if (state.count() > 0) { QDataStream stream(state); - stream >> systemFile; + QString diagram; + stream >> diagram; + systemFile = Utils::PathUtils().InsertDataPath(diagram); } } /** @@ -60,7 +63,8 @@ QByteArray SystemHealthGadgetConfiguration::saveState() const { QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); - stream << systemFile; + QString diagram = Utils::PathUtils().RemoveDataPath(systemFile); + stream << diagram; return bytes; }