| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "propertydialog.h" |
| 5 | |
| 6 | #include <QHeaderView> |
| 7 | #include <QLayout> |
| 8 | #include <QDebug> |
| 9 | |
| 10 | using namespace Qt::StringLiterals; |
| 11 | |
| 12 | PropertyDialog::PropertyDialog(QWidget *parent, Qt::WindowFlags f) |
| 13 | : QDialog(parent, f) |
| 14 | { |
| 15 | buttonBox = new QDialogButtonBox; |
| 16 | propertyTable = new QTableWidget; |
| 17 | label = new QLabel; |
| 18 | |
| 19 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 20 | propertyTable->setColumnCount(2); |
| 21 | const QStringList labels = QStringList() << tr(s: "Name" ) << tr(s: "Value" ); |
| 22 | propertyTable->setHorizontalHeaderLabels(labels); |
| 23 | propertyTable->horizontalHeader()->setStretchLastSection(true); |
| 24 | propertyTable->setEditTriggers(QAbstractItemView::AllEditTriggers); |
| 25 | |
| 26 | connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, context: this, slot: &QDialog::accept, type: Qt::QueuedConnection); |
| 27 | connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, context: this, slot: &QDialog::reject, type: Qt::QueuedConnection); |
| 28 | |
| 29 | QVBoxLayout *layout = new QVBoxLayout(this); |
| 30 | layout->addWidget(label); |
| 31 | layout->addWidget(propertyTable); |
| 32 | layout->addWidget(buttonBox); |
| 33 | } |
| 34 | |
| 35 | void PropertyDialog::setInfo(const QString &caption) |
| 36 | { |
| 37 | label->setText(caption); |
| 38 | } |
| 39 | |
| 40 | void PropertyDialog::addProperty(const QString &aname, int type) |
| 41 | { |
| 42 | int rowCount = propertyTable->rowCount(); |
| 43 | propertyTable->setRowCount(rowCount + 1); |
| 44 | |
| 45 | QString name = aname; |
| 46 | if (name.isEmpty()) |
| 47 | name = tr(s: "argument %1" ).arg(a: rowCount + 1); |
| 48 | name += " ("_L1 ; |
| 49 | name += QLatin1String(QMetaType(type).name()); |
| 50 | name += ")"_L1 ; |
| 51 | QTableWidgetItem *nameItem = new QTableWidgetItem(name); |
| 52 | nameItem->setFlags(nameItem->flags() & |
| 53 | ~(Qt::ItemIsEditable | Qt::ItemIsSelectable)); |
| 54 | propertyTable->setItem(row: rowCount, column: 0, item: nameItem); |
| 55 | |
| 56 | QTableWidgetItem *valueItem = new QTableWidgetItem; |
| 57 | valueItem->setData(role: Qt::DisplayRole, value: QVariant(QMetaType(type), /* copy */ 0)); |
| 58 | propertyTable->setItem(row: rowCount, column: 1, item: valueItem); |
| 59 | } |
| 60 | |
| 61 | int PropertyDialog::exec() |
| 62 | { |
| 63 | propertyTable->resizeColumnToContents(column: 0); |
| 64 | propertyTable->setFocus(); |
| 65 | propertyTable->setCurrentCell(row: 0, column: 1); |
| 66 | return QDialog::exec(); |
| 67 | } |
| 68 | |
| 69 | QList<QVariant> PropertyDialog::values() const |
| 70 | { |
| 71 | QList<QVariant> result; |
| 72 | |
| 73 | for (int i = 0; i < propertyTable->rowCount(); ++i) |
| 74 | result << propertyTable->item(row: i, column: 1)->data(role: Qt::EditRole); |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | |