]> cloud.milkyroute.net Git - dolphin.git/blob - src/admin/bar.cpp
Add "Act as Administrator" toggle action
[dolphin.git] / src / admin / bar.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2024 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "bar.h"
9
10 #include "workerintegration.h"
11
12 #include <KColorScheme>
13 #include <KContextualHelpButton>
14 #include <KLocalizedString>
15
16 #include <QEvent>
17 #include <QGuiApplication>
18 #include <QHBoxLayout>
19 #include <QLabel>
20 #include <QPushButton>
21 #include <QStyle>
22 #include <QToolButton>
23 #include <QWidgetAction>
24
25 using namespace Admin;
26
27 Bar::Bar(QWidget *parent)
28 : AnimatedHeightWidget{parent}
29 {
30 setAutoFillBackground(true);
31 updateColors();
32
33 QWidget *contenntsContainer = prepareContentsContainer();
34
35 m_fullLabelString = i18nc("@info label above the view explaining the state", "Acting as an Administrator – Be careful!");
36 m_shortLabelString = i18nc("@info label above the view explaining the state, keep short", "Acting as Admin");
37 m_label = new QLabel(contenntsContainer);
38 m_label->setMinimumWidth(0);
39 m_label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::LinksAccessibleByKeyboard); // for keyboard accessibility
40
41 m_warningButton = new KContextualHelpButton(warningMessage(), nullptr, contenntsContainer);
42 m_warningButton->setIcon(QIcon::fromTheme(QStringLiteral("emblem-warning")));
43
44 m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contenntsContainer);
45 m_closeButton->setToolTip(i18nc("@action:button", "Stop Acting as an Administrator"));
46 m_closeButton->setFlat(true);
47 connect(m_closeButton, &QAbstractButton::clicked, this, &Bar::activated); // Make sure the view connected to this bar is active before exiting admin mode.
48 connect(m_closeButton, &QAbstractButton::clicked, this, &WorkerIntegration::exitAdminMode);
49
50 QHBoxLayout *layout = new QHBoxLayout(contenntsContainer);
51 auto contentsMargins = layout->contentsMargins();
52 m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
53 m_warningButton->setFixedHeight(m_preferredHeight);
54 m_closeButton->setFixedHeight(m_preferredHeight);
55 layout->setContentsMargins(0, 0, 0, 0);
56
57 layout->addStretch();
58 layout->addWidget(m_label);
59 layout->addWidget(m_warningButton);
60 layout->addStretch();
61 layout->addWidget(m_closeButton);
62 }
63
64 bool Bar::event(QEvent *event)
65 {
66 if (event->type() == QEvent::PaletteChange) {
67 updateColors();
68 }
69 return AnimatedHeightWidget::event(event);
70 }
71
72 void Bar::resizeEvent(QResizeEvent *resizeEvent)
73 {
74 updateLabelString();
75 return QWidget::resizeEvent(resizeEvent);
76 }
77
78 void Bar::updateColors()
79 {
80 QPalette palette = parentWidget()->palette();
81 KColorScheme colorScheme{QPalette::Normal, KColorScheme::ColorSet::Window};
82 colorScheme.adjustBackground(palette, KColorScheme::NegativeBackground, QPalette::Window, KColorScheme::ColorSet::Window);
83 colorScheme.adjustForeground(palette, KColorScheme::NegativeText, QPalette::WindowText, KColorScheme::ColorSet::Window);
84 setPalette(palette);
85 }
86
87 void Bar::updateLabelString()
88 {
89 QFontMetrics fontMetrics = m_label->fontMetrics();
90 if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_warningButton->sizeHint().width() + m_closeButton->sizeHint().width()
91 + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2
92 < width()) {
93 m_label->setText(m_fullLabelString);
94 } else {
95 m_label->setText(m_shortLabelString);
96 }
97 }
98
99 #include "moc_bar.cpp"