/*! * * Copyright (c) 2021-2022 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file VSettings.cpp * \author (last) Behrouz NematiPour * \date (last) 13-Oct-2022 * \author (original) Behrouz NematiPour * \date (original) 29-Mar-2021 * */ #include "VSettings.h" // Qt #include // Project #include "GuiController.h" #include "MSettings.h" #include "Settings.h" VIEW_DEF_CLASS(VSettings) void VSettings::initConnections() { ACTION_RECEIVE_BRIDGE_CONNECTION(Gui::_GuiController, SettingsData); PROPERTY_POST_CONNECTION(VSettings, servicePass ); PROPERTY_POST_CONNECTION(VSettings, alarmVolume ); PROPERTY_POST_CONNECTION(VSettings, noCANBus ); connect(&_GuiController, SIGNAL(didActionReceive (GuiActionType, const QVariantList &)), this , SLOT( onActionReceive (GuiActionType, const QVariantList &))); } void VSettings::servicePass_post(const QString &vservicePass) { Storage::Settings settings; settings.save(servicePassCategory(), servicePassGroup(), servicePassKey(), vservicePass); } void VSettings::alarmVolume_post(const quint8 &valarmVolume) { Storage::Settings settings; settings.save(alarmVolumeCategory(), alarmVolumeGroup(), alarmVolumeKey(), QString::number(valarmVolume)); } void VSettings::noCANBus_post(const bool &vnoCANBus) { Storage::Settings settings; settings.save(noCANBusCategory(), noCANBusGroup(), noCANBusKey(), QString::number(vnoCANBus)); } void VSettings::onActionReceive(const SettingsData &) { // TODO: this function needs to be moved to the controller, to execute in settings thread not the main thread. // it should then send the output here to update the specific properties defined. QStringList mCategorys = _Settings.categorys(); for (const auto &category : mCategorys) { QStringList groups = _Settings.groups(category); for (const auto &group : groups) { QStringList keys = _Settings.keys (category, group); QVariantList values = _Settings.values (category, group); if ( Storage::Settings::isCategoryInstructions( category ) ) { updateInstructions(group, keys, values); } else { //TODO: Since it is global system settings, move this to the settings controller so the C++ backend can also use it. like Date/Time formats. for (const auto &key : keys) { // DEBUG: qDebug() << " ~~~~~~~~~~ " << key << _Settings.value(group, key).toString (); QVariantMap keyValue; if ( isservicePass (category, group, key) ) { QString mServicePass; mServicePass = _Settings.value(category, group, key).toString (); keyValue[key] = mServicePass ; servicePass ( mServicePass); } else if ( isalarmVolume (category, group, key) ) { quint8 mAlarmVolume; mAlarmVolume = _Settings.value(category, group, key).toInt (); // returns 0 if fails, so no error checking needed. keyValue[key] = mAlarmVolume ; alarmVolume ( mAlarmVolume); } else if ( isnoCANBus (category, group, key) ) { bool mNoCANBus; mNoCANBus = _Settings.value(category, group, key).toBool (); // returns 0/false if fails, so no error checking needed. keyValue[key] = mNoCANBus ; noCANBus ( mNoCANBus); if (_noCANBus ) LOG_APPED_UI(QString("System is working on NoCANBus set")); } else { keyValue[key] = _Settings.value(category, group, key); } QVariantMap groups; groups [group] = keyValue; } } } } categorys (mCategorys); emit instructionsChanged(_instructions); // If the configuration exits, then it has been set, and this call internally will be neutral, // otherwise will use the default value and will notify the update. servicePass ( _servicePass ); alarmVolume ( _alarmVolume ); // noCANBus ( _noCANBus ); // This line has been put here to remind developers that it is intentionally removed, to not to add a default value. adjustment(true); } void VSettings::updateReplacements(const QString &vGroup, QStringList &vKeys) { for ( quint16 keyIndex = 0; keyIndex < vKeys.count(); keyIndex++ ) { QRegExp regx("\\{\\w+:\\d+\\}"); QString key = vKeys[keyIndex]; int replacementCount = key.count(regx); int oLen = 0; for ( int j = 0; j < replacementCount; j++ ) { int cLen = 0; int pos = key.indexOf(regx); cLen = regx.matchedLength(); QString blk = key.mid(pos+1, cLen-2); key.remove(pos, regx.matchedLength()); QStringList lst = blk.split(":"); quint16 id = lst[0].toUInt(); quint8 ix = lst[1].toUInt(); TLocation loc; loc.group = vGroup ; loc.keyIndex = keyIndex ; loc.locIndex = pos - oLen ; loc.prmIndex = ix ; _replacements[id] += loc; qDebug() << "#" << replacementCount << id << ix << loc.group << loc.keyIndex << loc.locIndex << loc.prmIndex << oLen << cLen ; oLen = cLen; } vKeys[keyIndex] = key; } } void VSettings::updateInstructions(const QString &vGroup, QStringList &vKeys, const QVariantList &vValues) { // DEBUG : // qDebug() << " ##### " // << group // << keys // << values // << location ; QVariantMap details; QString location = QString(Storage::Settings_Category_InstructionsImagesLoc).arg(Storage::Settings_Path_Name); updateReplacements(vGroup, vKeys); details["location"] = location ; details["keys" ] = vKeys ; details["values" ] = vValues ; _instructions[vGroup] = details; } /*! * \brief VSettings::onActionReceive * \details emits didActionReceive signal to notify other classes (Gui) * , an action has been received. * \param vAction - the action * \param vData - the action data */ void VSettings::onActionReceive (GuiActionType vAction, const QVariantList &) { bool isChanged = false; QVariantMap mInstructions = _instructions; // the original should be kept unchanged since all the posIndexes will change if values added. quint16 id = qFromBigEndian((quint16)vAction); if (_replacements.contains(id)) { isChanged = true; qDebug() << _replacements[id].count(); for ( auto item : _replacements[id]) { qDebug() << id << item.group << item.keyIndex << item.locIndex << item.prmIndex; QVariantMap details = mInstructions[item.group].toMap(); // QStringList keys = details["keys" ].toStringList(); // qDebug() << keys.at(item.keyIndex).insert(); } qDebug() << "\n"; } if ( isChanged ) emit instructionsChanged(mInstructions); }