| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ksecuritylevelbar.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QDebug> |
| 27 |
#include <QLabel> |
| 28 |
#include <QPainter> |
| 29 |
#include <QPalette> |
| 30 |
#include "accessinfohelper.h" |
| 31 |
|
| 32 |
namespace kdk |
| 33 |
{ |
| 34 |
class KSecurityLevelBarPrivate : public QObject |
| 35 |
{ |
| 36 |
Q_OBJECT |
| 37 |
Q_DECLARE_PUBLIC(KSecurityLevelBar) |
| 38 |
public: |
| 39 |
KSecurityLevelBarPrivate(KSecurityLevelBar *parent); |
| 40 |
|
| 41 |
private: |
| 42 |
KSecurityLevelBar *q_ptr; |
| 43 |
SecurityLevel m_level; |
| 44 |
QLabel *m_pLabel; |
| 45 |
int m_contentMargin = 5; |
| 46 |
int m_textWidth = 0; |
| 47 |
int m_space = 5; |
| 48 |
}; |
| 49 |
|
| 50 |
KSecurityLevelBar::KSecurityLevelBar(QWidget *parent) |
| 51 |
: QWidget(parent) |
| 52 |
, d_ptr(new KSecurityLevelBarPrivate(this)) |
| 53 |
{ |
| 54 |
Q_D(KSecurityLevelBar); |
| 55 |
setFixedSize(300, 30); |
| 56 |
} |
| 57 |
|
| 58 |
void KSecurityLevelBar::setSecurityLevel(SecurityLevel level) |
| 59 |
{ |
| 60 |
Q_D(KSecurityLevelBar); |
| 61 |
d->m_level = level; |
| 62 |
switch (level) { |
| 63 |
case Low: |
| 64 |
d->m_pLabel->setText(tr("Low")); |
| 65 |
break; |
| 66 |
case Medium: |
| 67 |
d->m_pLabel->setText(tr("Medium")); |
| 68 |
break; |
| 69 |
case High: |
| 70 |
d->m_pLabel->setText(tr("High")); |
| 71 |
break; |
| 72 |
default: |
| 73 |
break; |
| 74 |
} |
| 75 |
repaint(); |
| 76 |
} |
| 77 |
|
| 78 |
SecurityLevel KSecurityLevelBar::securityLevel() |
| 79 |
{ |
| 80 |
Q_D(KSecurityLevelBar); |
| 81 |
return d->m_level; |
| 82 |
} |
| 83 |
|
| 84 |
void KSecurityLevelBar::paintEvent(QPaintEvent *event) |
| 85 |
{ |
| 86 |
Q_D(KSecurityLevelBar); |
| 87 |
QPainter painter(this); |
| 88 |
painter.setRenderHint(QPainter::Antialiasing); |
| 89 |
painter.setPen(Qt::NoPen); |
| 90 |
d->m_pLabel->adjustSize(); |
| 91 |
d->m_textWidth = this->fontMetrics().horizontalAdvance(d->m_pLabel->text()) + d->m_space; |
| 92 |
int rectWitdth = 32; //(rect().width() - d->m_textWidth - d->m_contentMargin * 2) / 3 - d->m_space; |
| 93 |
int rectHeight = 8; /*height() > 6 ? 6 : height();*/ |
| 94 |
int currentPos = d->m_contentMargin + d->m_textWidth + d->m_space; |
| 95 |
switch (d->m_level) { |
| 96 |
case Low: |
| 97 |
painter.setBrush(ThemeController::getCustomColorFromDT("kerror-normal")); |
| 98 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 99 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 100 |
painter.setBrush(ThemeController::getCustomColorFromDT("kcomponent-normal")); |
| 101 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 102 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 103 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 104 |
break; |
| 105 |
case Medium: |
| 106 |
painter.setBrush(ThemeController::getCustomColorFromDT("kwarning-normal")); |
| 107 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 108 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 109 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 110 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 111 |
painter.setBrush(ThemeController::getCustomColorFromDT("kcomponent-normal")); |
| 112 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 113 |
break; |
| 114 |
case High: |
| 115 |
painter.setBrush(ThemeController::getCustomColorFromDT("ksuccess-normal")); |
| 116 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 117 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 118 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 119 |
currentPos = currentPos + d->m_space + rectWitdth; |
| 120 |
painter.drawRoundedRect(currentPos, (rect().height() - rectHeight) / 2, rectWitdth, rectHeight, rectHeight / 2, rectHeight / 2); |
| 121 |
break; |
| 122 |
default: |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
KSecurityLevelBarPrivate::KSecurityLevelBarPrivate(KSecurityLevelBar *parent) |
| 128 |
: q_ptr(parent) |
| 129 |
{ |
| 130 |
Q_Q(KSecurityLevelBar); |
| 131 |
m_pLabel = new QLabel(q); |
| 132 |
m_pLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); |
| 133 |
int textHeight = q->fontMetrics().height(); |
| 134 |
int yPos = (q->height() - textHeight) / 2; |
| 135 |
m_pLabel->setGeometry(q->rect().left(), yPos, m_textWidth, textHeight); |
| 136 |
m_level = Low; |
| 137 |
m_pLabel->setText(tr("Low")); |
| 138 |
setParent(parent); |
| 139 |
|
| 140 |
KDK_ALL_INFO_FORMAT(m_pLabel,""); |
| 141 |
} |
| 142 |
} |
| 143 |
#include "ksecuritylevelbar.moc" |
| 144 |
#include "moc_ksecuritylevelbar.cpp" |