2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2024 Felix Ernst <felixernst@kde.org>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
10 #include "workerintegration.h"
12 #include <KColorScheme>
13 #include <KContextualHelpButton>
14 #include <KLocalizedString>
17 #include <QGuiApplication>
18 #include <QHBoxLayout>
20 #include <QPushButton>
22 #include <QToolButton>
23 #include <QWidgetAction>
25 using namespace Admin
;
27 Bar::Bar(QWidget
*parent
)
28 : AnimatedHeightWidget
{parent
}
30 setAutoFillBackground(true);
33 QWidget
*contenntsContainer
= prepareContentsContainer();
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
41 m_warningButton
= new KContextualHelpButton(warningMessage(), nullptr, contenntsContainer
);
42 m_warningButton
->setIcon(QIcon::fromTheme(QStringLiteral("emblem-warning")));
44 m_closeButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")),
45 i18nc("@action:button Finish/Stop/Done acting as an admin", "Finish"),
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
);
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);
60 layout
->addWidget(m_label
);
61 layout
->addWidget(m_warningButton
);
63 layout
->addWidget(m_closeButton
);
66 bool Bar::event(QEvent
*event
)
68 if (event
->type() == QEvent::PaletteChange
) {
71 return AnimatedHeightWidget::event(event
);
74 void Bar::resizeEvent(QResizeEvent
*resizeEvent
)
77 return QWidget::resizeEvent(resizeEvent
);
80 void Bar::updateColors()
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
);
89 void Bar::updateLabelString()
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
95 m_label
->setText(m_fullLabelString
);
97 m_label
->setText(m_shortLabelString
);
101 #include "moc_bar.cpp"