aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/studiowelcome/presetmodel.h
blob: 19150e0fe96febdd3a49796fb47d5ac793919109 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include <QAbstractListModel>
#include <QDebug>
#include <QUrl>

#include <utils/filepath.h>
#include <utils/optional.h>
#include <utils/qtcassert.h>

#include "userpresets.h"

namespace Utils {
class Wizard;
}

namespace StudioWelcome {

struct UserPresetItem;

struct PresetItem
{
    PresetItem() = default;
    PresetItem(const QString &wizardName, const QString &category, const QString &sizeName = "")
        : wizardName{wizardName}
        , categoryId{category}
        , screenSizeName{sizeName}
    {}

    virtual ~PresetItem() {}
    virtual QString displayName() const { return wizardName; }
    virtual QString screenSize() const { return screenSizeName; }
    virtual std::unique_ptr<PresetItem> clone() const
    {
        return std::unique_ptr<PresetItem>{new PresetItem{*this}};
    }

    virtual bool isUserPreset() const { return false; }
    virtual UserPresetItem *asUserPreset() { return nullptr; }
    std::function<Utils::Wizard *(const Utils::FilePath &path)> create;

public:
    QString wizardName;
    QString categoryId;
    QString screenSizeName;
    QString description;
    QUrl qmlPath;
    QString fontIconCode;
};

struct UserPresetItem : public PresetItem
{
    UserPresetItem() = default;
    UserPresetItem(const QString &userName,
                   const QString &wizardName,
                   const QString &category,
                   const QString &sizeName = "")
        : PresetItem{wizardName, category, sizeName}
        , userName{userName}
    {}

    QString displayName() const override { return userName; }
    std::unique_ptr<PresetItem> clone() const override
    {
        return std::unique_ptr<PresetItem>{new UserPresetItem{*this}};
    }

    bool isUserPreset() const override { return true; }
    UserPresetItem *asUserPreset() override { return this; }

    bool isValid() const
    {
        return !categoryId.isEmpty() && !wizardName.isEmpty() && !userName.isEmpty();
    }

public:
    QString userName;
    bool useQtVirtualKeyboard;
    QString qtVersion;
    QString styleName;
};

inline QDebug &operator<<(QDebug &d, const UserPresetItem &item);

inline QDebug &operator<<(QDebug &d, const PresetItem &item)
{
    d << "wizardName=" << item.wizardName;
    d << "; category = " << item.categoryId;
    d << "; size = " << item.screenSizeName;

    if (item.isUserPreset())
        d << "; " << (UserPresetItem &) item;

    return d;
}

inline QDebug &operator<<(QDebug &d, const UserPresetItem &item)
{
    d << "userName=" << item.userName;

    return d;
}

inline bool operator==(const PresetItem &lhs, const PresetItem &rhs)
{
    return lhs.categoryId == rhs.categoryId && lhs.wizardName == rhs.wizardName;
}

using PresetItems = std::vector<std::shared_ptr<PresetItem>>;

struct WizardCategory
{
    QString id;
    QString name;
    PresetItems items;
};

inline QDebug &operator<<(QDebug &d, const std::shared_ptr<PresetItem> &preset)
{
    if (preset)
        d << *preset;
    else
        d << "(null)";

    return d;
}

inline QDebug &operator<<(QDebug &d, const WizardCategory &cat)
{
    d << "id=" << cat.id;
    d << "; name=" << cat.name;
    d << "; items=" << cat.items;

    return d;
}

using PresetsByCategory = std::map<QString, WizardCategory>;
using Categories = std::vector<QString>;

/****************** PresetData  ******************/

class PresetData
{
public:
    void reload(const std::vector<UserPresetData> &userPresets,
                const std::vector<UserPresetData> &loadedRecents);
    void setData(const PresetsByCategory &presets,
                 const std::vector<UserPresetData> &userPresets,
                 const std::vector<UserPresetData> &recents);

    const std::vector<PresetItems> &presets() const { return m_presets; }
    const Categories &categories() const { return m_categories; }

    static QString recentsTabName();

private:
    PresetItems makeUserPresets(const PresetItems &wizardPresets, const std::vector<UserPresetData> &data);
    std::shared_ptr<PresetItem> findPresetItemForUserPreset(const UserPresetData &preset, const PresetItems &wizardPresets);

private:
    std::vector<PresetItems> m_presets;
    Categories m_categories;
    std::vector<UserPresetData> m_recents;
    std::vector<UserPresetData> m_userPresets;
    PresetsByCategory m_presetsByCategory;
};

/****************** PresetCategoryModel ******************/

class BasePresetModel : public QAbstractListModel
{
public:
    BasePresetModel(const PresetData *data, QObject *parent = nullptr);
    QHash<int, QByteArray> roleNames() const override;

    void reset()
    {
        beginResetModel();
        endResetModel();
    }

protected:
    const PresetData *m_data = nullptr;
};

/****************** PresetCategoryModel ******************/

class PresetCategoryModel : public BasePresetModel
{
public:
    PresetCategoryModel(const PresetData *data, QObject *parent = nullptr);
    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
};

/****************** PresetModel ******************/

class PresetModel : public BasePresetModel
{
    Q_OBJECT

public:
    PresetModel(const PresetData *data, QObject *parent = nullptr);
    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;

    Q_INVOKABLE void setPage(int index); // called from QML when view's header item is clicked
    Q_INVOKABLE QString fontIconCode(int index) const;

    int page() const { return static_cast<int>(m_page); }

    const std::shared_ptr<PresetItem> preset(size_t selection) const
    {
        auto presets = m_data->presets();
        if (presets.empty())
            return {};

        if (m_page < presets.size()) {
            const PresetItems presetsOfCategory = presets.at(m_page);
            if (selection < presetsOfCategory.size())
                return presets.at(m_page).at(selection);
        }
        return {};
    }

    bool empty() const { return m_data->presets().empty(); }

private:
    const PresetItems presetsOfCurrentCategory() const
    {
        QTC_ASSERT(m_page < m_data->presets().size(), return {});

        return m_data->presets().at(m_page);
    }

    std::vector<PresetItems> presets() const { return m_data->presets(); }

private:
    size_t m_page = 0;
};

} // namespace StudioWelcome