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")), "", 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
);
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);
58 layout
->addWidget(m_label
);
59 layout
->addWidget(m_warningButton
);
61 layout
->addWidget(m_closeButton
);
64 bool Bar::event(QEvent
*event
)
66 if (event
->type() == QEvent::PaletteChange
) {
69 return AnimatedHeightWidget::event(event
);
72 void Bar::resizeEvent(QResizeEvent
*resizeEvent
)
75 return QWidget::resizeEvent(resizeEvent
);
78 void Bar::updateColors()
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
);
87 void Bar::updateLabelString()
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
93 m_label
->setText(m_fullLabelString
);
95 m_label
->setText(m_shortLabelString
);
99 #include "moc_bar.cpp"