┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/admin/bar.cpp
blob: 2b65127f6336c4ec184d742d1336b643dddfe88b (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2024 Felix Ernst <[email protected]>

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

#include "bar.h"

#include "workerintegration.h"

#include <KColorScheme>
#include <KContextualHelpButton>
#include <KLocalizedString>

#include <QEvent>
#include <QGuiApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyle>
#include <QToolButton>
#include <QWidgetAction>

using namespace Admin;

Bar::Bar(QWidget *parent)
    : AnimatedHeightWidget{parent}
{
    setAutoFillBackground(true);
    updateColors();

    QWidget *contenntsContainer = prepareContentsContainer();

    m_fullLabelString = i18nc("@info label above the view explaining the state", "Acting as an Administrator – Be careful!");
    m_shortLabelString = i18nc("@info label above the view explaining the state, keep short", "Acting as Admin");
    m_label = new QLabel(contenntsContainer);
    m_label->setMinimumWidth(0);
    m_label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::LinksAccessibleByKeyboard); // for keyboard accessibility

    m_warningButton = new KContextualHelpButton(warningMessage(), nullptr, contenntsContainer);
    m_warningButton->setIcon(QIcon::fromTheme(QStringLiteral("emblem-warning")));

    m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contenntsContainer);
    m_closeButton->setToolTip(i18nc("@action:button", "Stop Acting as an Administrator"));
    m_closeButton->setFlat(true);
    connect(m_closeButton, &QAbstractButton::clicked, this, &Bar::activated); // Make sure the view connected to this bar is active before exiting admin mode.
    connect(m_closeButton, &QAbstractButton::clicked, this, &WorkerIntegration::exitAdminMode);

    QHBoxLayout *layout = new QHBoxLayout(contenntsContainer);
    auto contentsMargins = layout->contentsMargins();
    m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
    m_warningButton->setFixedHeight(m_preferredHeight);
    m_closeButton->setFixedHeight(m_preferredHeight);
    layout->setContentsMargins(0, 0, 0, 0);

    layout->addStretch();
    layout->addWidget(m_label);
    layout->addWidget(m_warningButton);
    layout->addStretch();
    layout->addWidget(m_closeButton);
}

bool Bar::event(QEvent *event)
{
    if (event->type() == QEvent::PaletteChange) {
        updateColors();
    }
    return AnimatedHeightWidget::event(event);
}

void Bar::resizeEvent(QResizeEvent *resizeEvent)
{
    updateLabelString();
    return QWidget::resizeEvent(resizeEvent);
}

void Bar::updateColors()
{
    QPalette palette = parentWidget()->palette();
    KColorScheme colorScheme{QPalette::Normal, KColorScheme::ColorSet::Window};
    colorScheme.adjustBackground(palette, KColorScheme::NegativeBackground, QPalette::Window, KColorScheme::ColorSet::Window);
    colorScheme.adjustForeground(palette, KColorScheme::NegativeText, QPalette::WindowText, KColorScheme::ColorSet::Window);
    setPalette(palette);
}

void Bar::updateLabelString()
{
    QFontMetrics fontMetrics = m_label->fontMetrics();
    if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_warningButton->sizeHint().width() + m_closeButton->sizeHint().width()
            + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2
        < width()) {
        m_label->setText(m_fullLabelString);
    } else {
        m_label->setText(m_shortLabelString);
    }
}

#include "moc_bar.cpp"