File indexing completed on 2024-04-28 15:32:16

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2004 Felix Berger <felixberger@beldesign.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "ktoolbarlabelaction.h"
0009 
0010 #include <QApplication>
0011 #include <QLabel>
0012 #include <QPointer>
0013 #include <QToolBar>
0014 
0015 class KToolBarLabelActionPrivate
0016 {
0017 public:
0018     QPointer<QAction> buddy;
0019     QPointer<QLabel> label;
0020 };
0021 
0022 KToolBarLabelAction::KToolBarLabelAction(const QString &text, QObject *parent)
0023     : QWidgetAction(parent)
0024     , d(new KToolBarLabelActionPrivate)
0025 {
0026     setText(text);
0027     d->label = nullptr;
0028 }
0029 
0030 KToolBarLabelAction::KToolBarLabelAction(QAction *buddy, const QString &text, QObject *parent)
0031     : QWidgetAction(parent)
0032     , d(new KToolBarLabelActionPrivate)
0033 {
0034     setBuddy(buddy);
0035     setText(text);
0036 
0037     d->label = nullptr;
0038 }
0039 
0040 KToolBarLabelAction::~KToolBarLabelAction() = default;
0041 
0042 void KToolBarLabelAction::setBuddy(QAction *buddy)
0043 {
0044     d->buddy = buddy;
0045 
0046     QList<QLabel *> labels;
0047     const auto associatedWidgets = this->associatedWidgets();
0048     for (QWidget *widget : associatedWidgets) {
0049         if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
0050             if (QLabel *label = qobject_cast<QLabel *>(toolBar->widgetForAction(this))) {
0051                 labels.append(label);
0052             }
0053         }
0054     }
0055     const auto buddysAssociatedWidgets = buddy->associatedWidgets();
0056     for (QWidget *widget : buddysAssociatedWidgets) {
0057         if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
0058             QWidget *newBuddy = toolBar->widgetForAction(buddy);
0059             for (QLabel *label : std::as_const(labels)) {
0060                 label->setBuddy(newBuddy);
0061             }
0062             return;
0063         }
0064     }
0065 }
0066 
0067 QAction *KToolBarLabelAction::buddy() const
0068 {
0069     return d->buddy;
0070 }
0071 
0072 bool KToolBarLabelAction::event(QEvent *event)
0073 {
0074     if (event->type() == QEvent::ActionChanged) {
0075         if (d->label && text() != d->label->text()) {
0076             Q_EMIT textChanged(text());
0077             d->label->setText(text());
0078         }
0079     }
0080 
0081     return QWidgetAction::event(event);
0082 }
0083 
0084 bool KToolBarLabelAction::eventFilter(QObject *watched, QEvent *event)
0085 {
0086     if (d->label && d->buddy && event->type() == QEvent::PolishRequest && watched == d->label) {
0087         const auto buddysAssociatedWidgets = d->buddy->associatedWidgets();
0088         for (QWidget *widget : buddysAssociatedWidgets) {
0089             if (QToolBar *toolBar = qobject_cast<QToolBar *>(widget)) {
0090                 QWidget *newBuddy = toolBar->widgetForAction(d->buddy);
0091                 d->label->setBuddy(newBuddy);
0092             }
0093         }
0094     }
0095 
0096     return QWidgetAction::eventFilter(watched, event);
0097 }
0098 
0099 QWidget *KToolBarLabelAction::createWidget(QWidget *_parent)
0100 {
0101     QToolBar *parent = qobject_cast<QToolBar *>(_parent);
0102     if (!parent) {
0103         return QWidgetAction::createWidget(_parent);
0104     }
0105     if (!d->label) {
0106         d->label = new QLabel(parent);
0107 
0108         // These lines were copied from Konqueror's KonqDraggableLabel class in
0109         // konq_misc.cc
0110         d->label->setBackgroundRole(QPalette::Button);
0111         d->label->setAlignment((QApplication::isRightToLeft() ? Qt::AlignRight : Qt::AlignLeft) | Qt::AlignVCenter);
0112         d->label->adjustSize();
0113         d->label->setText(text());
0114         d->label->installEventFilter(this);
0115     }
0116 
0117     return d->label;
0118 }
0119 
0120 #include "moc_ktoolbarlabelaction.cpp"