-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwsender.cpp
493 lines (426 loc) · 13.6 KB
/
cwsender.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "cwsender.h"
#include "ui_cwsender.h"
#include "logcategories.h"
cwSender::cwSender(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::cwSender)
{
ui->setupUi(this);
this->setWindowTitle("CW Sender");
ui->textToSendEdit->setFocus();
QFont f = QFont("Monospace");
f.setStyleHint(QFont::TypeWriter);
ui->textToSendEdit->setFont(f);
ui->transcriptText->setFont(f);
ui->textToSendEdit->setFocus();
ui->statusbar->setToolTipDuration(3000);
this->setToolTipDuration(3000);
connect(ui->textToSendEdit->lineEdit(), &QLineEdit::textEdited, this, &cwSender::textChanged);
}
cwSender::~cwSender()
{
qDebug(logCW()) << "Running CW Sender destructor.";
if (toneThread != Q_NULLPTR) {
toneThread->quit();
toneThread->wait();
toneThread = Q_NULLPTR;
tone = Q_NULLPTR;
/* Finally disconnect all connections */
for (auto conn: connections)
{
disconnect(conn);
}
connections.clear();
}
delete ui;
}
void cwSender::showEvent(QShowEvent *event)
{
(void)event;
emit getCWSettings();
QMainWindow::showEvent(event);
}
void cwSender::handleKeySpeed(unsigned char wpm)
{
if (wpm != ui->wpmSpin->value() && (wpm >= ui->wpmSpin->minimum()) && (wpm <= ui->wpmSpin->maximum()))
{
ui->wpmSpin->blockSignals(true);
QMetaObject::invokeMethod(ui->wpmSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, wpm));
ui->wpmSpin->blockSignals(false);
#if (QT_VERSION >= QT_VERSION_CHECK(5,10,0))
QMetaObject::invokeMethod(tone, [=]() {
tone->setSpeed(wpm);
}, Qt::QueuedConnection);
#else
emit setKeySpeed(ratio);
#endif
}
}
void cwSender::handleDashRatio(unsigned char ratio)
{
double calc = double(ratio/10);
if (calc != ui->dashSpin->value() && (calc >= ui->dashSpin->minimum()) && (ratio <= ui->dashSpin->maximum()))
{
ui->dashSpin->blockSignals(true);
QMetaObject::invokeMethod(ui->dashSpin, "setValue", Qt::QueuedConnection, Q_ARG(double, calc));
ui->dashSpin->blockSignals(false);
#if (QT_VERSION >= QT_VERSION_CHECK(5,10,0))
QMetaObject::invokeMethod(tone, [=]() {
tone->setRatio(ratio);
}, Qt::QueuedConnection);
#else
emit setDashRatio(ratio);
#endif
}
}
void cwSender::handlePitch(unsigned char pitch) {
int cwPitch = round((((600.0 / 255.0) * pitch) + 300) / 5.0) * 5.0;
if (cwPitch != ui->pitchSpin->value() && cwPitch >= ui->pitchSpin->minimum() && cwPitch <= ui->pitchSpin->maximum())
{
ui->pitchSpin->blockSignals(true);
QMetaObject::invokeMethod(ui->pitchSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, cwPitch));
ui->pitchSpin->blockSignals(false);
#if (QT_VERSION >= QT_VERSION_CHECK(5,10,0))
QMetaObject::invokeMethod(tone, [=]() {
tone->setFrequency(pitch);
}, Qt::QueuedConnection);
#else
emit setPitch(tone);
#endif
}
}
void cwSender::handleBreakInMode(unsigned char b)
{
if(b < 3)
{
ui->breakinCombo->blockSignals(true);
ui->breakinCombo->setCurrentIndex(b);
ui->breakinCombo->blockSignals(false);
}
}
void cwSender::handleCurrentModeUpdate(mode_kind mode)
{
this->currentMode = mode;
if( (currentMode==modeCW) || (currentMode==modeCW_R) )
{
} else {
ui->statusbar->showMessage("Note: Mode needs to be set to CW or CW-R to send CW.", 3000);
}
}
void cwSender::textChanged(QString text)
{
if (ui->sendImmediateChk->isChecked() && text.size() && text.back() == ' ')
{
int toSend = text.mid(0, 30).size();
if (toSend > 0) {
ui->textToSendEdit->clearEditText();
ui->transcriptText->moveCursor(QTextCursor::End);
ui->transcriptText->insertPlainText(text.mid(0, 30).toUpper());
ui->transcriptText->moveCursor(QTextCursor::End);
emit sendCW(text.mid(0, 30));
}
if( (currentMode != modeCW) && (currentMode != modeCW_R) )
{
ui->statusbar->showMessage("Note: Mode needs to be set to CW or CW-R to send CW.", 3000);
}
}
}
void cwSender::on_sendBtn_clicked()
{
if( (ui->textToSendEdit->currentText().length() > 0) &&
(ui->textToSendEdit->currentText().length() <= 30) )
{
QString text = ui->textToSendEdit->currentText();
ui->transcriptText->moveCursor(QTextCursor::End);
ui->transcriptText->insertPlainText(ui->textToSendEdit->currentText().toUpper()+"\n");
ui->transcriptText->moveCursor(QTextCursor::End);
if (!ui->sendImmediateChk->isChecked())
{
ui->textToSendEdit->addItem(ui->textToSendEdit->currentText());
if (ui->textToSendEdit->count() > 5) {
ui->textToSendEdit->removeItem(0);
}
ui->textToSendEdit->setCurrentIndex(-1);
} else {
ui->textToSendEdit->clearEditText();
ui->textToSendEdit->clear();
}
ui->textToSendEdit->setFocus();
ui->statusbar->showMessage("Sending CW", 3000);
emit sendCW(text);
}
if( (currentMode != modeCW) && (currentMode != modeCW_R) )
{
ui->statusbar->showMessage("Note: Mode needs to be set to CW or CW-R to send CW.", 3000);
}
}
void cwSender::on_stopBtn_clicked()
{
emit stopCW();
ui->textToSendEdit->setFocus();
ui->statusbar->showMessage("Stopping CW transmission.", 3000);
}
//void cwSender::on_textToSendEdit_returnPressed()
//{
// on_sendBtn_clicked();
//}
void cwSender::on_breakinCombo_activated(int brkmode)
{
// 0 = off, 1 = semi, 2 = full
emit setBreakInMode((unsigned char)brkmode);
ui->textToSendEdit->setFocus();
}
void cwSender::on_wpmSpin_valueChanged(int wpm)
{
emit setKeySpeed((unsigned char)wpm);
}
void cwSender::on_dashSpin_valueChanged(double ratio)
{
emit setDashRatio((unsigned char)double(ratio * 10));
}
void cwSender::on_pitchSpin_valueChanged(int arg1)
{
// quint16 cwPitch = round((((600.0 / 255.0) * pitch) + 300) / 5.0) * 5.0;
unsigned char pitch = 0;
pitch = ceil((arg1 - 300) * (255.0 / 600.0));
emit setPitch(pitch);
}
void cwSender::on_macro1btn_clicked()
{
processMacroButton(1, ui->macro1btn);
}
void cwSender::on_macro2btn_clicked()
{
processMacroButton(2, ui->macro2btn);
}
void cwSender::on_macro3btn_clicked()
{
processMacroButton(3, ui->macro3btn);
}
void cwSender::on_macro4btn_clicked()
{
processMacroButton(4, ui->macro4btn);
}
void cwSender::on_macro5btn_clicked()
{
processMacroButton(5, ui->macro5btn);
}
void cwSender::on_macro6btn_clicked()
{
processMacroButton(6, ui->macro6btn);
}
void cwSender::on_macro7btn_clicked()
{
processMacroButton(7, ui->macro7btn);
}
void cwSender::on_macro8btn_clicked()
{
processMacroButton(8, ui->macro8btn);
}
void cwSender::on_macro9btn_clicked()
{
processMacroButton(9, ui->macro9btn);
}
void cwSender::on_macro10btn_clicked()
{
processMacroButton(10, ui->macro10btn);
}
void cwSender::on_sidetoneEnableChk_clicked(bool clicked)
{
ui->sidetoneLevelSlider->setEnabled(clicked);
if (clicked && toneThread == Q_NULLPTR)
{
toneThread = new QThread(this);
toneThread->setObjectName("sidetone()");
tone = new cwSidetone(sidetoneLevel, ui->wpmSpin->value(),ui->pitchSpin->value(),ui->dashSpin->value(),this);
tone->moveToThread(toneThread);
toneThread->start();
connections.append(connect(toneThread, &QThread::finished,
[=]() { tone->deleteLater(); }));
connections.append(connect(this, &cwSender::sendCW,
[=](const QString& text) { tone->send(text); ui->sidetoneEnableChk->setEnabled(false); }));
connections.append(connect(this, &cwSender::setKeySpeed,
[=](const unsigned char& wpm) { tone->setSpeed(wpm); }));
connections.append(connect(this, &cwSender::setDashRatio,
[=](const unsigned char& ratio) { tone->setRatio(ratio); }));
connections.append(connect(this, &cwSender::setPitch,
[=](const unsigned char& pitch) { tone->setFrequency(pitch); }));
connections.append(connect(this, &cwSender::setLevel,
[=](const unsigned char& level) { tone->setLevel(level); }));
connections.append(connect(this, &cwSender::stopCW,
[=]() { tone->stopSending(); }));
connections.append(connect(tone, &cwSidetone::finished,
[=]() { ui->sidetoneEnableChk->setEnabled(true); }));
} else if (!clicked && toneThread != Q_NULLPTR) {
/* disconnect all connections */
toneThread->quit();
toneThread->wait();
toneThread = Q_NULLPTR;
tone = Q_NULLPTR;
for (auto conn: connections)
{
disconnect(conn);
}
connections.clear();
}
}
void cwSender::on_sidetoneLevelSlider_valueChanged(int val)
{
sidetoneLevel = val;
emit setLevel(val);
}
void cwSender::processMacroButton(int buttonNumber, QPushButton *btn)
{
if(ui->macroEditChk->isChecked())
{
editMacroButton(buttonNumber, btn);
} else {
runMacroButton(buttonNumber);
}
ui->textToSendEdit->setFocus();
}
void cwSender::runMacroButton(int buttonNumber)
{
if(macroText[buttonNumber].isEmpty())
return;
QString outText;
if(macroText[buttonNumber].contains("%1"))
{
outText = macroText[buttonNumber].arg(sequenceNumber, 3, 10, QChar('0'));
sequenceNumber++;
ui->sequenceSpin->blockSignals(true);
QMetaObject::invokeMethod(ui->sequenceSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, sequenceNumber));
ui->sequenceSpin->blockSignals(false);
} else {
outText = macroText[buttonNumber];
}
if (ui->cutNumbersChk->isChecked())
{
outText.replace("0", "T");
outText.replace("9", "N");
}
ui->transcriptText->moveCursor(QTextCursor::End);
ui->transcriptText->insertPlainText(outText.toUpper()+"\n");
ui->transcriptText->moveCursor(QTextCursor::End);
for (int i = 0; i < outText.size(); i = i + 30) {
emit sendCW(outText.mid(i,30));
}
ui->textToSendEdit->setFocus();
if( (currentMode==modeCW) || (currentMode==modeCW_R) )
{
ui->statusbar->showMessage(QString("Sending CW macro %1").arg(buttonNumber));
} else {
ui->statusbar->showMessage("Note: Mode needs to be set to CW or CW-R to send CW.");
}
}
void cwSender::editMacroButton(int buttonNumber, QPushButton* btn)
{
bool ok;
QString promptFirst = QString("Please enter the text for macro %1,\n"
"up to 60 characters.\n").arg(buttonNumber);
QString promptSecond = QString("You may use \"%1\" to insert a sequence number.");
QString prompt = promptFirst+promptSecond;
QString newMacroText = QInputDialog::getText(this, "Macro Edit",
prompt,
QLineEdit::Normal, macroText[buttonNumber], &ok).toUpper();
if(!ok)
return;
if (newMacroText.length() > 60)
{
QMessageBox msgBox;
msgBox.setText(QString("The text entered was too long \n"
"(max length is 60 characters).\n"
"Your input was %1 characters.").arg(newMacroText.length()));
msgBox.exec();
this->raise();
return;
}
macroText[buttonNumber] = newMacroText;
setMacroButtonText(newMacroText, btn);
}
void cwSender::setMacroButtonText(QString btnText, QPushButton *btn)
{
if(btn==Q_NULLPTR)
return;
if(btnText.isEmpty())
return;
QString shortBtnName;
if(btnText.length() <= 8)
{
shortBtnName = btnText;
} else {
shortBtnName = btnText.left(7);
shortBtnName.append("…");
}
btn->setText(shortBtnName);
}
void cwSender::on_sequenceSpin_valueChanged(int newSeq)
{
sequenceNumber = newSeq;
ui->textToSendEdit->setFocus();
}
bool cwSender::getCutNumbers()
{
return ui->cutNumbersChk->isChecked();
}
bool cwSender::getSendImmediate()
{
return ui->sendImmediateChk->isChecked();
}
bool cwSender::getSidetoneEnable()
{
return ui->sidetoneEnableChk->isChecked();
}
int cwSender::getSidetoneLevel()
{
return ui->sidetoneLevelSlider->value();
}
void cwSender::setCutNumbers(bool val)
{
ui->cutNumbersChk->setChecked(val);
}
void cwSender::setSendImmediate(bool val)
{
ui->sendImmediateChk->setChecked(val);
}
void cwSender::setSidetoneEnable(bool val)
{
ui->sidetoneEnableChk->setChecked(val);
on_sidetoneEnableChk_clicked(val);
}
void cwSender::setSidetoneLevel(int val)
{
QMetaObject::invokeMethod(ui->sidetoneLevelSlider, "setValue", Qt::QueuedConnection, Q_ARG(int, val));
}
QStringList cwSender::getMacroText()
{
// This is for preference saving:
QStringList mlist;
for(int i=1; i < 11; i++)
{
mlist << macroText[i];
}
return mlist;
}
void cwSender::setMacroText(QStringList macros)
{
if(macros.length() != 10)
{
qCritical(logCW()) << "Macro list must be exactly 10. Rejecting macro text load.";
return;
}
for(int i=0; i < 10; i++)
{
macroText[i+1] = macros.at(i);
}
setMacroButtonText(macroText[1], ui->macro1btn);
setMacroButtonText(macroText[2], ui->macro2btn);
setMacroButtonText(macroText[3], ui->macro3btn);
setMacroButtonText(macroText[4], ui->macro4btn);
setMacroButtonText(macroText[5], ui->macro5btn);
setMacroButtonText(macroText[6], ui->macro6btn);
setMacroButtonText(macroText[7], ui->macro7btn);
setMacroButtonText(macroText[8], ui->macro8btn);
setMacroButtonText(macroText[9], ui->macro9btn);
setMacroButtonText(macroText[10], ui->macro10btn);
}