1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //  sig_tempo_toolbar.cpp
5 //  (C) Copyright 2012 Florian Jung (flo93@users.sourceforge.net)
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; version 2 of
10 //  the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //=========================================================
22 
23 
24 #include "sig_tempo_toolbar.h"
25 #include "tempolabel.h"
26 #include "sigedit.h"
27 #include "song.h"
28 #include "icons.h"
29 #include "pixmap_button.h"
30 #include "sig.h"
31 #include "tempo.h"
32 
33 #include <QLabel>
34 #include <QToolButton>
35 
36 namespace MusEGui
37 {
38 
39 //---------------------------------
40 //   TempoToolbar
41 //---------------------------------
42 
TempoToolbar(QWidget * parent)43 TempoToolbar::TempoToolbar(QWidget* parent)
44     : QToolBar(parent)
45 {
46     init();
47 }
48 
TempoToolbar(const QString & title,QWidget * parent)49 TempoToolbar::TempoToolbar(const QString& title, QWidget* parent)
50     : QToolBar(title, parent)
51 {
52     init();
53 }
54 
init()55 void TempoToolbar::init()
56 {
57     setObjectName("Tempo toolbar");
58 
59     _masterButton = new QToolButton(this);
60     _masterButton->setIcon(*masterTrackOnSVGIcon);
61     _masterButton->setCheckable(true);
62     _masterButton->setToolTip(tr("Use mastertrack tempo"));
63     connect(_masterButton, SIGNAL(toggled(bool)), SLOT(masterToggled(bool)));
64 
65     tempo_edit=new TempoEdit(this);
66     tempo_edit->setToolTip(tr("Mastertrack tempo at current position, or fixed tempo"));
67     tempo_edit->setContentsMargins(0, 0, 0, 0);
68     tempo_edit->setFocusPolicy(Qt::StrongFocus);
69 
70     tap_button = new QToolButton(this);
71     tap_button->setText(tr("Tap"));
72     tap_button->setToolTip(tr("Tap this button to set tempo"));
73     tap_button->setStatusTip(tr("Set the tempo to the time difference of two successive clicks. Times out after two seconds."));
74     tap_button->setContentsMargins(0, 0, 0, 0);
75 
76     blink_timer = new QTimer(this);
77     connect(blink_timer, SIGNAL(timeout()), this, SLOT(tapButtonBlink()));
78     blink_timer->stop();
79 
80     //   addWidget(_externSyncButton);
81     addWidget(_masterButton);
82     addWidget(tempo_edit);
83     addWidget(tap_button);
84 
85     connect(MusEGlobal::song, SIGNAL(songChanged(MusECore::SongChangedStruct_t)), this, SLOT(song_changed(MusECore::SongChangedStruct_t)));
86     connect(MusEGlobal::song, SIGNAL(posChanged(int, unsigned, bool)), this, SLOT(pos_changed(int,unsigned,bool)));
87     //connect(&MusEGlobal::extSyncFlag, SIGNAL(valueChanged(bool)), SLOT(syncChanged(bool)));
88 
89     connect(tempo_edit, SIGNAL(tempoChanged(double)), MusEGlobal::song, SLOT(setTempo(double)));
90     connect(tempo_edit, SIGNAL(returnPressed()), SIGNAL(returnPressed()));
91     connect(tempo_edit, SIGNAL(escapePressed()), SIGNAL(escapePressed()));
92 
93     connect(tap_button, SIGNAL(clicked(bool)), SLOT(tap_tempo()));
94     connect(&tap_timer, SIGNAL(timeout()), SLOT(tap_timer_signal()));
95     tap_timer.stop();
96 
97     song_changed(-1);
98 
99     ensurePolished();
100     buttonDefColor = tap_button->palette().color(QPalette::Button).name();
101 }
102 
pos_changed(int,unsigned,bool)103 void TempoToolbar::pos_changed(int,unsigned,bool)
104 {
105     song_changed(SC_TEMPO);
106 }
107 
song_changed(MusECore::SongChangedStruct_t type)108 void TempoToolbar::song_changed(MusECore::SongChangedStruct_t type)
109 {
110     if(type & (SC_TEMPO | SC_MASTER))
111     {
112         int tempo = MusEGlobal::tempomap.tempo(MusEGlobal::song->cpos());
113         tempo_edit->blockSignals(true);
114         tempo_edit->setValue(double(60000000.0/tempo));
115         tempo_edit->blockSignals(false);
116     }
117     if(type & SC_MASTER)
118     {
119         setMasterTrack(MusEGlobal::tempomap.masterFlag());
120     }
121     if(type & SC_EXTERNAL_MIDI_SYNC)
122     {
123         syncChanged(MusEGlobal::extSyncFlag);
124     }
125 }
126 
syncChanged(bool flag)127 void TempoToolbar::syncChanged(bool flag)
128 {
129     tap_button->setEnabled(!flag);
130     tempo_edit->setExternalMode(flag);
131 }
132 
tap_tempo()133 void TempoToolbar::tap_tempo()
134 {
135     QDateTime local(QDateTime::currentDateTime());
136 
137     if (tap_timer.isActive()) {
138         qint64 msecs_tap = last_tap_time.msecsTo(local);
139         double t_tap = (double)60000.0f / (double)msecs_tap;
140         tempo_edit->setValue(t_tap);
141         emit tempo_edit->tempoChanged(t_tap);
142     }
143 
144     tap_timer.start(2000);
145     blinkButtonState = true;
146     blink_timer->start(100);
147 
148     last_tap_time = local;
149 }
150 
tap_timer_signal()151 void TempoToolbar::tap_timer_signal()
152 {
153     tap_timer.stop();
154     blink_timer->stop();
155     tap_button->setStyleSheet("QToolButton {background:" + buttonDefColor + "}");
156 }
157 
tapButtonBlink()158 void TempoToolbar::tapButtonBlink()
159 {
160     blinkButtonState = !blinkButtonState;
161     tap_button->setStyleSheet("QToolButton {background:" + (blinkButtonState ? buttonDefColor : "Crimson") + "}");
162 }
163 
164 //---------------------------------------------------------
165 //   masterToggled
166 //---------------------------------------------------------
167 
masterToggled(bool val)168 void TempoToolbar::masterToggled(bool val)
169 {
170     emit masterTrackChanged(val);
171 }
172 
masterTrack() const173 bool TempoToolbar::masterTrack() const
174 {
175     return _masterButton->isChecked();
176 }
177 
setMasterTrack(bool on)178 void TempoToolbar::setMasterTrack(bool on)
179 {
180     _masterButton->blockSignals(true);
181     _masterButton->setChecked(on);
182     _masterButton->blockSignals(false);
183 }
184 
185 
186 
187 //---------------------------------
188 //   SigToolbar
189 //---------------------------------
190 
SigToolbar(QWidget * parent)191 SigToolbar::SigToolbar(QWidget* parent)
192     : QToolBar(parent)
193 {
194     init();
195 }
196 
SigToolbar(const QString & title,QWidget * parent)197 SigToolbar::SigToolbar(const QString& title, QWidget* parent)
198     : QToolBar(title, parent)
199 {
200     init();
201 }
202 
init()203 void SigToolbar::init()
204 {
205     setObjectName("Signature toolbar");
206     sig_edit=new SigEdit(this);
207     sig_edit->setContentsMargins(0, 0, 0, 0);
208     sig_edit->setFocusPolicy(Qt::StrongFocus);
209     sig_edit->setValue(MusECore::TimeSignature(4, 4));
210     sig_edit->setToolTip(tr("Time signature at current position"));
211 
212     //  label=new QLabel(tr("Signature "),this);
213     //  label->setContentsMargins(0, 0, 0, 0);
214 
215     //  addWidget(label);
216     addWidget(sig_edit);
217 
218     connect(MusEGlobal::song, SIGNAL(songChanged(MusECore::SongChangedStruct_t)), this, SLOT(song_changed(MusECore::SongChangedStruct_t)));
219     connect(MusEGlobal::song, SIGNAL(posChanged(int, unsigned, bool)), this, SLOT(pos_changed(int,unsigned,bool)));
220 
221     connect(sig_edit, SIGNAL(valueChanged(const MusECore::TimeSignature&)), MusEGlobal::song, SLOT(setSig(const MusECore::TimeSignature&)));
222     connect(sig_edit, SIGNAL(returnPressed()), SIGNAL(returnPressed()));
223     connect(sig_edit, SIGNAL(escapePressed()), SIGNAL(escapePressed()));
224 
225     song_changed(-1);
226 }
227 
228 
pos_changed(int,unsigned,bool)229 void SigToolbar::pos_changed(int,unsigned,bool)
230 {
231     song_changed(SC_SIG);
232 }
233 
song_changed(MusECore::SongChangedStruct_t type)234 void SigToolbar::song_changed(MusECore::SongChangedStruct_t type)
235 {
236     if(type & SC_SIG)
237     {
238         int z, n;
239         MusEGlobal::sigmap.timesig(MusEGlobal::song->cpos(), z, n);
240         sig_edit->blockSignals(true);
241         sig_edit->setValue(MusECore::TimeSignature(z, n));
242         sig_edit->blockSignals(false);
243     }
244 }
245 
246 
247 }  // namespace MusEGui
248