~ubuntu-branches/ubuntu/raring/muse/raring

« back to all changes in this revision

Viewing changes to muse/widgets/wtscale.cpp

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2011-08-12 11:16:41 UTC
  • mfrom: (1.1.9) (10.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20110812111641-sjz0e93fr0ozfv0b
Tags: 2.0~beta2-1
* New maintainer (see bug 637185#12), thanks Daniel!
* New upstream release (Closes: #627371):
  - New interface Qt4-based (Closes: #604584)
* Refresh packaging:
  - Move to DH7 and source format 3.0 (quilt).
  - Refresh patchset.
  - Fix a bunch lintian warnings.
  - debian/postinst:
    + Use set -e in the body rather than pass -e on the #! line to make it
      have actually effect if it is run by hand with "sh /path/to/script".
  - Update instructions on how-to increase RTC clock frequency.
    Thanks to Torquil Macdonald Sørensen for the report (Closes: #570833).
  - Bump Standards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=========================================================
 
2
//  MusE
 
3
//  Linux Music Editor
 
4
//    $Id: wtscale.cpp,v 1.3 2004/04/11 13:03:32 wschweer Exp $
 
5
//  (C) Copyright 2000 Werner Schweer (ws@seh.de)
 
6
//=========================================================
 
7
 
 
8
#include <values.h>
 
9
 
 
10
#include <QPainter>
 
11
#include <QRect>
 
12
#include <QToolTip>
 
13
 
 
14
#include "wtscale.h"
 
15
#include "midieditor.h"
 
16
#include "globals.h"
 
17
#include "song.h"
 
18
#include "../marker/marker.h"
 
19
#include "icons.h"
 
20
 
 
21
//---------------------------------------------------------
 
22
//   WTScale
 
23
//    Wave Time Scale
 
24
//---------------------------------------------------------
 
25
 
 
26
WTScale::WTScale(int* r, QWidget* parent, int xs)
 
27
   : View(parent, xs, 1)
 
28
      {
 
29
      QToolTip::add(this, tr("bar scale"));
 
30
      barLocator = false;
 
31
      raster = r;
 
32
      pos[0] = int(song->tempomap()->tick2time(song->cpos()) * sampleRate);
 
33
      pos[1] = int(song->tempomap()->tick2time(song->lpos()) * sampleRate);
 
34
      pos[2] = int(song->tempomap()->tick2time(song->rpos()) * sampleRate);
 
35
      pos[3] = -1;            // do not show
 
36
      button = Qt::NoButton;
 
37
      setMouseTracking(true);
 
38
      connect(song, SIGNAL(posChanged(int, unsigned, bool)), SLOT(setPos(int, unsigned, bool)));
 
39
      connect(song, SIGNAL(songChanged(int)), SLOT(songChanged(int)));
 
40
      connect(song, SIGNAL(markerChanged(int)), SLOT(redraw()));
 
41
      setFixedHeight(28);
 
42
      setBg(QColor(0xe0, 0xe0, 0xe0));
 
43
      }
 
44
 
 
45
//---------------------------------------------------------
 
46
//   songChanged
 
47
//---------------------------------------------------------
 
48
 
 
49
void WTScale::songChanged(int /*type*/)
 
50
      {
 
51
      }
 
52
 
 
53
//---------------------------------------------------------
 
54
//   setPos
 
55
//---------------------------------------------------------
 
56
 
 
57
void WTScale::setPos(int idx, unsigned val, bool adjustScrollbar)
 
58
      {
 
59
      val = int(song->tempomap()->tick2time(val) * sampleRate);
 
60
      if (val == pos[idx])
 
61
            return;
 
62
      int opos = mapx(pos[idx] == -1 ? val : pos[idx]);
 
63
      pos[idx] = val;
 
64
      if (!isVisible())
 
65
            return;
 
66
      val   = mapx(val);
 
67
      int x = -9;
 
68
      int w = 18;
 
69
      if (opos > val) {
 
70
            w += opos - val;
 
71
            x += val;
 
72
            }
 
73
      else {
 
74
            w += val - opos;
 
75
            x += opos;
 
76
            }
 
77
      redraw(QRect(x, 0, w, height()));
 
78
      }
 
79
 
 
80
//---------------------------------------------------------
 
81
//   viewMousePressEvent
 
82
//---------------------------------------------------------
 
83
 
 
84
void WTScale::viewMousePressEvent(QMouseEvent* event)
 
85
      {
 
86
      button = event->button();
 
87
      viewMouseMoveEvent(event);
 
88
      }
 
89
 
 
90
//---------------------------------------------------------
 
91
//   viewMouseReleaseEvent
 
92
//---------------------------------------------------------
 
93
 
 
94
void WTScale::viewMouseReleaseEvent(QMouseEvent* event)
 
95
      {
 
96
      button = Qt::NoButton;
 
97
      }
 
98
 
 
99
//---------------------------------------------------------
 
100
//   viewMouseMoveEvent
 
101
//---------------------------------------------------------
 
102
 
 
103
void WTScale::viewMouseMoveEvent(QMouseEvent* event)
 
104
      {
 
105
      int x= song->tempomap()->time2tick(double(event->x())/double(sampleRate));
 
106
      x = song->raster(x, *raster);
 
107
      if (x < 0)
 
108
            x = 0;
 
109
      emit timeChanged(x);
 
110
      int i;
 
111
      switch (button) {
 
112
            case Qt::LeftButton:
 
113
                  i = 0;
 
114
                  break;
 
115
            case Qt::MidButton:
 
116
                  i = 1;
 
117
                  break;
 
118
            case Qt::RightButton:
 
119
                  i = 2;
 
120
                  break;
 
121
            default:
 
122
                  return;
 
123
            }
 
124
      song->setPos(i, x);
 
125
      }
 
126
 
 
127
//---------------------------------------------------------
 
128
//   leaveEvent
 
129
//---------------------------------------------------------
 
130
 
 
131
void WTScale::leaveEvent(QEvent*)
 
132
      {
 
133
//      emit timeChanged(MAXINT);
 
134
      }
 
135
 
 
136
//---------------------------------------------------------
 
137
//   draw
 
138
//---------------------------------------------------------
 
139
 
 
140
void WTScale::pdraw(QPainter& p, const QRect& r)
 
141
      {
 
142
      int x = r.x();
 
143
      int w = r.width();
 
144
 
 
145
      x -= 20;
 
146
      w += 40;    // wg. Text
 
147
 
 
148
      //
 
149
      //    draw Marker
 
150
      //
 
151
      int y = 12;
 
152
      p.setPen(Qt::black);
 
153
      p.setFont(font4);
 
154
      p.drawLine(r.x(), y+1, r.x() + r.width(), y+1);
 
155
      QRect tr(r);
 
156
      tr.setHeight(12);
 
157
      MarkerList* marker = song->marker();
 
158
      for (iMarker m = marker->begin(); m != marker->end(); ++m) {
 
159
            int xp = mapx(int(m->second.time() * sampleRate));
 
160
            if (xp > x+w)
 
161
                  break;
 
162
            int xe = r.x() + r.width();
 
163
            iMarker mm = m;
 
164
            ++mm;
 
165
            if (mm != marker->end()) {
 
166
                  xe = mapx(mm->first);
 
167
                  }
 
168
            QRect tr(xp, 0, xe-xp, 13);
 
169
            if (m->second.current()) {
 
170
                  p.fillRect(tr, Qt::white);
 
171
                  }
 
172
            if (r.intersects(tr)) {
 
173
                  int x2;
 
174
                  iMarker mm = m;
 
175
                  ++mm;
 
176
                  if (mm != marker->end())
 
177
                        x2 = mapx(mm->first);
 
178
                  else
 
179
                        x2 = xp+200;
 
180
                  QRect r  = QRect(xp+10, 0, x2-xp, 12);
 
181
                  p.drawPixmap(xp, 0, *flagIconS);
 
182
                  p.drawText(r, Qt::AlignLeft|Qt::AlignVCenter, m->second.name());
 
183
                  }
 
184
            }
 
185
 
 
186
      //---------------------------------------------------
 
187
      //    draw location marker
 
188
      //---------------------------------------------------
 
189
 
 
190
      int h = height()-12;
 
191
 
 
192
      if (barLocator) {
 
193
            p.setPen(Qt::red);
 
194
            int xp = mapx(pos[0]);
 
195
            if (xp >= x && xp < x+w)
 
196
                  p.drawLine(xp, y, xp, h);
 
197
            p.setPen(Qt::blue);
 
198
            xp = mapx(pos[1]);
 
199
            if (xp >= x && xp < x+w)
 
200
                  p.drawLine(xp, y, xp, h);
 
201
            xp = mapx(pos[2]);
 
202
            if (xp >= x && xp < x+w)
 
203
                  p.drawLine(xp, y, xp, h);
 
204
            }
 
205
      else {
 
206
            for (int i = 0; i < 3; ++i) {
 
207
                  int xp = mapx(pos[i]);
 
208
                  if (xp >= x && xp < x+w) {
 
209
                        QPixmap* pm = markIcon[i];
 
210
                        p.drawPixmap(xp - pm->width()/2, y-1, *pm);
 
211
                        }
 
212
                  }
 
213
            }
 
214
      p.setPen(Qt::black);
 
215
      if (pos[3] != -1) {
 
216
            int xp = mapx(pos[3]);
 
217
            if (xp >= x && xp < x+w)
 
218
                  p.drawLine(xp, 0, xp, height());
 
219
            }
 
220
 
 
221
      int ctick = song->samples2tick(mapxDev(x));
 
222
      int bar1, bar2, beat, tick;
 
223
      song->tickValues(ctick, &bar1, &beat, &tick);
 
224
      song->tickValues(song->samples2tick(mapxDev(x+w)), &bar2, &beat, &tick);
 
225
 
 
226
//printf("bar %d  %d-%d=%d\n", bar, ntick, stick, ntick-stick);
 
227
 
 
228
      int stick = song->bar2tick(bar1, 0, 0);
 
229
      int ntick;
 
230
      for (int bar = bar1; bar <= bar2; bar++, stick = ntick) {
 
231
            ntick     = song->bar2tick(bar+1, 0, 0);
 
232
            int a = song->tick2samples(ntick);
 
233
            int b = song->tick2samples(stick);
 
234
            int tpix  = rmapx(a - b);
 
235
            if (tpix < 64) {
 
236
                  // don�t show beats if measure is this small
 
237
                  int n = 1;
 
238
                  if (tpix < 32)
 
239
                        n = 2;
 
240
                  if (tpix <= 16)
 
241
                        n = 4;
 
242
                  if (tpix < 8)
 
243
                        n = 8;
 
244
                  if (tpix <= 4)
 
245
                        n = 16;
 
246
                  if (tpix <= 2)
 
247
                        n = 32;
 
248
                  if (bar % n)
 
249
                        continue;
 
250
                  p.setFont(font3);
 
251
                  int x = mapx(b);
 
252
                  QString s;
 
253
                  s.setNum(bar + 1);
 
254
                  p.drawLine(x, y+1, x, y+1+h);
 
255
                  QRect r = QRect(x+2, y, 0, h);
 
256
                  p.drawText(r, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip, s);
 
257
                  }
 
258
            else {
 
259
                  int z, n;
 
260
                  song->timesig(stick, z, n);
 
261
                  for (int beat = 0; beat < z; beat++) {
 
262
                        int xx = song->tick2samples(song->bar2tick(bar, beat, 0));
 
263
                        int xp = mapx(xx);
 
264
                        QString s;
 
265
                        QRect r(xp+2, y, 0, h);
 
266
                        int y1;
 
267
                        int num;
 
268
                        if (beat == 0) {
 
269
                              num = bar + 1;
 
270
                              y1  = y + 1;
 
271
                              p.setFont(font3);
 
272
                              }
 
273
                        else {
 
274
                              num = beat + 1;
 
275
                              y1  = y + 7;
 
276
                              p.setFont(font1);
 
277
                              r.setY(y+3);
 
278
                              }
 
279
                        s.setNum(num);
 
280
                        p.drawLine(xp, y1, xp, y+1+h);
 
281
                        p.drawText(r, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip, s);
 
282
                        }
 
283
                  }
 
284
            }
 
285
      }
 
286