1 //=============================================================================
2 //  Awl
3 //  Audio Widget Library
4 //  $Id:$
5 //
6 //  Copyright (C) 1999-2011 by Werner Schweer and others
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; version 2 of
11 //  the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //=============================================================================
22 
23 #include "pitchedit.h"
24 #include "pitchlabel.h"
25 #include "utils.h"
26 
27 #include <QApplication>
28 #include <QStyle>
29 
30 namespace Awl {
31 
32 //---------------------------------------------------------
33 //   PitchLabel
34 //---------------------------------------------------------
35 
PitchLabel()36 PitchLabel::PitchLabel()
37       {
38       _pitchMode = true;
39       _value = -1;
40       setFrameStyle(WinPanel | Sunken);
41       setLineWidth(2);
42       setMidLineWidth(3);
43       setValue(0);
44       int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
45       setIndent(fw);
46       }
47 
48 //---------------------------------------------------------
49 //   setPitchMode
50 //---------------------------------------------------------
51 
setPitchMode(bool val)52 void PitchLabel::setPitchMode(bool val)
53       {
54       _pitchMode = val;
55       }
56 
57 //---------------------------------------------------------
58 //   sizeHint
59 //---------------------------------------------------------
60 
sizeHint() const61 QSize PitchLabel::sizeHint() const
62       {
63       QFontMetrics fm(font());
64       int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
65       int h  = fm.height() + fw * 2;
66 //      int w = 2 + fm.width(QString("A#8")) +  fw * 4;
67 // Width() is obsolete. Qt >= 5.11 use horizontalAdvance().
68 #if QT_VERSION >= 0x050b00
69       int w = 2 + fm.horizontalAdvance(QString("-9999")) + fw * 4;     // must display 14Bit controller values
70 #else
71       int w = 2 + fm.width(QString("-9999")) + fw * 4;     // must display 14Bit controller values
72 #endif
73       return QSize(w, h).expandedTo(QApplication::globalStrut());
74       }
75 
76 //---------------------------------------------------------
77 //   setValue
78 //---------------------------------------------------------
79 
setValue(int val)80 void PitchLabel::setValue(int val)
81       {
82       if (val == _value)
83             return;
84       _value = val;
85       QString s;
86       if (_pitchMode)
87             s = pitch2string(_value);
88       else
89             s.setNum(_value);
90       setText(s);
91       }
92 
93 //---------------------------------------------------------
94 //   setInt
95 //---------------------------------------------------------
96 
setInt(int val)97 void PitchLabel::setInt(int val)
98       {
99       if (_pitchMode)
100             setPitchMode(false);
101       setValue(val);
102       }
103 
104 //---------------------------------------------------------
105 //   setPitch
106 //---------------------------------------------------------
107 
setPitch(int val)108 void PitchLabel::setPitch(int val)
109       {
110       if (!_pitchMode) {
111             setPitchMode(true);
112             }
113       setValue(val);
114       }
115 }
116 
117