aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qbsprojectmanager/customqbspropertiesdialog.cpp
blob: 4f3d88907ad00c1a4d924d481cde5ce37a446d3a (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
// 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 "customqbspropertiesdialog.h"

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

#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>

#include <QDialogButtonBox>
#include <QHeaderView>
#include <QPushButton>
#include <QTableWidgetItem>

namespace QbsProjectManager {
namespace Internal {

CustomQbsPropertiesDialog::CustomQbsPropertiesDialog(const QVariantMap &properties, QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(Tr::tr("Custom Properties"));

    m_propertiesTable = new QTableWidget;
    m_propertiesTable->setColumnCount(2);
    m_propertiesTable->setRowCount(properties.count());
    m_propertiesTable->setHorizontalHeaderLabels(QStringList() << Tr::tr("Key") << Tr::tr("Value"));
    m_propertiesTable->horizontalHeader()->setStretchLastSection(true);
    m_propertiesTable->verticalHeader()->setVisible(false);
    int currentRow = 0;
    for (QVariantMap::ConstIterator it = properties.constBegin(); it != properties.constEnd();
         ++it) {
        auto * const nameItem = new QTableWidgetItem;
        nameItem->setData(Qt::DisplayRole, it.key());
        m_propertiesTable->setItem(currentRow, 0, nameItem);
        auto * const valueItem = new QTableWidgetItem;
        valueItem->setData(Qt::DisplayRole, toJSLiteral(it.value()));
        m_propertiesTable->setItem(currentRow, 1, valueItem);
        ++currentRow;
    }
    m_removeButton = new QPushButton(Tr::tr("&Remove"));
    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    using namespace Utils::Layouting;
    Column {
        Row {
            m_propertiesTable,
            Column {
                PushButton {
                    text(Tr::tr("&Add")),
                    onClicked([this] { addProperty(); } ),
                },
                m_removeButton,
                st
            },
        },
        buttonBox,
    }.attachTo(this);

    connect(m_removeButton, &QAbstractButton::clicked,
            this, &CustomQbsPropertiesDialog::removeSelectedProperty);
    connect(m_propertiesTable, &QTableWidget::currentItemChanged,
            this, &CustomQbsPropertiesDialog::handleCurrentItemChanged);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &CustomQbsPropertiesDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &CustomQbsPropertiesDialog::reject);

    handleCurrentItemChanged();
}

QVariantMap CustomQbsPropertiesDialog::properties() const
{
    QVariantMap properties;
    for (int row = 0; row < m_propertiesTable->rowCount(); ++row) {
        const QTableWidgetItem * const nameItem = m_propertiesTable->item(row, 0);
        const QString name = nameItem->text();
        if (name.isEmpty())
            continue;
        properties.insert(name, fromJSLiteral(m_propertiesTable->item(row, 1)->text()));
    }
    return properties;
}

void CustomQbsPropertiesDialog::addProperty()
{
    const int row = m_propertiesTable->rowCount();
    m_propertiesTable->insertRow(row);
    m_propertiesTable->setItem(row, 0, new QTableWidgetItem);
    m_propertiesTable->setItem(row, 1, new QTableWidgetItem);
}

void CustomQbsPropertiesDialog::removeSelectedProperty()
{
    const QTableWidgetItem * const currentItem = m_propertiesTable->currentItem();
    QTC_ASSERT(currentItem, return);
    m_propertiesTable->removeRow(currentItem->row());
}

void CustomQbsPropertiesDialog::handleCurrentItemChanged()
{
    m_removeButton->setEnabled(m_propertiesTable->currentItem());
}

} // namespace Internal
} // namespace QbsProjectManager