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