┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/selectionmode/actionwithwidget.cpp
blob: 82ce045dbfce2d9a7501c73878d745649143cb93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2022 Felix Ernst <[email protected]>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "actionwithwidget.h"

#include <QAbstractButton>
#include <QFrame>
#include <QPushButton>
#include <QToolButton>

using namespace SelectionMode;

ActionWithWidget::ActionWithWidget(QAction *action) :
    m_action{action}
{   }

ActionWithWidget::ActionWithWidget(QAction *action, QAbstractButton *button) :
    m_action{action},
    m_widget{button}
{
    copyActionDataToButton(button, action);
}

QWidget *ActionWithWidget::newWidget(QWidget *parent)
{
    Q_CHECK_PTR(m_action);
    Q_ASSERT(!m_widget);

    if (m_action->isSeparator()) {
        auto line = new QFrame(parent);
        line->setFrameShape(QFrame::VLine);
        line->setFrameShadow(QFrame::Sunken);

        m_widget = line;
    } else {
        m_widget = newButtonForAction(m_action, parent);
    }
    return m_widget;
}

QAbstractButton *SelectionMode::newButtonForAction(QAction *action, QWidget *parent)
{
    Q_CHECK_PTR(action);
    Q_ASSERT(!action->isSeparator());

    if (action->priority() == QAction::LowPriority) {
        // We don't want the low priority actions to be displayed icon-only so we need trickery.
        auto button = new QPushButton(parent);
        copyActionDataToButton(static_cast<QAbstractButton *>(button), action);
        button->setMinimumWidth(0);
        return button;
    }

    auto *toolButton = new QToolButton(parent);
    toolButton->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon);
    toolButton->setDefaultAction(action);
    toolButton->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
    toolButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    toolButton->setMinimumWidth(0);
    return toolButton;
}

void SelectionMode::copyActionDataToButton(QAbstractButton *button, QAction *action)
{
    button->setText(action->text());
    button->setIcon(action->icon());
    button->setToolTip(action->toolTip());
    button->setWhatsThis(action->whatsThis());

    button->setVisible(action->isVisible());
    button->setEnabled(action->isEnabled());

    QObject::connect(button, &QAbstractButton::clicked, action, &QAction::trigger);
}