KWidgetsAddons

ktoolbarlabelaction.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2004 Felix Berger <felixberger@beldesign.de>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "ktoolbarlabelaction.h"
9
10#include <QApplication>
11#include <QLabel>
12#include <QPointer>
13#include <QToolBar>
14
15class KToolBarLabelActionPrivate
16{
17public:
19 QPointer<QLabel> label;
20};
21
23 : QWidgetAction(parent)
24 , d(new KToolBarLabelActionPrivate)
25{
27 d->label = nullptr;
28}
29
31 : QWidgetAction(parent)
32 , d(new KToolBarLabelActionPrivate)
33{
36
37 d->label = nullptr;
38}
39
41
43{
44 d->buddy = buddy;
45
46 QList<QLabel *> labels;
47 const auto associatedWidgets = this->associatedObjects();
48 for (auto *widget : associatedWidgets) {
49 if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
50 if (QLabel *label = qobject_cast<QLabel *>(toolBar->widgetForAction(this))) {
51 labels.append(label);
52 }
53 }
54 }
55 const auto buddysAssociatedWidgets = buddy->associatedObjects();
56 for (auto *widget : buddysAssociatedWidgets) {
57 if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
58 QWidget *newBuddy = toolBar->widgetForAction(buddy);
59 for (QLabel *label : std::as_const(labels)) {
60 label->setBuddy(newBuddy);
61 }
62 return;
63 }
64 }
65}
66
68{
69 return d->buddy;
70}
71
72bool KToolBarLabelAction::event(QEvent *event)
73{
74 if (event->type() == QEvent::ActionChanged) {
75 if (d->label && text() != d->label->text()) {
77 d->label->setText(text());
78 }
79 }
80
81 return QWidgetAction::event(event);
82}
83
84bool KToolBarLabelAction::eventFilter(QObject *watched, QEvent *event)
85{
86 if (d->label && d->buddy && event->type() == QEvent::PolishRequest && watched == d->label) {
87 const auto buddysAssociatedWidgets = d->buddy->associatedObjects();
88 for (auto *widget : buddysAssociatedWidgets) {
89 if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
90 QWidget *newBuddy = toolBar->widgetForAction(d->buddy);
91 d->label->setBuddy(newBuddy);
92 }
93 }
94 }
95
96 return QWidgetAction::eventFilter(watched, event);
97}
98
100{
102 if (!parent) {
103 return QWidgetAction::createWidget(_parent);
104 }
105 if (!d->label) {
106 d->label = new QLabel(parent);
107
108 // These lines were copied from Konqueror's KonqDraggableLabel class in
109 // konq_misc.cc
110 d->label->setBackgroundRole(QPalette::Button);
112 d->label->adjustSize();
113 d->label->setText(text());
114 d->label->installEventFilter(this);
115 }
116
117 return d->label;
118}
119
120#include "moc_ktoolbarlabelaction.cpp"
QWidget * createWidget(QWidget *parent) override
Reimplemented from QWidgetAction.
void setBuddy(QAction *buddy)
Sets the label's buddy to buddy.
void textChanged(const QString &newText)
This signal is emitted whenever the text of this action is changed.
KToolBarLabelAction(const QString &text, QObject *parent)
Creates a toolbar label.
QAction * buddy() const
Returns the label's buddy or a null pointer if no buddy is currently set.
~KToolBarLabelAction() override
Destroys the toolbar label.
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QList< QWidget * > associatedWidgets() const const
QList< QObject * > associatedObjects() const const
void setText(const QString &text)
bool isRightToLeft()
void append(QList< T > &&value)
Q_EMITQ_EMIT
QObject * parent() const const
T qobject_cast(QObject *object)
AlignRight
virtual QWidget * createWidget(QWidget *parent)
virtual bool event(QEvent *event) override
virtual bool eventFilter(QObject *obj, QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:51:03 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.