]> cloud.milkyroute.net Git - dolphin.git/blob - src/admin/bar.cpp
f2e7250ada999d2ddf3f74418ab297e132df0269
[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")),
45 i18nc("@action:button Finish/Stop/Done acting as an admin", "Finish"),
46 contenntsContainer);
47 m_closeButton->setToolTip(i18nc("@info:tooltip", "Finish acting as an administrator"));
48 m_closeButton->setFlat(true);
49 connect(m_closeButton, &QAbstractButton::clicked, this, &Bar::activated); // Make sure the view connected to this bar is active before exiting admin mode.
50 connect(m_closeButton, &QAbstractButton::clicked, this, &WorkerIntegration::exitAdminMode);
51
52 QHBoxLayout *layout = new QHBoxLayout(contenntsContainer);
53 auto contentsMargins = layout->contentsMargins();
54 m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
55 m_warningButton->setFixedHeight(m_preferredHeight);
56 m_closeButton->setFixedHeight(m_preferredHeight);
57 layout->setContentsMargins(0, 0, 0, 0);
58
59 layout->addStretch();
60 layout->addWidget(m_label);
61 layout->addWidget(m_warningButton);
62 layout->addStretch();
63 layout->addWidget(m_closeButton);
64 }
65
66 bool Bar::event(QEvent *event)
67 {
68 if (event->type() == QEvent::PaletteChange) {
69 updateColors();
70 }
71 return AnimatedHeightWidget::event(event);
72 }
73
74 void Bar::resizeEvent(QResizeEvent *resizeEvent)
75 {
76 updateLabelString();
77 return QWidget::resizeEvent(resizeEvent);
78 }
79
80 void Bar::updateColors()
81 {
82 QPalette palette = parentWidget()->palette();
83 KColorScheme colorScheme{QPalette::Normal, KColorScheme::ColorSet::Window};
84 colorScheme.adjustBackground(palette, KColorScheme::NegativeBackground, QPalette::Window, KColorScheme::ColorSet::Window);
85 colorScheme.adjustForeground(palette, KColorScheme::NegativeText, QPalette::WindowText, KColorScheme::ColorSet::Window);
86 setPalette(palette);
87 }
88
89 void Bar::updateLabelString()
90 {
91 QFontMetrics fontMetrics = m_label->fontMetrics();
92 if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_warningButton->sizeHint().width() + m_closeButton->sizeHint().width()
93 + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2
94 < width()) {
95 m_label->setText(m_fullLabelString);
96 } else {
97 m_label->setText(m_shortLabelString);
98 }
99 }
100
101 #include "moc_bar.cpp"