diff --git a/apps/OpenSpace/main.cpp b/apps/OpenSpace/main.cpp index e7f534c60c..56337cc78d 100644 --- a/apps/OpenSpace/main.cpp +++ b/apps/OpenSpace/main.cpp @@ -517,7 +517,7 @@ void mainPostSyncPreDrawFunc() { -void mainRenderFunc(const RenderData& data) { +void mainRenderFunc(const sgct::RenderData& data) { ZoneScoped #ifdef OPENSPACE_HAS_VTUNE @@ -592,7 +592,7 @@ void mainRenderFunc(const RenderData& data) { -void mainDraw2DFunc(const RenderData& data) { +void mainDraw2DFunc(const sgct::RenderData& data) { ZoneScoped #ifdef OPENSPACE_HAS_VTUNE @@ -1295,9 +1295,9 @@ int main(int argc, char** argv) { Engine::create(cluster, callbacks, config); } catch (...) { - Engine::destroy(); global::openSpaceEngine.deinitialize(); ghoul::deinitialize(); + Engine::destroy(); throw; } diff --git a/data/assets/default.profile b/data/assets/default.profile index b5614baf84..7d10fb2b71 100644 --- a/data/assets/default.profile +++ b/data/assets/default.profile @@ -3,10 +3,10 @@ #Asset scene/solarsystem/planets/earth/earth required -scene/solarsystem/planets/earth/satellites/satellites required +scene/solarsystem/planets/earth/satellites/satellites required #Property -setPropertyValue {earth_satellites}.Renderable.Enabled false +setPropertyValue {earth_satellites}.Renderable.Enabled false #Time relative -1d diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 76c06f8fd7..07838dc8c0 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -77,7 +77,7 @@ public: const glm::mat4& projectionMatrix); void drawOverlays(); void postDraw(); - std::vector listOfAllAssetEvents(); + std::vector assetEvents(); void resetAssetChangeTracking(); void resetPropertyChangeFlags(); void keyboardCallback(Key key, KeyModifier mod, KeyAction action); diff --git a/include/openspace/properties/property.h b/include/openspace/properties/property.h index 779ef9fe42..ce92102ece 100644 --- a/include/openspace/properties/property.h +++ b/include/openspace/properties/property.h @@ -500,7 +500,7 @@ public: * * \return true if the property has changed */ - bool hasChanged(); + bool hasChanged() const; /** * Reset the valChanged flag to an unchanged state, as if value has not been changed. @@ -544,7 +544,7 @@ protected: std::vector>> _onDeleteCallbacks; /// Flag indicating that this property value has been changed after initialization - bool valChanged = false; + bool _isValueDirty = false; private: void notifyDeleteListeners(); diff --git a/include/openspace/properties/templateproperty.inl b/include/openspace/properties/templateproperty.inl index 78af3f6039..f8bdb0ecd3 100644 --- a/include/openspace/properties/templateproperty.inl +++ b/include/openspace/properties/templateproperty.inl @@ -176,7 +176,7 @@ void openspace::properties::TemplateProperty::setValue(T val) { if (val != _value) { _value = std::move(val); notifyChangeListeners(); - valChanged = true; + _isValueDirty = true; } } @@ -197,7 +197,7 @@ void TemplateProperty::set(std::any value) { if (v != _value) { _value = std::move(v); notifyChangeListeners(); - valChanged = true; + _isValueDirty = true; } } diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index fbb7db24e2..dd7a1fb0af 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -174,12 +174,12 @@ public: * Retrieve a reference to vector list of all assets events, including require, * request, and remove */ - std::vector& listOfAllAssetEvents(); + const std::vector& assetEvents() const; /** * Clear lists of all assets that have been either requested, required, or removed */ - void listOfAllAssetEvents_reset(); + void resetAssetEvents(); private: std::shared_ptr require(const std::string& identifier); @@ -241,9 +241,6 @@ private: int _assetsTableRef; std::vector _profileAssets; - std::vector _profileAssetsRequired; - std::vector _profileAssetsRequested; - std::vector _profileAssetsRemoved; }; } // namespace openspace diff --git a/include/openspace/scene/assetmanager.h b/include/openspace/scene/assetmanager.h index db010c7d02..6caa9a66b4 100644 --- a/include/openspace/scene/assetmanager.h +++ b/include/openspace/scene/assetmanager.h @@ -71,8 +71,8 @@ public: std::shared_ptr child) override; bool update(); - const std::vector& listOfAllAssetEvents() const; - void listOfAllAssetEvents_reset(); + const std::vector& assetEvents() const; + void resetAssetEvents(); scripting::LuaLibrary luaLibrary(); private: diff --git a/include/openspace/scene/profile.h b/include/openspace/scene/profile.h index c758f97af2..89ad550fe2 100644 --- a/include/openspace/scene/profile.h +++ b/include/openspace/scene/profile.h @@ -25,9 +25,8 @@ #ifndef __OPENSPACE_CORE___PROFILE___H__ #define __OPENSPACE_CORE___PROFILE___H__ -#include - #include +#include #include #include #include @@ -45,24 +44,18 @@ namespace openspace { namespace documentation { struct Documentation; } namespace scripting { struct LuaLibrary; } -const std::string profileFormatVersion = "1.0"; - class Profile { public: + static constexpr const char* FormatVersion = "1.0"; + enum class AssetEventType { - add, - require, - request, - remove, - ignore - }; - const std::map AssetEventTypeString { - {AssetEventType::add, "add"}, - {AssetEventType::require, "required"}, - {AssetEventType::request, "requested"}, - {AssetEventType::remove, "removed"}, - {AssetEventType::ignore, "ignored"}, + Add, + Require, + Request, + Remove, + Ignore }; + struct AssetEvent { std::string name; AssetEventType eventType; @@ -70,16 +63,12 @@ public: virtual ~Profile() {}; - const std::string formatVersion() { - return profileFormatVersion; - } - /** * Saves all current settings, starting from the profile that was loaded at startup, * and all of the property & asset changes that were made since startup. * \param filename The filename of the new profile to be saved */ - void saveCurrentSettingsToProfile(std::string filename); + void saveCurrentSettingsToProfile(const std::string& filename); /** * Saves all current settings, similar to saveCurrentSettingsToProfile() except the @@ -94,8 +83,8 @@ public: * \param outFilePath The output file path that will be written with the converted * contents (in an .asset file) */ - void convertToSceneFile(const std::string inProfilePath, - const std::string outFilePath); + void convertToSceneFile(const std::string& inProfilePath, + const std::string& outFilePath); /** * Returns the string contents of a profileFile object converted to scene/asset @@ -120,10 +109,10 @@ private: std::vector base; std::vector changed; }; - virtual bool usingProfile(); - virtual std::string initialProfile(); - virtual std::string profileBaseDirectory(); - virtual std::vector listOfAllAssetEvents(); + virtual bool usingProfile() const; + virtual std::string initialProfile() const; + virtual std::string profileBaseDirectory() const; + virtual std::vector assetEvents() const; ProfileFile collateBaseWithChanges(); std::string convertToScene_assets(ProfileFile& pf); std::string convertToScene_modules(ProfileFile& pf); @@ -133,23 +122,16 @@ private: std::string convertToScene_time(ProfileFile& pf); std::string convertToScene_camera(ProfileFile& pf); - void updateToCurrentFormatVersion(ProfileFile& pf); std::vector modifyAssetsToReflectChanges(ProfileFile& pf); void parseAssetFileLines(std::vector& results, ProfileFile& pf); - void handleChangedAdd(std::vector& base, unsigned int changedIdx, - std::vector& changed, std::string asset); - void handleChangedRemove(std::vector& base, std::string asset); - void addAssetsToProfileFile(std::vector& allAssets, ProfileFile& pf); + void modifyPropertiesToReflectChanges(ProfileFile& pf); - virtual std::vector getChangedProperties(); - void checkForChangedProps(std::vector& changedList, - openspace::properties::PropertyOwner* po); + virtual std::vector changedProperties(); std::string getFullPropertyPath(openspace::properties::Property* prop); - virtual std::vector getChangedPropertiesFormatted(); - virtual std::string getCurrentTimeUTC(); - virtual interaction::NavigationHandler::NavigationState getCurrentCameraState(); - void addCurrentTimeToProfileFile(ProfileFile& pf); - void addCurrentCameraToProfileFile(ProfileFile& pf); + virtual std::vector changedPropertiesFormatted(); + virtual std::string currentTimeUTC() const; + virtual interaction::NavigationHandler::NavigationState currentCameraState() const; + void addCurrentCameraToProfileFile(ProfileFile& pf) const; }; } // namespace openspace diff --git a/include/openspace/scene/profilefile.h b/include/openspace/scene/profilefile.h index 807daf8cfd..840245fc86 100644 --- a/include/openspace/scene/profilefile.h +++ b/include/openspace/scene/profilefile.h @@ -150,21 +150,21 @@ public: * form. * \param filename The filename to write to. */ - void writeToFile(const std::string filename); + void writeToFile(const std::string& filename); /** * Updates the full string that defines the starting time. The format for this line * is defined by ProfileFile::parseTime and Profile::convertToAsset_time * \param line The time entry line to replace current time entry */ - void updateTime(const std::string line); + void updateTime(std::string line); /** * Updates the full string that defines the starting camera position. The format for * this line is defined by ProfileFile::parseCamera & Profile::convertToAsset_camera * \param line The camera entry line to replace current camera entry */ - void updateCamera(const std::string line); + void updateCamera(std::string line); /** * Adds a new module line to the list of module lines to be analyzed by the profile @@ -172,14 +172,14 @@ public: * and Profile::convertToAsset_modules * \param line The module name to be added */ - void addModuleLine(const std::string line); + void addModuleLine(std::string line); /** * Adds a new asset to the list of assets to be loaded at startup. The format for an * asset line is defined by ProfileFile::parseAsset & Profile::convertToAsset_assets * \param line The asset name to be added */ - void addAssetLine(const std::string line); + void addAssetLine(std::string line); /** * Clears all asset entries @@ -192,7 +192,7 @@ public: * ProfileFile::parseProperty and Profile::convertToAsset_properties * \param line The property set command to be added */ - void addPropertyLine(const std::string line); + void addPropertyLine(std::string line); /** * Adds a new keybinding shortcut to the list of keybindings. The format for a @@ -200,7 +200,7 @@ public: * Profile::convertToAsset_keybindings * \param line The keyboard shortcut line to be added */ - void addKeybindingLine(const std::string line); + void addKeybindingLine(std::string line); /** * Adds a new scenegraph node name to be added to the list of those marked as @@ -208,7 +208,7 @@ public: * ProfileFile::parseMarkNodes and Profile::convertToAsset_markNodes * \param line The scenegraph node to be added */ - void addMarkNodesLine(const std::string line); + void addMarkNodesLine(std::string line); /** * Returns the format version number (profiles syntax version) string diff --git a/modules/imgui/src/gui.cpp b/modules/imgui/src/gui.cpp index f1d2d3ffcd..04a6dd092b 100644 --- a/modules/imgui/src/gui.cpp +++ b/modules/imgui/src/gui.cpp @@ -190,16 +190,13 @@ GUI::GUI() GUI::~GUI() {} // NOLINT -void GUI::initialize() { - -} +void GUI::initialize() {} void GUI::deinitialize() { ImGui::Shutdown(); - int nWindows = global::windowDelegate.nWindows(); - for (int i = 0; i < nWindows; ++i) { - ImGui::DestroyContext(_contexts[i]); + for (ImGuiContext* ctx : _contexts) { + ImGui::DestroyContext(ctx); } for (GuiComponent* comp : _components) { diff --git a/openspace.cfg b/openspace.cfg index a557e5468d..0bf64655ba 100644 --- a/openspace.cfg +++ b/openspace.cfg @@ -45,7 +45,7 @@ SGCTConfig = sgct.config.single{} -- Sets the scene that is to be loaded by OpenSpace. A scene file is a description -- of all entities that will be visible during an instance of OpenSpace -Asset = "default" +-- Asset = "default" -- Asset = "asteroids" -- Asset = "default_full" -- Asset = "newhorizons" @@ -64,7 +64,7 @@ Asset = "default" -- Sets the profile that should be loaded by OpenSpace. Profiles are going to replace -- assets in a future versions and shouldn't be used at the same time as the 'Asset' -- setting above --- Profile = "default" +Profile = "default" -- These scripts are executed after the initialization of each scene, thus making -- it possible to have global overrides to default values or execute other scripts diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 10c4b9d9c8..576ae5372b 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -1286,11 +1286,11 @@ void OpenSpaceEngine::postDraw() { } void OpenSpaceEngine::resetAssetChangeTracking() { - global::openSpaceEngine._assetManager->listOfAllAssetEvents_reset(); + global::openSpaceEngine._assetManager->resetAssetEvents(); } -std::vector OpenSpaceEngine::listOfAllAssetEvents() { - return global::openSpaceEngine._assetManager->listOfAllAssetEvents(); +std::vector OpenSpaceEngine::assetEvents() { + return global::openSpaceEngine._assetManager->assetEvents(); } void OpenSpaceEngine::resetPropertyChangeFlags() { diff --git a/src/properties/property.cpp b/src/properties/property.cpp index 1bedc86ac7..85f2f6fdd9 100644 --- a/src/properties/property.cpp +++ b/src/properties/property.cpp @@ -312,12 +312,12 @@ void Property::notifyDeleteListeners() { } } -bool Property::hasChanged() { - return valChanged; +bool Property::hasChanged() const { + return _isValueDirty; } void Property::resetToUnchanged() { - valChanged = false; + _isValueDirty = false; } std::string Property::generateBaseJsonDescription() const { diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 31bfa3e180..d4ec30c2f4 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -456,7 +456,7 @@ std::shared_ptr AssetLoader::request(const std::string& identifier) { } void AssetLoader::addToProfileTracking(std::string asset, Profile::AssetEventType type) { - Profile::AssetEvent pa = {asset, type}; + Profile::AssetEvent pa = { std::move(asset), type }; _profileAssets.push_back(pa); } @@ -478,7 +478,7 @@ ghoul::filesystem::Directory AssetLoader::currentDirectory() const { std::shared_ptr AssetLoader::add(const std::string& identifier) { setCurrentAsset(_rootAsset); - addToProfileTracking(identifier, Profile::AssetEventType::add); + addToProfileTracking(identifier, Profile::AssetEventType::Add); return request(identifier); } @@ -486,8 +486,7 @@ std::shared_ptr AssetLoader::add(const std::string& identifier) { void AssetLoader::remove(const std::string& identifier) { setCurrentAsset(_rootAsset); unrequest(identifier); - _profileAssetsRemoved.push_back(identifier); - addToProfileTracking(identifier, Profile::AssetEventType::remove); + addToProfileTracking(identifier, Profile::AssetEventType::Remove); } std::shared_ptr AssetLoader::has(const std::string& identifier) const { @@ -814,11 +813,11 @@ void AssetLoader::assetUnrequested(std::shared_ptr parent, } } -std::vector& AssetLoader::listOfAllAssetEvents() { +const std::vector& AssetLoader::assetEvents() const { return _profileAssets; } -void AssetLoader::listOfAllAssetEvents_reset() { +void AssetLoader::resetAssetEvents() { _profileAssets.clear(); } diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index d9d8e5e12e..bb98f0d0cf 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -121,12 +121,12 @@ std::shared_ptr AssetManager::rootAsset() { return _assetLoader->rootAsset(); } -const std::vector& AssetManager::listOfAllAssetEvents() const { - return _assetLoader->listOfAllAssetEvents(); +const std::vector& AssetManager::assetEvents() const { + return _assetLoader->assetEvents(); } -void AssetManager::listOfAllAssetEvents_reset() { - _assetLoader->listOfAllAssetEvents_reset(); +void AssetManager::resetAssetEvents() { + _assetLoader->resetAssetEvents(); } scripting::LuaLibrary AssetManager::luaLibrary() { diff --git a/src/scene/profile.cpp b/src/scene/profile.cpp index 1da60105df..3728d9e7ee 100644 --- a/src/scene/profile.cpp +++ b/src/scene/profile.cpp @@ -51,15 +51,104 @@ #include "profile_lua.inl" +namespace openspace { + namespace { constexpr const char* _loggerCat = "Profile"; constexpr const char* KeyIdentifier = "Identifier"; constexpr const char* KeyParent = "Parent"; + + const std::map AssetEventTypeString{ + { Profile::AssetEventType::Add, "add" }, + { Profile::AssetEventType::Require, "required" }, + { Profile::AssetEventType::Request, "requested" }, + { Profile::AssetEventType::Remove, "removed" }, + { Profile::AssetEventType::Ignore, "ignored" }, + }; + + void handleChangedAdd(std::vector& base, unsigned int changedIdx, + std::vector& changed, std::string asset) + { + // @TODO: Replace the next for loop with std::any_of or std::all_of + + bool addThisAsset = true; + // Check base profile to see if has already been added there + for (const Profile::AssetEvent& b : base) { + if (b.name == asset) { + if (b.eventType == Profile::AssetEventType::Require + || b.eventType == Profile::AssetEventType::Request) + { + addThisAsset = false; + break; + } + } + } + + // Check changed asset commands only prior to this one to see if already added + for (unsigned int i = 0; i < changedIdx; i++) { + if (changed[i].name == asset) { + addThisAsset = false; + break; + } + } + + if (addThisAsset) { + Profile::AssetEvent ae = { + std::move(asset), + Profile::AssetEventType::Request + }; + base.push_back(ae); + } + } + + void handleChangedRemove(std::vector& base, std::string asset) { + base.push_back({ std::move(asset), Profile::AssetEventType::Remove }); + } + + void addAssetsToProfileFile(ProfileFile& pf, + const std::vector& allAssets) + { + pf.clearAssets(); + for (Profile::AssetEvent a : allAssets) { + if (a.eventType != Profile::AssetEventType::Ignore) { + std::string entry = + a.name + "\t" + AssetEventTypeString.at(a.eventType); + pf.addAssetLine(entry); + } + } + } + + std::string recurseForFullName(properties::PropertyOwner* po) { + if (po == nullptr) { + return ""; + } + std::string name = recurseForFullName(po->owner()) + po->identifier(); + if (!name.empty()) { + return name + "."; + } + else { + return ""; + } + } + + void checkForChangedProps(std::vector& changedList, + properties::PropertyOwner* po) + { + if (po) { + for (properties::PropertyOwner* subOwner : po->propertySubOwners()) { + checkForChangedProps(changedList, subOwner); + } + for (properties::Property* p : po->properties()) { + if (p->hasChanged()) { + changedList.push_back(p); + } + } + } + } + } // namespace -namespace openspace { - -void Profile::saveCurrentSettingsToProfile(std::string filename) { +void Profile::saveCurrentSettingsToProfile(const std::string& filename) { ProfileFile pf = collateBaseWithChanges(); pf.writeToFile(filename); } @@ -69,112 +158,67 @@ std::string Profile::saveCurrentSettingsToProfile_string() { return pf.writeToString(); } -bool Profile::usingProfile() { +bool Profile::usingProfile() const { return global::configuration.usingProfile; } -std::string Profile::initialProfile() { +std::string Profile::initialProfile() const { return global::configuration.profile; } -std::vector Profile::listOfAllAssetEvents() { - return global::openSpaceEngine.listOfAllAssetEvents(); +std::vector Profile::assetEvents() const { + return global::openSpaceEngine.assetEvents(); } -std::string Profile::profileBaseDirectory() { +std::string Profile::profileBaseDirectory() const { return _profileBaseDirectory; } ProfileFile Profile::collateBaseWithChanges() { - if (! usingProfile()) { + if (!usingProfile()) { std::string errorMessage = "Program was not started using a profile, " - "so cannot use this save-current-settings feature."; + "so cannot use this save-current-settings feature"; LERROR(errorMessage); } std::string initProfile = initialProfile(); std::string inputProfilePath = absPath(_profileBaseDirectory) + "/" + initProfile + ".profile"; ProfileFile pf(inputProfilePath); - updateToCurrentFormatVersion(pf); + pf.setVersion(FormatVersion); std::vector ass = modifyAssetsToReflectChanges(pf); - addAssetsToProfileFile(ass, pf); + addAssetsToProfileFile(pf, ass); modifyPropertiesToReflectChanges(pf); - addCurrentTimeToProfileFile(pf); + + // add current time to profile file + std::string t = currentTimeUTC(); + std::string update = "absolute\t" + t; + pf.updateTime(update); + addCurrentCameraToProfileFile(pf); return pf; } -void Profile::updateToCurrentFormatVersion(ProfileFile& pf) { - pf.setVersion(profileFormatVersion); -} - std::vector Profile::modifyAssetsToReflectChanges(ProfileFile& pf) { std::vector a; parseAssetFileLines(a, pf); AllAssetDetails assetDetails; assetDetails.base = a; - assetDetails.changed = listOfAllAssetEvents(); + assetDetails.changed = assetEvents(); for (unsigned int i = 0; i < assetDetails.changed.size(); i++) { AssetEvent event = assetDetails.changed[i]; - if (event.eventType == AssetEventType::add) { + if (event.eventType == AssetEventType::Add) { handleChangedAdd(assetDetails.base, i, assetDetails.changed, event.name); } - else if (event.eventType == AssetEventType::remove) { + else if (event.eventType == AssetEventType::Remove) { handleChangedRemove(assetDetails.base, event.name); } } return assetDetails.base; } -void Profile::handleChangedAdd(std::vector& base, unsigned int changedIdx, - std::vector& changed, std::string asset) -{ - bool addThisAsset = true; - //Check base profile to see if has already been added there - for (auto b : base) { - if (b.name == asset ) { - if ( b.eventType == AssetEventType::require - || b.eventType == AssetEventType::request) - { - addThisAsset = false; - break; - } - } - } - - //Check changed asset commands only prior to this one to see if already added - for (unsigned int i = 0; i < changedIdx; i++) { - if (changed[i].name == asset) { - addThisAsset = false; - break; - } - } - - if (addThisAsset) { - AssetEvent ae = {asset, AssetEventType::request}; - base.push_back(ae); - } -} - -void Profile::handleChangedRemove(std::vector& base, std::string asset) -{ - base.push_back({asset, AssetEventType::remove}); -} - -void Profile::addAssetsToProfileFile(std::vector& allAssets, ProfileFile& pf) -{ - pf.clearAssets(); - for (AssetEvent a : allAssets) { - if (a.eventType != AssetEventType::ignore) { - std::string entry = a.name + "\t" + AssetEventTypeString.at(a.eventType); - pf.addAssetLine(entry); - } - } -} - void Profile::parseAssetFileLines(std::vector& results, ProfileFile& pf) { std::vector elements; AssetEvent a; @@ -183,25 +227,29 @@ void Profile::parseAssetFileLines(std::vector& results, ProfileFile& pf.splitByTab(line, elements); if (elements[0].empty()) { - LERROR("Error in parsing asset file line '" + line - + "'. Asset name is needed (field 1/2)."); + LERROR(fmt::format( + "Error parsing profile line '{}'. Asset name is needed (field 1/2)", + line + )); } else { a.name = elements[0]; } - if (elements[1] == AssetEventTypeString.at(AssetEventType::require)) { - a.eventType = AssetEventType::require; + if (elements[1] == AssetEventTypeString.at(AssetEventType::Require)) { + a.eventType = AssetEventType::Require; } - else if (elements[1] == AssetEventTypeString.at(AssetEventType::request)) { - a.eventType = AssetEventType::request; + else if (elements[1] == AssetEventTypeString.at(AssetEventType::Request)) { + a.eventType = AssetEventType::Request; } else if (elements[1] == "") { - a.eventType = AssetEventType::request; + a.eventType = AssetEventType::Request; } else { - LERROR("Error in parsing asset file line '" + line - + "'. Invalid required param (field 2/2)."); + LERROR(fmt::format( + "Error parsing profile line '{}'. Invalid required param (field 2/2)", + line + )); } results.push_back(a); @@ -209,19 +257,18 @@ void Profile::parseAssetFileLines(std::vector& results, ProfileFile& } void Profile::modifyPropertiesToReflectChanges(ProfileFile& pf) { - std::vector formatted = getChangedPropertiesFormatted(); + std::vector formatted = changedPropertiesFormatted(); - for (std::string line: formatted) { - pf.addPropertyLine(line); + for (std::string line : formatted) { + pf.addPropertyLine(std::move(line)); } } -std::vector Profile::getChangedPropertiesFormatted() { - std::vector changedProps - = getChangedProperties(); +std::vector Profile::changedPropertiesFormatted() { + std::vector changedProps = changedProperties(); std::vector formattedLines; - for (auto prop : changedProps) { + for (properties::Property* prop : changedProps) { std::string newLine = "setPropertyValueSingle\t"; newLine += getFullPropertyPath(prop) + "\t"; newLine += prop->getStringValue(); @@ -230,69 +277,35 @@ std::vector Profile::getChangedPropertiesFormatted() { return formattedLines; } -std::string recurseForFullName(openspace::properties::PropertyOwner* po) { - std::string path; - if (po != nullptr) { - std::string name = recurseForFullName(po->owner()) + po->identifier(); - if (!name.empty()) { - path = name + "."; - } - } - return path; -} - -std::string Profile::getFullPropertyPath(openspace::properties::Property* prop) { +std::string Profile::getFullPropertyPath(properties::Property* prop) { return recurseForFullName(prop->owner()) + prop->identifier(); } -void Profile::checkForChangedProps( - std::vector& changedList, - openspace::properties::PropertyOwner* po) -{ - if (po != nullptr) { - for (auto subOwner : po->propertySubOwners()) { - checkForChangedProps(changedList, subOwner); - } - for (auto p : po->properties()) { - if (p->hasChanged()) { - changedList.push_back(p); - } - } - } -} - -std::vector Profile::getChangedProperties() -{ +std::vector Profile::changedProperties() { ZoneScoped - std::vector nodes + std::vector nodes = global::renderEngine.scene()->allSceneGraphNodes(); - std::vector changedProps; + std::vector changedProps; - for (auto n : nodes) { + for (SceneGraphNode* n : nodes) { checkForChangedProps(changedProps, n); } return changedProps; } -std::string Profile::getCurrentTimeUTC() { +std::string Profile::currentTimeUTC() const { return global::timeManager.time().ISO8601(); } -void Profile::addCurrentTimeToProfileFile(ProfileFile& pf) { - std::string t = getCurrentTimeUTC(); - std::string update = "absolute\t" + t; - pf.updateTime(update); -} - -interaction::NavigationHandler::NavigationState Profile::getCurrentCameraState() { +interaction::NavigationHandler::NavigationState Profile::currentCameraState() const { return global::navigationHandler.navigationState(); } -void Profile::addCurrentCameraToProfileFile(ProfileFile& pf) { +void Profile::addCurrentCameraToProfileFile(ProfileFile& pf) const { std::string update = "setNavigationState\t"; interaction::NavigationHandler::NavigationState nav; - nav = getCurrentCameraState(); + nav = currentCameraState(); update += "\"" + nav.anchor + "\"\t"; update += "\"" + nav.aim + "\"\t"; update += "\"" + nav.referenceFrame + "\"\t"; @@ -313,8 +326,8 @@ void Profile::addCurrentCameraToProfileFile(ProfileFile& pf) { pf.updateCamera(update); } -void Profile::convertToSceneFile(const std::string inProfilePath, - const std::string outFilePath) +void Profile::convertToSceneFile(const std::string& inProfilePath, + const std::string& outFilePath) { ZoneScoped @@ -324,7 +337,7 @@ void Profile::convertToSceneFile(const std::string inProfilePath, try { outFile.open(outFilePath, std::ofstream::out); } - catch (std::ofstream::failure& e) { + catch (const std::ofstream::failure& e) { LERROR("Exception opening scene file for write: " + outFilePath + " (" + e.what() + ")"); } @@ -332,7 +345,7 @@ void Profile::convertToSceneFile(const std::string inProfilePath, try { outFile << convertToScene(pf); } - catch (std::ofstream::failure& e) { + catch (const std::ofstream::failure& e) { LERROR("Data write error to scene file: " + outFilePath + " (" + e.what() + ")"); } @@ -340,7 +353,7 @@ void Profile::convertToSceneFile(const std::string inProfilePath, try { outFile.close(); } - catch (std::ofstream::failure& e) { + catch (const std::ofstream::failure& e) { LERROR("Exception closing scene file after write: " + outFilePath + " (" + e.what() + ")"); } @@ -371,18 +384,23 @@ std::string Profile::convertToScene_modules(ProfileFile& pf) { for (std::string m : pf.modules()) { pf.splitByTab(m, fields); - if (fields[moduleFieldLoaded] != "" && fields[moduleFieldNotLoaded] != "") { - result += "if openspace.modules.isLoaded(\"" + fields[moduleFieldName]; - result += "\") then\n " + fields[moduleFieldLoaded] + "\nelse\n"; - result += " " + fields[moduleFieldNotLoaded] + "\nend\n"; + if (!fields[moduleFieldLoaded].empty() && !fields[moduleFieldNotLoaded].empty()) { + result += fmt::format( + "if openspace.modules.isLoaded(\"{}\") then {} else {} end\n", + fields[moduleFieldName], fields[moduleFieldLoaded], fields[moduleFieldNotLoaded] + ); } - else if (fields[moduleFieldNotLoaded] == "") { - result += "if not openspace.modules.isLoaded(\"" + fields[moduleFieldName]; - result += "\") then\n " + fields[moduleFieldNotLoaded] + "\nend\n"; + else if (fields[moduleFieldNotLoaded].empty()) { + result += fmt::format( + "if not openspace.modules.isLoaded(\"{}\") then {} end\n", + fields[moduleFieldName], fields[moduleFieldNotLoaded] + ); } - else if (fields[moduleFieldLoaded] == "") { - result += "if openspace.modules.isLoaded(\"" + fields[moduleFieldName]; - result += "\") then\n " + fields[moduleFieldLoaded] + "\nend\n"; + else if (fields[moduleFieldLoaded].empty()) { + result += fmt::format( + "if openspace.modules.isLoaded(\"{}\") then {} end\n", + fields[moduleFieldName], fields[moduleFieldLoaded] + ); } } return result; @@ -413,12 +431,13 @@ std::string Profile::convertToScene_assets(ProfileFile& pf) { assetR = "require"; } else { - std::string err = "Asset " + std::to_string(i + 1) + " of "; - err += std::to_string(pf.assets().size()) + " has bad arg 2/2 which must "; - err += "be either 'required' or 'requested'"; + std::string err = fmt::format( + "Asset {} of {} has bad arg 2/2 which must be 'required' or 'requested'", + i + 1, pf.assets().size() + ); throw ghoul::RuntimeError(err); } - result += "asset." + assetR + "(\"" + fields[assetFieldName] + "\")\n"; + result += fmt::format("asset.{}(\"{}\")\n", assetR, fields[assetFieldName]); } return result; } @@ -434,14 +453,18 @@ std::string Profile::convertToScene_properties(ProfileFile& pf) { if (fields[propertyFieldType] != "setPropertyValue" && fields[propertyFieldType] != "setPropertyValueSingle") { - std::string err = "Property" + std::to_string(i + 1) + " of "; - err += std::to_string(pf.properties().size()) + " has bad arg 1/1 which "; - err += "must be either 'setPropertyValue' or 'setPropertyValueSingle'"; + std::string err = fmt::format( + "Property {} of {} has bad arg 1/1 which must be " + "'setPropertyValue' or 'setPropertyValueSingle'", + i + 1, pf.properties().size() + ); throw ghoul::RuntimeError(err); } else { - result += " openspace." + fields[propertyFieldType] + "(\"" - + fields[propertyFieldName] + "\", " + fields[propertyFieldValue] + ")\n"; + result += fmt::format( + " openspace.{}(\"{}\", {})\n", + fields[propertyFieldType], fields[propertyFieldName], fields[propertyFieldValue] + ); } } return result; @@ -458,12 +481,12 @@ std::string Profile::convertToScene_keybindings(ProfileFile& pf) { pf.splitByTab(k, fields); result += " {\n"; - result += " Key = \"" + fields[0] + "\",\n"; - result += " Documentation = \"" + fields[1] + "\",\n"; - result += " Name = \"" + fields[2] + "\",\n"; - result += " GuiPath = \"" + fields[3] + "\",\n"; - result += " Local = " + fields[4] + ",\n"; - result += " Command = " + fields[5] + "\n"; + result += fmt::format(" {} = \"{}\",\n", "Key", fields[0]); + result += fmt::format(" {} = \"{}\",\n", "Documentation", fields[1]); + result += fmt::format(" {} = \"{}\",\n", "Name", fields[2]); + result += fmt::format(" {} = \"{}\",\n", "GuiPath", fields[3]); + result += fmt::format(" {} = \"{}\",\n", "Local", fields[4]); + result += fmt::format(" {} = \"{}\"\n", "Command", fields[5]); result += " },\n"; } result += "}\n"; @@ -473,10 +496,10 @@ std::string Profile::convertToScene_keybindings(ProfileFile& pf) { std::string Profile::convertToScene_markNodes(ProfileFile& pf) { std::string result; - if (pf.markNodes().size() > 0) { + if (!pf.markNodes().empty()) { result += " openspace.markInterestingNodes({"; - for (std::string m : pf.markNodes()) { - result += "\"" + m + "\", "; + for (const std::string& m : pf.markNodes()) { + result += fmt::format("\"{}\",", m); } result += "})\n"; } @@ -491,9 +514,14 @@ std::string Profile::convertToScene_time(ProfileFile& pf) { pf.splitByTab(pf.time(), fields); if (fields[timeFieldType] == "absolute") { - result += " openspace.time.setTime(\"" + fields[timeFieldSet] + "\")\n"; + result += fmt::format(" openspace.time.setTime(\"{}\")\n", fields[timeFieldSet]); } else if (fields[timeFieldType] == "relative") { + result += fmt::format( + " openspace.time.setTime(openspace.time.advancedTime(" + "openspace.time.currentWallTime(), \"{}\"))\n", + fields[timeFieldSet] + ); result += " local now = openspace.time.currentWallTime(); "; result += "openspace.time.setTime("; result += "openspace.time.advancedTime(now, \"" + fields[timeFieldSet] + "\"))\n"; @@ -514,41 +542,41 @@ std::string Profile::convertToScene_camera(ProfileFile& pf) { if (fields[cameraFieldType] == "setNavigationState") { result += " openspace.navigation.setNavigationState({"; - result += "Anchor = " + fields[cameraNavigationFieldAnchor] + ", "; - if (fields[cameraNavigationFieldAim] != "") { - result += "Aim = " + fields[cameraNavigationFieldAim] + ", "; + result += fmt::format("Anchor = {}, ", fields[cameraNavigationFieldAnchor]); + if (!fields[cameraNavigationFieldAim].empty()) { + result += fmt::format("Aim = {}, ", fields[cameraNavigationFieldAim]); } - if (fields[cameraNavigationFieldRef] != "") { - result += "ReferenceFrame = " + fields[cameraNavigationFieldRef] + ", "; + if (!fields[cameraNavigationFieldRef].empty()) { + result += fmt::format("ReferenceFrame = {}, ", fields[cameraNavigationFieldRef]); } - result += "Position = {" + fields[cameraNavigationFieldPosition] + "}, "; - if (fields[cameraNavigationFieldUp] != "") { - result += "Up = {" + fields[cameraNavigationFieldUp] + "}, "; + result += fmt::format("Position = {{ {} }}, ", fields[cameraNavigationFieldPosition]); + if (!fields[cameraNavigationFieldUp].empty()) { + result += fmt::format("Up = {{ {} }}, ", fields[cameraNavigationFieldUp]); } - if (fields[cameraNavigationFieldYaw] != "") { - result += "Yaw = " + fields[cameraNavigationFieldYaw] + ", "; + if (!fields[cameraNavigationFieldYaw].empty()) { + result += fmt::format("Yaw = {}, ", fields[cameraNavigationFieldYaw]); } - if (fields[cameraNavigationFieldPitch] != "") { - result += "Pitch = " + fields[cameraNavigationFieldPitch] + " "; + if (!fields[cameraNavigationFieldPitch].empty()) { + result += fmt::format("Pitch = {} ", fields[cameraNavigationFieldPitch]); } result += "})\n"; } else if (fields[cameraFieldType] == "goToGeo") { result += " openspace.globebrowsing.goToGeo({ "; - if (fields[cameraGeoFieldAnchor] != "") { + if (!fields[cameraGeoFieldAnchor].empty()) { result += fields[cameraGeoFieldAnchor] + ", "; } result += fields[cameraGeoFieldLatitude] + ", "; result += fields[cameraGeoFieldLongitude] + ", "; - if (fields[cameraGeoFieldAltitude] != "") { + if (!fields[cameraGeoFieldAltitude].empty()) { result += fields[cameraGeoFieldAltitude] + ", "; } result += ")\n"; } else { - std::string err = "Camera entry's arg 1/1 must be either "; - err += "'setNavigationState' or 'goToGeo'"; - throw ghoul::RuntimeError(err); + throw ghoul::RuntimeError( + "Camera entry's arg 1/1 must be either 'setNavigationState' or 'goToGeo'" + ); } return result; } diff --git a/src/scene/profilefile.cpp b/src/scene/profilefile.cpp index 90029ac412..e68d6c7ec0 100644 --- a/src/scene/profilefile.cpp +++ b/src/scene/profilefile.cpp @@ -38,7 +38,6 @@ namespace { constexpr const char* _loggerCat = "ProfileFile"; constexpr const char* KeyIdentifier = "Identifier"; constexpr const char* KeyParent = "Parent"; - } // namespace namespace openspace { @@ -115,7 +114,7 @@ void ProfileFile::processIndividualLine(bool& insideSection, std::string line) { } } -void ProfileFile::writeToFile(const std::string filename) { +void ProfileFile::writeToFile(const std::string& filename) { if (filename.find("/") != std::string::npos) { LERROR("Profile filename must not contain path (/) elements"); return; @@ -195,11 +194,11 @@ const std::string ProfileFile::getVersion() const { } void ProfileFile::setVersion(std::string v) { - _version = v; + _version = std::move(v); } void ProfileFile::addAllElements(std::string& str, std::vector& list) { - for (auto s : list) { + for (const std::string& s : list) { str += s + '\n'; } } @@ -487,26 +486,32 @@ size_t ProfileFile::splitByTab(std::string line, std::vector& resul return result.size(); } -void ProfileFile::updateTime(const std::string line) { - _time = line; +void ProfileFile::updateTime(std::string line) { + _time = std::move(line); } -void ProfileFile::updateCamera(const std::string line) { - _camera = line; + +void ProfileFile::updateCamera(std::string line) { + _camera = std::move(line); } -void ProfileFile::addModuleLine(const std::string line) { - _modules.push_back(line); + +void ProfileFile::addModuleLine(std::string line) { + _modules.push_back(std::move(line)); } -void ProfileFile::addAssetLine(const std::string line) { - _assets.push_back(line); + +void ProfileFile::addAssetLine(std::string line) { + _assets.push_back(std::move(line)); } -void ProfileFile::addPropertyLine(const std::string line) { - _properties.push_back(line); + +void ProfileFile::addPropertyLine(std::string line) { + _properties.push_back(std::move(line)); } -void ProfileFile::addKeybindingLine(const std::string line) { - _keybindings.push_back(line); + +void ProfileFile::addKeybindingLine(std::string line) { + _keybindings.push_back(std::move(line)); } -void ProfileFile::addMarkNodesLine(const std::string line) { - _markNodes.push_back(line); + +void ProfileFile::addMarkNodesLine(std::string line) { + _markNodes.push_back(std::move(line)); } void ProfileFile::clearAssets() { diff --git a/tests/profile/test_common.h b/tests/profile/test_common.h index fabd2cedeb..44313bfdef 100644 --- a/tests/profile/test_common.h +++ b/tests/profile/test_common.h @@ -422,7 +422,7 @@ end)"; const std::string detectChangedPropsResult_1 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -456,7 +456,7 @@ Sun\n\ const std::string detectChangedAssetsResult_1 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -489,7 +489,7 @@ Sun\n\ const std::string detectChangedAssetsResult_2 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -521,7 +521,7 @@ Sun\n\ const std::string detectChangedAssetsResult_3 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -553,7 +553,7 @@ Sun\n\ const std::string detectChangedAssetsResult_4 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -585,7 +585,7 @@ Sun\n\ const std::string detectChangedAssetsResult_5 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ @@ -618,7 +618,7 @@ Sun\n\ const std::string detectChangedAssetsResult_6 = "\ #Version\n\ -" + profileFormatVersion + "\n\ +" + std::string(Profile::FormatVersion) + "\n\ \n\ #Module\n\ \n\ diff --git a/tests/profile/test_profile.cpp b/tests/profile/test_profile.cpp index 7ca31841f1..99477c1a02 100644 --- a/tests/profile/test_profile.cpp +++ b/tests/profile/test_profile.cpp @@ -56,10 +56,10 @@ namespace { class Profile2 : public Profile { public: Profile2(AssetLoader& refAssetLoader) : _assLoader(refAssetLoader) {} - std::string getCurrentTimeUTC() override { + std::string currentTimeUTC() const override { return "2020-02-29T01:23:45.00"; } - interaction::NavigationHandler::NavigationState getCurrentCameraState() override { + interaction::NavigationHandler::NavigationState currentCameraState() const override { interaction::NavigationHandler::NavigationState n; n.anchor = "Earth"; n.aim = "Sun"; @@ -73,24 +73,24 @@ public: void addPropertiesMarkedAsChanged(std::string formattedLine) { _scenegraphProps.push_back(formattedLine); } - std::vector getChangedPropertiesFormatted() override { + std::vector changedPropertiesFormatted() override { std::vector formattedLines; for (std::string s : _scenegraphProps) { formattedLines.push_back(s); } return formattedLines; } - bool usingProfile() override { + bool usingProfile() const override { return true; } - std::string initialProfile() override { + std::string initialProfile() const override { return _initProfile; } void setInitialProfile(std::string file) { _initProfile = file; } - std::vector listOfAllAssetEvents() override { - return _assLoader.listOfAllAssetEvents(); + std::vector assetEvents() const override { + return _assLoader.assetEvents(); } void setProfileBaseDirectory(std::string dir) { _profileBaseDirectory = dir; @@ -235,7 +235,7 @@ TEST_CASE("profile: Detect new added assets after reset", "[profile]") { asset->initialize(); std::shared_ptr asset2 = assetLoader.add("test2"); asset2->initialize(); - assetLoader.listOfAllAssetEvents_reset(); + assetLoader.resetAssetEvents(); std::shared_ptr asset3 = assetLoader.add("test3"); asset3->initialize(); std::shared_ptr asset4 = assetLoader.add("test4");