summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/poslabel.cpp
blob: 98b790ad91b1a86e765da2f8e8f4b8bd01211e79 (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
//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: poslabel.cpp,v 1.2.2.2 2009/04/06 01:24:55 terminator356 Exp $
//  (C) Copyright 2001 Werner Schweer (ws@seh.de)
//=========================================================

#include <stdlib.h>
#include <cmath>
#include <QApplication>
#include <QStyle>
//Added by qt3to4:
#include <QLabel>

#include "poslabel.h"
///#include "sig.h"
#include "al/sig.h"
#include "tempo.h"
#include "globals.h"

extern int mtcType;

//---------------------------------------------------------
//   PosLabel
//---------------------------------------------------------

PosLabel::PosLabel(QWidget* parent, const char* name)
  : QLabel(parent)
      {
      setObjectName(name);
      _tickValue = 0;
      _sampleValue = 0;
      _smpte = false;
      setFrameStyle(WinPanel | Sunken);
      setLineWidth(2);
      setMidLineWidth(3);
      //int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this); // ddskrjo 0
      int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 
      setIndent(fw);
      updateValue();
      }

//---------------------------------------------------------
//   sizeHint
//---------------------------------------------------------

QSize PosLabel::sizeHint() const
      {
      QFontMetrics fm(font());
      //int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this); // ddskrjo 0
      int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 
      int h  = fm.height() + fw * 2;
      int w;
      if (_smpte)
            w  = 2 + fm.width('9') * 9 + fm.width(':') * 3 + fw * 4;
      else
            w  = 2 + fm.width('9') * 9 + fm.width('.') * 2 + fw * 4;
      return QSize(w, h).expandedTo(QApplication::globalStrut());
      }

//---------------------------------------------------------
//   updateValue
//---------------------------------------------------------

void PosLabel::updateValue()
      {
      QString s;
      if (_smpte) {
            double time = double(_sampleValue) / double(sampleRate);
            int min  = int(time) / 60;
            int sec  = int(time) % 60;
            double rest = time - (min * 60 + sec);
            switch(mtcType) {
                  case 0:     // 24 frames sec
                        rest *= 24;
                        break;
                  case 1:     // 25
                        rest *= 25;
                        break;
                  case 2:     // 30 drop frame
                        rest *= 30;
                        break;
                  case 3:     // 30 non drop frame
                        rest *= 30;
                        break;
                  }
            int frame = int(rest);
            int subframe = int((rest-frame)*100);
            s.sprintf("%03d:%02d:%02d:%02d", min, sec, frame, subframe);
            }
      else {
            int bar, beat;
            unsigned tick;
            AL::sigmap.tickValues(_tickValue, &bar, &beat, &tick);
            //s.sprintf("%04d.%02d.%03ud", bar+1, beat+1, tick);
            s.sprintf("%04d.%02d.%03u", bar+1, beat+1, tick);
            }
      setText(s);
      }

//---------------------------------------------------------
//   setSampleValue
//---------------------------------------------------------

void PosLabel::setSampleValue(unsigned val)
      {
      if (val == _sampleValue)
            return;
      _sampleValue = val;
      updateValue();
      }

//---------------------------------------------------------
//   setTickValue
//---------------------------------------------------------

void PosLabel::setTickValue(unsigned val)
      {
      if (val == _tickValue)
            return;
      if (val >= MAX_TICK)
            abort();
      _tickValue = val;
      updateValue();
      }

//---------------------------------------------------------
//   setValue
//---------------------------------------------------------

void PosLabel::setValue(unsigned val)
      {
      unsigned oval = _smpte ? _sampleValue : _tickValue;
      if (val == oval)
            return;
      if (_smpte)
            _sampleValue = val;
      else
            _tickValue = val;
      updateValue();
      }

//---------------------------------------------------------
//   setSmpte
//---------------------------------------------------------

void PosLabel::setSmpte(bool val)
      {
      _smpte = val;
      if (val)
            _sampleValue = tempomap.tick2frame(_tickValue);
      else
            _tickValue = tempomap.frame2tick(_sampleValue);
      updateValue();
      }