aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/insight/insightmodel.h
blob: b21d7e36205e232229311f51b33618e7588d7c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include <model.h>

#include <QAbstractListModel>
#include <QFileInfo>
#include <QPointer>
#include <QPoint>

#include <utils/filesystemwatcher.h>

#include <3rdparty/json/json.hpp>

namespace QmlDesigner {

class InsightView;

typedef nlohmann::json json;

class InsightModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(bool enabled READ enabled NOTIFY enabledChanged)
    Q_PROPERTY(QString token READ token NOTIFY tokenChanged)
    Q_PROPERTY(int minutes READ minutes NOTIFY minutesChanged)
    Q_PROPERTY(Qt::CheckState predefinedSelectState MEMBER m_predefinedCheckState NOTIFY
                   predefinedSelectStateChanged)
    Q_PROPERTY(Qt::CheckState customSelectState MEMBER m_customCheckState NOTIFY customSelectStateChanged)

    enum {
        CategoryName = Qt::DisplayRole,
        CategoryColor = Qt::UserRole,
        CategoryType,
        CategoryActive
    };

public:
    InsightModel(InsightView *view, class ExternalDependenciesInterface &externalDependencies);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    void setup();

    Q_INVOKABLE void addCategory();
    Q_INVOKABLE void removeCateogry(int idx);
    Q_INVOKABLE bool renameCategory(int idx, const QString &name);

    Q_INVOKABLE void setCategoryActive(int idx, bool value);

    bool enabled() const;
    Q_INVOKABLE void setEnabled(bool value);

    QString token() const;
    Q_INVOKABLE void setToken(const QString &value);

    int minutes() const;
    Q_INVOKABLE void setMinutes(int value);

    Q_INVOKABLE void selectAllPredefined();
    Q_INVOKABLE void selectAllCustom();

    void handleFileChange(const QString &path);

    void setAuxiliaryEnabled(bool value);
    void setAuxiliaryCategories(const std::vector<std::string> &categories);

    Q_INVOKABLE void hideCursor();
    Q_INVOKABLE void restoreCursor();
    Q_INVOKABLE void holdCursorInPlace();

    Q_INVOKABLE int devicePixelRatio();

signals:
    void enabledChanged();
    void tokenChanged();
    void minutesChanged();

    void predefinedSelectStateChanged();
    void customSelectStateChanged();

private:
    void parseMainQml();
    void parseDefaultConfig();
    void parseConfig();
    void parseQtdsConfig();

    void createQtdsConfig();
    void updateQtdsConfig();

    void selectAll(const std::vector<std::string> &categories, Qt::CheckState checkState);

    std::vector<std::string> predefinedCategories() const;
    std::vector<std::string> activeCategories() const;
    std::vector<std::string> customCategories() const;
    std::vector<std::string> categories() const;

    bool hasCategory(const QString &name) const;
    void updateCheckState();

    template<typename T>
    void writeConfigValue(const json::json_pointer &ptr, T value);

private:
    QPointer<InsightView> m_insightView;
    ExternalDependenciesInterface &m_externalDependencies;

    Utils::FileSystemWatcher *m_fileSystemWatcher;

    bool m_enabled = false;
    bool m_initialized = false;

    QFileInfo m_mainQmlInfo;
    QFileInfo m_configInfo;
    QFileInfo m_qtdsConfigInfo;

    json m_defaultConfig;
    json m_config;
    json m_qtdsConfig;

    Qt::CheckState m_predefinedCheckState;
    Qt::CheckState m_customCheckState;

    QPoint m_lastPos;
};

} // namespace QmlDesigner