Debian Codegraph @

src/Gui/PropertyView.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QEvent>
25 #include <QGridLayout>
26 #include <QTimer>
27
28
29 #include <App/Document.h>
30 #include <App/DocumentObject.h>
31 #include <Base/Console.h>
32 #include <Base/Parameter.h>
33 #include <Base/Tools.h>
34
35 #include "PropertyView.h"
36 #include "Application.h"
37 #include "Document.h"
38 #include "MainWindow.h"
39 #include "SelectionObject.h"
40 #include "Tree.h"
41 #include "ViewParams.h"
42 #include "ViewProvider.h"
43 #include "ViewProviderDocumentObject.h"
44 #include "propertyeditor/PropertyEditor.h"
45
46
47 using namespace std;
48 using namespace Gui;
49 using namespace Gui::DockWnd;
50 using namespace Gui::PropertyEditor;
51 namespace sp = std::placeholders;
52
53 static ParameterGrp::handle _GetParam()
54 {
55 static ParameterGrp::handle hGrp;
56 if (!hGrp) {
57 hGrp = App::GetApplication().GetParameterGroupByPath(
58 "User parameter:BaseApp/Preferences/PropertyView"
59 );
60 }
61 return hGrp;
62 }
63
64 /* TRANSLATOR Gui::PropertyView */
65
66 /*! Property Editor Widget
67 *
68 * Provides two Gui::PropertyEditor::PropertyEditor widgets, for "View" and "Data",
69 * in two tabs.
70 */
71 PropertyView::PropertyView(QWidget* parent)
72 : QWidget(parent)
73 , SelectionObserver(false, ResolveMode::NoResolve)
74 {
75 auto pLayout = new QGridLayout(this);
76 pLayout->setSpacing(0);
77 pLayout->setContentsMargins(0, 0, 0, 0);
78
79 timer = new QTimer(this);
80 timer->setSingleShot(true);
81 connect(timer, &QTimer::timeout, this, &PropertyView::onTimer);
82
83 tabs = new QTabWidget(this);
84 tabs->setObjectName(QStringLiteral("propertyTab"));
85 tabs->setTabPosition(QTabWidget::South);
86 pLayout->addWidget(tabs, 0, 0);
87
88 propertyEditorView = new Gui::PropertyEditor::PropertyEditor();
89 propertyEditorView->setObjectName(QStringLiteral("propertyEditorView"));
90 propertyEditorView->setAutomaticDocumentUpdate(_GetParam()->GetBool("AutoTransactionView", false));
91 tabs->addTab(propertyEditorView, tr("View"));
92
93 propertyEditorData = new Gui::PropertyEditor::PropertyEditor();
94 propertyEditorData->setObjectName(QStringLiteral("propertyEditorData"));
95 propertyEditorData->setAutomaticDocumentUpdate(_GetParam()->GetBool("AutoTransactionData", true));
96 tabs->addTab(propertyEditorData, tr("Data"));
97
98 int preferredTab = _GetParam()->GetInt("LastTabIndex", 1);
99
100 if (preferredTab > 0 && preferredTab < tabs->count()) {
101 tabs->setCurrentIndex(preferredTab);
102 }
103
104 // connect after adding all tabs, so adding doesn't thrash the parameter
105 connect(tabs, &QTabWidget::currentChanged, this, &PropertyView::tabChanged);
106
107 // NOLINTBEGIN
108 this->connectPropData = App::GetApplication().signalChangedObject.connect(
109 std::bind(&PropertyView::slotChangePropertyData, this, sp::_2)
110 );
111 this->connectPropView = Gui::Application::Instance->signalChangedObject.connect(
112 std::bind(&PropertyView::slotChangePropertyView, this, sp::_1, sp::_2)
113 );
114 this->connectPropAppend = App::GetApplication().signalAppendDynamicProperty.connect(
115 std::bind(&PropertyView::slotAppendDynamicProperty, this, sp::_1)
116 );
117 this->connectPropRemove = App::GetApplication().signalRemoveDynamicProperty.connect(
118 std::bind(&PropertyView::slotRemoveDynamicProperty, this, sp::_1)
119 );
120 this->connectPropRename = App::GetApplication().signalRenameDynamicProperty.connect(
121 std::bind(&PropertyView::slotRenameDynamicProperty, this, sp::_1, sp::_2)
122 );
123 this->connectPropChange = App::GetApplication().signalChangePropertyEditor.connect(
124 std::bind(&PropertyView::slotChangePropertyEditor, this, sp::_1, sp::_2)
125 );
126 this->connectUndoDocument = App::GetApplication().signalUndoDocument.connect(
127 std::bind(&PropertyView::slotRollback, this)
128 );
129 this->connectRedoDocument = App::GetApplication().signalRedoDocument.connect(
130 std::bind(&PropertyView::slotRollback, this)
131 );
132 this->connectActiveDoc = Application::Instance->signalActiveDocument.connect(
133 std::bind(&PropertyView::slotActiveDocument, this, sp::_1)
134 );
135 this->connectDelDocument = Application::Instance->signalDeleteDocument.connect(
136 std::bind(&PropertyView::slotDeleteDocument, this, sp::_1)
137 );
138 this->connectDelViewObject = Application::Instance->signalDeletedObject.connect(
139 std::bind(&PropertyView::slotDeletedViewObject, this, sp::_1)
140 );
141 this->connectDelObject = App::GetApplication().signalDeletedObject.connect(
142 std::bind(&PropertyView::slotDeletedObject, this, sp::_1)
143 );
144 this->connectChangedDocument = App::GetApplication().signalChangedDocument.connect(
145 std::bind(&PropertyView::slotChangePropertyData, this, sp::_2)
146 );
147 // NOLINTEND
148 }
149
150 PropertyView::~PropertyView()
151 {
152 this->connectPropData.disconnect();
153 this->connectPropView.disconnect();
154 this->connectPropAppend.disconnect();
155 this->connectPropRemove.disconnect();
156 this->connectPropChange.disconnect();
157 this->connectUndoDocument.disconnect();
158 this->connectRedoDocument.disconnect();
159 this->connectActiveDoc.disconnect();
160 this->connectDelDocument.disconnect();
161 this->connectDelObject.disconnect();
162 this->connectDelViewObject.disconnect();
163 this->connectChangedDocument.disconnect();
164 }
165
166 static bool _ShowAll;
167
168 bool PropertyView::showAll()
169 {
170 return _ShowAll;
171 }
172
173 void PropertyView::setShowAll(bool enable)
174 {
175 if (_ShowAll != enable) {
176 _ShowAll = enable;
177 const auto views = getMainWindow()->findChildren<PropertyView*>();
178 for (auto view : views) {
179 if (view->isVisible()) {
180 view->propertyEditorData->buildUp();
181 view->propertyEditorView->buildUp();
182 view->onTimer();
183 }
184 }
185 }
186 }
187
188 void PropertyView::hideEvent(QHideEvent* ev)
189 {
190 this->timer->stop();
191 this->detachSelection();
192 // clear the properties before hiding.
193 propertyEditorData->buildUp();
194 propertyEditorView->buildUp();
195 clearPropertyItemSelection();
196 QWidget::hideEvent(ev);
197 }
198
199 void PropertyView::showEvent(QShowEvent* ev)
200 {
201 this->attachSelection();
202 this->timer->start(ViewParams::instance()->getPropertyViewTimer());
203 QWidget::showEvent(ev);
204 }
205
206 void PropertyView::clearPropertyItemSelection()
207 {
208 QModelIndex index;
209 propertyEditorData->clearSelection();
210 propertyEditorData->setCurrentIndex(index);
211 propertyEditorView->clearSelection();
212 propertyEditorView->setCurrentIndex(index);
213 }
214
215 void PropertyView::slotRollback()
216 {
217 // PropertyItemDelegate will setup application active transaction on
218 // entering edit mode, and close active transaction when exit editing. But,
219 // when the user clicks undo/redo button while editing some property, the
220 // current active transaction will be closed by design, which cause further
221 // editing to be not recorded. Hence, we force unselect any property item on
222 // undo/redo
223 clearPropertyItemSelection();
224 }
225
226 void PropertyView::slotChangePropertyData(const App::Property& prop)
227 {
228 if (propertyEditorData->propOwners.contains(prop.getContainer())) {
229 propertyEditorData->updateProperty(prop);
230 timer->start(ViewParams::instance()->getPropertyViewTimer());
231 }
232 }
233
234 void PropertyView::slotChangePropertyView(const Gui::ViewProvider&, const App::Property& prop)
235 {
236 if (propertyEditorView->propOwners.contains(prop.getContainer())) {
237 propertyEditorView->updateProperty(prop);
238 timer->start(ViewParams::instance()->getPropertyViewTimer());
239 }
240 }
241
242 bool PropertyView::isPropertyHidden(const App::Property* prop)
243 {
244 return prop && !showAll()
245 && ((prop->getType() & App::Prop_Hidden) || prop->testStatus(App::Property::Hidden));
246 }
247
248 void PropertyView::slotAppendDynamicProperty(const App::Property& prop)
249 {
250 if (isPropertyHidden(&prop)) {
251 return;
252 }
253
254 App::PropertyContainer* parent = prop.getContainer();
255 if (propertyEditorData->propOwners.contains(parent)) {
256 propertyEditorData->blockCollapseAll();
257 timer->start(ViewParams::instance()->getPropertyViewTimer());
258 }
259 if (propertyEditorView->propOwners.contains(parent)) {
260 propertyEditorView->blockCollapseAll();
261 timer->start(ViewParams::instance()->getPropertyViewTimer());
262 }
263 }
264
265 void PropertyView::slotRemoveDynamicProperty(const App::Property& prop)
266 {
267 App::PropertyContainer* parent = prop.getContainer();
268 if (propertyEditorData->propOwners.contains(parent)) {
269 propertyEditorData->removeProperty(prop);
270 }
271 else if (propertyEditorView->propOwners.contains(parent)) {
272 propertyEditorView->removeProperty(prop);
273 }
274 else {
275 return;
276 }
277 timer->start(ViewParams::instance()->getPropertyViewTimer());
278 }
279
280 void PropertyView::slotRenameDynamicProperty(const App::Property& prop, const char* /*oldName*/)
281 {
282 App::PropertyContainer* parent = prop.getContainer();
283 if (propertyEditorData->propOwners.contains(parent)) {
284 propertyEditorData->renameProperty(prop);
285 }
286 else if (propertyEditorView->propOwners.contains(parent)) {
287 propertyEditorView->renameProperty(prop);
288 }
289 else {
290 return;
291 }
292 clearPropertyItemSelection();
293 timer->start(ViewParams::instance()->getPropertyViewTimer());
294 }
295
296 void PropertyView::slotChangePropertyEditor(const App::Document&, const App::Property& prop)
297 {
298 App::PropertyContainer* parent = prop.getContainer();
299 if (propertyEditorData->propOwners.contains(parent)
300 || propertyEditorView->propOwners.contains(parent)) {
301 timer->start(ViewParams::instance()->getPropertyViewTimer());
302 }
303 }
304
305 void PropertyView::slotDeleteDocument(const Gui::Document& doc)
306 {
307 if (propertyEditorData->propOwners.contains(doc.getDocument())) {
308 propertyEditorView->buildUp();
309 propertyEditorData->buildUp();
310 clearPropertyItemSelection();
311 timer->start(ViewParams::instance()->getPropertyViewTimer());
312 }
313 }
314
315 void PropertyView::slotDeletedViewObject(const Gui::ViewProvider& vp)
316 {
317 if (propertyEditorView->propOwners.contains(&vp)) {
318 propertyEditorView->buildUp();
319 propertyEditorData->buildUp();
320 clearPropertyItemSelection();
321 timer->start(ViewParams::instance()->getPropertyViewTimer());
322 }
323 }
324
325 void PropertyView::slotDeletedObject(const App::DocumentObject& obj)
326 {
327 if (propertyEditorData->propOwners.contains(&obj)) {
328 propertyEditorView->buildUp();
329 propertyEditorData->buildUp();
330 clearPropertyItemSelection();
331 timer->start(ViewParams::instance()->getPropertyViewTimer());
332 }
333 }
334
335 void PropertyView::slotActiveDocument(const Gui::Document& doc)
336 {
337 checkEnable(doc.getDocument()->getName());
338 }
339
340 void PropertyView::checkEnable(const char* doc)
341 {
342 if (ViewParams::instance()->getEnablePropertyViewForInactiveDocument()) {
343 setEnabled(true);
344 return;
345 }
346 // check if at least one selected object is part of the active document
347 setEnabled(!Selection().hasSelection() || Selection().hasSelection(doc, ResolveMode::NoResolve));
348 }
349
350 struct PropertyView::PropInfo
351 {
352 std::string propName;
353 int propId;
354 std::vector<App::Property*> propList;
355 };
356
357 struct PropertyView::PropFind
358 {
359 const PropInfo& item;
360 explicit PropFind(const PropInfo& item)
361 : item(item)
362 {}
363 bool operator()(const PropInfo& elem) const
364 {
365 return (elem.propId == item.propId) && (elem.propName == item.propName);
366 }
367 };
368
369 void PropertyView::onSelectionChanged(const SelectionChanges& msg)
370 {
371 if (msg.Type != SelectionChanges::AddSelection && msg.Type != SelectionChanges::RmvSelection
372 && msg.Type != SelectionChanges::SetSelection && msg.Type != SelectionChanges::ClrSelection) {
373 return;
374 }
375
376 // clear the properties.
377 timer->start(ViewParams::instance()->getPropertyViewTimer());
378 }
379
380 void PropertyView::onTimer()
381 {
382 // See https://forum.freecad.org/viewtopic.php?f=8&t=72526
383 if (this->updating) {
384 Base::Console().log("Ignore recursive call of PropertyView::onTimer()\n");
385 return;
386 }
387 Base::StateLocker guard(this->updating);
388
389 timer->stop();
390
391 if (!this->isSelectionAttached()) {
392 propertyEditorData->buildUp();
393 propertyEditorView->buildUp();
394 clearPropertyItemSelection();
395 return;
396 }
397
398 if (!Gui::Selection().hasSelection()) {
399 auto gdoc = TreeWidget::selectedDocument();
400 if (!gdoc || !gdoc->getDocument()) {
401 propertyEditorData->buildUp();
402 propertyEditorView->buildUp();
403 clearPropertyItemSelection();
404 return;
405 }
406
407 PropertyModel::PropertyList docProps;
408
409 auto doc = gdoc->getDocument();
410 std::map<std::string, App::Property*> props;
411 doc->getPropertyMap(props);
412 for (auto& v : props) {
413 docProps.emplace_back(v.first, std::vector<App::Property*>(1, v.second));
414 }
415 propertyEditorData->buildUp(std::move(docProps));
416 tabs->setCurrentIndex(1);
417 return;
418 }
419
420 std::set<App::DocumentObject*> objSet;
421
422 // group the properties by <name,id>
423 std::vector<PropInfo> propDataMap;
424 std::vector<PropInfo> propViewMap;
425 bool checkLink = true;
426 ViewProviderDocumentObject* vpLast = nullptr;
427 auto sels = Gui::Selection().getSelectionEx("*");
428 for (auto& sel : sels) {
429 App::DocumentObject* ob = sel.getObject();
430 if (!ob) {
431 continue;
432 }
433
434 // Do not process an object more than once
435 if (!objSet.insert(ob).second) {
436 continue;
437 }
438
439 std::vector<App::Property*> dataList;
440 std::map<std::string, App::Property*> viewList;
441
442 auto vp = Application::Instance->getViewProvider(ob);
443 if (!vp) {
444 checkLink = false;
445 ob->getPropertyList(dataList);
446 continue;
447 }
448
449 if (vp->isDerivedFrom<ViewProviderDocumentObject>()) {
450 auto cvp = static_cast<ViewProviderDocumentObject*>(vp);
451 if (vpLast && cvp != vpLast) {
452 checkLink = false;
453 }
454 vpLast = cvp;
455 }
456
457 ob->getPropertyList(dataList);
458
459 // get the properties as map here because it doesn't matter to have them sorted alphabetically
460 vp->getPropertyMap(viewList);
461
462 // store the properties with <name,id> as key in a map
463 {
464 for (auto prop : dataList) {
465 if (isPropertyHidden(prop)) {
466 continue;
467 }
468
469 PropInfo nameType;
470 nameType.propName = prop->getName();
471 nameType.propId = prop->getTypeId().getKey();
472
473 auto pi = std::find_if(propDataMap.begin(), propDataMap.end(), PropFind(nameType));
474 if (pi != propDataMap.end()) {
475 pi->propList.push_back(prop);
476 }
477 else {
478 nameType.propList.push_back(prop);
479 propDataMap.push_back(nameType);
480 }
481 }
482 }
483 // the same for the view properties
484 {
485 std::map<std::string, App::Property*>::iterator pt;
486 for (pt = viewList.begin(); pt != viewList.end(); ++pt) {
487 if (isPropertyHidden(pt->second)) {
488 continue;
489 }
490
491 PropInfo nameType;
492 nameType.propName = pt->first;
493 nameType.propId = pt->second->getTypeId().getKey();
494
495 auto pi = std::find_if(propViewMap.begin(), propViewMap.end(), PropFind(nameType));
496 if (pi != propViewMap.end()) {
497 pi->propList.push_back(pt->second);
498 }
499 else {
500 nameType.propList.push_back(pt->second);
501 propViewMap.push_back(nameType);
502 }
503 }
504 }
505 }
506
507 // the property must be part of each selected object, i.e. the number
508 // of selected objects is equal to the number of properties with same
509 // name and id
510 std::vector<PropInfo>::const_iterator it;
511 PropertyModel::PropertyList dataProps;
512 std::map<std::string, std::vector<App::Property*>> dataPropsMap;
513 PropertyModel::PropertyList viewProps;
514
515 if (checkLink && vpLast) {
516 // In case the only selected object is a link, insert the link's own
517 // property before the linked object
518 App::DocumentObject* obj = vpLast->getObject();
519 auto linked = obj;
520 if (obj && obj->canLinkProperties() && (linked = obj->getLinkedObject(true)) != obj
521 && linked) {
522 std::vector<App::Property*> dataList;
523 std::map<std::string, App::Property*> propMap;
524 obj->getPropertyMap(propMap);
525 linked->getPropertyList(dataList);
526 for (auto prop : dataList) {
527 if (isPropertyHidden(prop)) {
528 continue;
529 }
530 std::string name(prop->getName());
531 auto it = propMap.find(name);
532 if (it != propMap.end() && !isPropertyHidden(it->second)) {
533 continue;
534 }
535 std::vector<App::Property*> items(1, prop);
536 if (prop->testStatus(App::Property::PropDynamic)) {
537 dataPropsMap.emplace(name + "*", std::move(items));
538 }
539 else {
540 dataProps.emplace_back(name + "*", std::move(items));
541 }
542 }
543 auto vpLinked = Application::Instance->getViewProvider(linked);
544 if (vpLinked) {
545 propMap.clear();
546 vpLast->getPropertyMap(propMap);
547 dataList.clear();
548 vpLinked->getPropertyList(dataList);
549 for (auto prop : dataList) {
550 if (isPropertyHidden(prop)) {
551 continue;
552 }
553 std::string name(prop->getName());
554 auto it = propMap.find(name);
555 if (it != propMap.end() && !isPropertyHidden(it->second)) {
556 continue;
557 }
558 std::vector<App::Property*> items(1, prop);
559 viewProps.emplace_back(name + "*", std::move(items));
560 }
561 }
562 }
563 }
564
565 for (auto& v : dataPropsMap) {
566 dataProps.emplace_back(v.first, std::move(v.second));
567 }
568
569 dataPropsMap.clear();
570
571 for (it = propDataMap.begin(); it != propDataMap.end(); ++it) {
572 if (it->propList.size() == sels.size()) {
573 if (it->propList[0]->testStatus(App::Property::PropDynamic)) {
574 dataPropsMap.emplace(it->propName, std::move(it->propList));
575 }
576 else {
577 dataProps.emplace_back(it->propName, std::move(it->propList));
578 }
579 }
580 }
581
582 for (auto& v : dataPropsMap) {
583 dataProps.emplace_back(v.first, std::move(v.second));
584 }
585
586 propertyEditorData->buildUp(std::move(dataProps), true);
587
588 for (it = propViewMap.begin(); it != propViewMap.end(); ++it) {
589 if (it->propList.size() == sels.size()) {
590 viewProps.emplace_back(it->propName, std::move(it->propList));
591 }
592 }
593
594 propertyEditorView->buildUp(std::move(viewProps));
595
596 // make sure the editors are enabled/disabled properly
597 checkEnable();
598 }
599
600 void PropertyView::tabChanged(int index)
601 {
602 _GetParam()->SetInt("LastTabIndex", index);
603 }
604
605 void PropertyView::changeEvent(QEvent* e)
606 {
607 if (e->type() == QEvent::LanguageChange) {
608 tabs->setTabText(0, tr("View"));
609 tabs->setTabText(1, tr("Data"));
610 }
611
612 QWidget::changeEvent(e);
613 }
614
615 /* TRANSLATOR Gui::DockWnd::PropertyDockView */
616
617 PropertyDockView::PropertyDockView(Gui::Document* pcDocument, QWidget* parent)
618 : DockWindow(pcDocument, parent)
619 {
620 setWindowTitle(tr("Property View"));
621
622 auto view = new PropertyView(this);
623 auto pLayout = new QGridLayout(this);
624 pLayout->setSpacing(0);
625 pLayout->setContentsMargins(0, 0, 0, 0);
626 pLayout->addWidget(view, 0, 0);
627
628 resize(200, 400);
629 }
630
631 PropertyDockView::~PropertyDockView() = default;
632
633 #include "moc_PropertyView.cpp"