Skip to content

Commit bb5a692

Browse files
committed
Added Pause and Stop actions
1 parent 2e7e18f commit bb5a692

File tree

7 files changed

+116
-5
lines changed

7 files changed

+116
-5
lines changed

ActionCycle.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "ActionCycle.h"
2+
#include <QIcon>
3+
4+
ActionCycle::ActionCycle(QObject *_parent)
5+
: QAction(_parent), m_currentIndex(0)
6+
{
7+
connect(this, SIGNAL(triggered()), this, SLOT(onTrigger()));
8+
}
9+
10+
void ActionCycle::addAction(const QString &_text)
11+
{
12+
m_texts.append(_text);
13+
m_icons.append(QIcon());
14+
}
15+
16+
void ActionCycle::addAction(const QString &_text, const QIcon &_icon)
17+
{
18+
m_texts.append(_text);
19+
m_icons.append(_icon);
20+
}
21+
22+
void ActionCycle::setIndex(int _index)
23+
{
24+
if(_index < 0 || _index >= m_texts.size())
25+
return;
26+
m_currentIndex = _index;
27+
_updateAction();
28+
}
29+
30+
void ActionCycle::_updateAction()
31+
{
32+
setText(m_texts[m_currentIndex]);
33+
setIcon(m_icons[m_currentIndex]);
34+
}
35+
36+
void ActionCycle::onTrigger()
37+
{
38+
if(m_texts.size() <= 0)
39+
return;
40+
41+
emit triggered(m_currentIndex);
42+
m_currentIndex = (m_currentIndex + 1) % m_texts.size();
43+
_updateAction();
44+
}

ActionCycle.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef ACTIONCYCLE_H
2+
#define ACTIONCYCLE_H
3+
4+
#include <QAction>
5+
6+
class ActionCycle : public QAction
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
ActionCycle(QObject* _parent = nullptr);
12+
13+
void addAction(const QString &_text);
14+
void addAction(const QString &_text, const QIcon &_icon);
15+
void setIndex(int _index);
16+
17+
public slots:
18+
inline void reset() { setIndex(0); }
19+
20+
private :
21+
void _updateAction();
22+
23+
signals:
24+
void triggered(int _index);
25+
26+
private slots:
27+
void onTrigger();
28+
29+
private:
30+
int m_currentIndex;
31+
QList<QString> m_texts;
32+
QList<QIcon> m_icons;
33+
};
34+
35+
#endif // ACTIONCYCLE_H

Signal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ void Signal::stop()
190190
if(m_audio->state() == QAudio::ActiveState)
191191
{
192192
m_audio->stop();
193+
emit stopped();
193194
}
194195
}
195196

@@ -218,6 +219,7 @@ void Signal::handleStateChanged(QAudio::State newState)
218219
case QAudio::IdleState:
219220
// Finished playing (no more data)
220221
m_audio->stop();
222+
emit stopped();
221223
break;
222224

223225
case QAudio::StoppedState:

Signal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Signal : public QObject
5050

5151
signals:
5252
void signalChanged();
53+
void stopped();
5354

5455
public slots:
5556
void play();

SoundGenerator.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ SOURCES += \
6565
UI/PushOrDragButton.cpp \
6666
UI/WaveFormChunk.cpp \
6767
UI/WaveFormView.cpp \
68-
LoopableBuffer.cpp
68+
LoopableBuffer.cpp \
69+
ActionCycle.cpp
6970

7071
HEADERS += \
7172
Components/AddComponent.h \
@@ -106,7 +107,8 @@ HEADERS += \
106107
UI/PushOrDragButton.h \
107108
UI/WaveFormChunk.h \
108109
UI/WaveFormView.h \
109-
LoopableBuffer.h
110+
LoopableBuffer.h \
111+
ActionCycle.h
110112

111113
FORMS += \
112114
UI/mainwindow.ui

UI/MainWindow.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#include "PushOrDragButton.h"
3030
#include "NodalScene.h"
3131
#include <QStyle>
32-
#include <Utils.h>
32+
#include "Utils.h"
33+
#include "ActionCycle.h"
3334

3435

3536
MainWindow::MainWindow(QWidget *parent) :
@@ -123,9 +124,16 @@ MainWindow::MainWindow(QWidget *parent) :
123124
act_generate->setShortcut(QKeySequence::Refresh);
124125
connect(act_generate, SIGNAL(triggered()), &m_signal, SLOT(generate()));
125126

126-
QAction* act_play = new QAction("Play", this);
127+
ActionCycle* act_play = new ActionCycle(this);
128+
act_play->addAction("Play");
129+
act_play->addAction("Pause");
127130
act_play->setShortcut(QKeySequence("Space"));
128-
connect(act_play, SIGNAL(triggered()), &m_signal, SLOT(play()));
131+
act_play->reset();
132+
connect(act_play, SIGNAL(triggered(int)), this, SLOT(playPause(int)));
133+
connect(&m_signal, SIGNAL(stopped()), act_play, SLOT(reset()));
134+
135+
QAction* act_stop = new QAction("Stop", this);
136+
connect(act_stop, SIGNAL(triggered()), &m_signal, SLOT(stop()));
129137

130138
QAction* act_loop = new QAction("Loop", this);
131139
act_loop->setCheckable(true);
@@ -194,6 +202,7 @@ MainWindow::MainWindow(QWidget *parent) :
194202

195203
ui->menuAudio->addAction(act_generate);
196204
ui->menuAudio->addAction(act_play);
205+
ui->menuAudio->addAction(act_stop);
197206
ui->menuAudio->addAction(act_loop);
198207

199208
ui->mainToolBar->clear();
@@ -203,6 +212,7 @@ MainWindow::MainWindow(QWidget *parent) :
203212
ui->mainToolBar->addSeparator();
204213
ui->mainToolBar->addAction(act_generate);
205214
ui->mainToolBar->addAction(act_play);
215+
ui->mainToolBar->addAction(act_stop);
206216

207217
QWidget* spacer = new QWidget(this);
208218
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
@@ -455,6 +465,22 @@ void MainWindow::quit()
455465
}
456466
}
457467

468+
void MainWindow::playPause(int _index)
469+
{
470+
switch(_index)
471+
{
472+
case 0:
473+
m_signal.play();
474+
break;
475+
case 1:
476+
m_signal.pause();
477+
break;
478+
default:
479+
Q_ASSERT(true);
480+
break;
481+
}
482+
}
483+
458484
void MainWindow::setDirty()
459485
{
460486
if(!m_dirty && m_dirtyable)

UI/MainWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public slots:
6060
void about();
6161
void exportWAV();
6262
void quit();
63+
void playPause(int _index);
6364
// other
6465
void setDirty();
6566
void changeVolume(int value);

0 commit comments

Comments
 (0)