1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "codecompletionmodel.h"
5#include "effectmanager.h"
6
7CodeCompletionModel::CodeCompletionModel(QObject *effectManager)
8 : QAbstractListModel(effectManager)
9{
10 m_effectManager = static_cast<EffectManager *>(effectManager);
11 connect(sender: this, signal: &QAbstractListModel::modelReset, context: this, slot: &CodeCompletionModel::rowCountChanged);
12}
13
14int CodeCompletionModel::rowCount(const QModelIndex &) const
15{
16 return m_modelList.size();
17}
18
19QHash<int, QByteArray> CodeCompletionModel::roleNames() const
20{
21 QHash<int, QByteArray> roles;
22 roles[Name] = "name";
23 roles[Type] = "type";
24 return roles;
25}
26
27QVariant CodeCompletionModel::data(const QModelIndex &index, int role) const
28{
29 if (!index.isValid())
30 return QVariant();
31
32 const auto &data = (m_modelList)[index.row()];
33
34 if (role == Name)
35 return QVariant::fromValue(value: data.name);
36 else if (role == Type)
37 return QVariant::fromValue(value: data.type);
38
39 return QVariant();
40}
41
42// Note: Doesn't reset the model, do that outside
43void CodeCompletionModel::addItem(const QString &text, int type)
44{
45 ModelData data;
46 data.name = text;
47 data.type = type;
48 m_modelList.append(t: data);
49}
50
51// Note: Doesn't reset the model, do that outside
52void CodeCompletionModel::clearItems()
53{
54 m_modelList.clear();
55}
56
57CodeCompletionModel::ModelData CodeCompletionModel::currentItem()
58{
59 if (m_modelList.size() > m_currentIndex)
60 return m_modelList.at(i: m_currentIndex);
61
62 return CodeCompletionModel::ModelData();
63}
64
65int CodeCompletionModel::currentIndex() const
66{
67 return m_currentIndex;
68}
69
70void CodeCompletionModel::setCurrentIndex(int index)
71{
72 if (m_currentIndex == index)
73 return;
74 m_currentIndex = index;
75 Q_EMIT currentIndexChanged();
76}
77
78void CodeCompletionModel::nextItem()
79{
80 if (m_modelList.size() > m_currentIndex + 1) {
81 m_currentIndex++;
82 Q_EMIT currentIndexChanged();
83 }
84}
85
86void CodeCompletionModel::previousItem()
87{
88 if (m_currentIndex > 0) {
89 m_currentIndex--;
90 Q_EMIT currentIndexChanged();
91 }
92}
93
94

source code of qtquickeffectmaker/tools/qqem/codecompletionmodel.cpp