Logo    
KDWinUtils
Helper library for MFC to Qt migration
Loading...
Searching...
No Matches
klistmodel.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of KDWinutils, KDAB's MFC to Qt migration tool.
4**
5** Copyright (C) 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6** All rights reserved.
7**
8** This file is intended solely for use by the migration tools and services
9** provided by Klarälvdalens Datakonsult AB.
10**
11** Any other use or distribution of this software that is not otherwise agreed
12** upon in writing and signed by an authorized representative of Klarälvdalens
13** Datakonsult AB, KDAB (USA) LLC, KDAB (Deutschland) GmbH & Co. K.G.,
14** KDAB (France) SAS, KDAB (UK), Ltd., or any future subsidiary of Klarälvdalens
15** Datakonsult AB is hereby prohibited.
16**
17** Contact info@kdab.com if any conditions stated above are unclear to you.
18**
19****************************************************************************/
20
21#pragma once
22
24
26#include "../corelib/krect.h"
27#include "../corelib/kstring.h"
29
30#include <QAbstractItemModel>
31#include <QFlags>
32
33#include <memory>
34
35class QAbstractItemDelegate;
36class QHelpEvent;
37class QPainter;
38class QStyleOptionViewItem;
39class QTreeView;
40
42
43class StyledItemDelegate;
44class KDialog;
45class KHeaderCtrl;
46class KImageList;
48
65class KDWINUTILS_EXPORT KListModel : public QAbstractItemModel
66{
67 Q_OBJECT
68
69public:
70 explicit KListModel(QObject *parent = nullptr);
72
74 QTreeView *widget() const;
76 void setWidget(QTreeView *widget);
77
78 // QAbstractItemModel
79 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
80 QModelIndex parent(const QModelIndex &child) const override;
81 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
82 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
83 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
84 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
85 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
86 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
87 Qt::ItemFlags flags(const QModelIndex &index) const override;
88
90 QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
92 QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
93
95 Qt::Alignment getColumnAlignment(int col) const;
96
97#ifdef KDWINUTILS_WITHMFC
99 static Qt::AlignmentFlag toQtAlignmentFlag(int nFormat);
100#endif // KDWINUTILS_WITHMFC
101
102 using CustomPaintFunction = std::function<void(QPainter *, const QStyleOptionViewItem &, const QModelIndex &)>;
108 template <typename Class>
110 void setCustomPaintFunction(Class *object,
111 void (Class::*method)(QPainter *painter, const QStyleOptionViewItem &styleOption,
112 const QModelIndex &index));
113
120 struct Position
121 {
122 Position() = default;
123 explicit Position(int p) noexcept
124 : pos(p) {};
125
127 explicit operator bool() const noexcept { return pos >= 0; }
128
129 friend inline bool operator==(Position lhs, Position rhs) noexcept { return lhs.pos == rhs.pos; }
130 friend inline bool operator!=(Position lhs, Position rhs) noexcept { return lhs.pos != rhs.pos; }
131
132 friend inline bool operator==(Position p, Private::CompareAgainstLiteralZero) noexcept
133 {
134 return !static_cast<bool>(p);
135 }
136 friend inline bool operator!=(Position p, Private::CompareAgainstLiteralZero) noexcept
137 {
138 return static_cast<bool>(p);
139 }
140
141 friend inline bool operator==(Position p, std::nullptr_t) noexcept { return !static_cast<bool>(p); }
142 friend inline bool operator!=(Position p, std::nullptr_t) noexcept { return static_cast<bool>(p); }
143
144 int pos = -1;
145 };
146
147 enum class Style : uint {
148 Icon = 0x00000001,
149 Report = 0x00000002,
150 SmallIcon = 0x00000004,
151 List = 0x00000008,
152 TypeMask = 0x00000008,
153 SingleSel =
154 0x00000010,
155 ShowSelAlways = 0x00000020,
156 SortAscending = 0x00000040,
157 SortDescending = 0x00000080,
158 ShareImageLists = 0x00000100,
159 NoLabelWrap = 0x00000200,
160 AutoArrange = 0x00000400,
161 EditLabels = 0x00000800,
162 OwnerData = 0x00001000,
163 NoScroll = 0x00002000,
164 TypeStyleMask = 0x00004000,
165 AlignTop = 0x00000001,
166 AlignLeft = 0x00008000,
167 AlignMask = 0x00010000,
168 OwnerDrawFixed = 0x00020000,
169 NoColumnHeader = 0x00040000,
170 NoSortHeader = 0x00080000,
171 };
173
174 enum class ExtendedStyle : uint {
175 GridLines = 0x00000001, // Displays gridlines around items and subitems.
176 SubItemImages = 0x00000002,
177 CheckBoxes = 0x00000004,
178 TrackSelect = 0x00000008,
179 HeaderDragDrop = 0x00000010,
180 FullRowSelect = 0x00000020, // Selects the entire row of an item when it is selected.
181 OneClickActivate = 0x00000040,
182 TwoClickActivate = 0x00000080,
183 FlatSB = 0x00000100,
184 Regional = 0x00000200,
185 InfoTip = 0x00000400, // Contextual tooltips from the listview, using infoTipRequested signal.
186 UnderlineHot = 0x00000800,
187 UnderlineCold = 0x00001000,
188 MultiWorkAreas = 0x00002000,
189 LabelTip = 0x00004000,
190 BorderSelect = 0x00008000,
191 DoubleBuffer = 0x00010000,
192 HideLabels = 0x00020000,
193 SingleRow = 0x00040000,
194 SnapToGrid = 0x00080000,
195 SimpleSelect = 0x00100000,
196 JustifyColumns = 0x00200000,
197 TransparentBkgnd = 0x00400000,
198 TransparentShadowText = 0x00800000,
199 AutoAutoArrange = 0x01000000,
200 HeaderInAllViews = 0x02000000,
201 AutoCheckSelect = 0x08000000,
202 AutoSizeColumns = 0x10000000,
203 ColumnSnapPoints = 0x40000000,
204 ColumnOverflow = 0x80000000,
205 };
207
208 enum class StateMask : uint {
209 Focused = 0x0001,
210 Selected = 0x0002,
211 Cut = 0x0004,
212 DropHilited = 0x0008,
213 Glow = 0x0010,
214 Activating = 0x0020,
215 OverlayMask = 0x0F00,
216 StateImageMask = 0xF000,
217 };
219
220 enum class Mask : uint {
221 Text = 0x00000001,
222 Image = 0x00000002,
223 Param = 0x00000004,
224 State = 0x00000008,
225 Indent = 0x00000010,
226 NoRecompute = 0x00000800,
227 GroupId = 0x00000100,
228 Columns = 0x00000200,
229 ColFmt = 0x00010000,
230 };
232
252
256 {
257 int iItem;
259 void *lParam;
260 };
261
264 {
265 QPoint pt;
266 int iItem;
268 };
269
277 {
279 };
280
281 enum class ImageList {
282 Normal,
283 Small,
284 State,
285 };
287
288 // MFC API from here on
290
291 bool Create(KListModel::Styles style, const KRect &rect, QWidget *pParentWnd);
292
299 int GetSelectedCount() const;
300
302 KString GetItemText(int nItem, int nSubItem) const;
304 int InsertColumn(int nCol, const KString &lpszColumnHeading, Qt::AlignmentFlag nFormat = Qt::AlignLeft,
305 int nWidth = -1, int nSubItem = -1);
307 bool SetColumnWidth(int nCol, int cx);
311 bool DeleteColumn(int nCol);
312
314 int InsertItem(const Item *pItem);
316 int InsertItem(int nItem, const KString &lpszItem);
317
319 bool SetItem(const Item *pItem);
321 bool SetItemText(int nItem, int nSubItem, const KString &lpszText);
322
324 void GetClientRect(KRect *rect) const;
326 bool GetWindowRect(KRect *r) const;
327
329 Styles GetStyle() const;
331 void SetStyle(Styles styles);
333 ExtendedStyles GetExtendedStyle() const;
335 void SetExtendedStyle(ExtendedStyles styles);
336
338 int GetItemCount() const;
340 bool SetItemData(int nItem, void *dwData);
342 void *GetItemData(int nItem) const;
343
345 bool GetCheckState(int nItem) const;
347 bool GetCheck(int nItem) const;
349 void SetCheckState(int nItem, bool checked);
350
352 int GetScrollPos(Qt::Orientation orientation) const;
355 int SetScrollPos(Qt::Orientation orientation, int pos, bool bRedraw = true);
356
359
362
364 bool GetColumn(int nCol, ColumnInfo *pColumn) const;
365
368
371#ifdef KDWINUTILS_WITHMFC
377 bool SortItems(PFNLVCOMPARE fn, std::uintptr_t param);
378#endif // KDWINUTILS_WITHMFC
379
381 bool EnsureVisible(int nItem, bool bPartialOK);
382
384 bool SetItemState(int nItem, StateMasks nState, StateMasks nMask);
385
386signals:
388 void infoTipRequested(QHelpEvent *, const QModelIndex &);
397 void contextMenuRequested(QObject *obj, const QPoint &);
399 void headerClicked(int section);
400
401public slots:
403 void setFilterFixedString(const KString &pattern);
407 void setFilterRegularExpression(const QRegularExpression &regularExpression);
409 void setFilterWildcard(const KString &pattern);
410
411private:
412 friend class StyledItemDelegate;
413
414 struct CellData
415 {
416 KString text;
417 int imageIndex;
418 void *lParam;
419 bool checkState;
420 };
421 using ListItem = std::vector<CellData>;
422
423 struct HeaderData
424 {
425 CellData cellData;
426 Qt::AlignmentFlag alignment;
427 };
428 using HeaderItem = std::vector<HeaderData>;
429
430 QTreeView *m_widget = nullptr;
431 std::unique_ptr<KItemViewToolTipHandler> m_toolTipHandler;
432 HeaderItem m_headerItem;
433 KHeaderCtrl *m_headerCtrl;
434 QList<ListItem> m_data;
435 Styles m_styles {};
436 ExtendedStyles m_extendedStyles {};
437
438 KImageList *m_imageListNormal = nullptr;
439 KImageList *m_imageListSmall = nullptr;
440 KImageList *m_imageListState = nullptr;
441
442 StyledItemDelegate *m_delegate;
443
444 KSortFilterProxyModel *m_sortFilterProxy = nullptr;
445
446 CustomPaintFunction m_customPaintFunction;
447
448 friend class KHeaderCtrl;
449};
450
451Q_DECLARE_OPERATORS_FOR_FLAGS(KListModel::Masks)
452Q_DECLARE_OPERATORS_FOR_FLAGS(KListModel::StateMasks)
453Q_DECLARE_OPERATORS_FOR_FLAGS(KListModel::Styles)
454Q_DECLARE_OPERATORS_FOR_FLAGS(KListModel::ExtendedStyles)
455
456template <typename Class>
457inline void KListModel::setCustomPaintFunction(Class *object,
458 void (Class::*method)(QPainter *painter,
459 const QStyleOptionViewItem &styleOption,
460 const QModelIndex &index))
461{
463 object, [object, method](QPainter *painter, const QStyleOptionViewItem &styleOption, const QModelIndex &index) {
464 return (object->*method)(painter, styleOption, index);
465 });
466}
467
The KDialog class is used to replace MFC KDialog.
Definition kdialog.h:40
The KHeaderCtrl class is used to replace CHeaderCtrl.
Definition kheaderctrl.h:45
The KImageList class defines a list of images, each of the same size.
Definition kimagelist.h:38
Special filter to handle tooltip events.
Definition kitemviewtooltiphandler.h:39
The KListModel class is used to replace DDX_Control edit data exchange.
Definition klistmodel.h:66
Position GetFirstSelectedItemPosition() const
Retrieves the position of the first selected list view item in a list view control.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setFilterRegularExpression(const QRegularExpression &regularExpression)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
int GetSelectedCount() const
Retrieves the number of selected items in the list view control.
bool Create(KListModel::Styles style, const KRect &rect, QWidget *pParentWnd)
Creates a KListModel with the given styles, rect and parent.
void setFilterWildcard(const KString &pattern)
bool GetCheck(int nItem) const
Returns true if the item is checked.
void * GetItemData(int nItem) const
Retrieves the application-specific value associated with an item.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int InsertItem(int nItem, const KString &lpszItem)
Inserts a new item in a list view control, with the text given in parameter.
Qt::Alignment getColumnAlignment(int col) const
void SetCheckState(int nItem, bool checked)
Set the check status of the item.
KListModel(QObject *parent=nullptr)
int GetItemCount() const
Retrieves the number of items in a list view control.
Q_DECLARE_FLAGS(ExtendedStyles, ExtendedStyle)
int GetScrollPos(Qt::Orientation orientation) const
Retrieves the current position of a scroll box.
StateMask
Definition klistmodel.h:208
KHeaderCtrl * GetHeaderCtrl() const
Retrieves the header control of a list view control.
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
bool DeleteAllItems()
Deletes all items from the control.
bool GetColumn(int nCol, ColumnInfo *pColumn) const
Retrieves the attributes of a control's column.
ImageList
Definition klistmodel.h:281
bool DeleteColumn(int nCol)
Deletes a column from the list view control.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
int SubItemHitTest(HitTestInfo *pInfo)
Determines which list view item, if any, is at a given position.
void SetStyle(Styles styles)
Sets the current style used.
bool SortItems(PFNLVCOMPARE fn, std::uintptr_t param)
Sorts list view items using an application-defined comparison function.
bool SetItemState(int nItem, StateMasks nState, StateMasks nMask)
Changes the state of an item in a list view control.
int InsertColumn(int nCol, const KString &lpszColumnHeading, Qt::AlignmentFlag nFormat=Qt::AlignLeft, int nWidth=-1, int nSubItem=-1)
Inserts a new column in a list view control.
ExtendedStyles GetExtendedStyle() const
Retrieves the current extended styles of a list view control.
void SetExtendedStyle(ExtendedStyles styles)
Sets the current extended styles of a list view control.
bool SortItems(KSortFilterProxyModel::SortFunction lessThan)
Sorts list view items using an application-defined comparison function.
void setWidget(QTreeView *widget)
static Qt::AlignmentFlag toQtAlignmentFlag(int nFormat)
KString GetItemText(int nItem, int nSubItem) const
Retrieves the text of a list view item or subitem.
Styles GetStyle() const
Returns the current style used.
void headerClicked(int section)
Signal emitted when the header is clicked.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool SetItemText(int nItem, int nSubItem, const KString &lpszText)
Changes the text of a list view item or subitem.
Style
Definition klistmodel.h:147
ExtendedStyle
Definition klistmodel.h:174
int GetNextSelectedItem(Position &pos) const
Retrieves the index of a list view item position, and the position of the next selected list view ite...
Q_DECLARE_FLAGS(Masks, Mask)
void itemChanged(const ChangedItem &)
Signal emitted when the item has been changed.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
void GetClientRect(KRect *rect) const
Gets the dimensions of the widget.
void setCustomPaintFunction(const CustomPaintFunction &function)
Sets the custom paint function individual items in the list.
bool SetColumnWidth(int nCol, int cx)
Changes the width of a column in report view or list view.
void infoTipRequested(QHelpEvent *, const QModelIndex &)
Signal emitted when the tooltip for an item is requested.
Q_DECLARE_FLAGS(StateMasks, StateMask)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
bool EnsureVisible(int nItem, bool bPartialOK)
Ensures that an item is visible.
Mask
Definition klistmodel.h:220
int InsertItem(const Item *pItem)
Inserts a new item in a list view control.
QTreeView * widget() const
Q_ENUM(ImageList)
void setFilterRegularExpression(const KString &pattern)
bool SetItemData(int nItem, void *dwData)
Sets the item's application-specific value.
bool GetWindowRect(KRect *r) const
Gets the screen coordinates of widget.
int SetScrollPos(Qt::Orientation orientation, int pos, bool bRedraw=true)
Sets the current position of a scroll box and, if specified, redraws the scroll bar to reflect the ne...
bool SetItem(const Item *pItem)
Sets some or all of a list view item's attributes.
bool GetCheckState(int nItem) const
Returns true if the item is checked.
void contextMenuRequested(QObject *obj, const QPoint &)
Signal emitted when the context menu is requested.
std::function< void(QPainter *, const QStyleOptionViewItem &, const QModelIndex &)> CustomPaintFunction
Definition klistmodel.h:102
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setFilterFixedString(const KString &pattern)
KImageList * SetImageList(KImageList *pImageList, KListModel::ImageList imageListType)
Assigns an image list to a list view control.
Q_DECLARE_FLAGS(Styles, Style)
QModelIndex parent(const QModelIndex &child) const override
The KRect class defines a rectangle in the plane using integer precision.
Definition krect.h:40
The KSortFilterProxyModel is a QSortFilterProxyModel that allows custom sorting.
Definition ksortfilterproxymodel.h:39
std::function< bool(const QModelIndex &, const QModelIndex &)> SortFunction
Definition ksortfilterproxymodel.h:45
The KString class provides a Unicode character string.
Definition kstring.h:45
#define KDWINUTILS_EXPORT
Definition kdwinutils_export.h:31
#define KDWINUTILS_BEGIN_NAMESPACE
Definition kdwinutils_global.h:27
#define KDWINUTILS_END_NAMESPACE
Definition kdwinutils_global.h:28
Q_DECLARE_OPERATORS_FOR_FLAGS(KHeaderCtrl::Masks)
The ChangedItem struct contains info on the changed item, it's emitted by KListModel::itemChanged sig...
Definition klistmodel.h:256
int iItem
Definition klistmodel.h:257
int iSubItem
Definition klistmodel.h:258
void * lParam
Definition klistmodel.h:259
The ColumnInfo struct contains information about a column in the list view control.
Definition klistmodel.h:277
KString pszText
Definition klistmodel.h:278
Contains information about a hit test.
Definition klistmodel.h:264
int iItem
Definition klistmodel.h:266
QPoint pt
Definition klistmodel.h:265
int iSubItem
Definition klistmodel.h:267
Contains attributes of a list view item or subitem.
Definition klistmodel.h:240
StateMask stateMask
Specifies which bits of the state member will be retrieved or modified.
Definition klistmodel.h:245
int iIndent
Not handled yet.
Definition klistmodel.h:250
int iImage
The zero-based index of an image within the image list.
Definition klistmodel.h:248
Masks mask
Specifies which members of the structure are valid input.
Definition klistmodel.h:241
KString pszText
The item's text.
Definition klistmodel.h:246
int iSubItem
The column of the item (1 is the first subItem).
Definition klistmodel.h:243
int iItem
The row of the item.
Definition klistmodel.h:242
StateMask state
Specifies the item's state.
Definition klistmodel.h:244
void * lParam
Application-defined item data.
Definition klistmodel.h:249
int cchTextMax
The length of the item string.
Definition klistmodel.h:247
The Position struct is the position of the item in the view.
Definition klistmodel.h:121
friend bool operator==(Position p, std::nullptr_t) noexcept
Definition klistmodel.h:141
friend bool operator!=(Position p, Private::CompareAgainstLiteralZero) noexcept
Definition klistmodel.h:136
friend bool operator!=(Position p, std::nullptr_t) noexcept
Definition klistmodel.h:142
friend bool operator==(Position lhs, Position rhs) noexcept
Definition klistmodel.h:129
Position(int p) noexcept
Definition klistmodel.h:123
friend bool operator==(Position p, Private::CompareAgainstLiteralZero) noexcept
Definition klistmodel.h:132
friend bool operator!=(Position lhs, Position rhs) noexcept
Definition klistmodel.h:130