comparison widgets/Thumbwheel.cpp @ 1324:13d9b422f7fe zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:31 +0100
parents 13f5f84fbfad
children c8a6fd3f9dff
comparison
equal deleted inserted replaced
1183:57d192e26331 1324:13d9b422f7fe
27 27
28 #include <cmath> 28 #include <cmath>
29 #include <iostream> 29 #include <iostream>
30 30
31 Thumbwheel::Thumbwheel(Qt::Orientation orientation, 31 Thumbwheel::Thumbwheel(Qt::Orientation orientation,
32 QWidget *parent) : 32 QWidget *parent) :
33 QWidget(parent), 33 QWidget(parent),
34 m_min(0), 34 m_min(0),
35 m_max(100), 35 m_max(100),
36 m_default(50), 36 m_default(50),
37 m_value(50), 37 m_value(50),
242 { 242 {
243 int step = int(lrintf(m_speed)); 243 int step = int(lrintf(m_speed));
244 if (step == 0) step = 1; 244 if (step == 0) step = 1;
245 245
246 if (up) { 246 if (up) {
247 setValue(m_value + step); 247 setValue(m_value + step);
248 } else { 248 } else {
249 setValue(m_value - step); 249 setValue(m_value - step);
250 } 250 }
251 251
252 emit valueChanged(getValue()); 252 emit valueChanged(getValue());
253 } 253 }
254 254
424 } 424 }
425 425
426 void 426 void
427 Thumbwheel::wheelEvent(QWheelEvent *e) 427 Thumbwheel::wheelEvent(QWheelEvent *e)
428 { 428 {
429 int step = int(lrintf(m_speed)); 429 int delta = m_wheelCounter.count(e);
430 if (step == 0) step = 1; 430
431 431 if (delta == 0) {
432 if (e->delta() > 0) { 432 return;
433 setValue(m_value + step); 433 }
434 } else { 434
435 setValue(m_value - step); 435 setValue(m_value + delta);
436 }
437
438 emit valueChanged(getValue()); 436 emit valueChanged(getValue());
439 } 437 }
440 438
441 void 439 void
442 Thumbwheel::paintEvent(QPaintEvent *) 440 Thumbwheel::paintEvent(QPaintEvent *)
443 { 441 {
444 Profiler profiler("Thumbwheel::paintEvent"); 442 Profiler profiler("Thumbwheel::paintEvent");
445 443
446 if (!m_cache.isNull()) { 444 if (!m_cache.isNull()) {
447 QPainter paint(this); 445 QPainter paint(this);
448 paint.drawImage(0, 0, m_cache); 446 paint.drawImage(rect(), m_cache, m_cache.rect());
449 return; 447 return;
450 } 448 }
451 449
452 Profiler profiler2("Thumbwheel::paintEvent (no cache)"); 450 Profiler profiler2("Thumbwheel::paintEvent (no cache)");
453 451
454 m_cache = QImage(size(), QImage::Format_ARGB32); 452 QSize imageSize = size() * devicePixelRatio();
453 m_cache = QImage(imageSize, QImage::Format_ARGB32);
455 m_cache.fill(Qt::transparent); 454 m_cache.fill(Qt::transparent);
456 455
457 int bw = 3; 456 int w = m_cache.width();
457 int h = m_cache.height();
458 int bw = 3; // border width
458 459
459 QRect subclip; 460 QRect subclip;
460 if (m_orientation == Qt::Horizontal) { 461 if (m_orientation == Qt::Horizontal) {
461 subclip = QRect(bw, bw+1, width() - bw*2, height() - bw*2 - 2); 462 subclip = QRect(bw, bw+1, w - bw*2, h - bw*2 - 2);
462 } else { 463 } else {
463 subclip = QRect(bw+1, bw, width() - bw*2 - 2, height() - bw*2); 464 subclip = QRect(bw+1, bw, w - bw*2 - 2, h - bw*2);
464 } 465 }
465 466
466 QPainter paint(&m_cache); 467 QPainter paint(&m_cache);
467 paint.setClipRect(rect()); 468 paint.setClipRect(m_cache.rect());
468 paint.fillRect(subclip, palette().background().color()); 469 paint.fillRect(subclip, palette().background().color());
469 470
470 paint.setRenderHint(QPainter::Antialiasing, true); 471 paint.setRenderHint(QPainter::Antialiasing, true);
471 472
472 double w = width();
473 double w0 = 0.5; 473 double w0 = 0.5;
474 double w1 = w - 0.5; 474 double w1 = w - 0.5;
475 475
476 double h = height();
477 double h0 = 0.5; 476 double h0 = 0.5;
478 double h1 = h - 0.5; 477 double h1 = h - 0.5;
479 478
480 for (int i = bw-1; i >= 0; --i) { 479 for (int i = bw-1; i >= 0; --i) {
481 480
506 505
507 double radians = m_rotation * 1.5f * M_PI; 506 double radians = m_rotation * 1.5f * M_PI;
508 507
509 // cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << endl; 508 // cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << endl;
510 509
511 w = (m_orientation == Qt::Horizontal ? width() : height()) - bw*2; 510 int ww = (m_orientation == Qt::Horizontal ? w : h) - bw*2; // wheel width
512 511
513 // total number of notches on the entire wheel 512 // total number of notches on the entire wheel
514 int notches = 25; 513 int notches = 25;
515 514
516 // radius of the wheel including invisible part 515 // radius of the wheel including invisible part
517 int radius = int(w / 2 + 2); 516 int radius = int(ww / 2 + 2);
518 517
519 for (int i = 0; i < notches; ++i) { 518 for (int i = 0; i < notches; ++i) {
520 519
521 double a0 = (2.0 * M_PI * i) / notches + radians; 520 double a0 = (2.0 * M_PI * i) / notches + radians;
522 double a1 = a0 + M_PI / (notches * 2); 521 double a1 = a0 + M_PI / (notches * 2);
523 double a2 = (2.0 * M_PI * (i + 1)) / notches + radians; 522 double a2 = (2.0 * M_PI * (i + 1)) / notches + radians;
524 523
525 double depth = cos((a0 + a2) / 2); 524 double depth = cos((a0 + a2) / 2);
526 if (depth < 0) continue; 525 if (depth < 0) continue;
527 526
528 double x0 = radius * sin(a0) + w/2; 527 double x0 = radius * sin(a0) + ww/2;
529 double x1 = radius * sin(a1) + w/2; 528 double x1 = radius * sin(a1) + ww/2;
530 double x2 = radius * sin(a2) + w/2; 529 double x2 = radius * sin(a2) + ww/2;
531 if (x2 < 0 || x0 > w) continue; 530 if (x2 < 0 || x0 > ww) continue;
532 531
533 if (x0 < 0) x0 = 0; 532 if (x0 < 0) x0 = 0;
534 if (x2 > w) x2 = w; 533 if (x2 > ww) x2 = ww;
535 534
536 x0 += bw; 535 x0 += bw;
537 x1 += bw; 536 x1 += bw;
538 x2 += bw; 537 x2 += bw;
539 538
555 } else { 554 } else {
556 prop = 0.f; 555 prop = 0.f;
557 } 556 }
558 557
559 if (m_orientation == Qt::Horizontal) { 558 if (m_orientation == Qt::Horizontal) {
560 paint.drawRect(QRectF(x1, height() - (height() - bw*2) * prop - bw, 559 paint.drawRect(QRectF(x1, h - (h - bw*2) * prop - bw,
561 x2 - x1, height() * prop)); 560 x2 - x1, h * prop));
562 } else { 561 } else {
563 paint.drawRect(QRectF(bw, x1, (width() - bw*2) * prop, x2 - x1)); 562 paint.drawRect(QRectF(bw, x1, (w - bw*2) * prop, x2 - x1));
564 } 563 }
565 } 564 }
566 565
567 paint.setPen(fc); 566 paint.setPen(fc);
568 paint.setBrush(palette().background().color()); 567 paint.setBrush(palette().background().color());
569 568
570 if (m_orientation == Qt::Horizontal) { 569 if (m_orientation == Qt::Horizontal) {
571 paint.drawRect(QRectF(x0, bw, x1 - x0, height() - bw*2)); 570 paint.drawRect(QRectF(x0, bw, x1 - x0, h - bw*2));
572 } else { 571 } else {
573 paint.drawRect(QRectF(bw, x0, width() - bw*2, x1 - x0)); 572 paint.drawRect(QRectF(bw, x0, w - bw*2, x1 - x0));
574 } 573 }
575 } 574 }
576 575
577 QPainter paint2(this); 576 QPainter paint2(this);
578 paint2.drawImage(0, 0, m_cache); 577 paint2.drawImage(rect(), m_cache, m_cache.rect());
579 } 578 }
580 579
581 QSize 580 QSize
582 Thumbwheel::sizeHint() const 581 Thumbwheel::sizeHint() const
583 { 582 {