File indexing completed on 2024-05-05 14:21:54
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "actionwithwidget.h" 0009 0010 #include <QAbstractButton> 0011 #include <QFrame> 0012 #include <QPushButton> 0013 #include <QToolButton> 0014 0015 using namespace SelectionMode; 0016 0017 ActionWithWidget::ActionWithWidget(QAction *action) 0018 : m_action{action} 0019 { 0020 } 0021 0022 ActionWithWidget::ActionWithWidget(QAction *action, QAbstractButton *button) 0023 : m_action{action} 0024 , m_widget{button} 0025 { 0026 copyActionDataToButton(button, action); 0027 } 0028 0029 QWidget *ActionWithWidget::newWidget(QWidget *parent) 0030 { 0031 Q_CHECK_PTR(m_action); 0032 Q_ASSERT(!m_widget); 0033 0034 if (m_action->isSeparator()) { 0035 auto line = new QFrame(parent); 0036 line->setFrameShape(QFrame::VLine); 0037 line->setFrameShadow(QFrame::Sunken); 0038 0039 m_widget = line; 0040 } else { 0041 m_widget = newButtonForAction(m_action, parent); 0042 } 0043 return m_widget; 0044 } 0045 0046 QAbstractButton *SelectionMode::newButtonForAction(QAction *action, QWidget *parent) 0047 { 0048 Q_CHECK_PTR(action); 0049 Q_ASSERT(!action->isSeparator()); 0050 0051 if (action->priority() == QAction::LowPriority) { 0052 // We don't want the low priority actions to be displayed icon-only so we need trickery. 0053 auto button = new QPushButton(parent); 0054 copyActionDataToButton(static_cast<QAbstractButton *>(button), action); 0055 button->setMinimumWidth(0); 0056 return button; 0057 } 0058 0059 auto *toolButton = new QToolButton(parent); 0060 toolButton->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon); 0061 toolButton->setDefaultAction(action); 0062 toolButton->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup); 0063 toolButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0064 toolButton->setMinimumWidth(0); 0065 return toolButton; 0066 } 0067 0068 void SelectionMode::copyActionDataToButton(QAbstractButton *button, QAction *action) 0069 { 0070 button->setText(action->text()); 0071 button->setIcon(action->icon()); 0072 button->setToolTip(action->toolTip()); 0073 button->setWhatsThis(action->whatsThis()); 0074 0075 button->setVisible(action->isVisible()); 0076 button->setEnabled(action->isEnabled()); 0077 0078 QObject::connect(button, &QAbstractButton::clicked, action, &QAction::trigger); 0079 }