aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/studiowelcome/fieldhelper.cpp
blob: c5e777ed252d43240dceb8facabc43d4cad1fcdf (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "fieldhelper.h"

#include <projectexplorer/jsonwizard/jsonfieldpage.h>
#include <projectexplorer/jsonwizard/jsonfieldpage_p.h>

#include <utils/qtcassert.h>

using namespace StudioWelcome::FieldHelper;

CheckBoxHelper::CheckBoxHelper(ProjectExplorer::JsonFieldPage *detailsPage, const QString &fieldName)
    : m_field(dynamic_cast<ProjectExplorer::CheckBoxField *>(detailsPage->jsonField(fieldName)))
{}

void CheckBoxHelper::setChecked(bool value)
{
    QTC_ASSERT(m_field, return);

    m_field->setChecked(value);
}

bool CheckBoxHelper::isChecked() const
{
    QTC_ASSERT(m_field, return false);
    return m_field->isChecked();
}

ComboBoxHelper::ComboBoxHelper(ProjectExplorer::JsonFieldPage *detailsPage, const QString &fieldName)
    : m_field(dynamic_cast<ProjectExplorer::ComboBoxField *>(detailsPage->jsonField(fieldName)))
{}

void ComboBoxHelper::selectIndex(int index)
{
    QTC_ASSERT(m_field, return);

    m_field->selectRow(index);
}

QString ComboBoxHelper::text(int index) const
{
    QTC_ASSERT(m_field, return {});

    QStandardItemModel *model = m_field->model();
    if (index < 0 || index >= model->rowCount())
        return {};

    return model->item(index)->text();
}

int ComboBoxHelper::indexOf(const QString &text) const
{
    QTC_ASSERT(m_field, return -1);

    const QStandardItemModel *model = m_field->model();
    for (int i = 0; i < model->rowCount(); ++i) {
        const QStandardItem *item = model->item(i, 0);
        if (text == item->text())
            return i;
    }

    return -1;
}

int ComboBoxHelper::selectedIndex() const
{
    QTC_ASSERT(m_field, return -1);

    return m_field->selectedRow();
}

QStandardItemModel *ComboBoxHelper::model() const
{
    QTC_ASSERT(m_field, return {});

    return m_field->model();
}

QStringList ComboBoxHelper::allTexts() const
{
    QTC_ASSERT(m_field, return {});

    QStandardItemModel *model = m_field->model();
    const int rows = model->rowCount();
    QStringList result;
    result.reserve(rows);

    for (int i = 0; i < rows; ++i)
        result.append(model->item(i)->text());

    return result;
}