~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to master/master.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2005-08-23 17:19:39 UTC
  • mto: (4.1.1 breezy) (1.1.9) (10.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050823171939-hd8fgzokb4dbj007
Tags: upstream-0.7.1+0.7.2pre2
ImportĀ upstreamĀ versionĀ 0.7.1+0.7.2pre2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//=========================================================
2
 
//  MusE
3
 
//  Linux Music Editor
4
 
//    $Id: master.cpp,v 1.1.1.1 2003/10/29 10:06:13 wschweer Exp $
5
 
//  (C) Copyright 2000 Werner Schweer (ws@seh.de)
6
 
//=========================================================
7
 
 
8
 
#include "globals.h"
9
 
#include "master.h"
10
 
#include <qpainter.h>
11
 
#include <stdio.h>
12
 
#include <qheader.h>
13
 
#include <qcursor.h>
14
 
#include "song.h"
15
 
#include "scrollscale.h"
16
 
#include "seq.h"
17
 
#include "midi.h"
18
 
#include "midieditor.h"
19
 
#include "icons.h"
20
 
#include "midithread.h"
21
 
 
22
 
#include <qlineedit.h>
23
 
#include <qpopupmenu.h>
24
 
 
25
 
extern void drawTickRaster(QPainter& p, int x, int y,
26
 
   int w, int h, int quant);
27
 
 
28
 
//---------------------------------------------------------
29
 
//   Master
30
 
//---------------------------------------------------------
31
 
 
32
 
Master::Master(MidiEditor* e, QWidget* parent, int xmag, int ymag)
33
 
   : View(parent, xmag, ymag)
34
 
      {
35
 
      editor = e;
36
 
      setBg(white);
37
 
      vscroll = 0;
38
 
      pos[0]  = 0;
39
 
      pos[1]  = 0;
40
 
      pos[2]  = 0;
41
 
      setMouseTracking(true);
42
 
      connect(song, SIGNAL(posChanged(int, int, bool)), this, SLOT(setPos(int, int, bool)));
43
 
      connect(song, SIGNAL(songChanged(int)), this, SLOT(redraw()));
44
 
      }
45
 
 
46
 
//---------------------------------------------------------
47
 
//   setPos
48
 
//---------------------------------------------------------
49
 
 
50
 
void Master::setPos(int idx, int val, bool adjustScrollbar)
51
 
      {
52
 
      if (pos[idx] == val)
53
 
            return;
54
 
 
55
 
      int opos = mapx(pos[idx]);
56
 
      int npos = mapx(val);
57
 
 
58
 
      if (adjustScrollbar && idx == 0) {
59
 
            switch (song->follow()) {
60
 
                  case  Song::NO:
61
 
                        break;
62
 
                  case Song::JUMP:
63
 
                        if (npos >= width()) {
64
 
                              int ppos =  val - rmapxDev(width()/4);
65
 
                              if (ppos < 0)
66
 
                                    ppos = 0;
67
 
                              emit followEvent(ppos);
68
 
                              opos = mapx(pos[idx]);
69
 
                              npos = mapx(val);
70
 
                              }
71
 
                        else if (npos < 0) {
72
 
                              int ppos =  val - rmapxDev(width()*3/4);
73
 
                              if (ppos < 0)
74
 
                                    ppos = 0;
75
 
                              emit followEvent(ppos);
76
 
                              opos = mapx(pos[idx]);
77
 
                              npos = mapx(val);
78
 
                              }
79
 
                        break;
80
 
                  case Song::CONTINUOUS:
81
 
                        if (npos > (width()*5)/8) {
82
 
                              int ppos =  pos[idx] - rmapxDev(width()*5/8);
83
 
                              if (ppos < 0)
84
 
                                    ppos = 0;
85
 
                              emit followEvent(ppos);
86
 
                              opos = mapx(pos[idx]);
87
 
                              npos = mapx(val);
88
 
                              }
89
 
                        else if (npos < (width()*3)/8) {
90
 
                              int ppos =  pos[idx] - rmapxDev(width()*3/8);
91
 
                              if (ppos < 0)
92
 
                                    ppos = 0;
93
 
                              emit followEvent(ppos);
94
 
                              opos = mapx(pos[idx]);
95
 
                              npos = mapx(val);
96
 
                              }
97
 
                        break;
98
 
                  }
99
 
            }
100
 
 
101
 
      int x;
102
 
      int w = 1;
103
 
      if (opos > npos) {
104
 
            w += opos - npos;
105
 
            x = npos;
106
 
            }
107
 
      else {
108
 
            w += npos - opos;
109
 
            x = opos;
110
 
            }
111
 
      pos[idx] = val;
112
 
      redraw(QRect(x, 0, w, height()));
113
 
      }
114
 
 
115
 
//---------------------------------------------------------
116
 
//   leaveEvent
117
 
//---------------------------------------------------------
118
 
 
119
 
void Master::leaveEvent(QEvent*)
120
 
      {
121
 
      emit tempoChanged(-1);
122
 
      emit timeChanged(-1);
123
 
      }
124
 
 
125
 
//---------------------------------------------------------
126
 
//   pdraw
127
 
//---------------------------------------------------------
128
 
 
129
 
void Master::pdraw(QPainter& p, const QRect& rect)
130
 
      {
131
 
      View::pdraw(p, rect);   // calls draw()
132
 
      p.resetXForm();
133
 
 
134
 
      int x = rect.x();
135
 
      int y = rect.y();
136
 
      int w = rect.width() + 2;
137
 
      int h = rect.height();
138
 
 
139
 
      int wh = height();
140
 
      //---------------------------------------------------
141
 
      // draw Canvas Items
142
 
      //---------------------------------------------------
143
 
 
144
 
      const TempoList* tl = &tempomap;
145
 
      for (ciTEvent i = tl->begin(); i != tl->end(); ++i) {
146
 
            TEvent* e = i->second;
147
 
            int etick = mapx(i->first);
148
 
            int stick = mapx(i->second->tick);
149
 
            int tempo = mapy(280000 - int(60000000000.0/(e->tempo)));
150
 
 
151
 
            if (tempo < 0)
152
 
                  tempo = 0;
153
 
            if (tempo < wh) {
154
 
                  p.fillRect(stick, tempo, etick-stick, wh, blue);
155
 
                  }
156
 
            }
157
 
 
158
 
      //---------------------------------------------------
159
 
      //    draw marker
160
 
      //---------------------------------------------------
161
 
 
162
 
      int xp = mapx(pos[0]);
163
 
      if (xp >= x && xp < x+w) {
164
 
            p.setPen(red);
165
 
            p.drawLine(xp, y, xp, y+h);
166
 
            }
167
 
      xp = mapx(pos[1]);
168
 
      if (xp >= x && xp < x+w) {
169
 
            p.setPen(blue);
170
 
            p.drawLine(xp, y, xp, y+h);
171
 
            }
172
 
      xp = mapx(pos[2]);
173
 
      if (xp >= x && xp < x+w) {
174
 
            p.setPen(blue);
175
 
            p.drawLine(xp, y, xp, y+h);
176
 
            }
177
 
      }
178
 
 
179
 
//---------------------------------------------------------
180
 
//   draw
181
 
//---------------------------------------------------------
182
 
 
183
 
void Master::draw(QPainter& p, const QRect& rect)
184
 
      {
185
 
      drawTickRaster(p, rect.x(), rect.y(),
186
 
         rect.width(), rect.height(), 0);
187
 
      }
188
 
 
189
 
//---------------------------------------------------------
190
 
//   viewMousePressEvent
191
 
//---------------------------------------------------------
192
 
 
193
 
void Master::viewMousePressEvent(QMouseEvent* event)
194
 
      {
195
 
      start = event->pos();
196
 
      Tool activeTool = tool;
197
 
//      bool shift = event->state() & ShiftButton;
198
 
 
199
 
      switch (activeTool) {
200
 
            case PointerTool:
201
 
                  drag = DRAG_LASSO_START;
202
 
                  break;
203
 
 
204
 
            case PencilTool:
205
 
                  drag = DRAG_NEW;
206
 
                  song->startUndo();
207
 
                  newVal(start.x(), start.x(), start.y());
208
 
                  break;
209
 
 
210
 
            case RubberTool:
211
 
                  drag = DRAG_DELETE;
212
 
                  song->startUndo();
213
 
                  deleteVal(start.x(), start.x());
214
 
                  break;
215
 
 
216
 
            default:
217
 
                  break;
218
 
            }
219
 
      }
220
 
 
221
 
//---------------------------------------------------------
222
 
//   viewMouseMoveEvent
223
 
//---------------------------------------------------------
224
 
 
225
 
void Master::viewMouseMoveEvent(QMouseEvent* event)
226
 
      {
227
 
      QPoint pos = event->pos();
228
 
//      QPoint dist = pos - start;
229
 
//      bool moving = dist.y() >= 3 || dist.y() <= 3 || dist.x() >= 3 || dist.x() <= 3;
230
 
 
231
 
      switch (drag) {
232
 
            case DRAG_NEW:
233
 
                  newVal(start.x(), pos.x(), pos.y());
234
 
                  start = pos;
235
 
                  break;
236
 
 
237
 
            case DRAG_DELETE:
238
 
                  deleteVal(start.x(), pos.x());
239
 
                  start = pos;
240
 
                  break;
241
 
 
242
 
            default:
243
 
                  break;
244
 
            }
245
 
      emit tempoChanged(280000 - event->y());
246
 
      emit timeChanged(event->x());
247
 
      }
248
 
 
249
 
//---------------------------------------------------------
250
 
//   viewMouseReleaseEvent
251
 
//---------------------------------------------------------
252
 
 
253
 
void Master::viewMouseReleaseEvent(QMouseEvent*)
254
 
      {
255
 
      switch (drag) {
256
 
            case DRAG_RESIZE:
257
 
            case DRAG_NEW:
258
 
            case DRAG_DELETE:
259
 
                  song->endUndo(SC_TEMPO);
260
 
                  break;
261
 
            default:
262
 
                  break;
263
 
            }
264
 
      drag = DRAG_OFF;
265
 
      }
266
 
 
267
 
//---------------------------------------------------------
268
 
//   deleteVal
269
 
//---------------------------------------------------------
270
 
 
271
 
bool Master::deleteVal1(int x1, int x2)
272
 
      {
273
 
      bool songChanged = false;
274
 
 
275
 
      TempoList* tl = &tempomap;
276
 
      for (iTEvent i = tl->begin(); i != tl->end(); ++i) {
277
 
            if (i->first < x1)
278
 
                  continue;
279
 
            if (i->first >= x2)
280
 
                  break;
281
 
            iTEvent ii = i;
282
 
            ++ii;
283
 
            if (ii != tl->end()) {
284
 
                  int tempo = ii->second->tempo;
285
 
                  midiThread->msgDeleteTempo(i->first, tempo, false);
286
 
                  songChanged = true;
287
 
                  }
288
 
            }
289
 
      return songChanged;
290
 
      }
291
 
 
292
 
void Master::deleteVal(int x1, int x2)
293
 
      {
294
 
      if (deleteVal1(editor->rasterVal1(x1), x2))
295
 
            redraw();
296
 
      }
297
 
 
298
 
//---------------------------------------------------------
299
 
//   setTool
300
 
//---------------------------------------------------------
301
 
 
302
 
void Master::setTool(int t)
303
 
      {
304
 
      if (tool == Tool(t))
305
 
            return;
306
 
      tool = Tool(t);
307
 
      switch(tool) {
308
 
            case PencilTool:
309
 
                  setCursor(QCursor(*pencilIcon, 4, 15));
310
 
                  break;
311
 
            default:
312
 
                  setCursor(QCursor(arrowCursor));
313
 
                  break;
314
 
            }
315
 
      }
316
 
 
317
 
//---------------------------------------------------------
318
 
//   newVal
319
 
//---------------------------------------------------------
320
 
 
321
 
void Master::newVal(int x1, int x2, int y)
322
 
      {
323
 
      int xx1 = editor->rasterVal1(x1);
324
 
      int xx2 = editor->rasterVal2(x2);
325
 
 
326
 
      if (xx1 > xx2) {
327
 
            int tmp = xx2;
328
 
            xx2 = xx1;
329
 
            xx1 = tmp;
330
 
            }
331
 
      deleteVal1(xx1, xx2);
332
 
      midiThread->msgAddTempo(xx1, int(60000000000.0/(280000 - y)), false);
333
 
      redraw();
334
 
      }