aboutsummaryrefslogtreecommitdiff
path: root/plugins/ProfileEditor/forms/settingstable.cpp
blob: 88781a7b6d886dc669834f68c4c59033f5033bde (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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "settingstable.h"
#include <QResizeEvent>
#include <QSettings>
#include <QToolButton>

SettingsTable::SettingsTable(QWidget *parent)
    : QTableWidget(parent)
{
    QObject::connect(this, &QTableWidget::currentCellChanged, [this](int currentRow, int currentColumn, int /*previousRow*/, int /*previousColumn*/) {
        if(currentColumn == 0) {
            const auto key = item(currentRow, currentColumn)->text();
            m_selectedKey = key;
        }
    });

    addBtn = new QToolButton(this);
    addBtn->setText("Add");
    removeBtn = new QToolButton(this);
    removeBtn->setText("Remove");
}

void SettingsTable::connect(QSettings *settings, const QString &section)
{
    settings->beginGroup(section);
    if(settings != nullptr) {
        const auto keys = settings->childKeys();
        setRowCount(keys.count());
        for(int i = 0; i < keys.count(); ++i) {
            setItem(i, 0, new QTableWidgetItem(keys[i]));
            setItem(i, 1, new QTableWidgetItem(settings->value(keys[i]).toString()));
        }
    }
    settings->endGroup();

    QObject::connect(addBtn, &QToolButton::clicked, [this]() {
        const auto row = rowCount();
        setRowCount(row + 1);
        setItem(row, 0, new QTableWidgetItem("key"));
        setItem(row, 1, new QTableWidgetItem("value"));
    });

    QObject::connect(removeBtn, &QToolButton::clicked, settings, [this, settings, section]() {
        settings->beginGroup(section);

        const int row = currentRow();
        const auto key = item(row, 0)->text();

        removeRow(row);
        qDebug("remove key: %s", qUtf8Printable(key));
        settings->remove(key);

        settings->endGroup();
    });

    QObject::connect(this, &QTableWidget::cellChanged, settings, [this, settings, section](int row, int column) {
        // no value has been created yet
        if(item(row, 1) == nullptr) {
            return;
        }

        settings->beginGroup(section);

        if(column == 0) {
            qDebug("remove old key: %s", qUtf8Printable(m_selectedKey));
            settings->remove(m_selectedKey);
        }

        const auto key = item(row, 0)->text();
        const auto val = item(row, 1)->text();
        qDebug("set: [%s]=[%s]", qUtf8Printable(key), qUtf8Printable(val));
        settings->setValue(key, val);

        settings->endGroup();
    });
}

void SettingsTable::resizeEvent(QResizeEvent *event)
{
    QTableWidget::resizeEvent(event);
    addBtn->move(width() - addBtn->width() - removeBtn->width(), 0);
    removeBtn->move(width() - removeBtn->width(), 0);
}