1 /*********************************************************************************/
2 /*!
3 @file           GuiTopBar.cpp
4 
5 @brief          xxxx.
6 
7 @author         L. J. Barman
8 
9     Copyright (c)   2008-2013, L. J. Barman, all rights reserved
10 
11     This file is part of the PianoBooster application
12 
13     PianoBooster is free software: you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation, either version 3 of the License, or
16     (at your option) any later version.
17 
18     PianoBooster is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22 
23     You should have received a copy of the GNU General Public License
24     along with PianoBooster.  If not, see <http://www.gnu.org/licenses/>.
25 
26 */
27 /*********************************************************************************/
28 
29 #include <QtWidgets>
30 
31 #include "GuiTopBar.h"
32 #include "TrackList.h"
33 #include "GuiLoopingPopup.h"
34 
GuiTopBar(QWidget * parent,CSettings * settings)35 GuiTopBar::GuiTopBar(QWidget *parent, CSettings* settings)
36     : QWidget(parent), m_settings(settings)
37 {
38 
39     m_atTheEndOfTheSong = false;
40     m_song = nullptr;
41     setupUi(this);
42 
43     parent->installEventFilter(this);
44 
45     speedSpin->setMaximum(200);
46     speedSpin->setMinimum(20);
47     speedSpin->setSuffix(" %");
48     speedSpin->setSingleStep(2);
49     speedSpin->setValue(100);
50 
51     transposeSpin->setMaximum(12);
52     transposeSpin->setMinimum(-12);
53 
54     majorCombo->addItem(tr("Major"));
55     majorCombo->addItem(tr("Minor"));
56     setMaximumHeight(30);
57     setMaximumSize(QSize(16777215, 30));
58 }
59 
init(CSong * songObj)60 void GuiTopBar::init(CSong* songObj)
61 {
62     m_song = songObj;
63     reloadKeyCombo(true);
64 }
65 
refresh(bool reset)66 void GuiTopBar::refresh(bool reset)
67 {
68     if (reset == true)
69     {
70         majorCombo->setCurrentIndex(0);
71         reloadKeyCombo(true);
72         transposeSpin->setValue(0);
73         startBarSpin->setValue(0);
74     }
75     int index = 0;
76     if (m_song)
77         index = CStavePos::getKeySignature() + 6;
78     if (index >= 0 && index < keyCombo->count())
79         keyCombo->setCurrentIndex(index);
80 
81 }
82 
reloadKeyCombo(bool major)83 void GuiTopBar::reloadKeyCombo(bool major)
84 {
85     keyCombo->clear();
86     if (major)
87     {
88         keyCombo->addItem(tr("Gb")); // -6
89         keyCombo->addItem(tr("Db")); // -5
90         keyCombo->addItem(tr("Ab")); // -4
91         keyCombo->addItem(tr("Eb")); // -3
92         keyCombo->addItem(tr("Bb")); // -2
93         keyCombo->addItem(tr("F")); // -1
94         keyCombo->addItem(tr("C"));  // 0
95         keyCombo->addItem(tr("G")); // 1
96         keyCombo->addItem(tr("D")); // 2
97         keyCombo->addItem(tr("A")); // 3
98         keyCombo->addItem(tr("E")); // 4
99         keyCombo->addItem(tr("B")); // 5
100         keyCombo->addItem(tr("F#")); // 6
101     }
102     else
103     {
104         keyCombo->addItem(tr("Eb"));
105         keyCombo->addItem(tr("Bb"));
106         keyCombo->addItem(tr("F"));
107         keyCombo->addItem(tr("C"));
108         keyCombo->addItem(tr("G"));
109         keyCombo->addItem(tr("D"));
110         keyCombo->addItem(tr("A"));
111         keyCombo->addItem(tr("E"));
112         keyCombo->addItem(tr("B"));
113         keyCombo->addItem(tr("F#"));
114         keyCombo->addItem(tr("G#"));
115         keyCombo->addItem(tr("C#"));
116         keyCombo->addItem(tr("D#"));
117     }
118     refresh(false);
119 }
120 
on_keyCombo_activated(int index)121 void GuiTopBar::on_keyCombo_activated(int index)
122 {
123     CStavePos::setKeySignature(index - 6, 0);
124     m_song->refreshScroll();
125 }
126 
on_transposeSpin_valueChanged(int value)127 void GuiTopBar::on_transposeSpin_valueChanged(int value)
128 {
129     unsigned int i;         //C  Db  D  Eb  E  F   F# G  Ab  A  Bb  B
130     const int nextKey[] = {   0, -5, 2, -3, 4, -1, 6, 1, -4, 3, -2, 5};
131     const int nextKeySize = arraySize(nextKey);
132     if (!m_song) return;
133     int diff = value - m_song->getTranspose();
134     int oldValue = CStavePos::getKeySignature();
135         if (oldValue == -6)
136             oldValue = 6; // if key is Eb change to D#
137 
138     // Find the old value in the table
139     for (i=0; i < nextKeySize; i++)
140     {
141         if (oldValue == nextKey[i])
142             break;
143     }
144 
145     int newValue = nextKey[(diff  + i + nextKeySize) % nextKeySize ];
146 
147     CStavePos::setKeySignature( newValue, 0 );
148 
149     newValue += 6;
150     keyCombo->setCurrentIndex(newValue);
151 
152     if (newValue >= 0 && newValue < keyCombo->count())
153         keyCombo->setCurrentIndex(newValue);
154 
155     m_song->transpose(value);
156     m_song->forceScoreRedraw();
157 }
158 
setPlayButtonState(bool checked,bool atTheEnd)159 void GuiTopBar::setPlayButtonState(bool checked, bool atTheEnd)
160 {
161     if (atTheEnd)
162         m_atTheEndOfTheSong = true;
163 
164     playButton->setChecked(checked);
165     if (checked)
166     {
167         playButton->setIcon(QIcon(":/images/stop.png"));
168         playButton->setToolTip("");
169         playFromStartButton->setToolTip("");
170     }
171     else
172     {
173         playButton->setIcon(QIcon(":/images/play.png"));
174         playButton->setToolTip(tr("Start and stop playing music"));
175         playFromStartButton->setToolTip(tr("Playing music from the beginning"));
176     }
177 }
178 
updateTranslate()179 void GuiTopBar::updateTranslate(){
180     // save original
181     if (listWidgetsRetranslateUi.size()==0){
182         QList<QWidget*> l2 = this->findChildren<QWidget *>();
183         for (auto &w:l2){
184             QMap<QString,QString> m;
185             m["toolTip"]=w->toolTip();
186             m["whatsThis"]=w->whatsThis();
187             m["windowTitle"]=w->windowTitle();
188             m["statusTip"]=w->statusTip();
189             listWidgetsRetranslateUi[w]=m;
190         }
191     }
192 
193     // retranslate UI
194     QList<QWidget*> l2 = this->findChildren<QWidget *>();
195     for (auto &w:l2){
196         if (!w->toolTip().isEmpty()) w->setToolTip(tr(listWidgetsRetranslateUi[w]["toolTip"].toStdString().c_str()));
197         if (!w->whatsThis().isEmpty()) w->setWhatsThis(tr(listWidgetsRetranslateUi[w]["whatsThis"].toStdString().c_str()));
198         if (!w->windowTitle().isEmpty()) w->setWindowTitle(tr(listWidgetsRetranslateUi[w]["windowTitle"].toStdString().c_str()));
199         if (!w->statusTip().isEmpty()) w->setStatusTip(tr(listWidgetsRetranslateUi[w]["statusTip"].toStdString().c_str()));
200     }
201 
202     majorCombo->setItemText(0,tr("Major"));
203     majorCombo->setItemText(1,tr("Minor"));
204     majorCombo->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);
205 
206     retranslateUi(this);
207 }
208 
on_playButton_clicked(bool clicked)209 void GuiTopBar::on_playButton_clicked(bool clicked)
210 {
211     if (!m_song) return;
212 
213     if (m_atTheEndOfTheSong)
214         m_song->rewind();
215     m_atTheEndOfTheSong = false;
216 
217     bool start = !m_song->playingMusic();
218     m_song->playMusic(start);
219     setPlayButtonState(start);
220 }
221 
on_playFromStartButton_clicked(bool clicked)222 void GuiTopBar::on_playFromStartButton_clicked(bool clicked)
223 {
224     if (!m_song) return;
225 
226     m_atTheEndOfTheSong = false;
227     m_song->playFromStartBar();
228     setPlayButtonState(true);
229 }
230 
on_speedSpin_valueChanged(int speed)231 void GuiTopBar::on_speedSpin_valueChanged(int speed)
232 {
233     if (!m_song) return;
234     m_song->setSpeed(speed/100.0);
235 }
236 
on_startBarSpin_valueChanged(double bar)237 void GuiTopBar::on_startBarSpin_valueChanged(double bar)
238 {
239     if (!m_song) return;
240 
241     stopMuiscPlaying();
242 
243     m_song->setPlayFromBar( bar);
244 }
245 
246 // Stop the muisc playing
stopMuiscPlaying()247 void GuiTopBar::stopMuiscPlaying()
248 {
249     if (!m_song) return;
250 
251     m_song->playMusic(false);
252     setPlayButtonState(false);
253 }
254 
on_saveBarButton_clicked(bool clicked)255 void GuiTopBar::on_saveBarButton_clicked(bool clicked)
256 {
257     if (!m_song) return;
258     double barNumber = m_song->getCurrentBarPos();
259     startBarSpin->setValue(barNumber);
260 }
261 
on_loopingBarsPopupButton_clicked(bool clicked)262 void GuiTopBar::on_loopingBarsPopupButton_clicked(bool clicked)
263 {
264     if (!m_song) return;
265 
266     m_song->playMusic(false);
267     setPlayButtonState(false);
268 
269     QPoint pos = mapToGlobal(loopingBarsPopupButton->pos()) ;
270 
271     pos.ry() += loopingBarsPopupButton->height() + 2;
272     pos.rx() += -5; // Tweak the position slightly
273     GuiLoopingPopup *loopingPopup = new GuiLoopingPopup(loopingBarsPopupButton);
274     loopingPopup->init(m_song);
275     loopingPopup->move (pos);
276     loopingPopup->show();
277 }
278 
eventFilter(QObject * obj,QEvent * event)279 bool GuiTopBar::eventFilter(QObject *obj, QEvent *event)
280 {
281     if (event->type() == QEvent::KeyPress) {
282         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
283         if (keyEvent->key()==Qt::Key_Up) {
284             speedSpin->stepUp();
285             return true;
286         } else if (keyEvent->key()==Qt::Key_Down) {
287             speedSpin->stepDown();
288             return true;
289         } else {
290             return false;
291         }
292     } else {
293         // it's not a key event, lets do standard event processing
294         return QObject::eventFilter(obj, event);
295     }
296 }
297