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 Qt Designer 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 "newdynamicpropertydialog.h" |
30 | #include "ui_newdynamicpropertydialog.h" |
31 | #include <abstractdialoggui_p.h> |
32 | #include <qdesigner_propertysheet_p.h> |
33 | |
34 | #include <QtWidgets/qpushbutton.h> |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | namespace qdesigner_internal { |
39 | |
40 | NewDynamicPropertyDialog::NewDynamicPropertyDialog(QDesignerDialogGuiInterface *dialogGui, |
41 | QWidget *parent) : |
42 | QDialog(parent), |
43 | m_dialogGui(dialogGui), |
44 | m_ui(new Ui::NewDynamicPropertyDialog) |
45 | { |
46 | m_ui->setupUi(this); |
47 | connect(sender: m_ui->m_lineEdit, signal: &QLineEdit::textChanged, receiver: this, slot: &NewDynamicPropertyDialog::nameChanged); |
48 | |
49 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
50 | |
51 | m_ui->m_comboBox->addItem(QStringLiteral("String" ), auserData: QVariant(QVariant::String)); |
52 | m_ui->m_comboBox->addItem(QStringLiteral("StringList" ), auserData: QVariant(QVariant::StringList)); |
53 | m_ui->m_comboBox->addItem(QStringLiteral("Char" ), auserData: QVariant(QVariant::Char)); |
54 | m_ui->m_comboBox->addItem(QStringLiteral("ByteArray" ), auserData: QVariant(QVariant::ByteArray)); |
55 | m_ui->m_comboBox->addItem(QStringLiteral("Url" ), auserData: QVariant(QVariant::Url)); |
56 | m_ui->m_comboBox->addItem(QStringLiteral("Bool" ), auserData: QVariant(QVariant::Bool)); |
57 | m_ui->m_comboBox->addItem(QStringLiteral("Int" ), auserData: QVariant(QVariant::Int)); |
58 | m_ui->m_comboBox->addItem(QStringLiteral("UInt" ), auserData: QVariant(QVariant::UInt)); |
59 | m_ui->m_comboBox->addItem(QStringLiteral("LongLong" ), auserData: QVariant(QVariant::LongLong)); |
60 | m_ui->m_comboBox->addItem(QStringLiteral("ULongLong" ), auserData: QVariant(QVariant::ULongLong)); |
61 | m_ui->m_comboBox->addItem(QStringLiteral("Double" ), auserData: QVariant(QVariant::Double)); |
62 | m_ui->m_comboBox->addItem(QStringLiteral("Size" ), auserData: QVariant(QVariant::Size)); |
63 | m_ui->m_comboBox->addItem(QStringLiteral("SizeF" ), auserData: QVariant(QVariant::SizeF)); |
64 | m_ui->m_comboBox->addItem(QStringLiteral("Point" ), auserData: QVariant(QVariant::Point)); |
65 | m_ui->m_comboBox->addItem(QStringLiteral("PointF" ), auserData: QVariant(QVariant::PointF)); |
66 | m_ui->m_comboBox->addItem(QStringLiteral("Rect" ), auserData: QVariant(QVariant::Rect)); |
67 | m_ui->m_comboBox->addItem(QStringLiteral("RectF" ), auserData: QVariant(QVariant::RectF)); |
68 | m_ui->m_comboBox->addItem(QStringLiteral("Date" ), auserData: QVariant(QVariant::Date)); |
69 | m_ui->m_comboBox->addItem(QStringLiteral("Time" ), auserData: QVariant(QVariant::Time)); |
70 | m_ui->m_comboBox->addItem(QStringLiteral("DateTime" ), auserData: QVariant(QVariant::DateTime)); |
71 | m_ui->m_comboBox->addItem(QStringLiteral("Font" ), auserData: QVariant(QVariant::Font)); |
72 | m_ui->m_comboBox->addItem(QStringLiteral("Palette" ), auserData: QVariant(QVariant::Palette)); |
73 | m_ui->m_comboBox->addItem(QStringLiteral("Color" ), auserData: QVariant(QVariant::Color)); |
74 | m_ui->m_comboBox->addItem(QStringLiteral("Pixmap" ), auserData: QVariant(QVariant::Pixmap)); |
75 | m_ui->m_comboBox->addItem(QStringLiteral("Icon" ), auserData: QVariant(QVariant::Icon)); |
76 | m_ui->m_comboBox->addItem(QStringLiteral("Cursor" ), auserData: QVariant(QVariant::Cursor)); |
77 | m_ui->m_comboBox->addItem(QStringLiteral("SizePolicy" ), auserData: QVariant(QVariant::SizePolicy)); |
78 | m_ui->m_comboBox->addItem(QStringLiteral("KeySequence" ), auserData: QVariant(QVariant::KeySequence)); |
79 | |
80 | m_ui->m_comboBox->setCurrentIndex(0); // String |
81 | setOkButtonEnabled(false); |
82 | } |
83 | |
84 | void NewDynamicPropertyDialog::setOkButtonEnabled(bool e) |
85 | { |
86 | m_ui->m_buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(e); |
87 | } |
88 | |
89 | NewDynamicPropertyDialog::~NewDynamicPropertyDialog() |
90 | { |
91 | delete m_ui; |
92 | } |
93 | |
94 | void NewDynamicPropertyDialog::setReservedNames(const QStringList &names) |
95 | { |
96 | m_reservedNames = names; |
97 | } |
98 | |
99 | void NewDynamicPropertyDialog::setPropertyType(QVariant::Type t) |
100 | { |
101 | const int index = m_ui->m_comboBox->findData(data: QVariant(t)); |
102 | if (index != -1) |
103 | m_ui->m_comboBox->setCurrentIndex(index); |
104 | } |
105 | |
106 | QString NewDynamicPropertyDialog::propertyName() const |
107 | { |
108 | return m_ui->m_lineEdit->text(); |
109 | } |
110 | |
111 | QVariant NewDynamicPropertyDialog::propertyValue() const |
112 | { |
113 | const int index = m_ui->m_comboBox->currentIndex(); |
114 | if (index == -1) |
115 | return QVariant(); |
116 | return m_ui->m_comboBox->itemData(index); |
117 | } |
118 | |
119 | void NewDynamicPropertyDialog::information(const QString &message) |
120 | { |
121 | m_dialogGui->message(parent: this, context: QDesignerDialogGuiInterface::PropertyEditorMessage, icon: QMessageBox::Information, title: tr(s: "Set Property Name" ), text: message); |
122 | } |
123 | |
124 | void NewDynamicPropertyDialog::nameChanged(const QString &s) |
125 | { |
126 | setOkButtonEnabled(!s.isEmpty()); |
127 | } |
128 | |
129 | bool NewDynamicPropertyDialog::validatePropertyName(const QString& name) |
130 | { |
131 | if (m_reservedNames.contains(str: name)) { |
132 | information(message: tr(s: "The current object already has a property named '%1'.\nPlease select another, unique one." ).arg(a: name)); |
133 | return false; |
134 | } |
135 | if (!QDesignerPropertySheet::internalDynamicPropertiesEnabled() && name.startsWith(QStringLiteral("_q_" ))) { |
136 | information(message: tr(s: "The '_q_' prefix is reserved for the Qt library.\nPlease select another name." )); |
137 | return false; |
138 | } |
139 | return true; |
140 | } |
141 | |
142 | void NewDynamicPropertyDialog::on_m_buttonBox_clicked(QAbstractButton *btn) |
143 | { |
144 | const int role = m_ui->m_buttonBox->buttonRole(button: btn); |
145 | switch (role) { |
146 | case QDialogButtonBox::RejectRole: |
147 | reject(); |
148 | break; |
149 | case QDialogButtonBox::AcceptRole: |
150 | if (validatePropertyName(name: propertyName())) |
151 | accept(); |
152 | break; |
153 | } |
154 | } |
155 | } |
156 | |
157 | QT_END_NAMESPACE |
158 | |