meter.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <QTime>
  23. #include <QPixmap>
  24. #include "tempomap.h"
  25. #include "assert.h"
  26. #define __devloglevel__ 4
  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) :
  33. QWidget(parent), _offset(0), _bgCache(nullptr)/*, _labelCache(nullptr)*/
  34. {
  35. }
  36. qtauMeterBar::~qtauMeterBar()
  37. {
  38. delete _bgCache;
  39. }
  40. void qtauMeterBar::setOffset(int off)
  41. {
  42. if (off != _offset)
  43. {
  44. _offset = off;
  45. update();
  46. }
  47. }
  48. void qtauMeterBar::configure2(SNoteSetup &newSetup)
  49. {
  50. _ns = newSetup;
  51. int barScreenOffset = 0;
  52. int lastBar = 128;
  53. int scale = _ns.note.width() * 4;
  54. for (int i = 0; i <= lastBar; ++i)
  55. {
  56. fraction ts = _ns.tmap->getTimeSignatureForBar(i);
  57. _ns.barScreenOffsets[i]=barScreenOffset;
  58. barScreenOffset += scale*ts.numerator*1.0/ts.denominator;
  59. }
  60. updateCache();
  61. update();
  62. }
  63. //---------------------------------------------------------
  64. void qtauMeterBar::paintEvent(QPaintEvent *event)
  65. {
  66. // draw bg
  67. int hSt = event->rect().x() + _offset;
  68. int firstBar = _ns.getBarForScreenOffset(hSt);
  69. int lastBar = _ns.getBarForScreenOffset (hSt + event->rect().width());
  70. QRect screenRect(event->rect());
  71. QRect cacheRect(screenRect);
  72. cacheRect.moveLeft(cacheRect.x() + hSt);
  73. QPainter p(this);
  74. p.drawPixmap(screenRect, *_bgCache, cacheRect);
  75. // draw bar numbers
  76. //TODO: get meter events from somewhere
  77. for (int i = firstBar; i <= lastBar; ++i)
  78. {
  79. int barScreenOffset = _ns.barScreenOffsets[i] - _offset;
  80. //QPainter p(this);
  81. int c_lblcache_font_size = 12;
  82. p.setFont(QFont(c_lblcache_font, c_lblcache_font_size));
  83. QRect lblR(barScreenOffset+5, 0, 200, 20);
  84. QRect lblR2(barScreenOffset+20, 0, 200, 20);
  85. QRect lblR3(barScreenOffset+20, 21, 200, 20);
  86. //lblR.moveTop();
  87. QString s = QString("%1").arg(i + 1);
  88. QString s2 = _ns.tmap->getLabel(i,TM_BPM);
  89. QString s3 = _ns.tmap->getLabel(i,TM_SIG);
  90. p.drawText(lblR, s);
  91. if(s2.length()) p.drawText(lblR2, s2);
  92. if(s3.length()) p.drawText(lblR3, s3);
  93. }
  94. // if (!cachedLabels.isEmpty())
  95. // p.drawPixmapFragments(cachedLabels.data(), cachedLabels.size(), *_labelCache);
  96. }
  97. void qtauMeterBar::resizeEvent(QResizeEvent*)
  98. {
  99. updateCache();
  100. }
  101. void qtauMeterBar::mouseDoubleClickEvent(QMouseEvent*)
  102. {
  103. }
  104. void qtauMeterBar::mouseMoveEvent(QMouseEvent*)
  105. {
  106. }
  107. void qtauMeterBar::mousePressEvent(QMouseEvent* evt)
  108. {
  109. int x=evt->x()+_offset;
  110. int y=evt->y();
  111. int bar = _ns.getBarForScreenOffset(x);
  112. int mode = y-20;
  113. if(mode<0)
  114. {
  115. emit barClicked(bar, TM_BPM);
  116. }
  117. if(mode>0)
  118. {
  119. emit barClicked(bar, TM_SIG);
  120. }
  121. }
  122. void qtauMeterBar::mouseReleaseEvent(QMouseEvent*)
  123. {
  124. //
  125. }
  126. void qtauMeterBar::wheelEvent(QWheelEvent *event)
  127. {
  128. if (event->modifiers() & Qt::ControlModifier)
  129. emit zoomed(event->delta());
  130. else
  131. emit scrolled(event->delta());
  132. }
  133. void qtauMeterBar::updateCache()
  134. {
  135. int requiredCacheWidth = _ns.barScreenOffsets[_ns.numBars-1] + _ns.note.width() * 8;
  136. if (!_bgCache || _bgCache->width() < requiredCacheWidth)
  137. {
  138. if (_bgCache)
  139. delete _bgCache;
  140. _bgCache = new QPixmap(requiredCacheWidth, geometry().height());
  141. }
  142. // prepare painting data
  143. int vSt = 0;
  144. int vEnd = geometry().height();
  145. int vMid = vEnd * 0.6;
  146. int bar = 0;
  147. int beat = 0;
  148. int pxOff = 0;
  149. QVector<QPoint> noteLines;
  150. QVector<QPoint> barLines;
  151. //loop1
  152. while (true)
  153. {
  154. if (pxOff >= requiredCacheWidth)
  155. break;
  156. fraction time = _ns.tmap->getTimeSignatureForBar(bar); //1
  157. if (beat==time.numerator) // bar line
  158. {
  159. barLines.append(QPoint(pxOff, vSt ));
  160. barLines.append(QPoint(pxOff, vEnd));
  161. beat = 1;
  162. bar++;
  163. time = _ns.tmap->getTimeSignatureForBar(bar); //2
  164. }
  165. else // note line (low)
  166. {
  167. noteLines.append(QPoint(pxOff, vMid));
  168. noteLines.append(QPoint(pxOff, vEnd));
  169. beat++;
  170. }
  171. pxOff += _ns.note.width()* 4.0/time.denominator;
  172. }
  173. //DEVLOG_DEBUG("done");
  174. // paint 'em!
  175. _bgCache->fill(Qt::white);
  176. QPainter p(_bgCache);
  177. p.setPen(QColor(cdef_color_inner_line));
  178. p.drawLines(noteLines);
  179. p.setPen(QColor(cdef_color_outer_line));
  180. p.drawLines(barLines);
  181. }
  182. MeterBarLineEdit::MeterBarLineEdit() : QLineEdit()
  183. {
  184. }
  185. void MeterBarLineEdit::keyPressEvent(QKeyEvent *event)
  186. {
  187. if(event->key() == Qt::Key_Return || event->key() ==Qt::Key_Enter){
  188. emit keyPressHooked(event->key());//it works
  189. }
  190. else{
  191. // default handler for event
  192. QLineEdit::keyPressEvent(event);
  193. }
  194. }