Debian Codegraph @

src/Gui/propertyeditor/PropertyEditor.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24
25 #ifndef PROPERTYEDITOR_H
26 #define PROPERTYEDITOR_H
27
28 #include <unordered_set>
29
30 #include <QTreeView>
31
32 #include "PropertyItem.h"
33 #include "PropertyModel.h"
34
35
36 namespace App
37 {
38 class Property;
39 class Document;
40 } // namespace App
41
42 namespace Gui
43 {
44
45 class PropertyView;
46
47 namespace PropertyEditor
48 {
49
50 class PropertyItemDelegate;
51 class PropertyModel;
52 /*!
53 Put this into the .qss file after Gui--PropertyEditor--PropertyEditor
54
55 Gui--PropertyEditor--PropertyEditor
56 {
57 qproperty-groupBackground: gray;
58 qproperty-groupTextColor: white;
59 }
60
61 See also: https://man42.net/blog/2011/09/qt-4-7-modify-a-custom-q_property-with-a-qt-style-sheet/
62 */
63
64 class GuiExport PropertyEditor: public QTreeView
65 {
66 // clang-format off
67 Q_OBJECT
68 Q_PROPERTY(QBrush groupBackground READ groupBackground WRITE setGroupBackground DESIGNABLE true SCRIPTABLE true) // clazy:exclude=qproperty-without-notify
69 Q_PROPERTY(QColor groupTextColor READ groupTextColor WRITE setGroupTextColor DESIGNABLE true SCRIPTABLE true) // clazy:exclude=qproperty-without-notify
70 Q_PROPERTY(QBrush itemBackground READ itemBackground WRITE setItemBackground DESIGNABLE true SCRIPTABLE true) // clazy:exclude=qproperty-without-notify
71 // clang-format on
72
73 public:
74 enum class ExpansionMode
75 {
76 DefaultExpand,
77 AutoExpand,
78 AutoCollapse
79 };
80
81 PropertyEditor(QWidget* parent = nullptr);
82 ~PropertyEditor() override;
83
84 /** Builds up the list view with the properties. */
85 void buildUp(
86 PropertyModel::PropertyList&& props = PropertyModel::PropertyList(),
87 bool checkDocument = false
88 );
89 void blockCollapseAll();
90 void updateProperty(const App::Property&);
91 void removeProperty(const App::Property&);
92 void renameProperty(const App::Property&);
93 void setAutomaticDocumentUpdate(bool);
94 bool isAutomaticDocumentUpdate(bool) const;
95 /*! Reset the internal state of the view. */
96 void reset() override;
97
98 QBrush groupBackground() const;
99 void setGroupBackground(const QBrush& c);
100 QColor groupTextColor() const;
101 void setGroupTextColor(const QColor& c);
102 QBrush itemBackground() const;
103 void setItemBackground(const QBrush& c);
104
105 bool isBinding() const
106 {
107 return binding;
108 }
109 void openEditor(const QModelIndex& index);
110 void closeEditor();
111
112 protected Q_SLOTS:
113 void onItemActivated(const QModelIndex& index);
114 void onItemExpanded(const QModelIndex& index);
115 void onItemCollapsed(const QModelIndex& index);
116 void onRowsMoved(const QModelIndex& parent, int start, int end, const QModelIndex& dst, int row);
117 void onRowsRemoved(const QModelIndex& parent, int start, int end);
118
119 protected:
120 bool eventFilter(QObject* object, QEvent* event) override;
121 void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint) override;
122 void commitData(QWidget* editor) override;
123 void editorDestroyed(QObject* editor) override;
124 void currentChanged(const QModelIndex& current, const QModelIndex& previous) override;
125 void rowsInserted(const QModelIndex& parent, int start, int end) override;
126 void rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end) override;
127 void drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const override;
128 void drawRow(
129 QPainter* painter,
130 const QStyleOptionViewItem& options,
131 const QModelIndex& index
132 ) const override;
133 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
134 QStyleOptionViewItem viewOptions() const override;
135 #else
136 void initViewItemOption(QStyleOptionViewItem* option) const override;
137 #endif
138 void contextMenuEvent(QContextMenuEvent* event) override;
139 bool event(QEvent*) override;
140 void keyPressEvent(QKeyEvent* event) override;
141
142 private:
143 void setFirstLevelExpanded(bool doExpand);
144 void expandToDefault();
145 QMenu* setupExpansionSubmenu(QWidget* parent);
146 void collapseAll();
147 void setEditorMode(const QModelIndex& parent, int start, int end);
148 void closeTransaction();
149 void recomputeDocument(App::Document*);
150 std::unordered_set<App::Property*> acquireSelectedProperties() const;
151 void removeProperties(const std::unordered_set<App::Property*>& props);
152 bool removeSelectedDynamicProperties();
153
154 // check if mouse_pos is around right or bottom side of a cell
155 // and return the index of that cell if found
156 QModelIndex indexResizable(QPoint mouse_pos);
157
158 private:
159 PropertyItemDelegate* delegate;
160 PropertyModel* propertyModel;
161 QStringList selectedProperty;
162 PropertyModel::PropertyList propList;
163 std::unordered_set<const App::PropertyContainer*> propOwners;
164 ExpansionMode expansionMode;
165 bool autoupdate;
166 bool committing;
167 bool delaybuild;
168 bool blockCollapse;
169 bool binding;
170 bool checkDocument;
171 bool closingEditor;
172 bool dragInProgress;
173
174 // max distance between mouse and a cell, small enough to trigger resize
175 int dragSensibility = 5; // NOLINT
176 int dragSection = 0;
177 int dragPreviousPos = 0;
178
179 int transactionID = 0;
180
181 QColor groupColor;
182 QBrush background;
183 QBrush _itemBackground;
184
185 QPointer<QWidget> activeEditor;
186 QPersistentModelIndex editingIndex;
187 int removingRows = 0;
188
189 friend class Gui::PropertyView;
190 friend class PropertyItemDelegate;
191 };
192
193 } // namespace PropertyEditor
194 } // namespace Gui
195
196 #endif // PROPERTYEDITOR_H