Libplasma

tooltipdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "tooltipdialog.h"
8
9#include <QDebug>
10#include <QFile>
11#include <QPlatformSurfaceEvent>
12#include <QQmlEngine>
13#include <QQuickItem>
14
15#include <KWindowSystem>
16#include <plasmaquick/sharedqmlengine.h>
17
18#include "plasmashellwaylandintegration.h"
19
20ToolTipDialog::ToolTipDialog()
21 : PopupPlasmaWindow(QStringLiteral("widgets/tooltip"))
22 , m_qmlObject(nullptr)
23 , m_hideTimeout(-1)
24 , m_interactive(false)
25 , m_owner(nullptr)
26{
30 } else {
32 PlasmaShellWaylandIntegration::get(this)->setRole(QtWayland::org_kde_plasma_surface::role_tooltip);
33 }
34 setFlags(flags);
35
36 m_hideTimer.setSingleShot(true);
37 connect(&m_hideTimer, &QTimer::timeout, this, [this]() {
38 setVisible(false);
39 });
40
41 connect(this, &PlasmaQuick::PlasmaWindow::mainItemChanged, this, [this]() {
42 if (m_lastMainItem) {
43 disconnect(m_lastMainItem, &QQuickItem::implicitWidthChanged, this, &ToolTipDialog::updateSize);
44 disconnect(m_lastMainItem, &QQuickItem::implicitHeightChanged, this, &ToolTipDialog::updateSize);
45 }
46 m_lastMainItem = mainItem();
47
48 if (!m_lastMainItem) {
49 return;
50 }
51 connect(m_lastMainItem, &QQuickItem::implicitWidthChanged, this, &ToolTipDialog::updateSize);
52 connect(m_lastMainItem, &QQuickItem::implicitHeightChanged, this, &ToolTipDialog::updateSize);
53 updateSize();
54 });
55}
56
57ToolTipDialog::~ToolTipDialog()
58{
59}
60
61void ToolTipDialog::updateSize()
62{
63 QScreen *s = screen();
64 if (!s) {
65 return;
66 }
67 QSize popupSize = QSize(mainItem()->implicitWidth(), mainItem()->implicitHeight());
68 popupSize = popupSize.grownBy(padding());
69 popupSize = popupSize.boundedTo(s->geometry().size());
70 if (!popupSize.isEmpty()) {
71 resize(popupSize);
72 }
73}
74
75QQuickItem *ToolTipDialog::loadDefaultItem()
76{
77 if (!m_qmlObject) {
78 m_qmlObject = new PlasmaQuick::SharedQmlEngine(this);
79 }
80
81 if (!m_qmlObject->rootObject()) {
82 m_qmlObject->setSourceFromModule("org.kde.plasma.core", "DefaultToolTip");
83 }
84
85 return qobject_cast<QQuickItem *>(m_qmlObject->rootObject());
86}
87
88void ToolTipDialog::showEvent(QShowEvent *event)
89{
90 keepalive();
91
93}
94
96{
97 m_hideTimer.stop();
99}
100
101bool ToolTipDialog::event(QEvent *e)
102{
103 if (e->type() == QEvent::Enter) {
104 if (m_interactive) {
105 m_hideTimer.stop();
106 }
107 } else if (e->type() == QEvent::Leave) {
108 dismiss();
109 }
110
111 return PopupPlasmaWindow::event(e);
112}
113
114QObject *ToolTipDialog::owner() const
115{
116 return m_owner;
117}
118
119void ToolTipDialog::setOwner(QObject *owner)
120{
121 m_owner = owner;
122}
123
124void ToolTipDialog::dismiss()
125{
126 m_hideTimer.start(200);
127}
128
129void ToolTipDialog::keepalive()
130{
131 if (m_hideTimeout > 0) {
132 m_hideTimer.start(m_hideTimeout);
133 } else {
134 m_hideTimer.stop();
135 }
136}
137
138bool ToolTipDialog::interactive()
139{
140 return m_interactive;
141}
142
143void ToolTipDialog::setInteractive(bool interactive)
144{
145 m_interactive = interactive;
146}
147
148void ToolTipDialog::valueChanged(const QVariant &value)
149{
150 setPosition(value.toPoint());
151}
152
153void ToolTipDialog::setHideTimeout(int timeout)
154{
155 m_hideTimeout = timeout;
156}
157
158int ToolTipDialog::hideTimeout() const
159{
160 return m_hideTimeout;
161}
162
163#include "moc_tooltipdialog.cpp"
static bool isPlatformX11()
An object that instantiates an entire QML context, with its own declarative engine.
static PlasmaShellWaylandIntegration * get(QWindow *window)
Returns the relevant PlasmaWaylandShellIntegration instance for this window creating one if needed.
KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags)
Type type() const const
T qobject_cast(QObject *object)
void implicitHeightChanged()
void implicitWidthChanged()
virtual void hideEvent(QHideEvent *) override
QSize boundedTo(const QSize &otherSize) const const
QSize grownBy(QMargins margins) const const
bool isEmpty() const const
typedef WindowFlags
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void timeout()
QPoint toPoint() const const
virtual void hideEvent(QHideEvent *ev)
void resize(const QSize &newSize)
QScreen * screen() const const
void setPosition(const QPoint &pt)
virtual void showEvent(QShowEvent *ev)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:09:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.