| 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 "kiconbar.h" |
| 24 |
#include "parmscontroller.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QApplication> |
| 27 |
#include <QDebug> |
| 28 |
#include <QHBoxLayout> |
| 29 |
#include <QMouseEvent> |
| 30 |
#include "accessinfohelper.h" |
| 31 |
|
| 32 |
namespace kdk |
| 33 |
{ |
| 34 |
class KIconBarPrivate : public QObject, public ThemeController |
| 35 |
{ |
| 36 |
Q_OBJECT |
| 37 |
Q_DECLARE_PUBLIC(KIconBar) |
| 38 |
|
| 39 |
public: |
| 40 |
KIconBarPrivate(KIconBar *parent); |
| 41 |
void elideWidgetName(); |
| 42 |
|
| 43 |
protected: |
| 44 |
void changeTheme(); |
| 45 |
void changeIconStyle(); |
| 46 |
bool eventFilter(QObject *watched, QEvent *event); |
| 47 |
|
| 48 |
private: |
| 49 |
KIconBar *q_ptr; |
| 50 |
QLabel *m_pIconLabel; |
| 51 |
QLabel *m_pNameLabel; |
| 52 |
QHBoxLayout *m_pMainLayout; |
| 53 |
QString m_IconName; |
| 54 |
QString m_widgetName; |
| 55 |
QColor inactiveColor; |
| 56 |
}; |
| 57 |
|
| 58 |
KIconBar::KIconBar(QWidget *parent) |
| 59 |
: QFrame(parent) |
| 60 |
, d_ptr(new KIconBarPrivate(this)) |
| 61 |
{ |
| 62 |
Q_D(KIconBar); |
| 63 |
setObjectName("IconBar"); |
| 64 |
setFixedHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_IconbarHeight)); |
| 65 |
connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() { |
| 66 |
setFixedHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_IconbarHeight)); |
| 67 |
d->m_pIconLabel->setPixmap(QIcon::fromTheme(d->m_IconName).pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize), Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize)))); |
| 68 |
updateGeometry(); |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
KIconBar::KIconBar(const QString &iconName, const QString &text, QWidget *parent) |
| 73 |
: KIconBar(parent) |
| 74 |
{ |
| 75 |
setIcon(iconName); |
| 76 |
setWidgetName(text); |
| 77 |
} |
| 78 |
|
| 79 |
KIconBar::~KIconBar() |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
void KIconBar::setIcon(const QString &iconName) |
| 84 |
{ |
| 85 |
Q_D(KIconBar); |
| 86 |
|
| 87 |
if (iconName.isEmpty()) |
| 88 |
return; |
| 89 |
if (!d->m_pIconLabel) |
| 90 |
return; |
| 91 |
d->m_IconName = iconName; |
| 92 |
d->m_pIconLabel->setPixmap(QIcon::fromTheme(iconName).pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize), Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize)))); |
| 93 |
setWindowIcon(QIcon::fromTheme(iconName).pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize), Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize)))); |
| 94 |
} |
| 95 |
|
| 96 |
void KIconBar::setIcon(const QIcon &icon) |
| 97 |
{ |
| 98 |
Q_D(KIconBar); |
| 99 |
|
| 100 |
if (!d->m_pIconLabel) |
| 101 |
return; |
| 102 |
d->m_pIconLabel->setPixmap(icon.pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize), Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize)))); |
| 103 |
d->m_IconName = icon.name(); |
| 104 |
setWindowIcon(icon.pixmap(QSize(Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize), Parmscontroller::parm(Parmscontroller::Parm::PM_IconBarIconSize)))); |
| 105 |
} |
| 106 |
|
| 107 |
void KIconBar::setWidgetName(const QString &widgetName) |
| 108 |
{ |
| 109 |
Q_D(KIconBar); |
| 110 |
|
| 111 |
if (widgetName.isEmpty()) |
| 112 |
return; |
| 113 |
if (!d->m_pNameLabel) |
| 114 |
return; |
| 115 |
d->m_widgetName = widgetName; |
| 116 |
d->elideWidgetName(); |
| 117 |
} |
| 118 |
|
| 119 |
QSize KIconBar::sizeHint() const |
| 120 |
{ |
| 121 |
auto size = QFrame::sizeHint(); |
| 122 |
size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_PushButtonHeight)); |
| 123 |
return size; |
| 124 |
} |
| 125 |
QLabel *KIconBar::nameLabel() |
| 126 |
{ |
| 127 |
Q_D(KIconBar); |
| 128 |
return d->m_pNameLabel; |
| 129 |
} |
| 130 |
|
| 131 |
QLabel *KIconBar::iconLabel() |
| 132 |
{ |
| 133 |
Q_D(KIconBar); |
| 134 |
return d->m_pIconLabel; |
| 135 |
} |
| 136 |
|
| 137 |
void KIconBar::mouseDoubleClickEvent(QMouseEvent *event) |
| 138 |
{ |
| 139 |
Q_D(KIconBar); |
| 140 |
if (event->button() == Qt::LeftButton) |
| 141 |
Q_EMIT doubleClick(); |
| 142 |
} |
| 143 |
|
| 144 |
void KIconBar::resizeEvent(QResizeEvent *event) |
| 145 |
{ |
| 146 |
Q_D(KIconBar); |
| 147 |
QFrame::resizeEvent(event); |
| 148 |
d->elideWidgetName(); |
| 149 |
} |
| 150 |
|
| 151 |
KIconBarPrivate::KIconBarPrivate(KIconBar *parent) |
| 152 |
: q_ptr(parent) |
| 153 |
{ |
| 154 |
Q_Q(KIconBar); |
| 155 |
setParent(parent); |
| 156 |
parent->installEventFilter(this); |
| 157 |
q->setContentsMargins(0, 0, 0, 0); |
| 158 |
m_pMainLayout = new QHBoxLayout(q); |
| 159 |
m_pIconLabel = new QLabel(q); |
| 160 |
m_pIconLabel->setScaledContents(true); |
| 161 |
m_pIconLabel->setFixedSize(24, 24); |
| 162 |
m_pNameLabel = new QLabel(q); |
| 163 |
|
| 164 |
QHBoxLayout *iconLayout = new QHBoxLayout(); |
| 165 |
iconLayout->setSpacing(0); |
| 166 |
iconLayout->setContentsMargins(0, 8, 0, 8); |
| 167 |
iconLayout->addWidget(m_pIconLabel); |
| 168 |
|
| 169 |
m_pMainLayout->setSpacing(8); |
| 170 |
m_pMainLayout->addSpacing(8); |
| 171 |
m_pMainLayout->addLayout(iconLayout); |
| 172 |
m_pMainLayout->addWidget(m_pNameLabel); |
| 173 |
m_pMainLayout->setContentsMargins(0, 0, 0, 0); |
| 174 |
m_pMainLayout->addStretch(); |
| 175 |
|
| 176 |
changeIconStyle(); |
| 177 |
changeTheme(); |
| 178 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 179 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName"){ |
| 180 |
changeIconStyle(); |
| 181 |
changeTheme(); |
| 182 |
} |
| 183 |
if(key == "systemFontSize") |
| 184 |
elideWidgetName(); |
| 185 |
}); |
| 186 |
|
| 187 |
KDK_ALL_INFO_FORMAT(m_pIconLabel,""); |
| 188 |
KDK_ALL_INFO_FORMAT(m_pNameLabel,""); |
| 189 |
} |
| 190 |
|
| 191 |
void KIconBarPrivate::elideWidgetName() |
| 192 |
{ |
| 193 |
Q_Q(KIconBar); |
| 194 |
|
| 195 |
QFontMetrics fm = QApplication::fontMetrics(); |
| 196 |
// bug 175664 修正1px |
| 197 |
auto visualWidth = q->width() - m_pNameLabel->geometry().left() - 1; |
| 198 |
QString elidedText = fm.elidedText(m_widgetName, Qt::TextElideMode::ElideRight, visualWidth); |
| 199 |
m_pNameLabel->setText(elidedText); |
| 200 |
if (fm.horizontalAdvance(m_widgetName) >= visualWidth) |
| 201 |
m_pNameLabel->setToolTip(m_widgetName); |
| 202 |
else |
| 203 |
m_pNameLabel->setToolTip(""); |
| 204 |
} |
| 205 |
|
| 206 |
void KIconBarPrivate::changeTheme() |
| 207 |
{ |
| 208 |
Q_Q(KIconBar); |
| 209 |
initThemeStyle(); |
| 210 |
} |
| 211 |
|
| 212 |
void KIconBarPrivate::changeIconStyle() |
| 213 |
{ |
| 214 |
Q_Q(KIconBar); |
| 215 |
initThemeStyle(); |
| 216 |
q->setIcon(m_IconName); |
| 217 |
if (!q->isActiveWindow()) { |
| 218 |
if(ThemeController::themeMode() == LightTheme) |
| 219 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-placeholdertext"); |
| 220 |
else |
| 221 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-secondary-disable"); |
| 222 |
QPalette palette = q->palette(); |
| 223 |
palette.setBrush(QPalette::Inactive, QPalette::Text, inactiveColor); |
| 224 |
m_pNameLabel->setPalette(palette); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 229 |
QPalette palette = q->palette(); |
| 230 |
palette.setBrush(QPalette::Active, QPalette::Text, inactiveColor); |
| 231 |
m_pNameLabel->setPalette(palette); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
bool KIconBarPrivate::eventFilter(QObject *watched, QEvent *event) |
| 236 |
{ |
| 237 |
Q_Q(KIconBar); |
| 238 |
// 窗口停用,变为不活动的窗口 |
| 239 |
if (QEvent::WindowDeactivate == event->type()) { |
| 240 |
if(ThemeController::themeMode() == LightTheme) |
| 241 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-placeholdertext"); |
| 242 |
else |
| 243 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-secondary-disable"); |
| 244 |
QPalette palette = q->palette(); |
| 245 |
palette.setBrush(QPalette::Inactive, QPalette::Text, inactiveColor); |
| 246 |
m_pNameLabel->setPalette(palette); |
| 247 |
} |
| 248 |
else if(QEvent::WindowActivate == event->type()) |
| 249 |
{ |
| 250 |
inactiveColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 251 |
QPalette palette = q->palette(); |
| 252 |
palette.setBrush(QPalette::Active, QPalette::Text, inactiveColor); |
| 253 |
m_pNameLabel->setPalette(palette); |
| 254 |
} |
| 255 |
return QObject::eventFilter(watched, event); |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
#include "kiconbar.moc" |
| 260 |
#include "moc_kiconbar.cpp" |