Messagelib

themedelegate.h
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#pragma once
10
11#include <QColor>
12#include <QRect>
13#include <QStyledItemDelegate>
14
15#include "core/item.h"
16#include "core/theme.h"
17
19
20namespace MessageList
21{
22namespace Core
23{
24class Item;
25
26/**
27 * The ThemeDelegate paints the message list view message and group items by
28 * using the supplied Theme.
29 */
31{
33
34public:
36 ~ThemeDelegate() override;
37 /**
38 * Called when the global fonts change (from systemsettings)
39 */
40 void generalFontChanged();
41
42private:
43 const Theme *mTheme = nullptr; ///< Shallow pointer to the current theme
44 QAbstractItemView *const mItemView;
45
46 QColor mGroupHeaderBackgroundColor; // cache
47
48 // hitTest results
49 QModelIndex mHitIndex;
50 Item *mHitItem = nullptr;
51 QRect mHitItemRect;
52 const Theme::Column *mHitColumn = nullptr;
53 const Theme::Row *mHitRow = nullptr;
54 int mHitRowIndex;
55 bool mHitRowIsMessageRow;
56 QRect mHitRowRect;
57 bool mHitContentItemRight;
58 const Theme::ContentItem *mHitContentItem = nullptr;
59 QRect mHitContentItemRect;
60
61 mutable QSize mCachedMessageItemSizeHint;
62 mutable QSize mCachedGroupHeaderItemSizeHint;
63
64public:
65 const Theme *theme() const;
66 void setTheme(const Theme *theme);
67
68 /**
69 * Returns a heuristic sizeHint() for the specified item type and column.
70 * The hint is based on the contents of the theme (and not of any message or group header).
71 */
72 [[nodiscard]] QSize sizeHintForItemTypeAndColumn(Item::Type type, int column, const Item *item = nullptr) const;
73
74 /**
75 * Performs a hit test on the specified viewport point.
76 * Returns true if the point hit something and false otherwise.
77 * When the hit test is successful then the hitIndex(), hitItem(), hitColumn(), hitRow(), and hitContentItem()
78 * function will return information about the item that was effectively hit.
79 * If exact is set to true then hitTest() will return true only if the viewportPoint
80 * is exactly over an item. If exact is set to false then the hitTest() function
81 * will do its best to find the closest object to be actually "hit": this is useful,
82 * for example, in drag and drop operations.
83 */
84 bool hitTest(const QPoint &viewportPoint, bool exact = true);
85
86 /**
87 * Returns the model index that was reported as hit by the previous call to hitTest().
88 * The result of this function is valid only if hitTest() returned true and only
89 * within the same calling function.
90 */
91 const QModelIndex &hitIndex() const;
92
93 /**
94 * Returns the Item that was reported as hit by the previous call to hitTest().
95 * The result of this function is valid only if hitTest() returned true and only
96 * within the same calling function.
97 */
98 Item *hitItem() const;
99
100 /**
101 * Returns the visual rectangle of the item that was reported as hit by the previous call to hitTest().
102 * The result of this function is valid only if hitTest() returned true and only
103 * within the same calling function. Please note that this rectangle refers
104 * to a specific item column (and not all of the columns).
105 */
106 [[nodiscard]] QRect hitItemRect() const;
107
108 /**
109 * Returns the theme column that was reported as hit by the previous call to hitTest().
110 * The result of this function is valid only if hitTest() returned true and only
111 * within the same calling function.
112 */
113 const Theme::Column *hitColumn() const;
114
115 /**
116 * Returns the index of the theme column that was reported as hit by the previous call to hitTest().
117 * The result of this function is valid only if hitTest() returned true and only
118 * within the same calling function.
119 * This is the same as hitIndex().column().
120 */
121 [[nodiscard]] int hitColumnIndex() const;
122
123 /**
124 * Returns the theme row that was reported as hit by the previous call to hitTest().
125 * The result of this function is valid only if hitTest() returned true and only
126 * within the same calling function. This function may also return a null row
127 * when hitTest() returned true. This means that the item was globally hit
128 * but no row was exactly hit (the user probably hit the margin instead).
129 */
130 const Theme::Row *hitRow() const;
131
132 /**
133 * Returns the index of the theme row that was reported as hit by the previous call to hitTest().
134 * The result of this function is valid only if hitRow() returns a non null value.
135 */
136 [[nodiscard]] int hitRowIndex() const;
137
138 /**
139 * Returns the rectangle of the row that was reported as hit by the previous call to hitTest().
140 * The result of this function is valid only if hitTest() returned true and only
141 * within the same calling function. The result of this function is also invalid
142 * if hitRow() returns 0.
143 */
144 [[nodiscard]] QRect hitRowRect() const;
145
146 /**
147 * Returns true if the hitRow() is a message row, false otherwise.
148 * The result of this function has a meaning only if hitRow() returns a non zero result.
149 */
150 bool hitRowIsMessageRow() const;
151
152 /**
153 * Returns the theme content item that was reported as hit by the previous call to hitTest().
154 * The result of this function is valid only if hitTest() returned true and only
155 * within the same calling function. This function may also return a null content item
156 * when hitTest() returned true. This means that the item was globally hit
157 * but no content item was exactly hit (the user might have clicked inside a blank unused space instead).
158 */
159 const Theme::ContentItem *hitContentItem() const;
160
161 /**
162 * Returns true if the hit theme content item was a right item and false otherwise.
163 * The result of this function is valid only if hitContentItem() returns true.
164 */
165 [[nodiscard]] bool hitContentItemRight() const;
166
167 /**
168 * Returns the bounding rect of the content item that was reported as hit by the previous call to hitTest().
169 * The result of this function is valid only if hitTest() returned true and only
170 * within the same calling function. The result of this function is to be considered
171 * invalid also when hitContentItem() returns 0.
172 */
173 [[nodiscard]] QRect hitContentItemRect() const;
174
175protected:
176 /**
177 * Returns the Item for the specified model index. Pure virtual: must be reimplemented
178 * by derived classes.
179 */
180 virtual Item *itemFromIndex(const QModelIndex &index) const = 0;
181
182 /**
183 * Reimplemented from QStyledItemDelegate
184 */
185 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
186
187 /**
188 * Reimplemented from QStyledItemDelegate
189 */
190 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
191};
192} // namespace Core
193} // namespace MessageList
A single item of the MessageList tree managed by MessageList::Model.
Definition item.h:36
Type
The type of the Item.
Definition item.h:44
The ThemeDelegate paints the message list view message and group items by using the supplied Theme.
QRect hitRowRect() const
Returns the rectangle of the row that was reported as hit by the previous call to hitTest().
const Theme::Column * hitColumn() const
Returns the theme column that was reported as hit by the previous call to hitTest().
bool hitTest(const QPoint &viewportPoint, bool exact=true)
Performs a hit test on the specified viewport point.
bool hitContentItemRight() const
Returns true if the hit theme content item was a right item and false otherwise.
Item * hitItem() const
Returns the Item that was reported as hit by the previous call to hitTest().
QRect hitContentItemRect() const
Returns the bounding rect of the content item that was reported as hit by the previous call to hitTes...
const Theme::ContentItem * hitContentItem() const
Returns the theme content item that was reported as hit by the previous call to hitTest().
QSize sizeHintForItemTypeAndColumn(Item::Type type, int column, const Item *item=nullptr) const
Returns a heuristic sizeHint() for the specified item type and column.
int hitColumnIndex() const
Returns the index of the theme column that was reported as hit by the previous call to hitTest().
void generalFontChanged()
Called when the global fonts change (from systemsettings)
virtual Item * itemFromIndex(const QModelIndex &index) const =0
Returns the Item for the specified model index.
const QModelIndex & hitIndex() const
Returns the model index that was reported as hit by the previous call to hitTest().
bool hitRowIsMessageRow() const
Returns true if the hitRow() is a message row, false otherwise.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplemented from QStyledItemDelegate.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplemented from QStyledItemDelegate.
const Theme::Row * hitRow() const
Returns the theme row that was reported as hit by the previous call to hitTest().
QRect hitItemRect() const
Returns the visual rectangle of the item that was reported as hit by the previous call to hitTest().
int hitRowIndex() const
Returns the index of the theme row that was reported as hit by the previous call to hitTest().
The Column class defines a view column available inside this theme.
Definition theme.h:501
The ContentItem class defines a content item inside a Row.
Definition theme.h:56
The Row class defines a row of items inside a Column.
Definition theme.h:408
The Theme class defines the visual appearance of the MessageList.
Definition theme.h:48
Q_OBJECTQ_OBJECT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.