Debian Codegraph @

src/Gui/Dialogs/DlgAddProperty.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2024 Ondsel <[email protected]> *
5 * Copyright (c) 2025 Pieter Hijma <[email protected]> *
6 * *
7 * This file is part of FreeCAD. *
8 * *
9 * FreeCAD is free software: you can redistribute it and/or modify it *
10 * under the terms of the GNU Lesser General Public License as *
11 * published by the Free Software Foundation, either version 2.1 of the *
12 * License, or (at your option) any later version. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with FreeCAD. If not, see *
21 * <https://www.gnu.org/licenses/>. *
22 * *
23 ***************************************************************************/
24
25 #ifndef GUI_DIALOG_DLG_ADD_PROPERTY_H
26 #define GUI_DIALOG_DLG_ADD_PROPERTY_H
27
28 #include <qcompleter.h>
29
30 #include <QDialog>
31 #include <QComboBox>
32 #include <QFormLayout>
33 #include <QStandardItemModel>
34
35 #include <FCGlobal.h>
36
37 #include <App/PropertyContainer.h>
38
39 #include "propertyeditor/PropertyItem.h"
40 #include "Macro.h"
41
42 namespace Gui
43 {
44
45 class ViewProviderVarSet;
46
47 namespace Dialog
48 {
49
50 class EditFinishedComboBox: public QComboBox
51 {
52 Q_OBJECT
53 public:
54 explicit EditFinishedComboBox(QWidget* parent = nullptr)
55 : QComboBox(parent)
56 {
57 setEditable(true);
58 connect(
59 this,
60 QOverload<int>::of(&QComboBox::currentIndexChanged),
61 this,
62 &EditFinishedComboBox::onIndexChanged
63 );
64 connect(
65 this->lineEdit(),
66 &QLineEdit::editingFinished,
67 this,
68 &EditFinishedComboBox::onEditingFinished
69 );
70 }
71
72 Q_SIGNALS:
73 void editFinished();
74
75 private:
76 void onEditingFinished()
77 {
78 Q_EMIT editFinished();
79 }
80
81 void onIndexChanged()
82 {
83 Q_EMIT editFinished();
84 }
85 };
86
87 class Ui_DlgAddProperty;
88
89 class TypeItemModel: public QStandardItemModel
90 {
91 Q_OBJECT
92 public:
93 static constexpr int SeparatorRole = Qt::UserRole + 1;
94
95 explicit TypeItemModel(QObject* parent = nullptr)
96 : QStandardItemModel(parent)
97 {}
98
99 Qt::ItemFlags flags(const QModelIndex& index) const override
100 {
101 Qt::ItemFlags flags = QStandardItemModel::flags(index);
102 if (index.isValid()) {
103 QVariant isSeparator = index.data(SeparatorRole);
104 if (isSeparator.isValid() && isSeparator.toBool()) {
105 return flags & ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
106 }
107 }
108 return flags;
109 }
110 };
111
112 class GuiExport DlgAddProperty: public QDialog
113 {
114 Q_OBJECT
115
116 public:
117 static const std::string GroupBase;
118
119 public:
120 DlgAddProperty(QWidget* parent, ViewProviderVarSet* viewProvider);
121 DlgAddProperty(QWidget* parent, App::PropertyContainer* container);
122
123 DlgAddProperty(const DlgAddProperty&) = delete;
124 DlgAddProperty(DlgAddProperty&&) = delete;
125 DlgAddProperty& operator=(const DlgAddProperty&) = delete;
126 DlgAddProperty& operator=(DlgAddProperty&&) = delete;
127
128 ~DlgAddProperty() override;
129
130 void changeEvent(QEvent* e) override;
131 void accept() override;
132 void reject() override;
133 static void populateGroup(EditFinishedComboBox& comboBox, const App::PropertyContainer* container);
134 static void setWidgetForLabel(const char* labelName, QWidget* widget, QLayout* layout);
135
136 public Q_SLOTS:
137 void valueChanged();
138 void valueChangedEnum();
139
140 private:
141 enum class TransactionOption : bool
142 {
143 Commit = false,
144 Abort = true
145 };
146
147 enum class FieldChange : std::uint8_t
148 {
149 Name,
150 Type
151 };
152
153 struct SupportedTypes
154 {
155 std::vector<Base::Type> commonTypes;
156 std::vector<Base::Type> otherTypes;
157 };
158
159 DlgAddProperty(QWidget* parent, App::PropertyContainer* container, ViewProviderVarSet* viewProvider);
160
161 void setupMacroRedirector();
162
163 void initializeGroup();
164
165 SupportedTypes getSupportedTypes();
166 void initializeTypes();
167
168 void removeSelectionEditor();
169 QVariant getEditorData() const;
170 void setEditorData(const QVariant& data);
171 bool isEnumPropertyItem() const;
172 void addEnumEditor(PropertyEditor::PropertyItem* propertyItem);
173 void addNormalEditor(PropertyEditor::PropertyItem* propertyItem);
174 void addEditor(PropertyEditor::PropertyItem* propertyItem);
175 bool isExcluded(const Base::Type& type) const;
176 bool isTypeWithEditor(PropertyEditor::PropertyItem* propertyItem) const;
177 bool isTypeWithEditor(const Base::Type& type) const;
178 bool isTypeWithEditor(const std::string& type) const;
179 void createSupportDataForType(const Base::Type& type);
180 void initializeValue();
181
182 void setTitle();
183 void setAddEnabled(bool enabled);
184 void initializeWidgets(ViewProviderVarSet* viewProvider);
185
186 bool isDocument() const;
187 bool isDocumentObject() const;
188 bool propertyExists(const std::string& name);
189 bool isNameValid();
190 bool isGroupValid();
191 bool isTypeValid();
192 bool areFieldsValid();
193
194 void setEditor(bool valueNeedsReset);
195 void buildForUnbound(bool valueNeedsReset);
196 void setPropertyItem(App::Property* prop, bool supportsExpressions);
197 void buildForBound(bool valueNeedsReset, bool supportsExpressions);
198 bool clearBoundProperty();
199 bool clear(FieldChange fieldChange);
200 void onNameChanged(const QString& text);
201 void onGroupFinished();
202 void onTypeChanged(const QString& text);
203
204 void showStatusMessage();
205
206 void removeEditor();
207
208 void openTransaction();
209 void critical(const QString& title, const QString& text);
210 void recordMacroAdd(
211 const App::PropertyContainer* container,
212 const std::string& type,
213 const std::string& name,
214 const std::string& group,
215 const std::string& doc
216 ) const;
217 App::Property* createProperty();
218 void closeTransaction(TransactionOption option);
219 void clearFields();
220 void addDocumentation();
221
222 static void removeExistingWidget(QFormLayout* layout, int labelRow);
223 static int findLabelRow(const char* labelName, QFormLayout* layout);
224
225 private:
226 App::PropertyContainer* container;
227 std::unique_ptr<Ui_DlgAddProperty> ui;
228
229 EditFinishedComboBox comboBoxGroup;
230 QCompleter completerType;
231
232 std::unique_ptr<QWidget> editor;
233 std::unique_ptr<PropertyEditor::PropertyItem> propertyItem;
234 std::unique_ptr<App::ObjectIdentifier> objectIdentifier;
235
236 // a transactionID of 0 means that there is no active transaction.
237 int transactionID;
238
239 QMetaObject::Connection connComboBoxGroup;
240 QMetaObject::Connection connComboBoxType;
241 QMetaObject::Connection connLineEditNameTextChanged;
242
243 std::unique_ptr<MacroManager::MacroRedirector> setValueRedirector;
244 std::string addCommand;
245 std::string setValueCommand;
246 };
247
248 } // namespace Dialog
249 } // namespace Gui
250
251 #endif // GUI_DIALOG_DLG_ADD_PROPERTY_H