KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
tableviewbase.h
1/******************************************************************************************
2**
3** tableviewbase.h
4**
5** Moved from my Tesseract Engineering repo to open-source
6**
7** Author: Stephen Punak
8** Created: Fri Oct 27 09:12:34 2023
9**
10******************************************************************************************/
11#ifndef TABLEVIEWBASE_H
12#define TABLEVIEWBASE_H
13#include <Kanoop/entitymetadata.h>
14#include <Kanoop/utility/loggingbaseclass.h>
15
16#include <QTableView>
17#include <Kanoop/gui/libkanoopgui.h>
18
19class QStyledItemDelegate;
20class QSortFilterProxyModel;
22
23/**
24 * @brief QTableView subclass integrating AbstractItemModel with entity metadata helpers.
25 *
26 * TableViewBase wraps the source model in a QSortFilterProxyModel and provides helpers
27 * for accessing EntityMetadata at positions, navigating by UUID, managing row delegates,
28 * and restoring column/row header states.
29 */
30class LIBKANOOPGUI_EXPORT TableViewBase : public QTableView,
31 public LoggingBaseClass
32{
33 Q_OBJECT
34
35public:
36 /**
37 * @brief Construct with an optional parent widget.
38 * @param parent Optional QWidget parent
39 */
40 explicit TableViewBase(QWidget* parent = nullptr);
41
42 /** @brief Destructor. */
43 virtual ~TableViewBase();
44
45 /**
46 * @brief Set the model, wrapping it in an internal sort/filter proxy.
47 * @param model Model to display (should derive from AbstractItemModel)
48 */
49 virtual void setModel(QAbstractItemModel* model) override;
50
51 /**
52 * @brief Return the entity type of the item at a view position.
53 * @param pos View-local position
54 * @return Entity type integer, or -1 if no item at that position
55 */
56 int entityTypeAtPos(const QPoint& pos);
57
58 /**
59 * @brief Return the EntityMetadata of the currently selected item.
60 * @return EntityMetadata for the current selection
61 */
62 EntityMetadata currentMetadata() const;
63
64 /**
65 * @brief Return the EntityMetadata of the item at a view position.
66 * @param pos View-local position
67 * @return EntityMetadata at that position
68 */
69 EntityMetadata metadataAtPos(const QPoint& pos) const;
70
71 /**
72 * @brief Return the underlying AbstractItemModel (without the proxy).
73 * @return Pointer to the source model
74 */
75 AbstractItemModel* sourceModel() const { return _sourceModel; }
76
77 /**
78 * @brief Return the internal sort/filter proxy model.
79 * @return Pointer to the proxy model
80 */
81 QSortFilterProxyModel* proxyModel() const { return _proxyModel; }
82
83 /**
84 * @brief Delete the row identified by the given model index.
85 * @param index Model index of the row to delete
86 */
87 virtual void deleteRow(const QModelIndex& index);
88
89 /**
90 * @brief Emit entityAdded() for the given metadata (override to customise).
91 * @param metadata Metadata describing the entity to add
92 */
93 virtual void addRow(const EntityMetadata& metadata) { emit entityAdded(metadata); }
94
95 /**
96 * @brief Return the number of rows currently in the view.
97 * @return Row count
98 */
99 int rowCount() const;
100
101 /**
102 * @brief Return model indexes for all rows in the view.
103 * @return List of all row indexes (first column of each row)
104 */
105 QModelIndexList allRows() const;
106
107 /**
108 * @brief Return the first model index where the given role matches a value.
109 * @param needle Value to search for
110 * @param role Model role to compare
111 * @return First matching index, or invalid index if not found
112 */
113 QModelIndex findFirstMatch(const QVariant& needle, int role) const;
114
115 /**
116 * @brief Return the first index whose item UUID matches.
117 * @param uuid UUID to search for
118 * @return First matching index, or invalid index if not found
119 */
120 virtual QModelIndex firstIndexOfEntityUuid(const QUuid& uuid) const;
121
122 /**
123 * @brief Select the item with the given UUID and optionally scroll to it.
124 * @param uuid UUID of the item to select
125 * @param scrollHint How to scroll to make the item visible
126 */
127 void setCurrentUuid(const QUuid& uuid, ScrollHint scrollHint = EnsureVisible);
128
129 /**
130 * @brief Return whether the given model index is currently visible in the viewport.
131 * @param index Model index to check
132 * @return true if the index is within the visible area
133 */
134 bool isIndexVisible(const QModelIndex& index) const;
135
136 /** @brief Restore both horizontal and vertical header states from settings. */
138 /** @brief Restore horizontal header state (column widths/order) from settings. */
140 /** @brief Restore vertical header state (row heights) from settings. */
142
143 /**
144 * @brief Assign a custom item delegate to the column of the given header type.
145 * @param type Column header type identifier
146 * @param delegate Delegate to install
147 */
148 void setColumnDelegate(int type, QStyledItemDelegate* delegate);
149
150public slots:
151 /** @brief Remove all rows from the view model. */
152 void clear();
153
154private:
155 AbstractItemModel* _sourceModel;
156 QSortFilterProxyModel* _proxyModel;
157 QMap<int, QStyledItemDelegate*> _columnDelegates;
158
159 QAction* _actionColSettings = nullptr;
160 QAction* _actionHideCol = nullptr;
161 QAction* _actionAutoResizeCols = nullptr;
162 QAction* _actionResetCols = nullptr;
163
164 QPoint _contextMenuPoint;
165
166signals:
167 /** @brief Emitted when the horizontal header is resized. */
169 /** @brief Emitted when the vertical header is resized. */
171
172 /** @brief Emitted when the current selection changes. */
174 /**
175 * @brief Emitted when the current index changes.
176 * @param current The newly current index
177 * @param previous The previously current index
178 */
179 void currentIndexChanged(const QModelIndex& current, const QModelIndex& previous);
180
181 /** @brief Emitted when a row is to be added with the given metadata. */
182 void entityAdded(const EntityMetadata& metadata);
183 /** @brief Emitted when an entity row is deleted. */
184 void entityDeleted(const EntityMetadata& metadata);
185 /** @brief Emitted when an entity row is updated. */
186 void entityUpdated(const EntityMetadata& metadata);
187
188protected slots:
189 /** @brief Internal override forwarding current-index changes to currentIndexChanged(). */
190 virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous) override;
191
192private slots:
193 virtual void onHorizontalHeaderResized(int /*logicalIndex*/, int /*oldSize*/, int /*newSize*/);
194 virtual void onVerticalHeaderResized(int /*logicalIndex*/, int /*oldSize*/, int /*newSize*/);
195
196 void onHeaderContextMenuRequested(const QPoint& pos);
197 void onColumnSettingsClicked();
198 void onHideColumnClicked();
199 void onAutoResizeColumnsClicked();
200 void onResetColumnsClicked();
201};
202
203#endif // TABLEVIEWBASE_H
Extended QAbstractItemModel providing EntityMetadata-based item lookup and header management.
QTableView subclass integrating AbstractItemModel with entity metadata helpers.
virtual ~TableViewBase()
Destructor.
void currentIndexChanged(const QModelIndex &current, const QModelIndex &previous)
Emitted when the current index changes.
QSortFilterProxyModel * proxyModel() const
Return the internal sort/filter proxy model.
void entityUpdated(const EntityMetadata &metadata)
Emitted when an entity row is updated.
void currentSelectionChanged()
Emitted when the current selection changes.
void horizontalHeaderChanged()
Emitted when the horizontal header is resized.
int rowCount() const
Return the number of rows currently in the view.
void entityDeleted(const EntityMetadata &metadata)
Emitted when an entity row is deleted.
virtual void deleteRow(const QModelIndex &index)
Delete the row identified by the given model index.
virtual QModelIndex firstIndexOfEntityUuid(const QUuid &uuid) const
Return the first index whose item UUID matches.
virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous) override
Internal override forwarding current-index changes to currentIndexChanged().
QModelIndex findFirstMatch(const QVariant &needle, int role) const
Return the first model index where the given role matches a value.
void restoreHeaderStates()
Restore both horizontal and vertical header states from settings.
void restoreVerticalHeaderState()
Restore vertical header state (row heights) from settings.
bool isIndexVisible(const QModelIndex &index) const
Return whether the given model index is currently visible in the viewport.
virtual void setModel(QAbstractItemModel *model) override
Set the model, wrapping it in an internal sort/filter proxy.
void entityAdded(const EntityMetadata &metadata)
Emitted when a row is to be added with the given metadata.
int entityTypeAtPos(const QPoint &pos)
Return the entity type of the item at a view position.
AbstractItemModel * sourceModel() const
Return the underlying AbstractItemModel (without the proxy).
void restoreHorizontalHeaderState()
Restore horizontal header state (column widths/order) from settings.
virtual void addRow(const EntityMetadata &metadata)
Emit entityAdded() for the given metadata (override to customise).
EntityMetadata metadataAtPos(const QPoint &pos) const
Return the EntityMetadata of the item at a view position.
void clear()
Remove all rows from the view model.
QModelIndexList allRows() const
Return model indexes for all rows in the view.
void setCurrentUuid(const QUuid &uuid, ScrollHint scrollHint=EnsureVisible)
Select the item with the given UUID and optionally scroll to it.
TableViewBase(QWidget *parent=nullptr)
Construct with an optional parent widget.
void setColumnDelegate(int type, QStyledItemDelegate *delegate)
Assign a custom item delegate to the column of the given header type.
EntityMetadata currentMetadata() const
Return the EntityMetadata of the currently selected item.
void verticalHeaderChanged()
Emitted when the vertical header is resized.