~ubuntu-branches/ubuntu/karmic/muse/karmic-proposed

« back to all changes in this revision

Viewing changes to master/master.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

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