summaryrefslogtreecommitdiff
path: root/attic/muse2-oom/muse2/muse/widgets/pcscale.cpp
blob: 103928768a9c5b8e5a4aa7a848c38381a6817bf1 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: mtscale.cpp,v 1.8.2.7 2009/05/03 04:14:01 terminator356 Exp $
//  (C) Copyright 1999 Werner Schweer (ws@seh.de)
//=========================================================

#include <values.h>

#include <QMouseEvent>
#include <QPainter>

#include "pcscale.h"
#include "song.h"
#include "icons.h"
#include "gconfig.h"
#include "prcanvas.h"

//---------------------------------------------------------
//   PCScale
//    Midi Time Scale
//---------------------------------------------------------

PCScale::PCScale(int* r, QWidget* parent, PianoRoll* editor, int xs, bool _mode)
   : View(parent, xs, 1)
      {
	  audio = 0;
	  currentEditor = editor;
      waveMode = _mode;
      setToolTip(tr("bar pcscale"));
      barLocator = false;
      raster = r;
      if (waveMode) {
            pos[0] = tempomap.tick2frame(song->cpos());
            pos[1] = tempomap.tick2frame(song->lpos());
            pos[2] = tempomap.tick2frame(song->rpos());
      }
      else {
            pos[0] = song->cpos();
            pos[1] = song->lpos();
            pos[2] = song->rpos();
      }
      pos[3] = MAXINT;            // do not show
      button = Qt::NoButton;
      setMouseTracking(true);
      connect(song, SIGNAL(posChanged(int, unsigned, bool)), SLOT(setPos(int, unsigned, bool)));
      connect(song, SIGNAL(songChanged(int)), SLOT(songChanged(int)));
      //connect(song, SIGNAL(markerChanged(int)), SLOT(redraw()));
	
      setFixedHeight(14);
      setBg(QColor(110, 141, 152));
}

//---------------------------------------------------------
//   songChanged
//---------------------------------------------------------

void PCScale::songChanged(int type)
{
      if (type & (SC_SIG|SC_TEMPO)) {
           if ((type & SC_TEMPO) && waveMode) {
                  pos[0] = tempomap.tick2frame(song->cpos());
                  pos[1] = tempomap.tick2frame(song->lpos());
                  pos[2] = tempomap.tick2frame(song->rpos());
           }
           redraw();
      }
      redraw();
}

//---------------------------------------------------------
//   setPos
//---------------------------------------------------------

void PCScale::setPos(int idx, unsigned val, bool)
{
      if (val == MAXINT) {
            if (idx == 3) {
                  pos[3] = MAXINT;
                  redraw(QRect(0, 0, width(), height()));
            }
            return;
      }
      if (waveMode)
            val = tempomap.tick2frame(val);
      if (val == pos[idx])
            return;
      //unsigned opos = mapx(pos[idx] == MAXINT ? val : pos[idx]);
      int opos = mapx(pos[idx] == MAXINT ? val : pos[idx]);
      pos[idx] = val;
      if (!isVisible())
            return;

      int tval   = mapx(val);
      int x = -9;
      int w = 18;

      if (tval < 0) { // tval<0 occurs whenever the window is scrolled left, so I switched to signed int (ml)
            //printf("PCScale::setPos - idx:%d val:%d tval:%d opos:%d w:%d h:%d\n", idx, val, tval, opos, width(), height());
      
            redraw(QRect(0,0,width(),height()));
            return;
      }
      //if (opos > (unsigned int) tval) {	//prevent compiler warning: comparison signed/unsigned
      if (opos > tval) { 
            w += opos - tval;
            x += tval;
      }
      else {
            w += tval - opos;
            x += opos;
      }
      //printf("PCScale::setPos idx:%d val:%d tval:%d opos:%d x:%d w:%d h:%d\n", idx, val, tval, opos, x, w, height());
      
      redraw(QRect(x, 0, w, height()));
}

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

void PCScale::viewMousePressEvent(QMouseEvent* event)
{
      button = event->button();
      viewMouseMoveEvent(event);
}

//---------------------------------------------------------
//   viewMouseReleaseEvent
//---------------------------------------------------------

void PCScale::viewMouseReleaseEvent(QMouseEvent*)
{
      button = Qt::NoButton;
}

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

void PCScale::viewMouseMoveEvent(QMouseEvent* event)
{
      if (event->modifiers() & Qt::ShiftModifier )
            setCursor(QCursor(Qt::PointingHandCursor));
      else
            setCursor(QCursor(Qt::ArrowCursor));
      
      int x = event->x();
      x = AL::sigmap.raster(x, *raster);
      if (x < 0)
            x = 0;
      //printf("PCScale::viewMouseMoveEvent\n");  
      int i;
      switch (button) {
            case Qt::LeftButton:
                  i = 0;
                  break;
            case Qt::MidButton:
                  i = 1;
                  break;
            case Qt::RightButton:
                  i = 2;
                  break;
            default:
                  return; // if no button is pressed the function returns here
      }
    Pos p(x, true);
	if (waveMode)
	{
		song->setPos(i, p);
		return;
	}
      
	if(i== 0 && (event->modifiers() & Qt::ShiftModifier )) {        // If shift +LMB we add a marker 
		//Add program change here
		song->setPos(i, p);                             // all other cases: relocating one of the locators
		emit selectInstrument();
		emit addProgramChange();
	}
	else if (i== 2 && (event->modifiers() & Qt::ShiftModifier )) {  // If shift +RMB we remove a marker 
		//Delete Program change here
		Track* track = song->findTrack(currentEditor->curCanvasPart());
		PartList* parts = track->parts();
		for (iPart p = parts->begin(); p != parts->end(); ++p) 
		{
			Part* mprt = p->second;
			EventList* eventList = mprt->events();
			for(iEvent evt = eventList->begin(); evt != eventList->end(); ++evt)
			{
				//Get event type.
				Event pcevt = evt->second;
				if(!pcevt.isNote())
				{
					if(pcevt.type() == Controller && pcevt.dataA() == CTRL_PROGRAM)
					{
						int xp = pcevt.tick()+mprt->tick();
						if(xp >= x && xp <= (x+50))
						{
							//currentEditor->deleteSelectedProgramChange(evt->second, p->second);
							if(audio)
							{
								song->startUndo();
								audio->msgDeleteEvent(evt->second, p->second, true, true, true);
								song->endUndo(SC_EVENT_MODIFIED);
							}
						}
					}
				}
			}
		}
	}                                                        
	else
		song->setPos(i, p);                             // all other cases: relocating one of the locators
}

//---------------------------------------------------------
//   leaveEvent
//---------------------------------------------------------

void PCScale::leaveEvent(QEvent*)
{
      //emit timeChanged(MAXINT);
}

void PCScale::setEditor(PianoRoll* editor)
{
	currentEditor = editor;
}

void PCScale::updateProgram()
{
		redraw();
}

void PCScale::setAudio(Audio* a)
{
	if(!a)
		return;
	audio = a;
}


//---------------------------------------------------------
//   draw
//---------------------------------------------------------

void PCScale::pdraw(QPainter& p, const QRect& r)
{
	if(waveMode) 
		return;
	int x = r.x();
	int w = r.width();
	
	x -= 20;
	w += 40;    // wg. Text
	
	//---------------------------------------------------
	//    draw Flag
	//---------------------------------------------------
	
	int y = 12;
	p.setPen(Qt::black);
	p.setFont(config.fonts[4]);
	p.drawLine(r.x(), y+1, r.x() + r.width(), y+1);
	QRect tr(r);
	tr.setHeight(12);
	Track* track = song->findTrack(currentEditor->curCanvasPart());
	PartList* parts = track->parts();
	for (iPart m = parts->begin(); m != parts->end(); ++m) 
	{
		Part* mprt = m->second;
		EventList* eventList = mprt->events();
		for(iEvent evt = eventList->begin(); evt != eventList->end(); ++evt)
		{
			//Get event type.
			Event pcevt = evt->second;
			if(!pcevt.isNote())
			{
				if(pcevt.type() == Controller && pcevt.dataA() == CTRL_PROGRAM)
				{
					int xp = mapx(pcevt.tick()+mprt->tick());
					if (xp > x+w)
					{
						//printf("Its dying from greater than bar size\n");
						break;
					}
					int xe = r.x() + r.width();
					iEvent mm = evt;
					++mm;
					
					QRect tr(xp, 0, xe-xp, 13);
					        
					QRect wr = r.intersect(tr);
					if(!wr.isEmpty()) 
					{
						int x2;
						if (mm != eventList->end())
						{
							x2 = mapx(pcevt.tick() + mprt->tick());
						}      
						else
							x2 = xp+200;
						
						//printf("PCScale::pdraw marker %s xp:%d y:%d h:%d r.x:%d r.w:%d\n", "Test Debug", xp, height(), y, r.x(), r.width());
						
						// Must be reasonable about very low negative x values! With long songs > 15min
						//  and with high horizontal magnification, 'ghost' drawings appeared,
						//  apparently the result of truncation later (xp = -65006 caused ghosting
						//  at bar 245 with magnification at max.), even with correct clipping region
						//  applied to painter in View::paint(). Tim.  Apr 5 2009 
						// Quote: "Warning: Note that QPainter does not attempt to work around 
						//  coordinate limitations in the underlying window system. Some platforms may 
						//  behave incorrectly with coordinates as small as +/-4000."
						if(xp >= -32)
							p.drawPixmap(xp, 0, *flagIconSP);
						  
					//	if(xp >= -1023)
					//	{
					//		QRect r = QRect(xp+10, 0, x2-xp, 12);
					//		p.setPen(Qt::black);
					//		//Use the program change info as name
					//		p.drawText(r, Qt::AlignLeft|Qt::AlignVCenter, "Test"/*pcevt.name()*/);
					//	}  
						
						//Andrew Commenting this line to test the new flag
						//if(xp >= 0)
						//{
						//	p.setPen(Qt::red);
						//	p.drawLine(xp, y, xp, height());
						//}  
					}//END if(wr.isEmpty)
				}//END if(CTRL_PROGRAM)
			}//END if(!isNote)
		}
	}
}