summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/muse/master/master.cpp
blob: d033753cf329918d3c4a9c5479f030e26c085006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "master.h"
#include "icons.h"
#include "audio.h"
#include "al/tempo.h"
#include "song.h"

static const int minTempo = 50;
static const int maxTempo = 250;

//---------------------------------------------------------
//   pix2tempo
//---------------------------------------------------------

int MasterCanvas::pix2tempo(int val) const
      {
      return maxTempo*1000 - mapyDev(val);
      }

//---------------------------------------------------------
//   tempo2pix
//---------------------------------------------------------

int MasterCanvas::tempo2pix(int val) const
      {
      return mapy(maxTempo*1000 - lrint(60000000000.0/val));
      }

//---------------------------------------------------------
//   MasterCanvas
//---------------------------------------------------------

MasterCanvas::MasterCanvas()
   : TimeCanvas(TIME_CANVAS)
      {
      setMarkerList(song->marker());
      setMag(xmag(), 400.0 / (maxTempo * 1000.0));
      setYMagRange(40.0 / (maxTempo*1000), 4000.0 / (maxTempo*1000));
      setVSize((maxTempo - minTempo) * 1000);
      setYFit(true);
      verticalScrollBar()->setSingleStep(1000);
      verticalScrollBar()->setMinimum(minTempo * 1000);
      verticalScrollBar()->setMaximum(maxTempo * 1000);
      drag = DRAG_OFF;
      }

//---------------------------------------------------------
//   paint
//---------------------------------------------------------

void MasterCanvas::paint(QPainter& p, QRect rect)
      {
      int y  = rect.y();

      //---------------------------------------------------
      // draw Canvas Items
      //---------------------------------------------------

      int y2 = lrint(maxTempo * 1000 * _ymag);
      const AL::TempoList* tl = &AL::tempomap;
      for (AL::ciTEvent i = tl->begin(); i != tl->end(); ++i) {
            AL::TEvent* e = i->second;
            int x1 = pos2pix(AL::Pos(i->first));
            int x2 = pos2pix(AL::Pos(i->second->tick));
            int y1 = lrint(_ymag * (maxTempo*1000 - lrint(60000000000.0/e->tempo)));
            if (y1 < y)
                  y1 = y;
            p.fillRect(x1, y1, x2 - x1, y2 - y1, Qt::blue);
            }
      }

//---------------------------------------------------------
//   viewMousePressEvent
//---------------------------------------------------------

void MasterCanvas::mousePress(QMouseEvent* event)
      {
      start = event->pos();
      int x = start.x() - rCanvasA.x();
      int y = start.y() - rCanvasA.y();

      switch (tool()) {
            case PointerTool:
                  drag = DRAG_LASSO_START;
                  break;

            case PencilTool:
                  drag = DRAG_NEW;
                  song->startUndo();
                  newVal(x, x, y);
                  break;

            case RubberTool:
                  drag = DRAG_DELETE;
                  song->startUndo();
                  deleteVal(x, x);
                  break;

            default:
                  break;
            }
      }

//---------------------------------------------------------
//   viewMouseMoveEvent
//---------------------------------------------------------

void MasterCanvas::mouseMove(QPoint pos)
      {
      int y = pos.y() - rCanvasA.y();
      int x = pos.x() - rCanvasA.x();

      QPoint dist = pos - start;
      // bool moving = dist.y() >= 3 || dist.y() <= 3 || dist.x() >= 3 || dist.x() <= 3;

      int sx = start.x() - rCanvasA.x();
      switch (drag) {
            case DRAG_NEW:
                  newVal(sx, x, y);
                  start = pos;
                  break;

            case DRAG_DELETE:
                  deleteVal(sx, x);
                  start = pos;
                  break;

            default:
                  break;
            }
      emit tempoChanged(pix2tempo(y));
//      emit timeChanged(editor->rasterVal(x));
      }

//---------------------------------------------------------
//   mouseRelease
//---------------------------------------------------------

void MasterCanvas::mouseRelease(QMouseEvent*)
      {
      switch (drag) {
            case DRAG_RESIZE:
            case DRAG_NEW:
            case DRAG_DELETE:
                  song->endUndo(SC_TEMPO);
                  break;
            default:
                  break;
            }
      drag = DRAG_OFF;
      }

//---------------------------------------------------------
//   deleteVal
//---------------------------------------------------------

bool MasterCanvas::deleteVal1(const AL::Pos& p1, const AL::Pos& p2)
      {
      bool songChanged = false;

      AL::TempoList* tl = &AL::tempomap;
      for (AL::iTEvent i = tl->begin(); i != tl->end(); ++i) {
            if (i->first < p1.tick())
                  continue;
            if (i->first >= p2.tick())
                  break;
            AL::iTEvent ii = i;
            ++ii;
            if (ii != tl->end()) {
                  int tempo = ii->second->tempo;
                  audio->msgDeleteTempo(i->first, tempo, false);
                  songChanged = true;
                  }
            }
      return songChanged;
      }

//---------------------------------------------------------
//   deleteVal
//---------------------------------------------------------

void MasterCanvas::deleteVal(int x1, int x2)
      {
      AL::Pos p1(pix2pos(x1));
      AL::Pos p2(pix2pos(x2));
      p1.downSnap(raster());
      p2.upSnap(raster());
      if (deleteVal1(p1, p2))
            widget()->update();
      }

//---------------------------------------------------------
//   newVal
//---------------------------------------------------------

void MasterCanvas::newVal(int x1, int x2, int y)
      {
      int tempo  = lrint(60000000000.0 /  pix2tempo(y));
      AL::Pos p1 = pix2pos(x1);
      p1.downSnap(raster());
      AL::Pos p2 = pix2pos(x2);
      p2.upSnap(raster());

      if (p1 > p2) {
            AL::Pos tmp = p2;
            p2 = p1;
            p1 = tmp;
            }
      deleteVal1(p1, p2);
      audio->msgAddTempo(p1.tick(), tempo, false);
      widget()->update();
      }