| 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 "kslider.h" |
| 24 |
#include "parmscontroller.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QDebug> |
| 27 |
#include <QMouseEvent> |
| 28 |
#include <QPainter> |
| 29 |
#include <QPainterPath> |
| 30 |
#include <QPalette> |
| 31 |
#include <QStyle> |
| 32 |
#include <QStyleOptionButton> |
| 33 |
#include <QStyleOptionSlider> |
| 34 |
#include <QToolTip> |
| 35 |
#include <QVariantAnimation> |
| 36 |
#include <QTime> |
| 37 |
|
| 38 |
using namespace kdk; |
| 39 |
|
| 40 |
namespace kdk |
| 41 |
{ |
| 42 |
|
| 43 |
static const int node_radius = 5; |
| 44 |
static const int handle_radius = 10; |
| 45 |
static const int spaceing = handle_radius + 2; |
| 46 |
static const int line_width = 4; |
| 47 |
static const int line_border_radius = 2; |
| 48 |
|
| 49 |
class KSliderPrivate : public QObject, public ThemeController |
| 50 |
{ |
| 51 |
Q_DECLARE_PUBLIC(KSlider) |
| 52 |
public: |
| 53 |
KSliderPrivate(KSlider *parent); |
| 54 |
~KSliderPrivate(); |
| 55 |
void animationStart(); |
| 56 |
|
| 57 |
private: |
| 58 |
KSlider *q_ptr; |
| 59 |
KSliderType m_sliderType; |
| 60 |
void drawCoverNode(QPainter *painter); |
| 61 |
void drawSlider(QPainter *painter); |
| 62 |
void locateNode(); |
| 63 |
void drawBasePath(QPainter *painter); |
| 64 |
void drawCoverLine(QPainter *painter); |
| 65 |
void updateValuePosition(); |
| 66 |
void updateValue(); |
| 67 |
void updateColor(QPalette palette); |
| 68 |
void changeTheme(); |
| 69 |
QRect baseLineRect(); |
| 70 |
QRect coverLineRect(); |
| 71 |
QList<QPoint> m_nodes; |
| 72 |
bool m_isPressed; |
| 73 |
bool m_isHovered; |
| 74 |
int m_valuePosition; |
| 75 |
QPoint m_currentpos; |
| 76 |
QColor m_baseColor; |
| 77 |
QColor m_coverColor; |
| 78 |
QColor m_startColor; |
| 79 |
QColor m_endColor; |
| 80 |
bool m_nodeVisible; |
| 81 |
QRect m_rect; |
| 82 |
QString m_pToolTipText; |
| 83 |
QLinearGradient m_linearGradient; |
| 84 |
bool m_isTranslucent; |
| 85 |
bool m_isTranslucentFlag; |
| 86 |
int m_handleRadius; |
| 87 |
QVariantAnimation *m_animation; |
| 88 |
bool m_animationStarted; |
| 89 |
bool m_ismoving; |
| 90 |
bool m_resize; |
| 91 |
bool m_forceUpdate; |
| 92 |
QTime m_clickTime; |
| 93 |
}; |
| 94 |
|
| 95 |
KSliderPrivate::KSliderPrivate(KSlider *parent) |
| 96 |
: q_ptr(parent) |
| 97 |
, m_sliderType(SmoothSlider) |
| 98 |
, m_isPressed(false) |
| 99 |
, m_isHovered(false) |
| 100 |
, m_valuePosition(0) |
| 101 |
, m_currentpos(QPoint(0, 0)) |
| 102 |
, m_nodeVisible(true) |
| 103 |
, m_handleRadius(0) |
| 104 |
, m_animationStarted(false) |
| 105 |
, m_ismoving(false) |
| 106 |
, m_resize(false) |
| 107 |
, m_forceUpdate(false) |
| 108 |
, m_isTranslucent(false) |
| 109 |
, m_isTranslucentFlag(false) |
| 110 |
{ |
| 111 |
Q_Q(KSlider); |
| 112 |
q->setProperty("animation", false); |
| 113 |
QStyleOptionSlider sliderOption; |
| 114 |
m_animation = new QVariantAnimation(this); |
| 115 |
m_animation->setDuration(150); |
| 116 |
m_animation->setEasingCurve(QEasingCurve::InOutQuad); |
| 117 |
connect(m_animation, &QVariantAnimation::finished, q, [=]() { |
| 118 |
m_animationStarted = false; |
| 119 |
if (m_animation->endValue().toInt() != m_valuePosition) |
| 120 |
q->update(); |
| 121 |
}); |
| 122 |
connect(m_animation, &QVariantAnimation::valueChanged, q, [=]() { |
| 123 |
q->update(); |
| 124 |
}); |
| 125 |
animationStart(); |
| 126 |
connect(q, &KSlider::valueChanged, this, [=]() { |
| 127 |
animationStart(); |
| 128 |
}); |
| 129 |
connect(q, &KSlider::sliderReleased, this, [=]() { |
| 130 |
if(m_isPressed) |
| 131 |
m_isPressed = false; |
| 132 |
}); |
| 133 |
// 获取滑动块大小 |
| 134 |
QRect handle = q->style()->proxy()->subControlRect(QStyle::CC_Slider, &sliderOption, QStyle::SC_SliderHandle, q); |
| 135 |
m_handleRadius = handle.width() / 2; |
| 136 |
} |
| 137 |
|
| 138 |
KSliderPrivate::~KSliderPrivate() |
| 139 |
{ |
| 140 |
} |
| 141 |
|
| 142 |
void KSliderPrivate::animationStart() |
| 143 |
{ |
| 144 |
Q_Q(KSlider); |
| 145 |
// int prePosition = m_valuePosition; |
| 146 |
updateValuePosition(); |
| 147 |
|
| 148 |
// if ((m_forceUpdate || !m_animationStarted) && !m_ismoving) { |
| 149 |
// m_animation->stop(); |
| 150 |
// m_animation->setStartValue(prePosition); |
| 151 |
// m_animation->setEndValue(m_valuePosition); |
| 152 |
// m_animation->start(); |
| 153 |
// m_animationStarted = true; |
| 154 |
// m_forceUpdate = false; |
| 155 |
// m_resize = false; |
| 156 |
// } |
| 157 |
} |
| 158 |
|
| 159 |
void KSliderPrivate::drawCoverNode(QPainter *painter) |
| 160 |
{ |
| 161 |
Q_Q(KSlider); |
| 162 |
painter->setRenderHint(QPainter::Antialiasing); |
| 163 |
painter->setPen(Qt::NoPen); |
| 164 |
painter->setBrush(ThemeController::getCustomColorFromDT("highlight-active")); |
| 165 |
int side = 0; |
| 166 |
if(q->isEnabled()) |
| 167 |
painter->setBrush(ThemeController::getCustomColorFromDT("kbrand-normal")); |
| 168 |
else |
| 169 |
painter->setBrush(ThemeController::getCustomColorFromDT("kbrand-disable")); |
| 170 |
if (q->orientation() == Qt::Horizontal) { |
| 171 |
for (auto point : m_nodes) { |
| 172 |
if (m_ismoving) |
| 173 |
side = m_currentpos.x(); |
| 174 |
else if (m_resize) |
| 175 |
side = m_valuePosition; |
| 176 |
else |
| 177 |
side = m_animation->currentValue().toInt(); |
| 178 |
QRect rect(point.x() - 1, point.y() - 5, 3, 10); |
| 179 |
if (point.x() <= side) |
| 180 |
{ |
| 181 |
int radius = ThemeController::getRadiusFromDT("kradius-min"); |
| 182 |
painter->drawRoundedRect(rect,radius,radius); // 选中基点 |
| 183 |
} |
| 184 |
} |
| 185 |
} else { |
| 186 |
for (auto point : m_nodes) { |
| 187 |
if (m_ismoving) |
| 188 |
side = m_currentpos.y(); |
| 189 |
else if (m_resize) |
| 190 |
side = m_valuePosition; |
| 191 |
else |
| 192 |
side = m_animation->currentValue().toInt(); |
| 193 |
QRect rect(point.x() - 5, point.y() - 3, 10, 3); |
| 194 |
if (point.y() >= side) |
| 195 |
{ |
| 196 |
int radius = ThemeController::getRadiusFromDT("kradius-min"); |
| 197 |
painter->drawRoundedRect(rect,radius,radius); // 选中基点 |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void KSliderPrivate::drawSlider(QPainter *painter) |
| 204 |
{ |
| 205 |
Q_Q(KSlider); |
| 206 |
painter->setRenderHint(QPainter::Antialiasing); |
| 207 |
painter->setBrush(Qt::white); |
| 208 |
QPen pen ; |
| 209 |
// if (ThemeController::widgetTheme() == FashionTheme && m_isHovered && !m_isPressed && q->isEnabled()) { |
| 210 |
pen.setBrush(m_linearGradient); |
| 211 |
pen.setWidth(3); |
| 212 |
painter->setPen(pen); |
| 213 |
// 获取滑动块大小 |
| 214 |
|
| 215 |
int handle_radius = Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) / 2 - 2; |
| 216 |
QPoint point; |
| 217 |
if (q->orientation() == Qt::Horizontal) { |
| 218 |
int x = 0; |
| 219 |
if (m_ismoving) |
| 220 |
x = m_currentpos.x(); |
| 221 |
else if (m_resize) |
| 222 |
x = m_valuePosition; |
| 223 |
else |
| 224 |
x = m_animation->currentValue().toInt(); |
| 225 |
point = QPoint(x, q->height() / 2); |
| 226 |
} else { |
| 227 |
int y = 0; |
| 228 |
if (m_ismoving) |
| 229 |
y = m_currentpos.y(); |
| 230 |
else if (m_resize) |
| 231 |
y = m_valuePosition; |
| 232 |
else |
| 233 |
y = m_animation->currentValue().toInt(); |
| 234 |
point = QPoint(q->width() / 2, y); |
| 235 |
} |
| 236 |
if( (!m_isPressed && !m_isHovered && q->isEnabled() ) || !q->isEnabled()) |
| 237 |
handle_radius = handle_radius - 2; |
| 238 |
painter->drawEllipse(point, handle_radius, handle_radius); |
| 239 |
m_rect = QRect(point.x() - handle_radius, point.y() - handle_radius, handle_radius * 2 , handle_radius * 2); |
| 240 |
} |
| 241 |
|
| 242 |
void KSliderPrivate::locateNode() |
| 243 |
{ |
| 244 |
Q_Q(KSlider); |
| 245 |
if (q->orientation() == Qt::Horizontal) { |
| 246 |
m_nodes.clear(); |
| 247 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 248 |
QPoint beginNode(baseLineRect().left(), q->height() / 2); |
| 249 |
QPoint endNode(baseLineRect().right(), q->height() / 2); |
| 250 |
m_nodes.append(beginNode); |
| 251 |
|
| 252 |
if (!q->tickInterval()) { |
| 253 |
m_nodes.append(endNode); |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
for (int i = q->minimum() + q->tickInterval(); i < q->maximum(); i += q->tickInterval()) { |
| 258 |
QPoint point; |
| 259 |
int x = baseLineRect().left() + baseLineRect().width() * (i - q->minimum()) / (q->maximum() - q->minimum()); |
| 260 |
int y = q->height() / 2; |
| 261 |
point.setX(x); |
| 262 |
point.setY(y); |
| 263 |
m_nodes.append(point); |
| 264 |
} |
| 265 |
m_nodes.append(endNode); |
| 266 |
} else { |
| 267 |
QPoint beginNode(baseLineRect().right(), q->height() / 2); |
| 268 |
QPoint endNode(baseLineRect().left(), q->height() / 2); |
| 269 |
m_nodes.append(beginNode); |
| 270 |
|
| 271 |
if (!q->tickInterval()) { |
| 272 |
m_nodes.append(endNode); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
for (int i = q->minimum() + q->tickInterval(); i < q->maximum(); i += q->tickInterval()) { |
| 277 |
QPoint point; |
| 278 |
int x = baseLineRect().right() - baseLineRect().width() * (i - q->minimum()) / (q->maximum() - q->minimum()); |
| 279 |
int y = q->height() / 2; |
| 280 |
point.setX(x); |
| 281 |
point.setY(y); |
| 282 |
m_nodes.append(point); |
| 283 |
} |
| 284 |
m_nodes.append(endNode); |
| 285 |
} |
| 286 |
} else { |
| 287 |
m_nodes.clear(); |
| 288 |
QPoint beginNode(q->width() / 2, baseLineRect().bottom()); |
| 289 |
QPoint endNode(q->width() / 2, baseLineRect().top()); |
| 290 |
m_nodes.append(beginNode); |
| 291 |
if (!q->tickInterval()) { |
| 292 |
m_nodes.append(endNode); |
| 293 |
return; |
| 294 |
} |
| 295 |
for (int i = q->minimum() + q->tickInterval(); i < q->maximum(); i += q->tickInterval()) { |
| 296 |
QPoint point; |
| 297 |
int x = q->width() / 2; |
| 298 |
int y = baseLineRect().top() + baseLineRect().height() * (i - q->minimum()) / (q->maximum() - q->minimum()); |
| 299 |
point.setX(x); |
| 300 |
point.setY(y); |
| 301 |
m_nodes.append(point); |
| 302 |
} |
| 303 |
m_nodes.append(endNode); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
void KSliderPrivate::drawBasePath(QPainter *painter) |
| 308 |
{ |
| 309 |
Q_Q(KSlider); |
| 310 |
painter->setRenderHint(QPainter::Antialiasing); |
| 311 |
painter->setPen(Qt::NoPen); |
| 312 |
painter->setBrush(m_baseColor); |
| 313 |
QPainterPath basePath; |
| 314 |
basePath.addRoundedRect(this->baseLineRect(), line_border_radius, line_border_radius); |
| 315 |
|
| 316 |
QColor color; |
| 317 |
color = ThemeController::getCustomColorFromDT("kcomponent-alpha-normal"); |
| 318 |
basePath.addRect(this->baseLineRect()); |
| 319 |
|
| 320 |
painter->setBrush(m_baseColor); |
| 321 |
|
| 322 |
if (m_nodeVisible) { |
| 323 |
painter->setBrush(color); |
| 324 |
for (auto point : m_nodes) { |
| 325 |
if (q->orientation() == Qt::Horizontal) |
| 326 |
basePath.addRect(point.x() - 1, point.y() - 5, 3, 10); |
| 327 |
else |
| 328 |
basePath.addRect(point.x() - 5, point.y() - 3, 10, 3); |
| 329 |
} |
| 330 |
} |
| 331 |
basePath.setFillRule(Qt::FillRule::WindingFill); |
| 332 |
painter->drawPath(basePath.simplified()); |
| 333 |
} |
| 334 |
|
| 335 |
void KSliderPrivate::drawCoverLine(QPainter *painter) |
| 336 |
{ |
| 337 |
Q_Q(KSlider); |
| 338 |
painter->setRenderHint(QPainter::Antialiasing); |
| 339 |
painter->setPen(Qt::NoPen); |
| 340 |
painter->setBrush(m_coverColor); |
| 341 |
painter->drawRoundedRect(this->coverLineRect(), line_border_radius, line_border_radius); |
| 342 |
} |
| 343 |
|
| 344 |
void KSliderPrivate::updateValuePosition() |
| 345 |
{ |
| 346 |
Q_Q(KSlider); |
| 347 |
|
| 348 |
int prePosition = m_valuePosition; |
| 349 |
if (q->orientation() == Qt::Horizontal) { |
| 350 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 351 |
if (q->maximum() != q->minimum()) |
| 352 |
m_valuePosition = this->baseLineRect().left() + this->baseLineRect().width() * (q->value() - q->minimum()) / (q->maximum() - q->minimum()); |
| 353 |
} else { |
| 354 |
if (q->maximum() != q->minimum()) { |
| 355 |
m_valuePosition = this->baseLineRect().adjusted(0, 0, 1, 0).right() - this->baseLineRect().width() * (q->value() - q->minimum()) / (q->maximum() - q->minimum()); |
| 356 |
} |
| 357 |
} |
| 358 |
} else { |
| 359 |
if (q->maximum() != q->minimum()) |
| 360 |
m_valuePosition = this->baseLineRect().top() + this->baseLineRect().height() |
| 361 |
- this->baseLineRect().height() * (q->value() - q->minimum()) / (q->maximum() - q->minimum()); |
| 362 |
} |
| 363 |
|
| 364 |
if ((m_forceUpdate || !m_animationStarted) && !m_ismoving) { |
| 365 |
m_animation->stop(); |
| 366 |
m_animation->setStartValue(prePosition); |
| 367 |
m_animation->setEndValue(m_valuePosition); |
| 368 |
m_animation->start(); |
| 369 |
m_animationStarted = true; |
| 370 |
m_resize = false; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
void KSliderPrivate::updateValue() |
| 375 |
{ |
| 376 |
Q_Q(KSlider); |
| 377 |
int dur = q->maximum() - q->minimum(); |
| 378 |
int pos, value; |
| 379 |
int step = q->singleStep(); |
| 380 |
int nodeInterval = q->tickInterval(); |
| 381 |
if (q->orientation() == Qt::Horizontal) { |
| 382 |
int left, width, right; |
| 383 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 384 |
if (m_currentpos.x() > baseLineRect().right()) |
| 385 |
value = q->maximum(); |
| 386 |
else if (m_currentpos.x() < baseLineRect().left()) |
| 387 |
value = q->minimum(); |
| 388 |
pos = qRound(q->minimum() + dur * ((double)m_currentpos.x() - baseLineRect().left()) / baseLineRect().width()); |
| 389 |
left = baseLineRect().left(); |
| 390 |
width = baseLineRect().width(); |
| 391 |
} else { |
| 392 |
if (m_currentpos.x() > baseLineRect().right()) |
| 393 |
value = q->minimum(); |
| 394 |
else if (m_currentpos.x() < baseLineRect().left()) |
| 395 |
value = q->maximum(); |
| 396 |
pos = qRound(q->minimum() - dur * ((double)m_currentpos.x() - baseLineRect().right() - 1) / baseLineRect().width()); |
| 397 |
right = baseLineRect().right(); |
| 398 |
width = baseLineRect().width(); |
| 399 |
} |
| 400 |
switch (m_sliderType) { |
| 401 |
case SingleSelectSlider: |
| 402 |
case SmoothSlider: |
| 403 |
value = pos; |
| 404 |
break; |
| 405 |
case StepSlider: { |
| 406 |
int frontIndex = (pos - q->minimum()) / step; |
| 407 |
int backIndex = (pos - q->minimum()) / step + 1; |
| 408 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 409 |
if ((m_currentpos.x() - left - frontIndex * step * width / dur) |
| 410 |
< (backIndex * step * width / dur - (m_currentpos.x() - left))) |
| 411 |
pos = frontIndex * step + q->minimum(); |
| 412 |
else |
| 413 |
pos = backIndex * step + q->minimum(); |
| 414 |
} else { |
| 415 |
if ((right - m_currentpos.x() - frontIndex * step * width / dur) |
| 416 |
< (backIndex * step * width / dur - (right - m_currentpos.x()))) |
| 417 |
pos = frontIndex * step + q->minimum(); |
| 418 |
else |
| 419 |
pos = backIndex * step + q->minimum(); |
| 420 |
} |
| 421 |
value = pos; |
| 422 |
} break; |
| 423 |
case NodeSlider: { |
| 424 |
int frontIndex = (pos - q->minimum()) / nodeInterval; |
| 425 |
int backIndex = (pos - q->minimum()) / nodeInterval + 1; |
| 426 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 427 |
if ((m_currentpos.x() - left - frontIndex * nodeInterval * width / dur) |
| 428 |
< (backIndex * nodeInterval * width / dur - (m_currentpos.x() - left))) |
| 429 |
pos = frontIndex * nodeInterval + q->minimum(); |
| 430 |
else |
| 431 |
pos = backIndex * nodeInterval + q->minimum(); |
| 432 |
} else { |
| 433 |
if ((right - m_currentpos.x() - frontIndex * nodeInterval * width / dur) |
| 434 |
< (backIndex * nodeInterval * width / dur - (right - m_currentpos.x()))) |
| 435 |
pos = frontIndex * nodeInterval + q->minimum(); |
| 436 |
else |
| 437 |
pos = backIndex * nodeInterval + q->minimum(); |
| 438 |
} |
| 439 |
value = pos; |
| 440 |
} break; |
| 441 |
default: |
| 442 |
break; |
| 443 |
} |
| 444 |
} else { |
| 445 |
if (m_currentpos.y() < baseLineRect().top()) |
| 446 |
value = q->maximum(); |
| 447 |
else if (m_currentpos.y() > baseLineRect().bottom()) |
| 448 |
value = q->minimum(); |
| 449 |
pos = qRound(q->minimum() + dur * ((double)q->height() - m_currentpos.y()) / q->height()); |
| 450 |
int top = baseLineRect().top(); |
| 451 |
int height = baseLineRect().height(); |
| 452 |
switch (m_sliderType) { |
| 453 |
case SingleSelectSlider: |
| 454 |
case SmoothSlider: |
| 455 |
value = pos; |
| 456 |
break; |
| 457 |
case StepSlider: { |
| 458 |
int frontIndex = (pos - q->minimum()) / step + 1; |
| 459 |
int backIndex = (pos - q->minimum()) / step; |
| 460 |
if ((m_currentpos.y() - top - (height - frontIndex * step * height / dur)) < ((height - backIndex * step * height / dur) - (m_currentpos.y() - top))) |
| 461 |
pos = frontIndex * step + q->minimum(); |
| 462 |
else |
| 463 |
pos = backIndex * step + q->minimum(); |
| 464 |
value = pos; |
| 465 |
} break; |
| 466 |
case NodeSlider: { |
| 467 |
int frontIndex = (pos - q->minimum()) / nodeInterval + 1; |
| 468 |
int backIndex = (pos - q->minimum()) / nodeInterval; |
| 469 |
if ((m_currentpos.y() - top - (height - frontIndex * nodeInterval * height / dur)) < ((height - backIndex * nodeInterval * height / dur) - (m_currentpos.y() - top))) |
| 470 |
pos = frontIndex * nodeInterval + q->minimum(); |
| 471 |
else |
| 472 |
pos = backIndex * nodeInterval + q->minimum(); |
| 473 |
value = pos; |
| 474 |
} break; |
| 475 |
default: |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
if (q->tickInterval() != 0) { |
| 480 |
if (!m_ismoving) |
| 481 |
{ |
| 482 |
q->setValue(value); |
| 483 |
} |
| 484 |
} else { |
| 485 |
q->setValue(value); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void KSliderPrivate::updateColor(QPalette palette) |
| 490 |
{ |
| 491 |
Q_Q(KSlider); |
| 492 |
QColor startColor; |
| 493 |
QColor endColor; |
| 494 |
|
| 495 |
if (m_isTranslucent) |
| 496 |
{ |
| 497 |
m_baseColor = ThemeController::getCustomColorFromDT("brighttext-active"); |
| 498 |
m_baseColor.setAlphaF(0.1); |
| 499 |
} else |
| 500 |
m_baseColor = ThemeController::getCustomColorFromDT("button-active"); |
| 501 |
|
| 502 |
if (!q->isEnabled()) |
| 503 |
{ |
| 504 |
m_coverColor = ThemeController::getCustomColorFromDT("kbrand-disable"); |
| 505 |
ThemeController::getGradientFromDT("kbrand-disable", startColor, endColor); |
| 506 |
m_linearGradient.setColorAt(0, startColor); |
| 507 |
m_linearGradient.setColorAt(1, endColor); |
| 508 |
return; |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
m_coverColor = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 513 |
if (m_isPressed) |
| 514 |
{ |
| 515 |
ThemeController::getGradientFromDT("kbrand-hover", startColor, endColor); |
| 516 |
m_linearGradient.setColorAt(0, startColor); |
| 517 |
m_linearGradient.setColorAt(1, endColor); |
| 518 |
} |
| 519 |
else if (m_isHovered) |
| 520 |
{ |
| 521 |
ThemeController::getGradientFromDT("kbrand-hover", startColor, endColor); |
| 522 |
m_linearGradient.setColorAt(0, startColor); |
| 523 |
m_linearGradient.setColorAt(1, endColor); |
| 524 |
} else { |
| 525 |
ThemeController::getGradientFromDT("kbrand-hover", startColor, endColor); |
| 526 |
m_linearGradient.setColorAt(0, startColor); |
| 527 |
m_linearGradient.setColorAt(1, endColor); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
void KSliderPrivate::changeTheme() |
| 533 |
{ |
| 534 |
Q_Q(KSlider); |
| 535 |
if (m_isTranslucentFlag) { |
| 536 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 537 |
m_isTranslucent = false; |
| 538 |
else |
| 539 |
m_isTranslucent = true; |
| 540 |
} else { |
| 541 |
m_isTranslucent = false; |
| 542 |
} |
| 543 |
initThemeStyle(); |
| 544 |
} |
| 545 |
|
| 546 |
QRect KSliderPrivate::baseLineRect() |
| 547 |
{ |
| 548 |
Q_Q(KSlider); |
| 549 |
QRect rect; |
| 550 |
if (q->orientation() == Qt::Horizontal) { |
| 551 |
rect.setBottom(q->height() / 2 + line_width / 2); |
| 552 |
rect.setTop(q->height() / 2 - line_width / 2); |
| 553 |
rect.setLeft(spaceing + Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) / 2); |
| 554 |
rect.setRight(q->width() - spaceing - Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) / 2); |
| 555 |
} else { |
| 556 |
rect.setBottom(q->height() - spaceing - m_handleRadius); |
| 557 |
rect.setTop(spaceing); |
| 558 |
rect.setLeft(q->width() / 2 - line_width / 2); |
| 559 |
rect.setRight(q->width() / 2 + line_width / 2); |
| 560 |
} |
| 561 |
return rect; |
| 562 |
} |
| 563 |
|
| 564 |
QRect KSliderPrivate::coverLineRect() |
| 565 |
{ |
| 566 |
Q_Q(KSlider); |
| 567 |
QRect rect; |
| 568 |
if (q->orientation() == Qt::Horizontal) { |
| 569 |
if (q->layoutDirection() == Qt::LeftToRight) { |
| 570 |
rect.setBottom(q->height() / 2 + line_width / 2); |
| 571 |
rect.setTop(q->height() / 2 - line_width / 2); |
| 572 |
rect.setLeft(spaceing + Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) / 2); |
| 573 |
if (m_ismoving) |
| 574 |
rect.setRight(m_currentpos.x()); |
| 575 |
else if (m_resize) |
| 576 |
rect.setRight(m_valuePosition); |
| 577 |
else |
| 578 |
rect.setRight(m_animation->currentValue().toInt()); |
| 579 |
} else { |
| 580 |
rect.setBottom(q->height() / 2 + line_width / 2); |
| 581 |
rect.setTop(q->height() / 2 - line_width / 2); |
| 582 |
if (m_ismoving) |
| 583 |
rect.setLeft(m_currentpos.x()); |
| 584 |
else if (m_resize) |
| 585 |
rect.setLeft(m_valuePosition); |
| 586 |
else |
| 587 |
rect.setLeft(m_animation->currentValue().toInt()); |
| 588 |
rect.setRight(q->width() - spaceing - Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) / 2); |
| 589 |
} |
| 590 |
} else { |
| 591 |
rect.setBottom(q->height() - spaceing - m_handleRadius); |
| 592 |
rect.setLeft(q->width() / 2 - line_width / 2); |
| 593 |
rect.setRight(q->width() / 2 + line_width / 2); |
| 594 |
if (m_ismoving) |
| 595 |
rect.setTop(m_currentpos.y()); |
| 596 |
else if (m_resize) |
| 597 |
rect.setTop(m_valuePosition); |
| 598 |
else |
| 599 |
rect.setTop(m_animation->currentValue().toInt()); |
| 600 |
} |
| 601 |
return rect; |
| 602 |
} |
| 603 |
|
| 604 |
KSlider::KSlider(QWidget *parent) |
| 605 |
: KSlider(Qt::Horizontal, parent) |
| 606 |
{ |
| 607 |
} |
| 608 |
|
| 609 |
KSlider::KSlider(Qt::Orientation orientation, QWidget *parent) |
| 610 |
: QSlider(orientation, parent) |
| 611 |
, d_ptr(new KSliderPrivate(this)) |
| 612 |
{ |
| 613 |
Q_D(KSlider); |
| 614 |
d->changeTheme(); |
| 615 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 616 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 617 |
{ |
| 618 |
d->changeTheme(); |
| 619 |
} |
| 620 |
}); |
| 621 |
connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() { |
| 622 |
updateGeometry(); |
| 623 |
update(); |
| 624 |
}); |
| 625 |
installEventFilter(this); |
| 626 |
setFocusPolicy(Qt::ClickFocus); |
| 627 |
d->m_resize = false; |
| 628 |
} |
| 629 |
|
| 630 |
void KSlider::setTickInterval(int interval) |
| 631 |
{ |
| 632 |
QSlider::setTickInterval(interval); |
| 633 |
update(); |
| 634 |
} |
| 635 |
|
| 636 |
void KSlider::setSliderType(KSliderType type) |
| 637 |
{ |
| 638 |
Q_D(KSlider); |
| 639 |
d->m_sliderType = type; |
| 640 |
if (!tickInterval() && type == KSliderType::NodeSlider) |
| 641 |
setTickInterval(10); |
| 642 |
if (!singleStep() && type == KSliderType::StepSlider) |
| 643 |
setSingleStep(10); |
| 644 |
if (type == KSliderType::SingleSelectSlider) { |
| 645 |
setTickInterval(1); |
| 646 |
setSingleStep(1); |
| 647 |
setRange(0, 2); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
KSliderType KSlider::sliderType() |
| 652 |
{ |
| 653 |
Q_D(KSlider); |
| 654 |
return d->m_sliderType; |
| 655 |
} |
| 656 |
|
| 657 |
int KSlider::tickInterval() const |
| 658 |
{ |
| 659 |
return QSlider::tickInterval(); |
| 660 |
} |
| 661 |
|
| 662 |
void KSlider::setValue(int value) |
| 663 |
{ |
| 664 |
Q_D(KSlider); |
| 665 |
QSlider::setValue(value); |
| 666 |
d->animationStart(); |
| 667 |
update(); |
| 668 |
} |
| 669 |
|
| 670 |
void KSlider::setNodeVisible(bool flag) |
| 671 |
{ |
| 672 |
Q_D(KSlider); |
| 673 |
if (d->m_nodeVisible != flag) { |
| 674 |
d->m_nodeVisible = flag; |
| 675 |
repaint(); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
bool KSlider::nodeVisible() |
| 680 |
{ |
| 681 |
Q_D(KSlider); |
| 682 |
return d->m_nodeVisible; |
| 683 |
} |
| 684 |
|
| 685 |
void KSlider::setToolTip(const QString &str) |
| 686 |
{ |
| 687 |
Q_D(KSlider); |
| 688 |
d->m_pToolTipText = str; |
| 689 |
} |
| 690 |
|
| 691 |
QString KSlider::toolTip() const |
| 692 |
{ |
| 693 |
Q_D(const KSlider); |
| 694 |
return d->m_pToolTipText; |
| 695 |
} |
| 696 |
|
| 697 |
void KSlider::setTranslucent(bool flag) |
| 698 |
{ |
| 699 |
Q_D(KSlider); |
| 700 |
d->m_isTranslucentFlag = flag; |
| 701 |
d->m_isTranslucent = flag; |
| 702 |
d->changeTheme(); |
| 703 |
} |
| 704 |
|
| 705 |
bool KSlider::isTranslucent() |
| 706 |
{ |
| 707 |
Q_D(KSlider); |
| 708 |
return d->m_isTranslucent; |
| 709 |
} |
| 710 |
|
| 711 |
void KSlider::paintEvent(QPaintEvent *event) |
| 712 |
{ |
| 713 |
Q_D(KSlider); |
| 714 |
if (orientation() == Qt::Horizontal) { |
| 715 |
for (auto point : d->m_nodes) { |
| 716 |
if (point.x() <= d->m_valuePosition) |
| 717 |
d->m_linearGradient = QLinearGradient(point.x(), point.y() - node_radius, point.x(), point.y() + node_radius); |
| 718 |
} |
| 719 |
} else { |
| 720 |
for (auto point : d->m_nodes) { |
| 721 |
if (point.y() >= d->m_valuePosition) |
| 722 |
d->m_linearGradient = QLinearGradient(point.x(), point.y() - node_radius, point.x(), point.y() + node_radius); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
d->updateColor(palette()); |
| 727 |
d->locateNode(); |
| 728 |
d->updateValuePosition(); |
| 729 |
QPainter p(this); |
| 730 |
p.setRenderHint(QPainter::Antialiasing, true); |
| 731 |
p.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 732 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 733 |
if (p.pen().width() == 1) |
| 734 |
p.translate(0.5, 0.5); |
| 735 |
} |
| 736 |
d->drawBasePath(&p); |
| 737 |
if (d->m_sliderType != SingleSelectSlider) |
| 738 |
d->drawCoverLine(&p); |
| 739 |
if (d->m_nodeVisible) { |
| 740 |
if (d->m_sliderType != SingleSelectSlider) |
| 741 |
d->drawCoverNode(&p); |
| 742 |
} |
| 743 |
d->drawSlider(&p); |
| 744 |
} |
| 745 |
|
| 746 |
void KSlider::resizeEvent(QResizeEvent *event) |
| 747 |
{ |
| 748 |
Q_D(KSlider); |
| 749 |
d->m_resize = true; |
| 750 |
QSlider::resizeEvent(event); |
| 751 |
} |
| 752 |
|
| 753 |
void KSlider::mousePressEvent(QMouseEvent *event) |
| 754 |
{ |
| 755 |
Q_D(KSlider); |
| 756 |
|
| 757 |
if (event->button() == Qt::LeftButton) { |
| 758 |
d->m_clickTime = QTime::currentTime(); |
| 759 |
d->m_currentpos = event->pos(); |
| 760 |
d->m_isPressed = true; |
| 761 |
d->updateColor(palette()); |
| 762 |
d->updateValue(); |
| 763 |
update(); |
| 764 |
} |
| 765 |
QSlider::mousePressEvent(event); |
| 766 |
} |
| 767 |
|
| 768 |
void KSlider::mouseReleaseEvent(QMouseEvent *event) |
| 769 |
{ |
| 770 |
Q_D(KSlider); |
| 771 |
if (event->button() == Qt::LeftButton) |
| 772 |
{ |
| 773 |
if (orientation() == Qt::Horizontal) { |
| 774 |
if (event->pos().x() >= d->baseLineRect().left() && event->pos().x() <= d->baseLineRect().adjusted(0, 0, 1, 0).right()) |
| 775 |
d->m_currentpos = event->pos(); |
| 776 |
else if (event->pos().x() < d->baseLineRect().left()) |
| 777 |
d->m_currentpos.setX(d->baseLineRect().left()); |
| 778 |
else if (event->pos().x() > d->baseLineRect().adjusted(0, 0, 1, 0).right()) |
| 779 |
d->m_currentpos.setX(d->baseLineRect().adjusted(0, 0, 1, 0).right()); |
| 780 |
} else { |
| 781 |
if (event->pos().y() >= d->baseLineRect().top() && event->pos().y() <= d->baseLineRect().adjusted(0, 0, 0, 1).bottom()) |
| 782 |
d->m_currentpos = event->pos(); |
| 783 |
else if (event->pos().y() > d->baseLineRect().adjusted(0, 0, 0, 1).bottom()) |
| 784 |
d->m_currentpos.setY(d->baseLineRect().adjusted(0, 0, 0, 1).bottom()); |
| 785 |
else if (event->pos().y() < d->baseLineRect().top()) |
| 786 |
d->m_currentpos.setY(d->baseLineRect().top()); |
| 787 |
} |
| 788 |
if (d->m_ismoving) { |
| 789 |
if (orientation() == Qt::Horizontal) |
| 790 |
d->m_valuePosition = d->m_currentpos.x(); |
| 791 |
else |
| 792 |
d->m_valuePosition = d->m_currentpos.y(); |
| 793 |
} |
| 794 |
d->m_ismoving = false; |
| 795 |
d->updateColor(palette()); |
| 796 |
d->updateValue(); |
| 797 |
update(); |
| 798 |
d->animationStart(); |
| 799 |
} |
| 800 |
QSlider::mouseReleaseEvent(event); |
| 801 |
if(d->m_isPressed) |
| 802 |
emit sliderReleased(); |
| 803 |
} |
| 804 |
|
| 805 |
void KSlider::mouseMoveEvent(QMouseEvent *event) |
| 806 |
{ |
| 807 |
Q_D(KSlider); |
| 808 |
|
| 809 |
QTime currentTime = QTime::currentTime(); |
| 810 |
int timeDiff = d->m_clickTime.msecsTo(currentTime); // 计算从按下到移动的时间差 |
| 811 |
|
| 812 |
if(d->m_isPressed && timeDiff >= 100 ) |
| 813 |
{ |
| 814 |
if (orientation() == Qt::Horizontal) { |
| 815 |
if (event->pos().x() >= d->baseLineRect().left() && event->pos().x() <= d->baseLineRect().adjusted(0, 0, 1, 0).right()) |
| 816 |
d->m_currentpos = event->pos(); |
| 817 |
else if (event->pos().x() < d->baseLineRect().left()) |
| 818 |
d->m_currentpos.setX(d->baseLineRect().left()); |
| 819 |
else if (event->pos().x() > d->baseLineRect().adjusted(0, 0, 1, 0).right()) |
| 820 |
d->m_currentpos.setX(d->baseLineRect().adjusted(0, 0, 1, 0).right()); |
| 821 |
d->m_ismoving = true; |
| 822 |
d->updateValue(); |
| 823 |
update(); |
| 824 |
} else { |
| 825 |
if (event->pos().y() >= d->baseLineRect().top() && event->pos().y() <= d->baseLineRect().adjusted(0, 0, 0, 1).bottom()) |
| 826 |
d->m_currentpos = event->pos(); |
| 827 |
else if (event->pos().y() > d->baseLineRect().adjusted(0, 0, 0, 1).bottom()) |
| 828 |
d->m_currentpos.setY(d->baseLineRect().adjusted(0, 0, 0, 1).bottom()); |
| 829 |
else if (event->pos().y() < d->baseLineRect().top()) |
| 830 |
d->m_currentpos.setY(d->baseLineRect().top()); |
| 831 |
d->m_ismoving = true; |
| 832 |
d->updateValue(); |
| 833 |
update(); |
| 834 |
} |
| 835 |
} else { |
| 836 |
d->m_ismoving = false; |
| 837 |
} |
| 838 |
QSlider::mouseMoveEvent(event); |
| 839 |
} |
| 840 |
|
| 841 |
void KSlider::wheelEvent(QWheelEvent *event) |
| 842 |
{ |
| 843 |
QSlider::wheelEvent(event); |
| 844 |
} |
| 845 |
|
| 846 |
bool KSlider::eventFilter(QObject *watched, QEvent *event) |
| 847 |
{ |
| 848 |
Q_D(KSlider); |
| 849 |
if (watched == this) { |
| 850 |
switch (event->type()) { |
| 851 |
case QEvent::Enter: |
| 852 |
d->m_isHovered = true; |
| 853 |
d->updateColor(palette()); |
| 854 |
break; |
| 855 |
case QEvent::Leave: |
| 856 |
d->m_isHovered = false; |
| 857 |
d->updateColor(palette()); |
| 858 |
break; |
| 859 |
case QEvent::ToolTip: { |
| 860 |
QHelpEvent *ex = static_cast<QHelpEvent *>(event); |
| 861 |
d->m_rect = QRect(d->m_currentpos.x() - handle_radius, d->m_currentpos.y() - handle_radius, handle_radius * 2, handle_radius * 2); |
| 862 |
if (d->m_rect.contains(ex->pos())) { |
| 863 |
QToolTip::showText(cursor().pos(), d->m_pToolTipText); |
| 864 |
} else |
| 865 |
return true; |
| 866 |
break; |
| 867 |
} |
| 868 |
case QEvent::Wheel: |
| 869 |
d->m_forceUpdate = true; |
| 870 |
break; |
| 871 |
case QEvent::KeyPress: |
| 872 |
d->m_forceUpdate = true; |
| 873 |
break; |
| 874 |
case QEvent::Show: |
| 875 |
d->animationStart(); |
| 876 |
break; |
| 877 |
default: |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
return QSlider::eventFilter(watched, event); |
| 882 |
} |
| 883 |
|
| 884 |
QSize KSlider::sizeHint() const |
| 885 |
{ |
| 886 |
auto size = QSlider::sizeHint(); |
| 887 |
if (orientation() == Qt::Horizontal) |
| 888 |
size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) + 2); |
| 889 |
else |
| 890 |
size.setWidth(Parmscontroller::parm(Parmscontroller::Parm::PM_SliderHandleRadius) + 2); |
| 891 |
return size; |
| 892 |
} |
| 893 |
|
| 894 |
} |
| 895 |
// #include "kslider.moc" |
| 896 |
#include "moc_kslider.cpp" |