| 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: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "knativetabbar.h" |
| 24 |
#include "parmscontroller.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QApplication> |
| 27 |
#include <QDebug> |
| 28 |
#include <QEvent> |
| 29 |
#include <QFontMetrics> |
| 30 |
#include <QMouseEvent> |
| 31 |
#include <QPainter> |
| 32 |
#include <QPainterPath> |
| 33 |
#include <QStyleOptionTab> |
| 34 |
#include <QStylePainter> |
| 35 |
#include <QVariantAnimation> |
| 36 |
|
| 37 |
namespace kdk |
| 38 |
{ |
| 39 |
class Q_DECL_HIDDEN KNativeTabbarPrivate : public QObject, public ThemeController |
| 40 |
{ |
| 41 |
Q_OBJECT |
| 42 |
Q_DECLARE_PUBLIC(KNativeTabbar) |
| 43 |
|
| 44 |
public: |
| 45 |
KNativeTabbarPrivate(KNativeTabbar *parent) |
| 46 |
: q_ptr(parent) |
| 47 |
, m_animation(new QVariantAnimation(this)) |
| 48 |
, m_nextTabWidth(0) |
| 49 |
, m_animationStarted(false) |
| 50 |
{ |
| 51 |
Q_Q(KNativeTabbar); |
| 52 |
setParent(parent); |
| 53 |
parent->installEventFilter(this); |
| 54 |
m_animation->setDuration(300); |
| 55 |
m_animation->setEasingCurve(QEasingCurve::InOutQuad); |
| 56 |
connect(m_animation, &QVariantAnimation::valueChanged, q, [=]() { |
| 57 |
q->update(); |
| 58 |
}); |
| 59 |
connect(m_animation, &QVariantAnimation::finished, this, [=]() { |
| 60 |
m_animationStarted = false; |
| 61 |
}); |
| 62 |
|
| 63 |
m_borderRadius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 64 |
if (m_borderRadius == -1) |
| 65 |
m_borderRadius = 6; |
| 66 |
} |
| 67 |
void changeTheme(); |
| 68 |
int getIndexAtPos(const QPoint &p); |
| 69 |
|
| 70 |
protected: |
| 71 |
bool eventFilter(QObject *watched, QEvent *event); |
| 72 |
|
| 73 |
private: |
| 74 |
KNativeTabbar *q_ptr; |
| 75 |
KNativeTabbarStyle m_KNativeTabbarStyle; |
| 76 |
int m_borderRadius; |
| 77 |
int m_horizontalMargin = 1; |
| 78 |
int m_topMargin = 0; |
| 79 |
QColor m_bkgrdColor; |
| 80 |
bool m_useCustomColor; |
| 81 |
QVariantAnimation *m_animation; |
| 82 |
int m_nextTabWidth; |
| 83 |
bool m_animationStarted; |
| 84 |
QLinearGradient m_linearGradient ; |
| 85 |
}; |
| 86 |
|
| 87 |
KNativeTabbar::KNativeTabbar(KNativeTabbarStyle barStyle, QWidget *parent) |
| 88 |
: KCustomTabBar(parent) |
| 89 |
, d_ptr(new KNativeTabbarPrivate(this)) |
| 90 |
{ |
| 91 |
Q_D(KNativeTabbar); |
| 92 |
setAttribute(Qt::WA_Hover, true); |
| 93 |
d->m_borderRadius = 6; |
| 94 |
d->m_KNativeTabbarStyle = barStyle; |
| 95 |
d->m_useCustomColor = false; |
| 96 |
|
| 97 |
// this->setObjectName("KNativeTabbar"); |
| 98 |
d->changeTheme(); |
| 99 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 100 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 101 |
{ |
| 102 |
d->changeTheme(); |
| 103 |
} |
| 104 |
}); |
| 105 |
connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=](bool flag) { |
| 106 |
updateGeometry(); |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
KNativeTabbar::~KNativeTabbar() |
| 111 |
{ |
| 112 |
} |
| 113 |
|
| 114 |
void KNativeTabbar::setTabbarStyle(KNativeTabbarStyle barStyle) |
| 115 |
{ |
| 116 |
Q_D(KNativeTabbar); |
| 117 |
d->m_KNativeTabbarStyle = barStyle; |
| 118 |
d->changeTheme(); |
| 119 |
} |
| 120 |
KNativeTabbarStyle KNativeTabbar::barStyle() |
| 121 |
{ |
| 122 |
Q_D(KNativeTabbar); |
| 123 |
return d->m_KNativeTabbarStyle; |
| 124 |
} |
| 125 |
|
| 126 |
void KNativeTabbar::setBorderRadius(int radius) |
| 127 |
{ |
| 128 |
Q_D(KNativeTabbar); |
| 129 |
if (radius < 0 || radius > 20) |
| 130 |
return; |
| 131 |
d->m_borderRadius = radius; |
| 132 |
update(); |
| 133 |
} |
| 134 |
|
| 135 |
int KNativeTabbar::borderRadius() |
| 136 |
{ |
| 137 |
Q_D(KNativeTabbar); |
| 138 |
if (d->m_KNativeTabbarStyle == Sliding) |
| 139 |
return 0; |
| 140 |
else |
| 141 |
return d->m_borderRadius; |
| 142 |
} |
| 143 |
|
| 144 |
void KNativeTabbar::setBackgroundColor(const QColor &color) |
| 145 |
{ |
| 146 |
Q_D(KNativeTabbar); |
| 147 |
d->m_useCustomColor = true; |
| 148 |
d->m_bkgrdColor = color; |
| 149 |
} |
| 150 |
|
| 151 |
QSize KNativeTabbar::sizeHint() const |
| 152 |
{ |
| 153 |
auto size = KCustomTabBar::sizeHint(); |
| 154 |
size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_TabBarHeight)); |
| 155 |
return size; |
| 156 |
} |
| 157 |
|
| 158 |
QSize KNativeTabbar::minimumTabSizeHint(int index) const |
| 159 |
{ |
| 160 |
Q_UNUSED(index) |
| 161 |
QSize size(100, Parmscontroller::parm(Parmscontroller::Parm::PM_PushButtonHeight)); |
| 162 |
return size; |
| 163 |
} |
| 164 |
|
| 165 |
QSize KNativeTabbar::tabSizeHint(int index) const |
| 166 |
{ |
| 167 |
Q_UNUSED(index) |
| 168 |
// 判断外部是否设置了固定宽度或者固定高度,如果设置,走默认的tabSizeHint |
| 169 |
if ((this->maximumHeight() == this->minimumHeight()) || this->maximumWidth() == this->minimumWidth()) |
| 170 |
return KCustomTabBar::tabSizeHint(index); |
| 171 |
auto size = KCustomTabBar::tabSizeHint(index); |
| 172 |
size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_TabBarHeight)); |
| 173 |
return size; |
| 174 |
} |
| 175 |
|
| 176 |
void KNativeTabbar::paintEvent(QPaintEvent *event) |
| 177 |
{ |
| 178 |
Q_D(KNativeTabbar); |
| 179 |
QPainter p(this); |
| 180 |
p.setRenderHint(QPainter::Antialiasing); |
| 181 |
p.setRenderHint(QPainter::TextAntialiasing); |
| 182 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
| 183 |
|
| 184 |
QColor fontColor; |
| 185 |
QColor mix; |
| 186 |
QColor startColor; |
| 187 |
QColor endColor; |
| 188 |
|
| 189 |
QFontMetrics fm = p.fontMetrics(); |
| 190 |
for (int i = 0; i < count(); ++i) { |
| 191 |
QStyleOptionTab option; |
| 192 |
initStyleOption(&option, i); |
| 193 |
QRect rect = option.rect.adjusted(d->m_horizontalMargin, 0, 0, -d->m_topMargin); |
| 194 |
rect.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_PushButtonHeight)); |
| 195 |
d->m_linearGradient = QLinearGradient(rect.left() + rect.width()/2, rect.top(),rect.left() + rect.width()/2, rect.bottom()); |
| 196 |
switch (d->m_KNativeTabbarStyle) { |
| 197 |
case SegmentDark: { |
| 198 |
mix = ThemeController::getCustomColorFromDT("brighttext-active"); |
| 199 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 200 |
QColor bkgrdColor = d->m_bkgrdColor; |
| 201 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 202 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 203 |
if (option.state.testFlag(QStyle::State_Selected)) { |
| 204 |
bkgrdColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 205 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 206 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 207 |
fontColor = ThemeController::getCustomColorFromDT("kwhite"); |
| 208 |
} else if (option.state.testFlag(QStyle::State_MouseOver)) { |
| 209 |
ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor); |
| 210 |
d->m_linearGradient.setColorAt(0,startColor); |
| 211 |
d->m_linearGradient.setColorAt(1,endColor); |
| 212 |
} |
| 213 |
if (option.position == QStyleOptionTab::Middle) { |
| 214 |
p.save(); |
| 215 |
p.setBrush(d->m_linearGradient); |
| 216 |
p.setPen(Qt::NoPen); |
| 217 |
// 利用quadto绘制圆角矩形会出现一个像素的偏差,修正一下QRect底部高度 |
| 218 |
p.drawRect(rect.adjusted(0, 1, -1, -1)); // 调整启典 上描边 |
| 219 |
p.restore(); |
| 220 |
p.setBrush(Qt::NoBrush); |
| 221 |
p.setPen(fontColor); |
| 222 |
QPoint point; |
| 223 |
uint tf = Qt::AlignVCenter; |
| 224 |
if (!option.icon.isNull()) { |
| 225 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 226 |
: QIcon::Disabled; |
| 227 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 228 |
mode = QIcon::Active; |
| 229 |
QIcon::State state = QIcon::Off; |
| 230 |
if (option.state & QStyle::State_On) |
| 231 |
state = QIcon::On; |
| 232 |
|
| 233 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 234 |
if(ThemeController::isPixmapPureColor(pixmap)) |
| 235 |
pixmap = ThemeController::drawColoredPixmap(pixmap,fontColor); |
| 236 |
|
| 237 |
|
| 238 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 239 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 240 |
|
| 241 |
if (!tabText(i).isEmpty()) |
| 242 |
w += option.fontMetrics.boundingRect(option.rect, tf, tabText(i)).width() + 2; |
| 243 |
|
| 244 |
point = QPoint(rect.x() + rect.width() / 2 - w / 2, |
| 245 |
rect.y() + rect.height() / 2 - h / 2); |
| 246 |
|
| 247 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 248 |
|
| 249 |
if (option.direction == Qt::RightToLeft) |
| 250 |
point.rx() += w; |
| 251 |
|
| 252 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) |
| 253 |
p.drawPixmap(option.rect.x() + 4, this->style()->visualPos(option.direction, option.rect, point).y(), pixmap); |
| 254 |
else |
| 255 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 256 |
|
| 257 |
if (!tabText(i).isEmpty()) { |
| 258 |
int subH = std::max(option.iconSize.height(), option.fontMetrics.height()); |
| 259 |
int icon_Y = (rect.height() - subH) / 2; |
| 260 |
int text_X = point.x() + option.iconSize.width() + 4; |
| 261 |
int text_Y = icon_Y; |
| 262 |
|
| 263 |
QRect textRect; |
| 264 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) { |
| 265 |
textRect = QRect(option.rect.x() + option.iconSize.width() + 8, text_Y, option.rect.width() - option.iconSize.width() - 7, option.fontMetrics.height()); |
| 266 |
setTabToolTip(i, tabText(i)); |
| 267 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width() - option.iconSize.width() - 7); |
| 268 |
p.drawText(textRect, tf, elidedText); |
| 269 |
} else { |
| 270 |
textRect = QRect(text_X, text_Y, option.fontMetrics.horizontalAdvance(tabText(i)), option.fontMetrics.height()); |
| 271 |
setTabToolTip(i, ""); |
| 272 |
p.drawText(textRect, tf, tabText(i)); |
| 273 |
} |
| 274 |
} |
| 275 |
} else { |
| 276 |
tf |= Qt::AlignHCenter; |
| 277 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width()) { |
| 278 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width()); |
| 279 |
p.drawText(rect, tf, elidedText); |
| 280 |
setTabToolTip(i, tabText(i)); |
| 281 |
} else { |
| 282 |
setTabToolTip(i, ""); |
| 283 |
p.drawText(rect, tf, tabText(i)); |
| 284 |
} |
| 285 |
} |
| 286 |
} else if (option.position == QStyleOptionTab::Beginning) { |
| 287 |
p.save(); |
| 288 |
p.setBrush(d->m_linearGradient); |
| 289 |
p.setPen(Qt::NoPen); |
| 290 |
QPainterPath path; |
| 291 |
auto tempRect = rect.adjusted(0, 0, 1, 0); |
| 292 |
path.moveTo(tempRect.topLeft() + QPointF(0, d->m_borderRadius)); |
| 293 |
path.lineTo(tempRect.bottomLeft() - QPointF(0, d->m_borderRadius)); |
| 294 |
path.quadTo(tempRect.bottomLeft(), tempRect.bottomLeft() + QPointF(d->m_borderRadius, 0)); |
| 295 |
path.lineTo(tempRect.bottomRight()); |
| 296 |
path.lineTo(tempRect.topRight()); |
| 297 |
path.lineTo(tempRect.topLeft() + QPointF(d->m_borderRadius, 0)); |
| 298 |
path.quadTo(tempRect.topLeft(), tempRect.topLeft() + QPointF(0, d->m_borderRadius)); |
| 299 |
p.drawPath(path); |
| 300 |
p.restore(); |
| 301 |
p.setBrush(Qt::NoBrush); |
| 302 |
p.setPen(fontColor); |
| 303 |
QPoint point; |
| 304 |
uint tf = Qt::AlignVCenter; |
| 305 |
if (!option.icon.isNull()) { |
| 306 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 307 |
: QIcon::Disabled; |
| 308 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 309 |
mode = QIcon::Active; |
| 310 |
QIcon::State state = QIcon::Off; |
| 311 |
if (option.state & QStyle::State_On) |
| 312 |
state = QIcon::On; |
| 313 |
|
| 314 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 315 |
if(ThemeController::isPixmapPureColor(pixmap)) |
| 316 |
pixmap = ThemeController::drawColoredPixmap(pixmap,fontColor); |
| 317 |
|
| 318 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 319 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 320 |
|
| 321 |
if (!tabText(i).isEmpty()) |
| 322 |
w += option.fontMetrics.boundingRect(option.rect, tf, tabText(i)).width() + 2; |
| 323 |
|
| 324 |
point = QPoint(rect.x() + rect.width() / 2 - w / 2, |
| 325 |
rect.y() + rect.height() / 2 - h / 2); |
| 326 |
|
| 327 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 328 |
|
| 329 |
if (option.direction == Qt::RightToLeft) |
| 330 |
point.rx() += w; |
| 331 |
|
| 332 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) |
| 333 |
p.drawPixmap(option.rect.x() + 4, this->style()->visualPos(option.direction, option.rect, point).y(), pixmap); |
| 334 |
else |
| 335 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 336 |
|
| 337 |
if (option.direction == Qt::RightToLeft) |
| 338 |
rect.translate(-point.x() - 2, 0); |
| 339 |
else |
| 340 |
rect.translate(point.x() + w + 4, 0); |
| 341 |
|
| 342 |
if (!tabText(i).isEmpty()) { |
| 343 |
tf |= Qt::AlignLeft; |
| 344 |
int subH = std::max(option.iconSize.height(), option.fontMetrics.height()); |
| 345 |
int icon_Y = (rect.height() - subH) / 2; |
| 346 |
int text_X = point.x() + option.iconSize.width() + 4; |
| 347 |
int text_Y = icon_Y; |
| 348 |
QRect textRect; |
| 349 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) { |
| 350 |
textRect = QRect(option.rect.x() + option.iconSize.width() + 8, text_Y, option.rect.width() - option.iconSize.width() - 8, option.fontMetrics.height()); |
| 351 |
setTabToolTip(i, tabText(i)); |
| 352 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width() - option.iconSize.width() - 8); |
| 353 |
p.drawText(textRect, tf, elidedText); |
| 354 |
} else { |
| 355 |
textRect = QRect(text_X, text_Y, option.fontMetrics.horizontalAdvance(tabText(i)), option.fontMetrics.height()); |
| 356 |
setTabToolTip(i, ""); |
| 357 |
p.drawText(textRect, tf, tabText(i)); |
| 358 |
} |
| 359 |
} |
| 360 |
} else { |
| 361 |
tf |= Qt::AlignHCenter; |
| 362 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width()) { |
| 363 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width()); |
| 364 |
p.drawText(rect, tf, elidedText); |
| 365 |
setTabToolTip(i, tabText(i)); |
| 366 |
} else { |
| 367 |
p.drawText(rect, tf, tabText(i)); |
| 368 |
setTabToolTip(i, ""); |
| 369 |
} |
| 370 |
} |
| 371 |
} else { |
| 372 |
p.save(); |
| 373 |
p.setBrush(d->m_linearGradient); |
| 374 |
rect = rect.adjusted(0, 1, 0, 0); // 调整启典 上描边 |
| 375 |
p.setPen(Qt::NoPen); |
| 376 |
QPainterPath path; |
| 377 |
path.moveTo(rect.topLeft()); |
| 378 |
path.lineTo(rect.bottomLeft()); |
| 379 |
path.lineTo(rect.bottomRight() - QPointF(d->m_borderRadius, 0)); |
| 380 |
path.quadTo(rect.bottomRight(), rect.bottomRight() - QPointF(0, d->m_borderRadius)); |
| 381 |
path.lineTo(rect.topRight() + QPointF(0, d->m_borderRadius)); |
| 382 |
path.quadTo(rect.topRight(), rect.topRight() - QPointF(d->m_borderRadius, 0)); |
| 383 |
path.lineTo(rect.topLeft()); |
| 384 |
p.drawPath(path); |
| 385 |
p.restore(); |
| 386 |
p.setBrush(Qt::NoBrush); |
| 387 |
p.setPen(fontColor); |
| 388 |
QPoint point; |
| 389 |
uint tf = Qt::AlignVCenter; |
| 390 |
if (!option.icon.isNull()) { |
| 391 |
|
| 392 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 393 |
: QIcon::Disabled; |
| 394 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 395 |
mode = QIcon::Active; |
| 396 |
QIcon::State state = QIcon::Off; |
| 397 |
if (option.state & QStyle::State_On) |
| 398 |
state = QIcon::On; |
| 399 |
|
| 400 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 401 |
if(ThemeController::isPixmapPureColor(pixmap)) |
| 402 |
pixmap = ThemeController::drawColoredPixmap(pixmap,fontColor); |
| 403 |
|
| 404 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 405 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 406 |
|
| 407 |
if (!tabText(i).isEmpty()) |
| 408 |
w += option.fontMetrics.boundingRect(option.rect, tf, tabText(i)).width() + 4; |
| 409 |
|
| 410 |
point = QPoint(rect.x() + rect.width() / 2 - w / 2, |
| 411 |
rect.y() + rect.height() / 2 - h / 2); |
| 412 |
|
| 413 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 414 |
|
| 415 |
if (option.direction == Qt::RightToLeft) |
| 416 |
point.rx() += w; |
| 417 |
|
| 418 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) |
| 419 |
p.drawPixmap(option.rect.x() + 4, this->style()->visualPos(option.direction, option.rect, point).y(), pixmap); |
| 420 |
else |
| 421 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 422 |
|
| 423 |
if (!tabText(i).isEmpty()) { |
| 424 |
int subH = std::max(option.iconSize.height(), option.fontMetrics.height()); |
| 425 |
int icon_Y = (rect.height() - subH) / 2; |
| 426 |
int text_X = point.x() + option.iconSize.width() + 4; |
| 427 |
int text_Y = icon_Y; |
| 428 |
QRect textRect; |
| 429 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) { |
| 430 |
textRect = QRect(option.rect.x() + option.iconSize.width() + 8, text_Y, option.rect.width() - option.iconSize.width() - 8, option.fontMetrics.height()); |
| 431 |
setTabToolTip(i, tabText(i)); |
| 432 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width() - option.iconSize.width() - 8); |
| 433 |
p.drawText(textRect, elidedText); |
| 434 |
} else { |
| 435 |
textRect = QRect(text_X, text_Y, option.fontMetrics.horizontalAdvance(tabText(i)), option.fontMetrics.height()); |
| 436 |
setTabToolTip(i, ""); |
| 437 |
p.drawText(textRect, tabText(i)); |
| 438 |
} |
| 439 |
} |
| 440 |
} else { |
| 441 |
tf |= Qt::AlignHCenter; |
| 442 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width()) { |
| 443 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width()); |
| 444 |
p.drawText(rect, tf, elidedText); |
| 445 |
setTabToolTip(i, tabText(i)); |
| 446 |
} else { |
| 447 |
p.drawText(rect, tf, tabText(i)); |
| 448 |
setTabToolTip(i, ""); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
break; |
| 453 |
} |
| 454 |
case SegmentLight: { |
| 455 |
mix = ThemeController::getCustomColorFromDT("brighttext-active"); |
| 456 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 457 |
QColor bkgrdColor = d->m_bkgrdColor; |
| 458 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 459 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 460 |
if (option.state.testFlag(QStyle::State_Selected)) { |
| 461 |
bkgrdColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 462 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 463 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 464 |
fontColor = ThemeController::getCustomColorFromDT("kwhite"); |
| 465 |
} else if (option.state.testFlag(QStyle::State_MouseOver)) { |
| 466 |
ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor); |
| 467 |
d->m_linearGradient.setColorAt(0,startColor); |
| 468 |
d->m_linearGradient.setColorAt(1,endColor); |
| 469 |
} |
| 470 |
p.save(); |
| 471 |
p.setBrush(d->m_linearGradient); |
| 472 |
p.setPen(Qt::NoPen); |
| 473 |
p.drawRoundedRect(/*option.rect.adjusted*/ rect.adjusted(d->m_horizontalMargin, 0, 0, -d->m_topMargin), |
| 474 |
d->m_borderRadius, d->m_borderRadius); |
| 475 |
p.restore(); |
| 476 |
p.setBrush(Qt::NoBrush); |
| 477 |
p.setPen(fontColor); |
| 478 |
/*QRect*/ rect = /*option.*/ rect.adjusted(d->m_horizontalMargin, 0, 0, -d->m_topMargin); |
| 479 |
QPoint point; |
| 480 |
uint tf = Qt::AlignVCenter; |
| 481 |
if (!option.icon.isNull()) { |
| 482 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 483 |
: QIcon::Disabled; |
| 484 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 485 |
mode = QIcon::Active; |
| 486 |
QIcon::State state = QIcon::Off; |
| 487 |
if (option.state & QStyle::State_On) |
| 488 |
state = QIcon::On; |
| 489 |
|
| 490 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 491 |
if(ThemeController::isPixmapPureColor(pixmap)) |
| 492 |
pixmap = ThemeController::drawColoredPixmap(pixmap,fontColor); |
| 493 |
|
| 494 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 495 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 496 |
|
| 497 |
if (!tabText(i).isEmpty()) |
| 498 |
w += option.fontMetrics.boundingRect(option.rect, tf, tabText(i)).width() + 2; |
| 499 |
|
| 500 |
point = QPoint(rect.x() + rect.width() / 2 - w / 2, |
| 501 |
rect.y() + rect.height() / 2 - h / 2); |
| 502 |
|
| 503 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 504 |
|
| 505 |
if (option.direction == Qt::RightToLeft) |
| 506 |
point.rx() += w; |
| 507 |
|
| 508 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) |
| 509 |
p.drawPixmap(option.rect.x() + 4, this->style()->visualPos(option.direction, option.rect, point).y(), pixmap); |
| 510 |
else |
| 511 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 512 |
|
| 513 |
if (!tabText(i).isEmpty()) { |
| 514 |
int subH = std::max(option.iconSize.height(), option.fontMetrics.height()); |
| 515 |
int icon_Y = (rect.height() - subH) / 2; |
| 516 |
int text_X = point.x() + option.iconSize.width() + 4; |
| 517 |
int text_Y = icon_Y; |
| 518 |
QRect textRect; |
| 519 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) { |
| 520 |
textRect = QRect(option.rect.x() + option.iconSize.width() + 8, text_Y, option.rect.width() - option.iconSize.width() - 8, option.fontMetrics.height()); |
| 521 |
setTabToolTip(i, tabText(i)); |
| 522 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width() - option.iconSize.width() - 8); |
| 523 |
p.drawText(textRect, elidedText); |
| 524 |
} else { |
| 525 |
textRect = QRect(text_X, text_Y, option.fontMetrics.horizontalAdvance(tabText(i)), option.fontMetrics.height()); |
| 526 |
setTabToolTip(i, ""); |
| 527 |
p.drawText(textRect, tabText(i)); |
| 528 |
} |
| 529 |
} |
| 530 |
} else { |
| 531 |
tf |= Qt::AlignHCenter; |
| 532 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width()) { |
| 533 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width()); |
| 534 |
p.drawText(rect, tf, elidedText); |
| 535 |
setTabToolTip(i, tabText(i)); |
| 536 |
} else { |
| 537 |
p.drawText(rect, tf, tabText(i)); |
| 538 |
setTabToolTip(i, ""); |
| 539 |
} |
| 540 |
} |
| 541 |
break; |
| 542 |
} |
| 543 |
case Sliding: { |
| 544 |
mix = ThemeController::getCustomColorFromDT("brighttext-active"); |
| 545 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 546 |
QColor bkgrdColor = d->m_bkgrdColor; |
| 547 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 548 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 549 |
if (option.state.testFlag(QStyle::State_Selected)) |
| 550 |
{ |
| 551 |
bkgrdColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 552 |
fontColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 553 |
d->m_linearGradient.setColorAt(0,bkgrdColor); |
| 554 |
d->m_linearGradient.setColorAt(1,bkgrdColor); |
| 555 |
} |
| 556 |
else if (option.state.testFlag(QStyle::State_MouseOver)) |
| 557 |
{ |
| 558 |
ThemeController::getGradientFromDT("kline-normal",startColor,endColor); |
| 559 |
d->m_linearGradient.setColorAt(0,Qt::red); |
| 560 |
d->m_linearGradient.setColorAt(1,Qt::blue); |
| 561 |
} |
| 562 |
|
| 563 |
p.save(); |
| 564 |
QPen pen; |
| 565 |
if (i != currentIndex()) |
| 566 |
pen.setColor(bkgrdColor); |
| 567 |
else |
| 568 |
pen.setColor(d->m_bkgrdColor); |
| 569 |
pen.setWidth(2); |
| 570 |
p.setPen(pen); |
| 571 |
/*QRect*/ rect = /*option.*/ rect.adjusted(d->m_horizontalMargin, 0, 0, -5); |
| 572 |
p.drawLine(rect.bottomLeft(), rect.bottomRight()); |
| 573 |
p.restore(); |
| 574 |
|
| 575 |
if (d->m_animationStarted) { |
| 576 |
int left_border = d->m_animation->currentValue().toInt(); |
| 577 |
p.save(); |
| 578 |
QPen pen; |
| 579 |
pen.setWidth(2); |
| 580 |
pen.setColor(ThemeController::getCustomColorFromDT("highlight-active")); |
| 581 |
p.setPen(pen); |
| 582 |
p.drawLine(QPoint(left_border, rect.height() - 1), QPoint(left_border + d->m_nextTabWidth, rect.height() - 1)); |
| 583 |
p.restore(); |
| 584 |
} else { |
| 585 |
p.save(); |
| 586 |
QPen pen; |
| 587 |
pen.setColor(bkgrdColor); |
| 588 |
pen.setWidth(2); |
| 589 |
p.setPen(pen); |
| 590 |
p.drawLine(rect.bottomLeft(), rect.bottomRight()); |
| 591 |
p.restore(); |
| 592 |
} |
| 593 |
|
| 594 |
p.setBrush(Qt::NoBrush); |
| 595 |
p.setPen(fontColor); |
| 596 |
QPoint point; |
| 597 |
uint tf = Qt::AlignVCenter; |
| 598 |
if (!option.icon.isNull()) { |
| 599 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 600 |
: QIcon::Disabled; |
| 601 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 602 |
mode = QIcon::Active; |
| 603 |
QIcon::State state = QIcon::Off; |
| 604 |
if (option.state & QStyle::State_On) |
| 605 |
state = QIcon::On; |
| 606 |
|
| 607 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 608 |
if(ThemeController::isPixmapPureColor(pixmap)) |
| 609 |
pixmap = ThemeController::drawColoredPixmap(pixmap,fontColor); |
| 610 |
|
| 611 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 612 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 613 |
|
| 614 |
if (!tabText(i).isEmpty()) |
| 615 |
w += option.fontMetrics.boundingRect(option.rect, tf, tabText(i)).width() + 2; |
| 616 |
|
| 617 |
point = QPoint(rect.x() + rect.width() / 2 - w / 2, |
| 618 |
rect.y() + rect.height() / 2 - h / 2); |
| 619 |
|
| 620 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 621 |
|
| 622 |
if (option.direction == Qt::RightToLeft) |
| 623 |
point.rx() += w; |
| 624 |
|
| 625 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) |
| 626 |
p.drawPixmap(option.rect.x() + 4, this->style()->visualPos(option.direction, option.rect, point).y(), pixmap); |
| 627 |
else |
| 628 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 629 |
|
| 630 |
if (!tabText(i).isEmpty()) { |
| 631 |
int subH = std::max(option.iconSize.height(), option.fontMetrics.height()); |
| 632 |
int icon_Y = (rect.height() - subH) / 2; |
| 633 |
int text_X = point.x() + option.iconSize.width() + 4; |
| 634 |
int text_Y = icon_Y; |
| 635 |
QRect textRect; |
| 636 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width() - option.iconSize.width() - 7) { |
| 637 |
textRect = QRect(option.rect.x() + option.iconSize.width() + 8, text_Y, option.rect.width() - option.iconSize.width() - 8, option.fontMetrics.height()); |
| 638 |
setTabToolTip(i, tabText(i)); |
| 639 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width() - option.iconSize.width() - 8); |
| 640 |
p.drawText(textRect, elidedText); |
| 641 |
} else { |
| 642 |
textRect = QRect(text_X, text_Y, option.fontMetrics.horizontalAdvance(tabText(i)), option.fontMetrics.height()); |
| 643 |
setTabToolTip(i, ""); |
| 644 |
p.drawText(textRect, tabText(i)); |
| 645 |
} |
| 646 |
} |
| 647 |
} else { |
| 648 |
tf |= Qt::AlignHCenter; |
| 649 |
if (fm.horizontalAdvance(tabText(i)) >= option.rect.width()) { |
| 650 |
QString elidedText = fm.elidedText(tabText(i), Qt::ElideRight, option.rect.width()); |
| 651 |
p.drawText(rect, tf, elidedText); |
| 652 |
setTabToolTip(i, tabText(i)); |
| 653 |
} else { |
| 654 |
p.drawText(rect, tf, tabText(i)); |
| 655 |
setTabToolTip(i, ""); |
| 656 |
} |
| 657 |
} |
| 658 |
break; |
| 659 |
} |
| 660 |
default: |
| 661 |
break; |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
void KNativeTabbarPrivate::changeTheme() |
| 667 |
{ |
| 668 |
Q_Q(KNativeTabbar); |
| 669 |
switch (m_KNativeTabbarStyle) { |
| 670 |
case SegmentDark: { |
| 671 |
if (m_useCustomColor) |
| 672 |
return; |
| 673 |
else |
| 674 |
m_bkgrdColor = ThemeController::getCustomColorFromDT("button-active"); |
| 675 |
break; |
| 676 |
} |
| 677 |
case SegmentLight: { |
| 678 |
if (m_useCustomColor) |
| 679 |
return; |
| 680 |
else |
| 681 |
m_bkgrdColor = ThemeController::getCustomColorFromDT("kgray-alpha0"); |
| 682 |
break; |
| 683 |
} |
| 684 |
case Sliding: |
| 685 |
if (m_useCustomColor) |
| 686 |
return; |
| 687 |
else |
| 688 |
m_bkgrdColor = ThemeController::getCustomColorFromDT("button-active"); |
| 689 |
break; |
| 690 |
default: |
| 691 |
break; |
| 692 |
}; |
| 693 |
q->update(); |
| 694 |
} |
| 695 |
|
| 696 |
int KNativeTabbarPrivate::getIndexAtPos(const QPoint &p) |
| 697 |
{ |
| 698 |
Q_Q(KNativeTabbar); |
| 699 |
if (q->tabRect(q->currentIndex()).contains(p)) |
| 700 |
return q->currentIndex(); |
| 701 |
for (int i = 0; i < q->count(); ++i) |
| 702 |
if (q->tabRect(i).contains(p)) |
| 703 |
return i; |
| 704 |
return -1; |
| 705 |
} |
| 706 |
|
| 707 |
bool KNativeTabbarPrivate::eventFilter(QObject *watched, QEvent *event) |
| 708 |
{ |
| 709 |
Q_Q(KNativeTabbar); |
| 710 |
if (event->type() == QEvent::MouseButtonPress) { |
| 711 |
if (q->isEnabled()) { |
| 712 |
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event); |
| 713 |
if (mouseEvent && mouseEvent->button() == Qt::MouseButton::LeftButton) { |
| 714 |
int nextIndex = getIndexAtPos(mouseEvent->pos()); |
| 715 |
int currentIndex = q->currentIndex(); |
| 716 |
if (nextIndex != currentIndex) { |
| 717 |
int cur_left_border = q->tabRect(currentIndex).left(); |
| 718 |
int next_left_border = q->tabRect(nextIndex).left(); |
| 719 |
m_nextTabWidth = q->tabRect(nextIndex).width(); |
| 720 |
m_animation->setStartValue(cur_left_border); |
| 721 |
m_animation->setEndValue(next_left_border); |
| 722 |
m_animation->start(); |
| 723 |
m_animationStarted = true; |
| 724 |
} |
| 725 |
} |
| 726 |
if (mouseEvent && mouseEvent->button() == Qt::MouseButton::RightButton) { |
| 727 |
emit q->rightlicked(mouseEvent->globalPos()); |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
return QObject::eventFilter(watched, event); |
| 732 |
} |
| 733 |
|
| 734 |
} |
| 735 |
#include "knativetabbar.moc" |
| 736 |
#include "moc_knativetabbar.cpp" |