diff --git a/src/core/machineinfo.cpp b/src/core/machineinfo.cpp index 39a32d1..bfa0d31 100644 --- a/src/core/machineinfo.cpp +++ b/src/core/machineinfo.cpp @@ -1,176 +1,195 @@ /* AtCore Copyright (C) <2019> Authors: Chris Rizzitello This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "machineinfo.h" #include #include Q_LOGGING_CATEGORY(MACHINE_INFO, "org.kde.atelier.core.machineInfo") const QMap MachineInfo::decoderMap = { {KEY::NAME, {QStringLiteral("Name"), QStringLiteral("ProfileName")}}, {KEY::BAUDRATE, {QStringLiteral("bps"), 115200}}, {KEY::FIRMWARE, {QStringLiteral("firmware"), QStringLiteral("Auto-Detect")}}, {KEY::MAXBEDTEMP, {QStringLiteral("maximumTemperatureBed"), 0}}, {KEY::MAXEXTTEMP, {QStringLiteral("maximumTemperatureExtruder"), 0}}, {KEY::POSTPAUSE, {QStringLiteral("postPause"), QString()}}, {KEY::ISCARTESIAN, {QStringLiteral("isCartesian"), false}}, {KEY::XMAX, {QStringLiteral("dimensionX"), 200}}, {KEY::YMAX, {QStringLiteral("dimensionY"), 200}}, {KEY::ZMAX, {QStringLiteral("dimensionZ"), 180}}, {KEY::AUTOTEMPREPORT, {QStringLiteral("autoReportTemp"), false}} }; MachineInfo *MachineInfo::instance() { static MachineInfo m; return &m; } MachineInfo::MachineInfo(QObject *parent) : QObject(parent) , m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral("atcore"), QStringLiteral("profiles"), this)) { } QObject *MachineInfo::qmlSingletonRegister(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(scriptEngine) engine->setObjectOwnership(instance(), QQmlEngine::CppOwnership); return instance(); } QVariantMap MachineInfo::readProfile(const QString &profileName) const { m_settings->sync(); m_settings->beginGroup(profileName); QVariantMap data{{decoderMap[KEY::NAME].name, m_settings->group()}}; for (int i = 1 ; i <= 10; i++) { data.insert(decoderMap[MachineInfo::KEY(i)].name, m_settings->value(decoderMap[MachineInfo::KEY(i)].name, decoderMap[MachineInfo::KEY(i)].defaultValue)); } m_settings->endGroup(); return data; } QVariant MachineInfo::readKey(const QString &profileName, MachineInfo::KEY key) const { if (profileName.isEmpty()) { return decoderMap[key].defaultValue; } if (key == KEY::NAME) { return profileName; } m_settings->sync(); return m_settings->value(QStringLiteral("%1/%2").arg(profileName, decoderMap[key].name), decoderMap[key].defaultValue); } bool MachineInfo::storeKey(const QString &profileName, const MachineInfo::KEY key, const QVariant &value) const { if (key == KEY::NAME) { //copyProfile emits profilesChanged return copyProfile(profileName, value.toString(), true); } if (!profileNames().contains(profileName)) { qCWarning(MACHINE_INFO) << "Keysave: Profile does not exsits, create profile before attempting to store keys."; return false; } if (readKey(profileName, key) == value) { return false; } m_settings->beginGroup(profileName); m_settings->setValue(decoderMap[key].name, value); m_settings->endGroup(); m_settings->sync(); emit profilesChanged(); return true; } bool MachineInfo::copyProfile(const QString &srcProfile, const QString &destProfile, bool rmSrc) const { if (srcProfile.isEmpty() || destProfile.isEmpty()) { qCWarning(MACHINE_INFO) << "Profile Copy Error: Source or Destination Profile Is Empty."; return false; } if (!profileNames().contains(srcProfile)) { qCWarning(MACHINE_INFO) << "Profile Copy Error: Source Not Found."; return false; } if (srcProfile == destProfile) { qCWarning(MACHINE_INFO) << "Profile Copy Error: Source is Destination."; return false; } QVariantMap newProfile = readProfile(srcProfile); m_settings->beginGroup(destProfile); for (int i = 1 ; i <= 10; i++) { m_settings->setValue(decoderMap[MachineInfo::KEY(i)].name, newProfile[decoderMap[MachineInfo::KEY(i)].name]); } m_settings->endGroup(); m_settings->sync(); if (rmSrc) { removeProfile(srcProfile); //removeProfile emits profilesChanged } else { emit profilesChanged(); } return true; } bool MachineInfo::removeProfile(const QString &profileName) const { if (!profileNames().contains(profileName)) { qCWarning(MACHINE_INFO) << "Profile Remove Error: Profile not found."; return false; } m_settings->beginGroup(profileName); m_settings->remove(QString()); m_settings->endGroup(); m_settings->sync(); emit profilesChanged(); return true; } void MachineInfo::storeProfile(const QMap &profile) const { m_settings->beginGroup(profile[KEY::NAME].toString()); for (int i = 1 ; i <= 10; i++) { m_settings->setValue(decoderMap[MachineInfo::KEY(i)].name, profile[MachineInfo::KEY(i)]); } m_settings->endGroup(); m_settings->sync(); emit profilesChanged(); } +void MachineInfo::storeProfile(const QVariantMap &profile) const +{ + m_settings->beginGroup(profile.first().toString()); + for(auto it = profile.begin(), end = profile.end(); it != end; ++it){ + m_settings->setValue(it.key(), it.value()); + } + m_settings->endGroup(); + m_settings->sync(); + emit profilesChanged(); +} + QStringList MachineInfo::profileNames() const { m_settings->sync(); return m_settings->childGroups(); } +QString MachineInfo::keyName(const MachineInfo::KEY key) const +{ + auto it = decoderMap.find(key); + if(it != decoderMap.end()){ + return it.value().name; + } + return QString(); +} diff --git a/src/core/machineinfo.h b/src/core/machineinfo.h index 5a3bc0f..e47f764 100644 --- a/src/core/machineinfo.h +++ b/src/core/machineinfo.h @@ -1,148 +1,164 @@ /* AtCore Copyright (C) <2019> Authors: Chris Rizzitello This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #pragma once #include "atcore_export.h" #include #include #include +#include class ATCORE_EXPORT MachineInfo : public QObject { Q_OBJECT public: /** * @brief KEYS enum Possible keys for the printer settings */ enum class KEY { NAME = 0, //!< Profile Name BAUDRATE, //!< Machine BAUD Rate FIRMWARE, //! &profile) const; + void storeProfile(const QMap &profile) const; + + /** + * @brief Store a new profile, Must be used for new profiles or to override a profile. + * @param profile: A complete machine profile or one that at very least has a profile Name and any other valid Key. + * @sa storeKey() + */ + Q_INVOKABLE void storeProfile(const QVariantMap &profile) const; /** * @brief Store a key to an existing profile, Sending a key of Key::NAME will rename the profile * @param profileName: profile to write into * @param key: The key you will write to * @param value: The value you will store. * @return true if successful * @sa storeProfile() */ Q_INVOKABLE bool storeKey(const QString &profileName, const MachineInfo::KEY key, const QVariant &value) const; /** * @brief copyies a profile and optionally deletes the src profile * @param srcProfile: profiles Current Name * @param destProfile: profiles New Name * @param rmSrc: delete srcProfile (defalut false) * @return true if successful */ Q_INVOKABLE bool copyProfile(const QString &srcProfile, const QString &destProfile, bool rmSrc = false) const; /** * @brief Remove a full profile * @param profileName: name of the profile you want to remove. * @return true if successful. */ Q_INVOKABLE bool removeProfile(const QString &profileName) const; /** * @brief Get a list of all the profile names * @return QStringList containing the stored profile names. */ Q_INVOKABLE QStringList profileNames() const; + /** + * @brief Translate a key enum to string + * @param key: name of the key to be translated. + * @return key string if successful. + */ + Q_INVOKABLE QString keyName(const MachineInfo::KEY key) const; + + signals: /** * @brief A profile has changed */ void profilesChanged() const; private: MachineInfo *operator = (MachineInfo &other) = delete; MachineInfo(const MachineInfo &other) = delete; explicit MachineInfo(QObject *parent = nullptr); ~MachineInfo() = default; /** * @brief used to hold MachineInfo::KEY name and defaultValues. */ struct keyInfo { QString name; //!< Key name used in the settings file QVariant defaultValue; //!< Defaut Value for the key }; /** * @brief Map of MachineInfo::KEY , KeyString and DefaultValue */ static const QMap decoderMap; /** * @brief m_settings our settings object. */ QSettings *m_settings = nullptr; };