Debian Codegraph @

src/Gui/propertyeditor/PropertyItemDelegate.cpp

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 #include <QApplication>
25 #include <QCheckBox>
26 #include <QComboBox>
27 #include <QModelIndex>
28 #include <QPainter>
29 #include <QTimer>
30 #include <QKeyEvent>
31
32 #include <Base/Tools.h>
33
34 #include "PropertyItemDelegate.h"
35 #include "MDIView.h"
36 #include "PropertyEditor.h"
37 #include "PropertyItem.h"
38 #include "Tree.h"
39
40
41 FC_LOG_LEVEL_INIT("PropertyView", true, true)
42
43 using namespace Gui::PropertyEditor;
44
45
46 PropertyItemDelegate::PropertyItemDelegate(QObject* parent)
47 : QItemDelegate(parent)
48 , expressionEditor(nullptr)
49 , pressed(false)
50 , changed(false)
51 {}
52
53 PropertyItemDelegate::~PropertyItemDelegate() = default;
54
55 QSize PropertyItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
56 {
57 QSize size = QItemDelegate::sizeHint(option, index);
58 size += QSize(0, 5);
59 return size;
60 }
61
62 void PropertyItemDelegate::paint(
63 QPainter* painter,
64 const QStyleOptionViewItem& opt,
65 const QModelIndex& index
66 ) const
67 {
68 QStyleOptionViewItem option = opt;
69
70 auto property = static_cast<PropertyItem*>(index.internalPointer());
71
72 if (property && property->isSeparator()) {
73 QColor color = option.palette.color(QPalette::BrightText);
74 QObject* par = parent();
75 if (par) {
76 QVariant value = par->property("groupTextColor");
77 if (value.canConvert<QColor>()) {
78 color = value.value<QColor>();
79 }
80 }
81 option.palette.setColor(QPalette::Text, color);
82 option.font.setBold(true);
83
84 // Since the group item now parents all the property items and can be
85 // collapsed, it makes sense to have some selection visual clue for it.
86 //
87 // option.state &= ~QStyle::State_Selected;
88 }
89 else if (index.column() == 1) {
90 option.state &= ~QStyle::State_Selected;
91 if (property && property->isReadOnly()) {
92 option.state &= ~QStyle::State_Enabled;
93 }
94 }
95
96 option.state &= ~QStyle::State_HasFocus;
97
98 if (property && property->isSeparator()) {
99 QBrush brush = option.palette.dark();
100 QObject* par = parent();
101 if (par) {
102 QVariant value = par->property("groupBackground");
103 if (value.canConvert<QBrush>()) {
104 brush = value.value<QBrush>();
105 }
106 }
107 painter->fillRect(option.rect, brush);
108 }
109
110 QPen savedPen = painter->pen();
111
112 if (index.column() == 1 && property && dynamic_cast<PropertyBoolItem*>(property)) {
113 bool checked = index.data(Qt::EditRole).toBool();
114 bool readonly = property->isReadOnly();
115
116 QStyle* style = option.widget ? option.widget->style() : QApplication::style();
117 QPalette palette = option.widget ? option.widget->palette() : QApplication::palette();
118
119 QStyleOptionButton checkboxOption;
120
121 checkboxOption.state |= readonly ? QStyle::State_ReadOnly : QStyle::State_Enabled;
122 checkboxOption.state |= checked ? QStyle::State_On : QStyle::State_Off;
123
124 // draw the item (background etc.)
125 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
126
127 // Draw the checkbox
128 checkboxOption.rect
129 = style->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption, option.widget);
130 int leftSpacing = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, option.widget);
131
132 QRect checkboxRect = QStyle::alignedRect(
133 option.direction,
134 Qt::AlignVCenter,
135 checkboxOption.rect.size(),
136 option.rect.adjusted(leftSpacing, 0, -leftSpacing, 0)
137 );
138 checkboxOption.rect = checkboxRect;
139
140 style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkboxOption, painter, option.widget);
141
142 // Draw the label of the checkbox
143 QString labelText = checked ? tr("Yes") : tr("No");
144 int spacing = style->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, nullptr, option.widget);
145 QRect textRect(
146 checkboxOption.rect.right() + spacing,
147 checkboxOption.rect.top(),
148 option.rect.right() - (checkboxOption.rect.right() + spacing),
149 checkboxOption.rect.height()
150 );
151 if (readonly) {
152 painter->setPen(palette.color(QPalette::Disabled, QPalette::Text));
153 }
154 else {
155 painter->setPen(palette.color(QPalette::Text));
156 }
157 painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, labelText);
158 }
159 else {
160 QItemDelegate::paint(painter, option, index);
161 }
162
163 QColor color = static_cast<QRgb>(QApplication::style()->styleHint(
164 QStyle::SH_Table_GridLineColor,
165 &opt,
166 qobject_cast<QWidget*>(parent())
167 ));
168 painter->setPen(QPen(color));
169 if (index.column() == 1 || !(property && property->isSeparator())) {
170 int right = (option.direction == Qt::LeftToRight) ? option.rect.right() : option.rect.left();
171 painter->drawLine(right, option.rect.y(), right, option.rect.bottom());
172 }
173 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
174 painter->setPen(savedPen);
175 }
176
177 bool PropertyItemDelegate::editorEvent(
178 QEvent* event,
179 QAbstractItemModel* model,
180 const QStyleOptionViewItem& option,
181 const QModelIndex& index
182 )
183 {
184 auto property = static_cast<PropertyItem*>(index.internalPointer());
185
186 if ((property && !property->isSeparator())
187 && (!event || event->type() == QEvent::MouseButtonDblClick)) {
188 // ignore double click, as it could cause editor lock with checkboxes
189 // due to the editor being close immediately after toggling the checkbox
190 // which is currently done on first click
191 this->pressed = true;
192 return true;
193 }
194 bool mouseButton = event->type() == QEvent::MouseButtonPress;
195 if (mouseButton) {
196 this->pressed = true;
197 }
198 return QItemDelegate::editorEvent(event, model, option, index);
199 }
200
201 bool PropertyItemDelegate::eventFilter(QObject* o, QEvent* ev)
202 {
203 if (ev->type() == QEvent::KeyPress) {
204 auto* checkBox = qobject_cast<QCheckBox*>(o);
205 if (checkBox) {
206 auto* keyEvent = static_cast<QKeyEvent*>(ev);
207 if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter
208 || keyEvent->key() == Qt::Key_Space) {
209
210 checkBox->toggle();
211
212 // Manually commit the data WITHOUT closing the editor.
213 // This keeps the focus on the checkbox so subsequent 'Enter'
214 // presses will toggle it again immediately.
215 if (propertyEditor) {
216 // We must set 'changed' to true so setModelData updates the model,
217 // then revert it back (handled by FlagToggler).
218 Base::FlagToggler<> flag(changed);
219 Q_EMIT commitData(propertyEditor);
220 }
221 return true;
222 }
223 }
224 }
225 else if (ev->type() == QEvent::FocusIn) {
226 auto* comboBox = qobject_cast<QComboBox*>(o);
227 if (comboBox) {
228 auto parentEditor = qobject_cast<PropertyEditor*>(this->parent());
229 if (parentEditor && parentEditor->activeEditor == comboBox) {
230 comboBox->showPopup();
231 }
232 }
233 auto* checkBox = qobject_cast<QCheckBox*>(o);
234 if (checkBox) {
235 auto parentEditor = qobject_cast<PropertyEditor*>(this->parent());
236 if (parentEditor && parentEditor->activeEditor == checkBox) {
237 if (this->pressed) {
238 checkBox->toggle();
239 // Delay valueChanged to ensure proper recomputation
240 QTimer::singleShot(0, this, [this]() { valueChanged(); });
241 }
242 }
243 }
244 this->pressed = false;
245 }
246 else if (ev->type() == QEvent::FocusOut) {
247 if (auto button = qobject_cast<Gui::ColorButton*>(o)) {
248 // Ignore the event if the ColorButton's modal dialog is active.
249 if (button->property("modal_dialog_active").toBool()) {
250 return true;
251 }
252 }
253 auto parentEditor = qobject_cast<PropertyEditor*>(this->parent());
254 if (auto* comboBox = qobject_cast<QComboBox*>(o)) {
255 if (parentEditor && parentEditor->activeEditor == comboBox) {
256 parentEditor->activeEditor = nullptr;
257 }
258 }
259 auto widget = qobject_cast<QWidget*>(o);
260 if (widget && parentEditor && parentEditor->activeEditor
261 && widget != parentEditor->activeEditor) {
262 // All the attempts to ignore the focus-out event has been approved to not work
263 // reliably because there are still cases that cannot be handled.
264 // So, the best for now is to always ignore this event.
265 // See https://forum.freecad.org/viewtopic.php?p=579530#p579530 why this is not
266 // possible.
267 return false;
268 }
269 }
270 return QItemDelegate::eventFilter(o, ev);
271 }
272
273 QWidget* PropertyItemDelegate::createEditor(
274 QWidget* parent,
275 const QStyleOptionViewItem& /*option*/,
276 const QModelIndex& index
277 ) const
278 {
279 if (!index.isValid()) {
280 return nullptr;
281 }
282
283 auto childItem = static_cast<PropertyItem*>(index.internalPointer());
284 if (!childItem || childItem->isSeparator() || childItem->isReadOnly()) {
285 return nullptr;
286 }
287
288 auto parentEditor = qobject_cast<PropertyEditor*>(this->parent());
289 if (parentEditor) {
290 parentEditor->closeEditor();
291 }
292
293 auto createEditor = [this, childItem, parent]() {
294 // Can't use a terniary here because the lambdas have different types.
295 if (qobject_cast<PropertyBoolItem*>(childItem)) {
296 // Boolean properties use a checkbox that is basically artificial
297 // (it is not rendered). Therefore, the callback is handled in
298 // eventFilter()
299 return childItem->createEditor(parent, []() noexcept {});
300 }
301 return childItem->createEditor(parent, [this]() {
302 const_cast<PropertyItemDelegate*>(this)->valueChanged(); // NOLINT
303 });
304 };
305
306 FC_LOG("create editor " << index.row() << "," << index.column());
307 QWidget* editor = nullptr;
308 expressionEditor = nullptr;
309 userEditor = nullptr;
310 if (parentEditor && parentEditor->isBinding()) {
311 expressionEditor = editor = childItem->createExpressionEditor(parent, [this]() {
312 const_cast<PropertyItemDelegate*>(this)->valueChanged(); // NOLINT
313 });
314 propertyEditor = editor;
315 }
316 else {
317 const auto& props = childItem->getPropertyData();
318 if (!props.empty() && props[0]->testStatus(App::Property::UserEdit)) {
319 editor = userEditor = childItem->createPropertyEditorWidget(parent);
320 propertyEditor = editor;
321 }
322 else {
323 propertyEditor = editor = createEditor();
324 }
325 }
326 if (editor) {
327 // Make sure the editor background is painted so the cell content doesn't show through
328 editor->setAutoFillBackground(true);
329 }
330 if (editor && childItem->isReadOnly()) {
331 editor->setDisabled(true);
332 }
333 else if (editor /*&& this->pressed*/) {
334 // We changed the way editor is activated in PropertyEditor (in response
335 // of signal activated and clicked), so now we should grab focus
336 // regardless of "pressed" or not (e.g. when activated by keyboard
337 // enter)
338 editor->setFocus();
339 }
340
341 if (editor) {
342 const auto widgets = editor->findChildren<QWidget*>();
343 for (auto w : widgets) {
344 if (qobject_cast<QAbstractButton*>(w) || qobject_cast<QLabel*>(w)) {
345 w->installEventFilter(const_cast<PropertyItemDelegate*>(this));
346 }
347 }
348 parentEditor->activeEditor = editor;
349 parentEditor->editingIndex = index;
350 }
351
352 return editor;
353 }
354
355 void PropertyItemDelegate::valueChanged()
356 {
357 if (propertyEditor) {
358 Base::FlagToggler<> flag(changed);
359 Q_EMIT commitData(propertyEditor);
360 if (qobject_cast<QComboBox*>(propertyEditor) || qobject_cast<QCheckBox*>(propertyEditor)) {
361 Q_EMIT closeEditor(propertyEditor);
362 return;
363 }
364 }
365 }
366
367 void PropertyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
368 {
369 if (!index.isValid()) {
370 return;
371 }
372 QVariant data = index.data(Qt::EditRole);
373 auto childItem = static_cast<PropertyItem*>(index.internalPointer());
374 editor->blockSignals(true);
375 if (expressionEditor == editor) {
376 childItem->setExpressionEditorData(editor, data);
377 }
378 else if (userEditor == editor) {
379 userEditor->setValue(PropertyItemAttorney::toString(childItem, data));
380 }
381 else {
382 childItem->setEditorData(editor, data);
383 }
384 editor->blockSignals(false);
385 return;
386 }
387
388 void PropertyItemDelegate::setModelData(
389 QWidget* editor,
390 QAbstractItemModel* model,
391 const QModelIndex& index
392 ) const
393 {
394 if (!index.isValid() || !changed || userEditor) {
395 return;
396 }
397 auto childItem = static_cast<PropertyItem*>(index.internalPointer());
398 QVariant data;
399 if (expressionEditor == editor) {
400 data = childItem->expressionEditorData(editor);
401 }
402 else {
403 data = childItem->editorData(editor);
404 }
405 model->setData(index, data, Qt::EditRole);
406 }
407
408 #include "moc_PropertyItemDelegate.cpp"