1
#ifndef MORSE_H
2
#define MORSE_H
3
4
/**
5
 * @file
6
 * @author Holger Schurig
7
 *
8
 * @section LICENSE
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License as
12
 * published by the Free Software Foundation; either version 2 of
13
 * the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * General Public License for more details at
19
 * http://www.gnu.org/copyleft/gpl.html
20
 */
21
22
23
#include <QObject>
24
#include <QString>
25
#include <QHash>
26
27
28
class QTimer;
29
30
31
class Morse {
32
public:
33
	//Morse();
34
	bool contains(const QString &clearText) const;
35
	const QString operator[] (const QString &clearText) const;
36
};
37
38
39
/*!
40
 * \brief Class to generate and play morse code
41
 */
42
class GenerateMorse : public QObject {
43
	Q_OBJECT
44
45
public:
46
	GenerateMorse(QObject *parent=0);
47
	/*! Checks if the morse code for \c clearText exists */
48
	bool exists(const QString clearText) { return codes.contains(clearText); };
49
	void append(const QString &s, bool addSpace=true);
50
	void appendMorse(const QString &dahdits, const QString &clear);
51
	int  totalElements(int from=0) const; //!< Total elements in \ref morse.
52
public slots:
53
	void clear();
54
	/*! \brief Set new text */
55
	void setText(const QString &s) { clear(); append(s); };
56
private:
57
	/*!
58
	 * \brief Morse storage
59
	 *
60
	 * Here we store elements of sound and silence. To make things
61
	 * simple, we use positive values for sound and negative values for silence:
62
	 * - 1  one element sound, representing a dit
63
	 * - 3  three element sound, representing a dah
64
	 * - -1 one element silence, represinting the silence inside between
65
	 *      dits and dahs inside a character
66
	 * - -3 three elements silence, representing the silence between two
67
	 *      morse characters
68
	 * - -7 seven elements silence, representing the silence between two
69
	 *      word
70
	 * - 0  next \ref clearText entry
71
	 *
72
	 * \sa playIdx
73
	 */
74
	QList<int> morse;
75
	/*!
76
	 * \brief Clear text storage
77
	 *
78
	 * Stores the clear text for each morse characters from \ref morse.
79
	 * Whenever a \c 0 is found in \ref morse, the next entry from \ref
80
	 * clearText will be be used in \ref charChanged().
81
	 *
82
	 * \sa clearIdx
83
	 * \sa charChanged
84
	 */
85
	QList<QString> clearText;
86
87
public slots:
88
	void play();
89
	void stop();
90
	void setLoop(bool b);
91
	void setWpm(float wpm);
92
	void setDitFactor(float factor);
93
	void setDahFactor(float factor);
94
	void setIntraFactor(float factor);
95
	void setCharFactor(float factor);
96
	void setWordFactor(float factor);
97
public:
98
	float getWpm() const;
99
signals:
100
	/*! \brief Emitted whenever the sound should be turned on or off */
101
	void playSound(bool onoff);
102
	/*! \brief Emitted whenever the sound should be turned on for for the specified
103
	milliseconds */
104
	void playSound(unsigned int ms);
105
	/*! \brief Emitted whenever \ref play() finished. \sa play() */
106
	void hasStopped();
107
	/*! \brief Emitted whenever a new cleartext characters get's morsed */
108
	void charChanged(const QString &);
109
	/*! \brief Emitted whenever a new dit or dah get's morsed */
110
	void symbolChanged(const QString &);
111
	/*! \brief Emits how many duration elements are stored \ref morse.
112
	 * Usage:
113
	 * \code
114
	 *    connect(morse, SIGNAL(maxElements(int), progressBar, SLOT(setMaximum(int)) );
115
	 * \endcode
116
	 */
117
	void maxElements(int);
118
	/*! \brief Emits the current position inside \ref morse
119
	 * Usage:
120
	 * \code
121
	 *    connect(morse, SIGNAL(currElement(int), progressBar, SLOT(setValue(int)) );
122
	 * \endcode
123
	 */
124
	void currElement(int);
125
private:
126
	/*! \brief Element index from \ref morse. \sa maxElements(), currElement() */
127
	int playElement;
128
	/*! \brief Index into \ref morse */
129
	int playIdx;
130
	/*! \brief Index into \ref clearText */
131
	int clearIdx;
132
	/*! \brief Timer for \ref play(), used to call \ref slotPlayNext() */
133
	QTimer *playTimer;
134
	/*! \brief Should \ref play() loop?  \sa setLoop() */
135
	float playLoop;
136
	/*! \brief Current replay speed \sa setWpm() */
137
	float playWpm;
138
	/*! \brief Current dit facto, normally 1.0. \sa setDitFactor() */
139
	float ditFactor;
140
	/*! \brief Current dah factor, normally 1.0. \sa setDahFactor() */
141
	float dahFactor;
142
	/*! \brief Current intra-character spacing, normally 1.0. \sa setIntraFactor() */
143
	float intraFactor;
144
	/*! \brief Current character spacing, normally 1.0. \sa setCharFactor() */
145
	float charFactor;
146
	/*! \brief Current word spacing, normally 1.0. \sa setWordFactor() */
147
	float wordFactor;
148
149
	/*! \brief Translation from characters to morse-code */
150
	Morse codes;
151
private slots:
152
	void slotPlayNext();
153
};
154
155
#endif