1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "propertydialog.h" |
30 | |
31 | #include <QHeaderView> |
32 | #include <QLayout> |
33 | #include <QDebug> |
34 | |
35 | PropertyDialog::PropertyDialog(QWidget *parent, Qt::WindowFlags f) |
36 | : QDialog(parent, f) |
37 | { |
38 | buttonBox = new QDialogButtonBox; |
39 | propertyTable = new QTableWidget; |
40 | label = new QLabel; |
41 | |
42 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
43 | propertyTable->setColumnCount(2); |
44 | const QStringList labels = QStringList() << QLatin1String("Name" ) << QLatin1String("Value" ); |
45 | propertyTable->setHorizontalHeaderLabels(labels); |
46 | propertyTable->horizontalHeader()->setStretchLastSection(true); |
47 | propertyTable->setEditTriggers(QAbstractItemView::AllEditTriggers); |
48 | |
49 | connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, receiver: this, slot: &QDialog::accept, type: Qt::QueuedConnection); |
50 | connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, receiver: this, slot: &QDialog::reject, type: Qt::QueuedConnection); |
51 | |
52 | QVBoxLayout *layout = new QVBoxLayout(this); |
53 | layout->addWidget(label); |
54 | layout->addWidget(propertyTable); |
55 | layout->addWidget(buttonBox); |
56 | } |
57 | |
58 | void PropertyDialog::setInfo(const QString &caption) |
59 | { |
60 | label->setText(caption); |
61 | } |
62 | |
63 | void PropertyDialog::addProperty(const QString &aname, int type) |
64 | { |
65 | int rowCount = propertyTable->rowCount(); |
66 | propertyTable->setRowCount(rowCount + 1); |
67 | |
68 | QString name = aname; |
69 | if (name.isEmpty()) |
70 | name = QLatin1String("argument " ) + QString::number(rowCount + 1); |
71 | name += QLatin1String(" (" ); |
72 | name += QLatin1String(QMetaType::typeName(type)); |
73 | name += QLatin1String(")" ); |
74 | QTableWidgetItem *nameItem = new QTableWidgetItem(name); |
75 | nameItem->setFlags(nameItem->flags() & |
76 | ~(Qt::ItemIsEditable | Qt::ItemIsSelectable)); |
77 | propertyTable->setItem(row: rowCount, column: 0, item: nameItem); |
78 | |
79 | QTableWidgetItem *valueItem = new QTableWidgetItem; |
80 | valueItem->setData(role: Qt::DisplayRole, value: QVariant(type, /* copy */ 0)); |
81 | propertyTable->setItem(row: rowCount, column: 1, item: valueItem); |
82 | } |
83 | |
84 | int PropertyDialog::exec() |
85 | { |
86 | propertyTable->resizeColumnToContents(column: 0); |
87 | propertyTable->setFocus(); |
88 | propertyTable->setCurrentCell(row: 0, column: 1); |
89 | return QDialog::exec(); |
90 | } |
91 | |
92 | QList<QVariant> PropertyDialog::values() const |
93 | { |
94 | QList<QVariant> result; |
95 | |
96 | for (int i = 0; i < propertyTable->rowCount(); ++i) |
97 | result << propertyTable->item(row: i, column: 1)->data(role: Qt::EditRole); |
98 | |
99 | return result; |
100 | } |
101 | |
102 | |