meter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #include "ui/meter.h"
  19. #include <ui/Config.h>
  20. #include <qevent.h>
  21. #include <qpainter.h>
  22. #include <QPixmap>
  23. #include <QTime>
  24. #include "assert.h"
  25. #include "tempomap.h"
  26. #define __devloglevel__ 0
  27. #include <QMessageBox>
  28. #include "tempomap.h"
  29. const int c_lblcache_hoff = 15; // drawing offset from top-left of octave line
  30. const int c_lblcache_voff = 10;
  31. const QString c_lblcache_font = "Arial";
  32. qtauMeterBar::qtauMeterBar(QWidget *parent) : QWidget(parent), _offset(0) {}
  33. qtauMeterBar::~qtauMeterBar() {}
  34. void qtauMeterBar::setOffset(int off) {
  35. if (off != _offset) {
  36. _offset = off;
  37. update();
  38. }
  39. }
  40. void qtauMeterBar::configure2(SNoteSetup &newSetup) {
  41. _ns = newSetup;
  42. int barScreenOffset = 0;
  43. int lastBar = 128;
  44. int scale = _ns.note.width() * 4;
  45. for (int i = 0; i <= lastBar; ++i) {
  46. fraction ts = _ns.tmap->getTimeSignatureForBar(i);
  47. _ns.barScreenOffsets[i] = barScreenOffset;
  48. barScreenOffset += scale * ts.numerator * 1.0 / ts.denominator;
  49. }
  50. // updateCache();
  51. update();
  52. }
  53. //---------------------------------------------------------
  54. void qtauMeterBar::paintEvent(QPaintEvent *event) {
  55. // draw bg
  56. int hSt = event->rect().x() + _offset;
  57. int firstBar = _ns.getBarForScreenOffset(hSt);
  58. int lastBar = _ns.getBarForScreenOffset(hSt + event->rect().width());
  59. // if(lastBar==-1) resizeScore()
  60. QRect screenRect(event->rect());
  61. QPainter p(this);
  62. p.fillRect(screenRect, Qt::white);
  63. // prepare painting data
  64. int vSt = 0;
  65. int vEnd = geometry().height();
  66. int vMid = vEnd * 0.6;
  67. int bar = 0;
  68. int beat = 0;
  69. int pxOff = -_offset;
  70. QVector<QPoint> noteLines;
  71. QVector<QPoint> barLines;
  72. while (true) {
  73. if (bar > lastBar) break;
  74. fraction time = _ns.tmap->getTimeSignatureForBar(bar); // 1
  75. if (beat == time.numerator) // bar line
  76. {
  77. barLines.append(QPoint(pxOff, vSt));
  78. barLines.append(QPoint(pxOff, vEnd));
  79. beat = 1;
  80. bar++;
  81. time = _ns.tmap->getTimeSignatureForBar(bar); // 2
  82. } else // note line (low)
  83. {
  84. noteLines.append(QPoint(pxOff, vMid));
  85. noteLines.append(QPoint(pxOff, vEnd));
  86. beat++;
  87. }
  88. pxOff += _ns.note.width() * 4.0 / time.denominator;
  89. }
  90. p.setPen(QColor(cdef_color_inner_line));
  91. p.drawLines(noteLines);
  92. p.setPen(QColor(cdef_color_outer_line));
  93. p.drawLines(barLines);
  94. p.setPen(Qt::black);
  95. DEVLOG_DEBUG("firstBar " + STR(firstBar));
  96. DEVLOG_DEBUG("lastBar " + STR(lastBar));
  97. for (int i = firstBar; i <= lastBar; ++i) {
  98. int barScreenOffset = _ns.barScreenOffsets[i] - _offset;
  99. // QPainter p(this);
  100. int c_lblcache_font_size = 12;
  101. p.setFont(QFont(c_lblcache_font, c_lblcache_font_size));
  102. QRect lblR(barScreenOffset + 5, 0, 200, 20);
  103. QRect lblR2(barScreenOffset + 20, 0, 200, 20);
  104. QRect lblR3(barScreenOffset + 20, 21, 200, 20);
  105. // lblR.moveTop();
  106. QString s = QString("%1").arg(i + 1);
  107. QString s2 = _ns.tmap->getLabel(i, TM_BPM);
  108. QString s3 = _ns.tmap->getLabel(i, TM_SIG);
  109. p.drawText(lblR, s);
  110. if (s2.length()) p.drawText(lblR2, s2);
  111. if (s3.length()) p.drawText(lblR3, s3);
  112. }
  113. // if (!cachedLabels.isEmpty())
  114. // p.drawPixmapFragments(cachedLabels.data(), cachedLabels.size(),
  115. // *_labelCache);
  116. }
  117. void qtauMeterBar::resizeEvent(QResizeEvent *) {
  118. // updateCache();
  119. }
  120. void qtauMeterBar::mouseDoubleClickEvent(QMouseEvent *) {}
  121. void qtauMeterBar::mouseMoveEvent(QMouseEvent *) {}
  122. void qtauMeterBar::mousePressEvent(QMouseEvent *evt) {
  123. int x = evt->x() + _offset;
  124. int y = evt->y();
  125. int bar = _ns.getBarForScreenOffset(x);
  126. if (bar == -1) return;
  127. int mode = y - 20;
  128. if (mode < 0) {
  129. emit barClicked(bar, TM_BPM);
  130. }
  131. if (mode > 0) {
  132. emit barClicked(bar, TM_SIG);
  133. }
  134. }
  135. void qtauMeterBar::mouseReleaseEvent(QMouseEvent *) {
  136. //
  137. }
  138. void qtauMeterBar::wheelEvent(QWheelEvent *event) {
  139. if (event->modifiers() & Qt::ControlModifier)
  140. emit zoomed(event->delta());
  141. else
  142. emit scrolled(event->delta());
  143. }
  144. MeterBarLineEdit::MeterBarLineEdit() : QLineEdit() {}
  145. void MeterBarLineEdit::keyPressEvent(QKeyEvent *event) {
  146. if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
  147. emit keyPressHooked(event->key()); // it works
  148. } else {
  149. // default handler for event
  150. QLineEdit::keyPressEvent(event);
  151. }
  152. }