<commit_before>#ifndef __CANDEVICE_P_H
#define __CANDEVICE_P_H

#include "candevice.h"
#include "candeviceqt.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QtCore/QVector>
#include <propertyfields.h>

class CanDevicePrivate : public QObject {
    Q_OBJECT
    Q_DECLARE_PUBLIC(CanDevice)

public:
    CanDevicePrivate(CanDevice* q, CanDeviceCtx&& ctx = CanDeviceCtx(new CanDeviceQt()))
        : _ctx(std::move(ctx))
        , _canDevice(_ctx.get<CanDeviceInterface>())
        , q_ptr(q)
    {
        initProps();
        _canDevice.setParent(q);
    }

    void saveSettings(QJsonObject& json)
    {
        QJsonArray viewModelsArray;

        for (const auto& p : _props) {
            json[p.first] = QJsonValue::fromVariant(p.second);
        }
    }

    bool restoreConfiguration(const QJsonObject& json)
    {
        for (const auto& p : _supportedProps) {
            QString propName = ComponentInterface::propertyName(p);
            if (json.contains(propName))
                _props[propName] = json[propName].toVariant();
        }
        return true;
    }

    using devConfigPair = std::pair<int, QVariant>;
    std::vector<devConfigPair> getDevConfig()
    {
        QString c = _props.at(_configProperty).toString().simplified().replace(" ", "");
        std::vector<devConfigPair> ret;

        if (c.length() == 0) {
            // Skip parsing for empty parameter
            return {};
        }

        auto&& propList = c.split(";");

        for (auto& item : propList) {
            devConfigPair pair;
            bool res = getConfigPair(item, pair);

            if (res) {
                ret.push_back(pair);
            }
        }

        return ret;
    }

signals:
    void backendChanged(const QString& backend);

public:
    CanDeviceCtx _ctx;
    QVector<QCanBusFrame> _sendQueue;
    CanDeviceInterface& _canDevice;
    bool _initialized{ false };
    bool _simStarted{ false };

    const QString _nameProperty = "name";
    const QString _backendProperty = "backend";
    const QString _interfaceProperty = "interface";
    const QString _configProperty = "configuration";

    // workaround for clang 3.5
    using cf = ComponentInterface::CustomEditFieldCbk;

    // clang-format off
    ComponentInterface::ComponentProperties _supportedProps = {
            std::make_tuple(_nameProperty,  QVariant::String, true, cf(nullptr)),

#if QT_VERSION >= 0x050900

            std::make_tuple(_backendProperty,  QVariant::String, true, cf([this] {
                    auto *p = new PropertyFieldCombo();
                    p->addItems(QCanBus::instance()->plugins());
                    connect(p, &PropertyFieldCombo::currentTextChanged, this, &CanDevicePrivate::backendChanged);

                    return p;
                })),

            std::make_tuple(_interfaceProperty, QVariant::String, true, cf([&] {
                    auto *p = new PropertyFieldCombo();

                    // Connection needs to be destroyed manually. We expect to have only one such conn at a time
                    disconnect(_prevConn);
                    _prevConn = connect(this, &CanDevicePrivate::backendChanged, [p](const QString& backend){
                            QString errorString;
                            const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices(
                                backend, &errorString);

                            QStringList list;<commit_msg>Fix current backend display issue in CanDevice property<commit_after>#ifndef __CANDEVICE_P_H
#define __CANDEVICE_P_H

#include "candevice.h"
#include "candeviceqt.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QtCore/QVector>
#include <propertyfields.h>

class CanDevicePrivate : public QObject {
    Q_OBJECT
    Q_DECLARE_PUBLIC(CanDevice)

public:
    CanDevicePrivate(CanDevice* q, CanDeviceCtx&& ctx = CanDeviceCtx(new CanDeviceQt()))
        : _ctx(std::move(ctx))
        , _canDevice(_ctx.get<CanDeviceInterface>())
        , q_ptr(q)
    {
        initProps();
        _canDevice.setParent(q);
    }

    void saveSettings(QJsonObject& json)
    {
        QJsonArray viewModelsArray;

        for (const auto& p : _props) {
            json[p.first] = QJsonValue::fromVariant(p.second);
        }
    }

    bool restoreConfiguration(const QJsonObject& json)
    {
        for (const auto& p : _supportedProps) {
            QString propName = ComponentInterface::propertyName(p);
            if (json.contains(propName))
                _props[propName] = json[propName].toVariant();
        }
        return true;
    }

    using devConfigPair = std::pair<int, QVariant>;
    std::vector<devConfigPair> getDevConfig()
    {
        QString c = _props.at(_configProperty).toString().simplified().replace(" ", "");
        std::vector<devConfigPair> ret;

        if (c.length() == 0) {
            // Skip parsing for empty parameter
            return {};
        }

        auto&& propList = c.split(";");

        for (auto& item : propList) {
            devConfigPair pair;
            bool res = getConfigPair(item, pair);

            if (res) {
                ret.push_back(pair);
            }
        }

        return ret;
    }

signals:
    void backendChanged(const QString& backend);

public:
    CanDeviceCtx _ctx;
    QVector<QCanBusFrame> _sendQueue;
    CanDeviceInterface& _canDevice;
    bool _initialized{ false };
    bool _simStarted{ false };

    const QString _nameProperty = "name";
    const QString _backendProperty = "backend";
    const QString _interfaceProperty = "interface";
    const QString _configProperty = "configuration";

    // workaround for clang 3.5
    using cf = ComponentInterface::CustomEditFieldCbk;

    // clang-format off
    ComponentInterface::ComponentProperties _supportedProps = {
            std::make_tuple(_nameProperty,  QVariant::String, true, cf(nullptr)),

#if QT_VERSION >= 0x050900

            std::make_tuple(_backendProperty,  QVariant::String, true, cf([this] {
                    auto *p = new PropertyFieldCombo();
                    p->addItems(QCanBus::instance()->plugins());
                    connect(p, &PropertyFieldCombo::currentTextChanged, this, &CanDevicePrivate::backendChanged);

                    p->setPropText(_props[_backendProperty].toString());

                    return p;
                })),

            std::make_tuple(_interfaceProperty, QVariant::String, true, cf([&] {
                    auto *p = new PropertyFieldCombo();

                    // Connection needs to be destroyed manually. We expect to have only one such conn at a time
                    disconnect(_prevConn);
                    _prevConn = connect(this, &CanDevicePrivate::backendChanged, [p](const QString& backend){
                            QString errorString;
                            const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices(
                                backend, &errorString);

                            QStringList list;
{'fraction_non_alphanumeric': 0.10694288913773796, 'fraction_numerical': 0.002799552071668533, 'mean_word_length': 2.3048103607770583, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938621', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1948, 'n_words': 437}