Krita Source Code Documentation
Loading...
Searching...
No Matches
KoItemToolTip Class Referenceabstract

#include <KoItemToolTip.h>

+ Inheritance diagram for KoItemToolTip:

Public Member Functions

 KoItemToolTip (QWidget *parent)
 
void showTip (const QPoint &pos, const QStyleOptionViewItem &option, const QModelIndex &index)
 
QSize sizeHint () const override
 
 ~KoItemToolTip () override
 

Public Attributes

std::unique_ptr< QTextDocument > document
 
QPersistentModelIndex index
 
QPoint pos
 
QTimer timer
 

Protected Member Functions

virtual QTextDocument * createDocument (const QModelIndex &index)=0
 
bool eventFilter (QObject *object, QEvent *event) override
 
void paintEvent (QPaintEvent *e) override
 

Private Member Functions

void updatePosition (const QPoint &pos, const QStyleOptionViewItem &option)
 
- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Private Attributes

std::unique_ptr< Private > const d
 
- Private Attributes inherited from Private
KisCanvas2 * canvas
 
int displayedFrame
 
int intendedFrame
 

Detailed Description

Base class for tooltips that can show extensive information about the contents of the data pointed to by something that contains a QModelIndex. Subclasses need to use this data to create a QTextDocument that is formatted to provide the complete tooltip.

Definition at line 33 of file KoItemToolTip.cpp.

Constructor & Destructor Documentation

◆ KoItemToolTip()

KoItemToolTip::KoItemToolTip ( QWidget * parent)

Definition at line 42 of file KoItemToolTip.cpp.

43 : QFrame(parent, Qt::FramelessWindowHint | Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint)
44 , d(new Private)
45{
46 Q_ASSERT(parent);
47
48 d->timer.setSingleShot(true);
49 d->timer.setInterval(10000);
50 connect(&d->timer, &QTimer::timeout, this, &KoItemToolTip::hide);
51
52 d->document.reset(new QTextDocument(this));
53
54 parent->installEventFilter(this);
55}
std::unique_ptr< Private > const d
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References d.

◆ ~KoItemToolTip()

KoItemToolTip::~KoItemToolTip ( )
override

Definition at line 57 of file KoItemToolTip.cpp.

58{
59}

Member Function Documentation

◆ createDocument()

virtual QTextDocument * KoItemToolTip::createDocument ( const QModelIndex & index)
protectedpure virtual

Re-implement this to provide the actual tooltip contents. For instance:

QTextDocument *doc = new QTextDocument(this);
QImage thumb = index.data(Qt::UserRole + KisAbstractResourceModel::LargeThumbnail).value<QImage>();
doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
QString name = index.data(Qt::DisplayRole).toString();
const QString image = QString("<img src=\"data:thumbnail\">");
const QString body = QString("<h3 align=\"center\">%1</h3>").arg(name) + image;
const QString html = QString("<html><body>%1</body></html>").arg(body);
doc->setHtml(html);
doc->setTextWidth(qMin(doc->size().width(), 500.0));
return doc;
@ LargeThumbnail
A larger thumbnail for displaying in a tooltip. 200x200 or so.
QPersistentModelIndex index

Implemented in KisIconToolTip, and NodeToolTip.

◆ eventFilter()

bool KoItemToolTip::eventFilter ( QObject * object,
QEvent * event )
overrideprotected

Definition at line 165 of file KoItemToolTip.cpp.

166{
167 switch(event->type())
168 {
169 case QEvent::Leave:
170 hide();
171 default: break;
172 }
173
174 return QFrame::eventFilter(object, event);
175}

◆ paintEvent()

void KoItemToolTip::paintEvent ( QPaintEvent * e)
overrideprotected

Definition at line 158 of file KoItemToolTip.cpp.

159{
160 QPainter p(this);
161 d->document->drawContents(&p, rect());
162 p.drawRect(0, 0, width() - 1, height() - 1);
163}
const Params2D p

References d, and p.

◆ showTip()

void KoItemToolTip::showTip ( const QPoint & pos,
const QStyleOptionViewItem & option,
const QModelIndex & index )

Definition at line 61 of file KoItemToolTip.cpp.

62{
63 std::unique_ptr<QTextDocument> doc(createDocument(index));
64
65 QPoint p = (isVisible() && index == d->index) ? d->pos : pos;
66
67 if (!isVisible() || index != d->index || doc->toHtml() != d->document->toHtml()) {
68 d->pos = p;
69 d->index = index;
70 d->document.swap(doc);
71 updatePosition(p, option);
72 if (!isVisible())
73 show();
74 else
75 update();
76 }
77
78 d->timer.start();
79}
void updatePosition(const QPoint &pos, const QStyleOptionViewItem &option)
virtual QTextDocument * createDocument(const QModelIndex &index)=0
KRITAUI_EXPORT void show()
bool update(QSpinBox *spinBox)

References createDocument(), d, index, p, pos, and updatePosition().

◆ sizeHint()

QSize KoItemToolTip::sizeHint ( ) const
override

Definition at line 153 of file KoItemToolTip.cpp.

154{
155 return d->document->size().toSize();
156}

References d.

◆ updatePosition()

void KoItemToolTip::updatePosition ( const QPoint & pos,
const QStyleOptionViewItem & option )
private

Definition at line 81 of file KoItemToolTip.cpp.

82{
83 QWidget *widget = parentWidget();
85
86 const QPoint gpos = widget->mapToGlobal(pos);
87 const QSize size = sizeHint();
88
89 // resize **must** come before `setParentControlGeometry()`, otherwise
90 // compositor will place the window incorrectly!
91 resize(size);
92
93 // make sure that all the platform-specific structures for the window
94 // are created (i.e. QWaylandWindow) before trying to request them
95 // via `windowHandle()->handle()`
96 create();
97
98#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API
99
100 if (auto waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(windowHandle()->handle())) {
101
102 const QPoint parentPos = widget->window()->mapFromGlobal(gpos);
103 const QRect gravityRect(parentPos.x() - 20, parentPos.y() - 20, 40, 40);
104 waylandWindow->setParentControlGeometry(gravityRect);
105 waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::ToolTip);
106 } else
107#endif /* HAVE_QT_PRIVATE_TOOLTIP_POSITION_API */
108
109 {
110 QScreen *screen = widget->screen();
111 const QRect availableRect = screen->availableGeometry();
112 const int width = size.width(), height = size.height();
113 const QRect itemRect(widget->mapToGlobal(option.rect.topLeft()), option.rect.size());
114
115 int y = gpos.y() + 20;
116 if (y + height > availableRect.bottom()) {
117 y = itemRect.bottom();
118
119 if (y + height > availableRect.bottom()) {
120 y = gpos.y() - 20 - height;
121
122 if (y < availableRect.top()) {
123 y = itemRect.top() - height;
124
125 if (y < availableRect.top()) {
126 y = availableRect.top();
127 }
128 }
129 }
130 }
131
132 int x = gpos.x() + 20;
133 if (x + width > availableRect.right()) {
134 x = itemRect.right();
135
136 if (x + width > availableRect.right()) {
137 x = gpos.x() - 20 - width;
138
139 if (x < availableRect.left()) {
140 x = itemRect.left() - width;
141
142 if (x < availableRect.left()) {
143 x = availableRect.left();
144 }
145 }
146 }
147 }
148
149 move(QPoint(x, y));
150 }
151}
QSize sizeHint() const override
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
int size(const Forest< T > &forest)
Definition KisForest.h:1232
QPainterPath create(const char32_t codepoint, double height)
Creates a tofu missing glyph indicator representing the provided Unicode codepoint.

References KIS_SAFE_ASSERT_RECOVER_RETURN, pos, and sizeHint().

Member Data Documentation

◆ d

std::unique_ptr<Private> const KoItemToolTip::d
private

Definition at line 58 of file KoItemToolTip.h.

◆ document

std::unique_ptr<QTextDocument> KoItemToolTip::document

Definition at line 36 of file KoItemToolTip.cpp.

◆ index

QPersistentModelIndex KoItemToolTip::index

Definition at line 37 of file KoItemToolTip.cpp.

◆ pos

QPoint KoItemToolTip::pos

Definition at line 38 of file KoItemToolTip.cpp.

◆ timer

QTimer KoItemToolTip::timer

Definition at line 39 of file KoItemToolTip.cpp.


The documentation for this class was generated from the following files: