Debian Codegraph @

src/Gui/Tree.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 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 #ifndef GUI_TREE_H
25 #define GUI_TREE_H
26
27 #include <unordered_map>
28 #include <QElapsedTimer>
29 #include <QStyledItemDelegate>
30 #include <QTreeWidget>
31
32 #include <App/Application.h>
33 #include <App/DocumentObject.h>
34 #include <Base/Parameter.h>
35 #include <Base/Persistence.h>
36 #include <Gui/DockWindow.h>
37 #include <Gui/Selection/Selection.h>
38 #include <Gui/TreeItemMode.h>
39
40 class QLineEdit;
41
42 namespace Gui
43 {
44
45 class TreeParams;
46 class ViewProviderDocumentObject;
47 class DocumentObjectItem;
48 class DocumentObjectData;
49 using DocumentObjectDataPtr = std::shared_ptr<DocumentObjectData>;
50 class TreeWidgetItemDelegate;
51
52 class DocumentItem;
53 class Command;
54
55 GuiExport bool isTreeViewDragging();
56
57 /** Tree view that allows drag & drop of document objects.
58 * @author Werner Mayer
59 */
60 class TreeWidget: public QTreeWidget, public SelectionObserver
61 {
62 Q_OBJECT
63
64 public:
65 explicit TreeWidget(const char* name, QWidget* parent = nullptr);
66 ~TreeWidget() override;
67
68 static void setupResizableColumn(TreeWidget* tree = nullptr);
69 static void scrollItemToTop();
70 void selectAllInstances(const ViewProviderDocumentObject& vpd);
71 void selectLinkedObject(App::DocumentObject* linked);
72 void selectAllLinks(App::DocumentObject* obj);
73 void expandSelectedItems(TreeItemMode mode);
74 static int iconSize();
75
76 int iconHeight() const;
77 void setIconHeight(int height);
78
79 int itemSpacing() const;
80 void setItemSpacing(int);
81
82 bool eventFilter(QObject*, QEvent* ev) override;
83
84 struct SelInfo
85 {
86 App::DocumentObject* topParent;
87 std::string subname;
88 ViewProviderDocumentObject* parentVp;
89 ViewProviderDocumentObject* vp;
90 };
91 /* Return a list of selected object of a give document and their parent
92 *
93 * This function can return the non-group parent of the selected object,
94 * which Gui::Selection() cannot provide.
95 */
96 static std::vector<SelInfo> getSelection(App::Document* doc = nullptr);
97 static std::vector<Document*> getSelectedDocuments();
98
99 static TreeWidget* instance();
100
101 static const int DocumentType;
102 static const int ObjectType;
103
104 void markItem(const App::DocumentObject* Obj, bool mark);
105 void syncView(ViewProviderDocumentObject* vp);
106
107 void selectAll() override;
108
109 const char* getTreeName() const;
110
111 static void updateStatus(bool delay = true);
112
113 static bool isObjectShowable(App::DocumentObject* obj);
114
115 // Check if obj can be considered as a top level object
116 static void checkTopParent(App::DocumentObject*& obj, std::string& subname);
117
118 DocumentItem* getDocumentItem(const Gui::Document*) const;
119
120 static Gui::Document* selectedDocument();
121
122 void startDragging();
123
124 void resetItemSearch();
125 void startItemSearch(QLineEdit*);
126 void itemSearch(const QString& text, bool select);
127
128 static void synchronizeSelectionCheckBoxes();
129 static void updateVisibilityIcons();
130
131 QList<QTreeWidgetItem*> childrenOfItem(const QTreeWidgetItem& item) const;
132
133 protected:
134 /// Observer message from the Selection
135 void onSelectionChanged(const SelectionChanges& msg) override;
136 void contextMenuEvent(QContextMenuEvent* e) override;
137 void drawRow(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const override;
138 /** @name Drag and drop */
139 //@{
140 void startDrag(Qt::DropActions supportedActions) override;
141 bool dropMimeData(
142 QTreeWidgetItem* parent,
143 int index,
144 const QMimeData* data,
145 Qt::DropAction action
146 ) override;
147 Qt::DropActions supportedDropActions() const override;
148 void dragEnterEvent(QDragEnterEvent* event) override;
149 void dragLeaveEvent(QDragLeaveEvent* event) override;
150 void dragMoveEvent(QDragMoveEvent* event) override;
151 void dropEvent(QDropEvent* event) override;
152
153 private:
154 struct TargetItemInfo
155 {
156 QTreeWidgetItem* targetItem = nullptr; // target may be the parent of underMouse
157 QTreeWidgetItem* underMouseItem = nullptr;
158 App::Document* targetDoc = nullptr;
159 QPoint pos;
160 bool inBottomHalf = false;
161 bool inThresholdZone = false;
162 };
163 TargetItemInfo getTargetInfo(QEvent* ev);
164 using ObjectItemSubname = std::pair<DocumentObjectItem*, std::vector<std::string>>;
165 bool dropInObject(QDropEvent* event, TargetItemInfo& targetInfo, std::vector<ObjectItemSubname> items);
166 bool dropInDocument(
167 QDropEvent* event,
168 TargetItemInfo& targetInfo,
169 std::vector<ObjectItemSubname> items
170 );
171 bool canDragFromParents(
172 DocumentObjectItem* parentItem,
173 App::DocumentObject* obj,
174 App::DocumentObject* target
175 );
176 void sortDroppedObjects(TargetItemInfo& targetInfo, std::vector<App::DocumentObject*> draggedObjects);
177 //@}
178
179 protected:
180 bool event(QEvent* e) override;
181 void keyPressEvent(QKeyEvent* event) override;
182 void mousePressEvent(QMouseEvent* event) override;
183 void mouseDoubleClickEvent(QMouseEvent* event) override;
184
185 void showEvent(QShowEvent* ev) override;
186 void hideEvent(QHideEvent* ev) override;
187 void leaveEvent(QEvent* event) override;
188
189 private:
190 void _updateStatus(bool delay = true);
191
192 protected Q_SLOTS:
193 void onCreateGroup();
194 void onRelabelObject();
195 void onActivateDocument(QAction*);
196 void onStartEditing();
197 void onFinishEditing();
198 void onSelectDependents();
199 void onSkipRecompute(bool on);
200 void onAllowPartialRecompute(bool on);
201 void onReloadDoc();
202 void onCloseDoc();
203 void onMarkRecompute();
204 void onRecomputeObject();
205 void onPreSelectTimer();
206 void onSelectTimer();
207 void onShowHidden();
208 void onToggleVisibilityInTree();
209 void onSearchObjects();
210 void onOpenFileLocation();
211
212 private Q_SLOTS:
213 void onItemSelectionChanged();
214 void onItemChanged(QTreeWidgetItem*, int);
215 void onItemEntered(QTreeWidgetItem* item);
216 void onItemCollapsed(QTreeWidgetItem* item);
217 void onItemExpanded(QTreeWidgetItem* item);
218 void onUpdateStatus();
219
220 Q_SIGNALS:
221 void emitSearchObjects();
222
223 private:
224 void slotNewDocument(const Gui::Document&, bool);
225 void slotDeleteDocument(const Gui::Document&);
226 void slotRenameDocument(const Gui::Document&);
227 void slotActiveDocument(const Gui::Document&);
228 void slotRelabelDocument(const Gui::Document&);
229 void slotShowHidden(const Gui::Document&);
230 void slotChangedViewObject(const Gui::ViewProvider&, const App::Property&);
231 void slotStartOpenDocument();
232 void slotFinishOpenDocument();
233 void _slotDeleteObject(const Gui::ViewProviderDocumentObject&, DocumentItem* deletingDoc);
234 void slotDeleteObject(const Gui::ViewProviderDocumentObject&);
235 void slotChangeObject(const Gui::ViewProviderDocumentObject&, const App::Property& prop);
236 void slotTouchedObject(const App::DocumentObject&);
237
238 void changeEvent(QEvent* e) override;
239 void setupText();
240
241 void updateChildren(
242 App::DocumentObject* obj,
243 const std::set<DocumentObjectDataPtr>& data,
244 bool output,
245 bool force
246 );
247
248 bool CheckForDependents();
249 void addDependentToSelection(App::Document* doc, App::DocumentObject* docObject);
250 static TreeWidget* getTreeForSelection();
251
252 private:
253 QAction* createGroupAction;
254 QAction* relabelObjectAction;
255 QAction* finishEditingAction;
256 QAction* selectDependentsAction;
257 QAction* skipRecomputeAction;
258 QAction* allowPartialRecomputeAction;
259 QAction* markRecomputeAction;
260 QAction* recomputeObjectAction;
261 QAction* showHiddenAction;
262 QAction* toggleVisibilityInTreeAction;
263 QAction* reloadDocAction;
264 QAction* closeDocAction;
265 QAction* searchObjectsAction;
266 QAction* openFileLocationAction;
267 Command* skipRecomputeCommand;
268 QTreeWidgetItem* contextItem;
269 App::DocumentObject* searchObject;
270 Gui::Document* searchDoc;
271 Gui::Document* searchContextDoc;
272 DocumentObjectItem* editingItem;
273 DocumentItem* currentDocItem;
274 QTreeWidgetItem* rootItem;
275 QTimer* statusTimer;
276 QTimer* selectTimer;
277 QTimer* preselectTimer;
278 QElapsedTimer preselectTime;
279 static std::unique_ptr<QPixmap> documentPixmap;
280 static std::unique_ptr<QPixmap> documentPartialPixmap;
281 std::unordered_map<const Gui::Document*, DocumentItem*> DocumentMap;
282 std::unordered_map<App::DocumentObject*, std::set<DocumentObjectDataPtr>> ObjectTable;
283
284 enum ChangedObjectStatus
285 {
286 CS_Output,
287 CS_Error,
288 };
289 std::unordered_map<App::DocumentObject*, std::bitset<32>> ChangedObjects;
290
291 std::unordered_map<std::string, std::vector<long>> NewObjects;
292
293 static std::set<TreeWidget*> Instances;
294
295 std::string myName; // for debugging purpose
296 int updateBlocked = 0;
297
298 friend class DocumentItem;
299 friend class DocumentObjectItem;
300 friend class TreeParams;
301 friend class TreeWidgetItemDelegate;
302
303 using Connection = boost::signals2::connection;
304 Connection connectNewDocument;
305 Connection connectDelDocument;
306 Connection connectRenDocument;
307 Connection connectActDocument;
308 Connection connectRelDocument;
309 Connection connectShowHidden;
310 Connection connectChangedViewObj;
311 };
312
313 /** The link between the tree and a document.
314 * Every document in the application gets its associated DocumentItem which controls
315 * the visibility and the functions of the document.
316 * \author Jürgen Riegel
317 */
318 class DocumentItem: public QTreeWidgetItem, public Base::Persistence
319 {
320 public:
321 DocumentItem(const Gui::Document* doc, QTreeWidgetItem* parent);
322 ~DocumentItem() override;
323
324 Gui::Document* document() const;
325 void clearSelection(DocumentObjectItem* exclude = nullptr);
326 void updateSelection(QTreeWidgetItem*, bool unselect = false);
327 void updateSelection();
328 void updateItemSelection(DocumentObjectItem*);
329
330 enum SelectionReason
331 {
332 SR_SELECT, // only select, no expansion
333 SR_EXPAND, // select and expand but respect ObjectStatus::NoAutoExpand
334 SR_FORCE_EXPAND, // select and force expansion
335 };
336 void selectItems(SelectionReason reason = SR_SELECT);
337
338 void testStatus();
339 void setData(int column, int role, const QVariant& value) override;
340 void populateItem(DocumentObjectItem* item, bool refresh = false, bool delayUpdate = true);
341 bool populateObject(App::DocumentObject* obj);
342 void sortObjectItems();
343 void selectAllInstances(const ViewProviderDocumentObject& vpd);
344 bool showItem(DocumentObjectItem* item, bool select, bool force = false);
345 void updateItemsVisibility(QTreeWidgetItem* item, bool show);
346 void updateLinks(const ViewProviderDocumentObject& view);
347 ViewProviderDocumentObject* getViewProvider(App::DocumentObject*);
348
349 bool showHidden() const;
350 void setShowHidden(bool show);
351
352 TreeWidget* getTree() const;
353 const char* getTreeName() const;
354
355 bool isObjectShowable(App::DocumentObject* obj);
356
357 unsigned int getMemSize() const override;
358 void Save(Base::Writer&) const override;
359 void Restore(Base::XMLReader&) override;
360
361 class ExpandInfo;
362 using ExpandInfoPtr = std::shared_ptr<ExpandInfo>;
363
364 protected:
365 /** Adds a view provider to the document item.
366 * If this view provider is already added nothing happens.
367 */
368 void slotNewObject(const Gui::ViewProviderDocumentObject&);
369 /** Removes a view provider from the document item.
370 * If this view provider is not added nothing happens.
371 */
372 void slotInEdit(const Gui::ViewProviderDocumentObject&);
373 void slotResetEdit(const Gui::ViewProviderDocumentObject&);
374 void slotHighlightObject(
375 const Gui::ViewProviderDocumentObject&,
376 const Gui::HighlightMode&,
377 bool,
378 const App::DocumentObject* parent,
379 const char* subname
380 );
381 void slotExpandObject(
382 const Gui::ViewProviderDocumentObject&,
383 const Gui::TreeItemMode&,
384 const App::DocumentObject* parent,
385 const char* subname
386 );
387 void slotScrollToObject(const Gui::ViewProviderDocumentObject&);
388 void slotRecomputed(const App::Document& doc, const std::vector<App::DocumentObject*>& objs);
389 void slotRecomputedObject(const App::DocumentObject&);
390
391 bool updateObject(const Gui::ViewProviderDocumentObject&, const App::Property& prop);
392
393 bool createNewItem(
394 const Gui::ViewProviderDocumentObject&,
395 QTreeWidgetItem* parent = nullptr,
396 int index = -1,
397 DocumentObjectDataPtr ptrs = DocumentObjectDataPtr()
398 );
399
400 int findRootIndex(App::DocumentObject* childObj);
401
402 DocumentObjectItem* findItemByObject(
403 bool sync,
404 App::DocumentObject* obj,
405 const char* subname,
406 bool select = false
407 );
408
409 DocumentObjectItem* findItem(
410 bool sync,
411 DocumentObjectItem* item,
412 const char* subname,
413 bool select = true
414 );
415 DocumentObjectItem* findItem(App::DocumentObject* obj, const std::string& subname) const;
416
417 App::DocumentObject* getTopParent(App::DocumentObject* obj, std::string& subname);
418
419 using ViewParentMap
420 = std::unordered_map<const ViewProvider*, std::vector<ViewProviderDocumentObject*>>;
421 void populateParents(const ViewProvider* vp, ViewParentMap&);
422
423 private:
424 const char* treeName; // for debugging purpose
425 Gui::Document* pDocument;
426 std::unordered_map<App::DocumentObject*, DocumentObjectDataPtr> ObjectMap;
427 std::unordered_map<App::DocumentObject*, std::set<App::DocumentObject*>> _ParentMap;
428 std::vector<App::DocumentObject*> PopulateObjects;
429
430 ExpandInfoPtr _ExpandInfo;
431 void restoreItemExpansion(const ExpandInfoPtr&, DocumentObjectItem*);
432
433 using Connection = boost::signals2::connection;
434 Connection connectNewObject;
435 Connection connectDelObject;
436 Connection connectChgObject;
437 Connection connectTouchedObject;
438 Connection connectEdtObject;
439 Connection connectResObject;
440 Connection connectHltObject;
441 Connection connectExpObject;
442 Connection connectScrObject;
443 Connection connectRecomputed;
444 Connection connectRecomputedObj;
445
446 friend class TreeWidget;
447 friend class DocumentObjectData;
448 friend class DocumentObjectItem;
449 };
450
451 /** The link between the tree and a document object.
452 * Every object in the document gets its associated DocumentObjectItem which controls
453 * the visibility and the functions of the object.
454 * @author Werner Mayer
455 */
456 class DocumentObjectItem: public QTreeWidgetItem
457 {
458 public:
459 DocumentObjectItem(DocumentItem* ownerDocItem, DocumentObjectDataPtr data);
460 ~DocumentObjectItem() override;
461
462 Gui::ViewProviderDocumentObject* object() const;
463 void testStatus(bool resetStatus, QIcon& icon1, QIcon& icon2);
464 void testStatus(bool resetStatus);
465 void displayStatusInfo();
466 void setExpandedStatus(bool);
467 void setData(int column, int role, const QVariant& value) override;
468 bool isChildOfItem(DocumentObjectItem*);
469
470 void restoreBackground();
471
472 // Get the parent document (where the object is stored) of this item
473 DocumentItem* getParentDocument() const;
474 // Get the owner document (where the object is displayed, either stored or
475 // linked in) of this object
476 DocumentItem* getOwnerDocument() const;
477
478 // check if a new item is required at root
479 bool requiredAtRoot(bool excludeSelf = true) const;
480
481 // return the owner, and full qualified subname
482 App::DocumentObject* getFullSubName(
483 std::ostringstream& str,
484 DocumentObjectItem* parent = nullptr
485 ) const;
486
487 // return the immediate descendent of the common ancestor of this item and
488 // 'cousin'.
489 App::DocumentObject* getRelativeParent(
490 std::ostringstream& str,
491 DocumentObjectItem* cousin,
492 App::DocumentObject** topParent = nullptr,
493 std::string* topSubname = nullptr
494 ) const;
495
496 // return the top most linked group owner's name, and subname. This method
497 // is necessary despite have getFullSubName above is because native geo group
498 // cannot handle selection with sub name. So only a linked group can have
499 // subname in selection
500 int getSubName(std::ostringstream& str, App::DocumentObject*& topParent) const;
501 const std::vector<std::string>& getSubNames() const
502 {
503 return mySubs;
504 }
505
506 void setHighlight(bool set, HighlightMode mode = HighlightMode::LightBlue);
507
508 const char* getName() const;
509 const char* getTreeName() const;
510
511 bool isLink() const;
512 bool isLinkFinal() const;
513 bool isParentLink() const;
514 int isGroup() const;
515 int isParentGroup() const;
516
517 DocumentObjectItem* getParentItem() const;
518 DocumentObjectItem* getNextSibling() const;
519 DocumentObjectItem* getPreviousSibling() const;
520 TreeWidget* getTree() const;
521
522 private:
523 void setCheckState(bool checked);
524 void getExpandedSnapshot(std::vector<bool>& snapshot) const;
525 void applyExpandedSnapshot(
526 const std::vector<bool>& snapshot,
527 std::vector<bool>::const_iterator& from
528 );
529
530 QBrush bgBrush;
531 DocumentItem* myOwner;
532 DocumentObjectDataPtr myData;
533 std::vector<std::string> mySubs;
534 using Connection = boost::signals2::connection;
535 int previousStatus;
536 int selected;
537 bool populated;
538
539 friend class TreeWidget;
540 friend class DocumentItem;
541 };
542
543 class TreePanel: public QWidget
544 {
545 Q_OBJECT
546
547 public:
548 explicit TreePanel(const char* name, QWidget* parent = nullptr);
549 ~TreePanel() override;
550
551 bool eventFilter(QObject* obj, QEvent* ev) override;
552
553 private Q_SLOTS:
554 void accept();
555 void showEditor();
556 void hideEditor();
557 void itemSearch(const QString& text);
558
559 private:
560 QLineEdit* searchBox;
561 TreeWidget* treeWidget;
562 };
563
564 /**
565 * The dock window containing the tree view.
566 * @author Werner Mayer
567 */
568 class TreeDockWidget: public Gui::DockWindow
569 {
570 Q_OBJECT
571
572 public:
573 explicit TreeDockWidget(Gui::Document* pcDocument, QWidget* parent = nullptr);
574 ~TreeDockWidget() override;
575 };
576
577 } // namespace Gui
578
579 #endif // GUI_TREE_H