File: GuiTopBar.cpp

package info (click to toggle)
pianobooster 0.6.7~svn156-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,796 kB
  • ctags: 1,930
  • sloc: cpp: 11,738; makefile: 66; ansic: 26
file content (263 lines) | stat: -rw-r--r-- 7,032 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*********************************************************************************/
/*!
@file           GuiTopBar.cpp

@brief          xxxx.

@author         L. J. Barman

    Copyright (c)   2008-2009, L. J. Barman, all rights reserved

    This file is part of the PianoBooster application

    PianoBooster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PianoBooster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PianoBooster.  If not, see <http://www.gnu.org/licenses/>.

*/
/*********************************************************************************/

#include <QtGui>

#include "GuiTopBar.h"
#include "TrackList.h"
#include "GuiLoopingPopup.h"


GuiTopBar::GuiTopBar(QWidget *parent, CSettings* settings)
    : QWidget(parent), m_settings(settings)
{

    m_atTheEndOfTheSong = false;
    m_song = 0;
    setupUi(this);

    parent->installEventFilter(this);

    speedSpin->setMaximum(200);
    speedSpin->setMinimum(20);
    speedSpin->setSuffix(" %");
    speedSpin->setSingleStep(2);
    speedSpin->setValue(100);

    transposeSpin->setMaximum(12);
    transposeSpin->setMinimum(-12);

    majorCombo->addItem(tr("Major"));
    majorCombo->addItem(tr("Minor"));
    setMaximumHeight(30);
    setMaximumSize(QSize(16777215, 30));
}

void GuiTopBar::init(CSong* songObj, CTrackList* trackList)
{
    m_song = songObj;
    reloadKeyCombo(true);
}

void GuiTopBar::refresh(bool reset)
{
    if (reset == true)
    {
        majorCombo->setCurrentIndex(0);
        reloadKeyCombo(true);
        transposeSpin->setValue(0);
        startBarSpin->setValue(0);
    }
    int index = 0;
    if (m_song)
        index = CStavePos::getKeySignature() + 6;
    if (index >= 0 && index < keyCombo->count())
        keyCombo->setCurrentIndex(index);

}

void GuiTopBar::reloadKeyCombo(bool major)
{
    keyCombo->clear();
    if (major)
    {
        keyCombo->addItem(tr("Gb")); // -6
        keyCombo->addItem(tr("Db")); // -5
        keyCombo->addItem(tr("Ab")); // -4
        keyCombo->addItem(tr("Eb")); // -3
        keyCombo->addItem(tr("Bb")); // -2
        keyCombo->addItem(tr("F ")); // -1
        keyCombo->addItem(tr("C"));  // 0
        keyCombo->addItem(tr("G ")); // 1
        keyCombo->addItem(tr("D ")); // 2
        keyCombo->addItem(tr("A ")); // 3
        keyCombo->addItem(tr("E ")); // 4
        keyCombo->addItem(tr("B ")); // 5
        keyCombo->addItem(tr("F#")); // 6
    }
    else
    {
        keyCombo->addItem(tr("Eb"));
        keyCombo->addItem(tr("Bb"));
        keyCombo->addItem(tr("F"));
        keyCombo->addItem(tr("C"));
        keyCombo->addItem(tr("G"));
        keyCombo->addItem(tr("D "));
        keyCombo->addItem(tr("A"));
        keyCombo->addItem(tr("E "));
        keyCombo->addItem(tr("B "));
        keyCombo->addItem(tr("F#"));
        keyCombo->addItem(tr("G#"));
        keyCombo->addItem(tr("C#"));
        keyCombo->addItem(tr("D#"));
    }
    refresh(false);
}

void GuiTopBar::on_keyCombo_activated(int index)
{
    CStavePos::setKeySignature(index - 6, 0);
    m_song->refreshScroll();
}

void GuiTopBar::on_transposeSpin_valueChanged(int value)
{
    unsigned int i;         //C  Db  D  Eb  E  F   F# G  Ab  A  Bb  B
    const int nextKey[] = {   0, -5, 2, -3, 4, -1, 6, 1, -4, 3, -2, 5};
    if (!m_song) return;
    int diff = value - m_song->getTranspose();
    int oldValue = CStavePos::getKeySignature();
        if (oldValue == -6)
            oldValue = 6; // if key is Eb change to D#

    // Find the old value in the table
    for (i=0; i < arraySize(nextKey); i++)
    {
        if (oldValue == nextKey[i])
            break;
    }

    int newValue = nextKey[(diff  + i + arraySize(nextKey)) % arraySize(nextKey) ];

    CStavePos::setKeySignature( newValue, 0 );

    newValue += 6;
    keyCombo->setCurrentIndex(newValue);

    if (newValue >= 0 && newValue < keyCombo->count())
        keyCombo->setCurrentIndex(newValue);

    m_song->transpose(value);
    m_song->forceScoreRedraw();
}

void GuiTopBar::setPlayButtonState(bool checked, bool atTheEnd)
{
    if (atTheEnd)
        m_atTheEndOfTheSong = true;

    playButton->setChecked(checked);
    if (checked)
    {
        playButton->setIcon(QIcon(":/images/stop.png"));
        playButton->setToolTip("");
        playFromStartButton->setToolTip("");
    }
    else
    {
        playButton->setIcon(QIcon(":/images/play.png"));
        playButton->setToolTip(tr("Start and stop playing music"));
        playFromStartButton->setToolTip(tr("Playing music from the beginning"));
    }
}


void GuiTopBar::on_playButton_clicked(bool clicked)
{
    if (!m_song) return;

    if (m_atTheEndOfTheSong)
        m_song->rewind();
    m_atTheEndOfTheSong = false;

    bool start = !m_song->playingMusic();
    m_song->playMusic(start);
    setPlayButtonState(start);
}

void GuiTopBar::on_playFromStartButton_clicked(bool clicked)
{
    if (!m_song) return;

    m_atTheEndOfTheSong = false;
    m_song->playFromStartBar();
    setPlayButtonState(true);
}

void GuiTopBar::on_speedSpin_valueChanged(int speed)
{
    if (!m_song) return;
    m_song->setSpeed(speed/100.0);
}

void GuiTopBar::on_startBarSpin_valueChanged(double bar)
{
    if (!m_song) return;

    // Stop the muisc playing
    m_song->playMusic(false);
    setPlayButtonState(false);

    m_song->setPlayFromBar( bar);
}



void GuiTopBar::on_saveBarButton_clicked(bool clicked)
{
    if (!m_song) return;
    double barNumber = m_song->getCurrentBarPos();
    startBarSpin->setValue(barNumber);
}


void GuiTopBar::on_loopingBarsPopupButton_clicked(bool clicked)
{
    if (!m_song) return;

    m_song->playMusic(false);
    setPlayButtonState(false);

    QPoint pos = mapToGlobal(loopingBarsPopupButton->pos()) ;

    pos.ry() += loopingBarsPopupButton->height() + 2;
    pos.rx() += -5; // Tweak the position slightly
    GuiLoopingPopup *loopingPopup = new GuiLoopingPopup(loopingBarsPopupButton);
    loopingPopup->init(m_song);
    loopingPopup->move (pos);
    loopingPopup->show();
}

bool GuiTopBar::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key()==Qt::Key_Up) {
            speedSpin->stepUp();
            return true;
        } else if (keyEvent->key()==Qt::Key_Down) {
            speedSpin->stepDown();
            return true;
        } else {
            return false;
        }
    } else {
        // it's not a key event, lets do standard event processing
        return QObject::eventFilter(obj, event);
    }
}