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

#include "qbsprofilessettingspage.h"

#include "qbsprofilemanager.h"
#include "qbsprojectmanagerconstants.h"
#include "qbsprojectmanagertr.h"

#include <coreplugin/icore.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorericons.h>
#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>
#include <utils/treemodel.h>

#include <QCoreApplication>
#include <QComboBox>
#include <QHash>
#include <QHeaderView>
#include <QTreeView>
#include <QWidget>

using namespace ProjectExplorer;

namespace QbsProjectManager::Internal {

class ProfileTreeItem : public Utils::TypedTreeItem<ProfileTreeItem, ProfileTreeItem>
{
public:
    ProfileTreeItem() = default;
    ProfileTreeItem(const QString &key, const QString &value) : m_key(key), m_value(value) { }

private:
    QVariant data(int column, int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};
        if (column == 0)
            return m_key;
        if (column == 1)
            return m_value;
        return {};
    }

    const QString m_key;
    const QString m_value;
};

class ProfileModel : public Utils::TreeModel<ProfileTreeItem>
{
public:
    ProfileModel() : TreeModel(static_cast<QObject *>(nullptr))
    {
        setHeader(QStringList{Tr::tr("Key"), Tr::tr("Value")});
        reload();
    }

    void reload()
    {
        ProfileTreeItem * const newRoot = new ProfileTreeItem(QString(), QString());
        QHash<QStringList, ProfileTreeItem *> itemMap;
        const QStringList output = QbsProfileManager::runQbsConfig(
                    QbsProfileManager::QbsConfigOp::Get, "profiles").split('\n', Qt::SkipEmptyParts);
        for (QString line : output) {
            line = line.trimmed();
            line = line.mid(QString("profiles.").length());
            const int colonIndex = line.indexOf(':');
            if (colonIndex == -1)
                continue;
            const QStringList key = line.left(colonIndex).trimmed().split('.', Qt::SkipEmptyParts);
            const QString value = line.mid(colonIndex + 1).trimmed();
            QStringList partialKey;
            ProfileTreeItem *parent = newRoot;
            for (const QString &keyComponent : key) {
                partialKey << keyComponent;
                ProfileTreeItem *&item = itemMap[partialKey];
                if (!item) {
                    item = new ProfileTreeItem(keyComponent, partialKey == key ? value : QString());
                    parent->appendChild(item);
                }
                parent = item;
            }
        }
        setRootItem(newRoot);
    }
};

class QbsProfilesSettingsWidget : public Core::IOptionsPageWidget
{
public:
    QbsProfilesSettingsWidget();

private:
    void apply() final {}

    void refreshKitsList();
    void displayCurrentProfile();

    ProfileModel m_model;
    QComboBox *m_kitsComboBox;
    QLabel *m_profileValueLabel;
    QTreeView *m_propertiesView;
};

QbsProfilesSettingsPage::QbsProfilesSettingsPage()
{
    setId("Y.QbsProfiles");
    setDisplayName(Tr::tr("Profiles"));
    setCategory(Constants::QBS_SETTINGS_CATEGORY);
    setWidgetCreator([] { return new QbsProfilesSettingsWidget; });
}

QbsProfilesSettingsWidget::QbsProfilesSettingsWidget()
{
    m_kitsComboBox = new QComboBox;
    m_profileValueLabel = new QLabel;
    m_propertiesView = new QTreeView;

    using namespace Layouting;
    Column {
        Form {
            Tr::tr("Kit:"), m_kitsComboBox, br,
            Tr::tr("Associated profile:"), m_profileValueLabel, br,
        },
        hr,
        Tr::tr("Profile properties:"),
        Row {
            m_propertiesView,
            Column {
                PushButton {
                    text(Tr::tr("E&xpand All")),
                    onClicked([this] { m_propertiesView->expandAll(); }),
                },
                PushButton {
                    text(Tr::tr("&Collapse All")),
                    onClicked([this] { m_propertiesView->collapseAll(); }),
                },
                st,
            },
        },
    }.attachTo(this);

    connect(QbsProfileManager::instance(), &QbsProfileManager::qbsProfilesUpdated,
            this, &QbsProfilesSettingsWidget::refreshKitsList);
    refreshKitsList();
}

void QbsProfilesSettingsWidget::refreshKitsList()
{
    m_kitsComboBox->disconnect(this);
    m_propertiesView->setModel(nullptr);
    m_model.reload();
    m_profileValueLabel->clear();
    Utils::Id currentId;
    if (m_kitsComboBox->count() > 0)
        currentId = Utils::Id::fromSetting(m_kitsComboBox->currentData());
    m_kitsComboBox->clear();
    int newCurrentIndex = -1;
    QList<Kit *> validKits = KitManager::kits();
    Utils::erase(validKits, [](const Kit *k) { return !k->isValid(); });
    const bool hasKits = !validKits.isEmpty();
    for (const Kit * const kit : std::as_const(validKits)) {
        if (kit->id() == currentId)
            newCurrentIndex = m_kitsComboBox->count();
        m_kitsComboBox->addItem(kit->displayName(), kit->id().toSetting());
    }
    if (newCurrentIndex != -1)
        m_kitsComboBox->setCurrentIndex(newCurrentIndex);
    else if (hasKits)
        m_kitsComboBox->setCurrentIndex(0);
    displayCurrentProfile();
    connect(m_kitsComboBox, &QComboBox::currentIndexChanged,
            this, &QbsProfilesSettingsWidget::displayCurrentProfile);
}

void QbsProfilesSettingsWidget::displayCurrentProfile()
{
    m_propertiesView->setModel(nullptr);
    if (m_kitsComboBox->currentIndex() == -1)
        return;
    const Utils::Id kitId = Utils::Id::fromSetting(m_kitsComboBox->currentData());
    const Kit * const kit = KitManager::kit(kitId);
    QTC_ASSERT(kit, return);
    const QString profileName = QbsProfileManager::ensureProfileForKit(kit);
    m_profileValueLabel->setText(profileName);
    for (int i = 0; i < m_model.rowCount(); ++i) {
        const QModelIndex currentProfileIndex = m_model.index(i, 0);
        if (m_model.data(currentProfileIndex, Qt::DisplayRole).toString() != profileName)
            continue;
        m_propertiesView->setModel(&m_model);
        m_propertiesView->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
        m_propertiesView->setRootIndex(currentProfileIndex);
        return;
    }
}

} // QbsProjectManager::Internal