OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
aTreeWidget.h
Go to the documentation of this file.
1/*
2 * File: aTreeWidget.h
3 * Package: akWidgets
4 *
5 * Created on: March 10, 2020
6 * Author: Alexander Kuester
7 * Copyright (c) 2022 Alexander Kuester
8 * This file is part of the uiCore component.
9 * This file is subject to the terms and conditions defined in
10 * file 'LICENSE', which is part of this source code package.
11 */
12
13#pragma once
14
15 // AK header
17#include <akCore/akCore.h>
18#include <akWidgets/aWidget.h>
19
20// Qt header
21#include <qobject.h> // Base class
22#include <qlayout.h> // layout for the VBoxLayout
23#include <qicon.h> // QIcon
24#include <qcolor.h> // QColor
25#include <qstring.h> // QString
26#include <qtreewidget.h> // base class
27
28// C++ header
29#include <map>
30#include <vector>
31#include <list>
32
33// Forward declaration
34class QTreeWidgetItem;
35class QKeyEvent;
36class QWidget;
37class QMouseEvent;
38
39namespace ak {
40
41 // Forward declaration
42 class aUidManager;
43 class aObjectManager;
44 class aSignalLinker;
45 class aTreeWidget;
46 class aTreeWidgetBase;
47 class aTreeWidgetItem;
48 class aLineEditWidget;
49
50 class UICORE_API_EXPORT aTreeWidget : public QObject, public aWidget {
51 Q_OBJECT
52 public:
58
60 virtual ~aTreeWidget();
61
63 virtual QWidget * widget(void) override;
64
65 // ###########################################################################################################################################
66
67 // Data manipulation
68
71 ID add(
72 ID _parentId = -1,
73 const QString & _text = "",
74 textAlignment _textAlignment = alignLeft,
75 QIcon _icon = QIcon()
76 );
77
91 ID add(
92 const QString & _cmd,
93 char _delimiter = '|',
94 textAlignment _textAlignment = alignLeft,
95 const QIcon & _icon = QIcon()
96 );
97
99 void clear(bool _emitEvent = true);
100
104 void setItemEnabled(
105 ID _itemId,
106 bool _enabled = true
107 );
108
112 void setIsReadOnly(
113 bool _readOnly = true
114 );
115
120 void setItemSelected(
121 ID _itemId,
122 bool _selected = true
123 );
124
128 void setItemVisible(
129 ID _itemId,
130 bool _visible
131 );
132
136 void setItemText(
137 ID _itemId,
138 const QString & _text
139 );
140
145 void setSingleItemSelected(
146 ID _itemId,
147 bool _selected
148 );
149
154 void toggleItemSelection(
155 ID _itemId
156 );
157
160 void deselectAllItems(
161 bool _emitEvent
162 );
163
165 void setEnabled(
166 bool _enabled = true
167 );
168
170 void setVisible(
171 bool _visible = true
172 );
173
177 void setItemIcon(
178 ID _itemId,
179 const QIcon & _icon
180 );
181
184 void setSortingEnabled(
185 bool _enabled
186 );
187
188 // ###########################################################################################################################################
189
190 // Filter
191
194 void setFilterVisible(
195 bool _visible
196 );
197
200 void applyCurrentFilter(void);
201
205 void setFilterCaseSensitive(
206 bool _caseSensitive,
207 bool _refresh = true
208 );
209
212 void setFilterRefreshOnChange(
213 bool _refreshOnChange
214 );
215
218 void setMultiSelectionEnabled(
219 bool _multiSelection
220 );
221
224 void setAutoSelectAndDeselectChildrenEnabled(
225 bool _enabled
226 );
227
229 void expandAllItems(void);
230
231 void expandItem(
232 ak::ID _itemID,
233 bool _expanded
234 );
235
236 bool isItemExpanded(
237 ak::ID _itemID
238 );
239
241 void collapseAllItems(void);
242
245 void deleteItem(
246 ID _itemID,
247 bool _supressSelectionChangedEvent = false
248 );
249
252 void deleteItems(
253 const std::vector<ID> & _itemIDs,
254 bool _supressSelectionChangedEvent = false
255 );
256
260 void setItemsAreEditable(
261 bool _editable = true,
262 bool _applyToAll = true
263 );
264
268 void setItemIsEditable(
269 ID _itemID,
270 bool _editable
271 );
272
276 void setItemIsEditable(
277 const std::vector<ID> & _itemIDs,
278 bool _editable
279 );
280
284 void setItemSelectChildren(
285 ID _itemID,
286 bool _selectChildren
287 );
288
290 void setChildItemsVisibleWhenApplyingFilter(bool _visible) { m_displayChildsOnFilter = _visible; }
291
292 // ###########################################################################################################################################
293
294 // Information gathering
295
297 std::vector<ID> selectedItems(void);
298
301 std::vector<QString> getItemPath(
302 ID _itemId
303 );
304
308 QString getItemPathString(
309 ID _itemId,
310 char _delimiter = '|'
311 );
312
316 ID getItemID(
317 const QString & _itemPath,
318 char _delimiter = '|'
319 );
320
323 QString getItemText(
324 ID _itemId
325 );
326
329 aTreeWidgetItem * item(ID _itemID);
330
331 aTreeWidgetItem* itemFromPath(const QString& _itemPath, char _delimiter = '|');
332
334 bool enabled() const;
335
337 int itemCount(void) const;
338
340 bool isSortingEnabled(void) const;
341
342 bool isReadOnly(void) const { return m_isReadOnly; }
343
344 bool getAutoSelectAndDeselectChildrenEnabled(void) { return m_selectAndDeselectChildren; }
345 bool getMultiSelectionEnabled(void);
346
347 aTreeWidgetBase * treeWidget(void) { return m_tree; }
348 aLineEditWidget * filterWidget(void) { return m_filter; }
349
350 // ###########################################################################################################################################
351
352 // Events
353
359 /*void raiseItemEvent(
360 ID _itemId,
361 eventType _eventType,
362 int _info2 = 0
363 );
364 */
365
368 void selectionChangedEvent(
369 bool _emitEvent = true
370 );
371
372 aTreeWidgetItem * itemAt(const QPoint& _pos);
373
374 Q_SIGNALS:
375 void keyPressed(QKeyEvent *);
376 void keyReleased(QKeyEvent *);
377 void cleared(void);
378 void focusLost(void);
380 void itemActivated(QTreeWidgetItem *, int);
381 void itemChanged(QTreeWidgetItem *, int);
382 void itemClicked(QTreeWidgetItem *, int);
383 void itemCollapsed(QTreeWidgetItem *);
384 void itemDoubleClicked(QTreeWidgetItem *, int);
385 void itemFocused(QTreeWidgetItem *);
386 void itemExpanded(QTreeWidgetItem *);
387 void itemTextChanged(QTreeWidgetItem *, int);
388 void itemLocationChanged(QTreeWidgetItem *, int);
389 void itemsMoved(const QList<ID>& _itemIds, const QList<ID>& _oldParentIds, const QList<ID>& _newParentIds);
390 void customContextMenuRequested(const QPoint& _pos);
391
392 public Q_SLOTS:
393
394 void slotTreeKeyPressed(QKeyEvent * _event);
395 void slotTreeKeyReleased(QKeyEvent * _event);
396 void slotTreeItemActivated(QTreeWidgetItem * _item, int _column);
397 void slotTreeItemChanged(QTreeWidgetItem * _item, int _column);
398 void slotTreeItemClicked(QTreeWidgetItem * _item, int _column);
399 void slotTreeItemCollapsed(QTreeWidgetItem * _item);
400 void slotTreeItemDoubleClicked(QTreeWidgetItem * _item, int _column);
401 void slotTreeItemExpanded(QTreeWidgetItem * _item);
402 void slotTreeSelectionChanged();
403 void slotTreeMouseMove(QMouseEvent *);
404 void slotTreeLeave(QEvent *);
405 void slotCustomContextMenuRequested(const QPoint& _pos);
406
408 void slotFilterTextChanged(void);
409
411 void slotFilterKeyPressed(QKeyEvent * _event);
412
413 private:
414
415 friend class aTreeWidgetBase;
416
423 aTreeWidgetItem * createItem(
424 const QString & _text = "",
425 textAlignment _textAlignment = alignLeft,
426 QIcon _icon = QIcon()
427 );
428
429 void refreshAfterItemsMoved(const QList<aTreeWidgetItem *>& _items, const QList<ID>& _itemIds,
430 const QList<ID>& _oldParentIds, const QList<ID>& _newParentIds);
431
433 void memFree(void);
434
437 void clearItem(
438 aTreeWidgetItem * _itm
439 );
440
441 aTreeWidgetBase * m_tree;
442 aLineEditWidget * m_filter;
443 QWidget * m_widget;
444 QVBoxLayout * m_layout;
445 bool m_filterCaseSensitive;
446 bool m_filterRefreshOnChange;
447 bool m_ignoreEvents;
448 bool m_isReadOnly;
449 bool m_displayChildsOnFilter;
450
451 ID m_focusedItem;
452
453 QString m_headerText;
454
455 ID m_currentId;
456
457 std::map<ID, aTreeWidgetItem *> m_items;
458
459 bool m_selectAndDeselectChildren;
460 bool m_itemsAreEditable;
461 };
462
463 // #########################################################################################################################################
464
465 // #########################################################################################################################################
466
467 // #########################################################################################################################################
468
470 class UICORE_API_EXPORT aTreeWidgetBase : public QTreeWidget, public aWidget {
471 Q_OBJECT
472 public:
474 aTreeWidgetBase(aTreeWidget * _ownerTree);
475
476 virtual ~aTreeWidgetBase();
477
478 // #######################################################################################################
479 // Event handling
480
482 virtual void keyPressEvent(QKeyEvent * _event) override;
483
485 virtual void keyReleaseEvent(QKeyEvent * _event) override;
486
488 virtual void mouseMoveEvent(QMouseEvent *event) override;
489
491 virtual void leaveEvent(QEvent *event) override;
492
493 virtual void dropEvent(QDropEvent * _event) override;
494
495 virtual void dragEnterEvent(QDragEnterEvent * _event) override;
496
497 virtual void dragLeaveEvent(QDragLeaveEvent * _event) override;
498
499 // #######################################################################################################
500
502 virtual QWidget * widget(void) override;
503
504 // ####################################################################################################################################
505
508 void AddTopLevelItem(
509 aTreeWidgetItem * _item
510 );
511
512 void itemWasMovedToTopLevel(aTreeWidgetItem * _item);
513
516 aTreeWidgetItem * topLevelItem(
517 const QString & _text
518 );
519
522 aTreeWidgetItem * topLevelItem(
523 ID _id
524 );
525
527 std::vector<QString> topLevelItemsText(void);
528
530 void Clear(void);
531
534 void removeTopLevelItem(
535 ID _id
536 );
537
538 // ####################################################################################################################################
539
541 static ID getItemId(
542 QTreeWidgetItem * _item
543 );
544
545 QList<aTreeWidgetItem *> selectedItemsRef(void) const;
546 void extendedItemSelectionInformation(const QList<aTreeWidgetItem *>& _selectedItems, QList<ID>& _selectedItemIds, QList<ID>& _itemParentIds) const;
547
548 Q_SIGNALS:
549 void keyPressed(QKeyEvent *);
550 void keyReleased(QKeyEvent *);
551 void mouseMove(QMouseEvent *);
552 void leave(QEvent *);
553
554 private:
555 aTreeWidget * m_ownerTree;
556 std::map<ID, aTreeWidgetItem *> m_topLevelItems;
557
559 aTreeWidgetBase& operator = (aTreeWidgetBase &) = delete;
560 };
561
562 // #########################################################################################################################################
563
564 // #########################################################################################################################################
565
566 // #########################################################################################################################################
567
568 class UICORE_API_EXPORT aTreeWidgetItem : public QTreeWidgetItem, public aObject {
569 public:
570
575 ID _newId,
576 aTreeWidgetItem * _parent = nullptr,
577 int _type = 0
578 );
579
585 aTreeWidgetBase * _view,
586 ID _newId,
587 aTreeWidgetItem * _parent = nullptr,
588 int _type = 0
589 );
590
592 virtual ~aTreeWidgetItem();
593
594 // ##############################################################################################
595
596 // Setter
597
600 void AddChild(
601 aTreeWidgetItem * _child
602 );
603
606 void setParentItem(
607 aTreeWidgetItem * _parent
608 );
609
612 void setChildsEnabled(
613 bool _enabled
614 );
615
618 void setChildsSelected(
619 bool _selected
620 );
621
624 void setChildsVisible(
625 bool _visible
626 );
627
629 void ensureTopLevelSelectionVisible();
630
633 void expandAllParents(
634 bool _expandThis
635 );
636
638 void collapse(void);
639
640 void setEditable(bool _editable = true);
641
642 void setLocked(bool _locked = true);
643
646 void setVisible(
647 bool _isVisible,
648 bool _expandParents = true
649 );
650
651 // ##############################################################################################
652
653 // Getter
654
657 aTreeWidgetItem * findChild(
658 ID _id
659 );
660
663 aTreeWidgetItem * findChild(
664 const QString & _text
665 );
666
670 ID getItemID(
671 const QStringList & _itemPath,
672 int _currentIndex
673 );
674
678 void eraseChild(
679 ID _id
680 );
681
683 bool hasChilds(void);
684
686 aTreeWidgetItem * popFirstChild(void);
687
689 const std::list<aTreeWidgetItem *> & childs(void);
690
692 const std::list<aTreeWidgetItem *> & allChilds(void);
693
695 const std::list<ID> & allChildsIDs(void);
696
698 int childCount(void) const;
699
701 ID id(void) const;
702
705 ID parentId(void) const;
706
708 void setStoredText(const QString & _text) { m_text = _text; }
709
711 QString storedText(void) const { return m_text; }
712
714 std::list<QString> getItemPath();
715
719 QString getItemPathString(
720 char _delimiter = '|'
721 );
722
723 bool isVisible(void) const { return m_isVisible; }
724
725 aTreeWidgetItem * parentItem(void) const { return m_parent; }
726
727 void setSelectChilds(bool flag) { m_selectChilds = flag; }
728 bool getSelectChilds(void) { return m_selectChilds; }
729
730 private:
731
732 void refreshEditableState(void);
733
734 aTreeWidgetItem * m_parent;
735 std::list<aTreeWidgetItem *> m_childs;
736 std::list<aTreeWidgetItem *> m_allChilds;
737 std::list<ID> m_allChildsIDs;
738 ID m_id;
739 QString m_text;
740 bool m_isEditable;
741 bool m_isLockedForEdit;
742 bool m_isVisible;
743 bool m_selectChilds;
744
745 aTreeWidgetItem() = delete;
747 aTreeWidgetItem & operator = (aTreeWidgetItem &) = delete;
748 };
749} // namespace ak
#define UICORE_API_EXPORT
Definition globalDataTypes.h:20
This class combines the functionallity of a QTextEdit and a ak::ui::core::aWidget.
Definition aLineEditWidget.h:30
This class is used to store the main information of any object used Information contained is the UID ...
Definition aObject.h:34
This class represents a tree.
Definition aTreeWidget.h:470
void mouseMove(QMouseEvent *)
void keyReleased(QKeyEvent *)
void keyPressed(QKeyEvent *)
void leave(QEvent *)
Definition aTreeWidget.h:50
void itemLocationChanged(QTreeWidgetItem *, int)
void itemClicked(QTreeWidgetItem *, int)
void itemCollapsed(QTreeWidgetItem *)
aLineEditWidget * filterWidget(void)
Definition aTreeWidget.h:348
void keyPressed(QKeyEvent *)
bool getAutoSelectAndDeselectChildrenEnabled(void)
Definition aTreeWidget.h:344
void itemTextChanged(QTreeWidgetItem *, int)
void itemChanged(QTreeWidgetItem *, int)
void setChildItemsVisibleWhenApplyingFilter(bool _visible)
If true all child of the matching item will be visible (not expanded)
Definition aTreeWidget.h:290
aTreeWidgetBase * treeWidget(void)
Definition aTreeWidget.h:347
void cleared(void)
void selectionChanged(void)
void customContextMenuRequested(const QPoint &_pos)
void itemActivated(QTreeWidgetItem *, int)
void itemsMoved(const QList< ID > &_itemIds, const QList< ID > &_oldParentIds, const QList< ID > &_newParentIds)
void itemFocused(QTreeWidgetItem *)
void itemExpanded(QTreeWidgetItem *)
bool isReadOnly(void) const
Definition aTreeWidget.h:342
void keyReleased(QKeyEvent *)
void focusLost(void)
void itemDoubleClicked(QTreeWidgetItem *, int)
Definition aTreeWidget.h:568
aTreeWidgetItem * parentItem(void) const
Definition aTreeWidget.h:725
QString storedText(void) const
Will return the stored text.
Definition aTreeWidget.h:711
bool getSelectChilds(void)
Definition aTreeWidget.h:728
bool isVisible(void) const
Definition aTreeWidget.h:723
void setStoredText(const QString &_text)
Will set the stored text, this value is only used to evaluate the changed event.
Definition aTreeWidget.h:708
void setSelectChilds(bool flag)
Definition aTreeWidget.h:727
This class provides a interface that represents a widget. Every class derived from this class must be...
Definition aWidget.h:27
Definition uiAPI.h:45
textAlignment
Describes the text alignment.
Definition akCore.h:210
@ alignLeft
Definition akCore.h:211
int ID
The ID datatype used for items.
Definition globalDataTypes.h:68