-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout.cpp
482 lines (407 loc) · 13 KB
/
layout.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
/************************************************************************
* THIS CODE IS RELEASED UNDER THE
* GNU GENERAL PUBLIC LICENSE
* Version 2, June 1991
*
* See LICENSE for more information
*
************************************************************************/
#include <time.h>
#include <qpushbutton.h>
#include <qlabel.h>
//#include <qgrid.h>
#include <QGridLayout>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qcheckbox.h>
#include <qfont.h>
#include <qfiledialog.h>
#include <qfile.h>
#include <qtextstream.h>
#include "layout.h"
#include "cwsound.h"
#include "cwtest.h"
#include "lcdrange.h"
#include <QTextEdit>
#include <QGroupBox>
#include <iostream>
#define CODESPEED_MIN 15
#define CODESPEED_MAX 40
#define CODESPEED_DEFAULT 20
#define SPACING_MIN 5
#define SPACING_MAX 40
#define SPACING_DEFAULT 20
#define CHARACTER_GRID_ROWS 8
const char * MyWidget::m_cwchars = "KMRSUAPTLOWI.NJEFOY,VG5/Q9ZH38B?427C1D6X";
MyWidget::MyWidget( QWidget *parent )/*{{{*/
: QWidget( parent )
{
srand(time(NULL));
m_cw = new CWSoundMachine(20, 1, 600);
m_lastchar = '&';
//QGridLayout *grid = new QGridLayout(this, 2, 2, 10);
QGridLayout *grid = new QGridLayout(this);
//grid->setSpacing(10);
//m_wpm = new LCDRange(CODESPEED_MIN, 40, this);
LCDRange *m_wpm = new LCDRange(this);
m_wpm->setRange(CODESPEED_MIN, CODESPEED_MAX);
//m_wpm->setValue(20);
m_wpm->setValue(CODESPEED_DEFAULT);
NewSpeed(CODESPEED_DEFAULT);
connect(m_wpm, SIGNAL(valueChanged(int)),
SLOT(NewSpeed(int)) );
//QLabel *label = new QLabel ( m_wpm, "CW Character Speed", this);
QLabel *label = new QLabel ( "CW Character Speed", this);
//label->setBuddy(m_wpm);
grid->addWidget(label, 0, 0);
grid->addWidget(m_wpm, 1, 0, Qt::Alignment(Qt::AlignLeft) );
//m_spacing = new LCDRange(5, 40, this);
m_spacing = new LCDRange(this);
m_spacing->setRange(SPACING_MIN, SPACING_MAX);
//m_spacing->setValue(20);
m_spacing->setValue(SPACING_DEFAULT);
NewSpacing(SPACING_DEFAULT);
connect(m_spacing, SIGNAL(valueChanged(int)),
SLOT(NewSpacing(int)) );
grid->addWidget(m_spacing, 1, 1);
//label = new QLabel (m_wpm, "Character Spacing", this);
label = new QLabel ("Character Spacing");
grid->addWidget(label, 0, 1);
//m_cwtext = new QMultiLineEdit(this);
m_cwtext = new QTextEdit(this);
m_cwtext->setFont(QFont( "Courier", 18, QFont::Bold ));
m_cwtext->setReadOnly(true);
//m_cwtext->setWordWrap(QMultiLineEdit::WidgetWidth);
m_cwtext->setWordWrapMode(QTextOption::WordWrap);
//grid->addMultiCellWidget(m_cwtext, 2, 2, 0, 1);
grid->addWidget(m_cwtext, 2, 0, 1, 2);
QPushButton *start = new QPushButton( "Start CW", this);
QPushButton *stop = new QPushButton( "Stop CW", this);
connect( start, SIGNAL(clicked()),
SLOT(StartCW()) );
connect( stop, SIGNAL(clicked()),
SLOT(EndCW()) );
grid->addWidget(start, 3, 0);
grid->addWidget(stop, 3, 1);
const char *ptr = m_cwchars;
m_letters = new QCheckBox*[QString(m_cwchars).length()];
//m_lettergroup = new QGroupBox(5, Horizontal, "Chars", this) ;
m_lettergroup = new QGroupBox("Chars", this) ;
m_lettergroup->setAlignment( Qt::Alignment(Qt::AlignHCenter) );
m_letterlayout = new QGridLayout;
//int chars_in_row = QString(ptr).length() / CHARACTER_GRID_ROWS;
for(int i = 0 ; ptr[i] ; i++)
{
//QCheckBox *cb = new QCheckBox(QString(QChar(ptr[i])), m_lettergroup);
QCheckBox *cb = new QCheckBox(QString(QChar(ptr[i])), m_lettergroup);
//m_lettergroup->addButton( cb );
cb->setChecked(CWTest::CharInUse(ptr[i]));
m_letters[i] = cb;
// guessing at coordinates.
// QButtonGroup cannot be added to QGridLayout
// http://www.qtcentre.org/threads/42754-Can-t-add-QButtonGroup-to-QGridLayout
//grid->addWidget(cb , 4, i, Qt::Alignment(Qt::AlignLeft));
//int row = 0;
//if( i != 0) // take care of divide-by-zero
// row = i / chars_in_row;
//int column = i - row*chars_in_row;
int row, column;
ConvertToPosition( i, &row, &column); // modifies row and column
m_letterlayout->addWidget(cb, row, column);
// m_lettergroup->insert(cb);
}
m_lettergroup->setLayout(m_letterlayout);
//grid->addWidget(m_lettergroup, 4, 0);
//grid->addWidget(m_letterlayout, 4, 0);
//grid->addLayout(m_letterlayout, 4, 0);
grid->addWidget(m_lettergroup, 4, 0);
//QButtonGroup *bg = new QButtonGroup(1, Horizontal, "Actions", this) ;
//QGroupBox *bg = new QGroupBox(1, Horizontal, "Actions", this) ;
QGroupBox *bg = new QGroupBox("Actions", this) ;
bg->setAlignment( Qt::Alignment(Qt::AlignHCenter) );
//QPushButton *clearall = new QPushButton( "Clear All", bg);
QPushButton *clearall = new QPushButton( "Clear All", bg);
connect( clearall, SIGNAL(clicked()),
SLOT(ClearAll()) );
//QPushButton *selectall = new QPushButton( "Select All", bg);
QPushButton *selectall = new QPushButton( "Select All");
connect( selectall, SIGNAL(clicked()),
SLOT(SelectAll()) );
//QPushButton *loadfile = new QPushButton( "Load File...", bg);
QPushButton *loadfile = new QPushButton( "Load File...");
connect( loadfile, SIGNAL(clicked()),
SLOT(ReadFile()) );
//QPushButton *about = new QPushButton( "About...", bg);
QPushButton *about = new QPushButton( "About...");
connect( about, SIGNAL(clicked()),
SLOT(About()) );
// add the QPushButtons to the QButtonGroup
QVBoxLayout *vbuttons = new QVBoxLayout;
//vbuttons->setSpacing(0);
vbuttons->addWidget( clearall );
vbuttons->addWidget( selectall );
vbuttons->addWidget( loadfile );
vbuttons->addWidget( about );
bg->setLayout( vbuttons );
grid->addWidget(bg, 4, 1);
// guessing at coordinates
/*grid->addWidget( clearall , 4, 1);
grid->addWidget( selectall, 4, 2);
grid->addWidget( loadfile , 4, 3);
grid->addWidget( about , 4, 4);*/
//m_timer = new QTimer(this, "mycwtimer");
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()),
SLOT(playNextChar()) );
//setCaption("CW Trainer");
setWindowTitle("CW Trainer");
setLayout( grid );
}/*}}}*/
//this function is ugly.../*{{{*/
//too many special cases and flags
void MyWidget::playNextChar()
{
//file mode or randchar mode?
bool dospace = false;
bool play_char = true;
char next_char;
char prosign[2];
prosign[0] = prosign[1] = 0;
if(m_charpipe.isEmpty())
{
int j=(int) ((float)m_numInTest*rand()/(RAND_MAX+1.0));
if(m_numingroup > 6)
dospace = true;
else if(m_numingroup < 2)
dospace = false;
else
dospace = (2 == (int) ((float)4*rand()/(RAND_MAX+1.0))) && m_lastchar != ' ';
m_numingroup++;
next_char = m_charsInTest[j];
}
else
{
QChar ch = m_charpipe[m_numingroup];
m_numingroup++;
if(ch == '\0')
{
EndCW();
return;
}
//next_char = (char)ch.upper();
next_char = ch.toUpper().toAscii();
//couple of translations?
if(next_char == '-' || next_char == ':' || next_char == '\\')
next_char = '/';
if(next_char == '^') //prosign
{
ch = m_charpipe[m_numingroup];
m_numingroup++;
if(ch == '\0')
{
EndCW();
return;
}
//prosign[0] = (char)ch.upper();
prosign[0] = ch.toUpper().toAscii();
ch = m_charpipe[m_numingroup];
m_numingroup++;
if(ch == '\0')
{
EndCW();
return;
}
//prosign[1] = (char)ch.upper();
prosign[1] = ch.toUpper().toAscii();
}
if(next_char == ' ')
dospace = true;
if(CWTest::GetCW(next_char) == " ")
{
play_char = false;
}
}
if(dospace == false)
{
m_lastchar = next_char;
//m_cwtext->setText(m_cwtext->text() + QString(QChar(next_char)));
m_cwtext->setText(m_cwtext->toPlainText() + QString(QChar(next_char)));
if(play_char)
m_cw->PlayCWNote(CWTest::GetCW(next_char).c_str());
else if(prosign[1])
{
m_cw->PlayCWNote(CWTest::GetCW(prosign[0]).c_str());
//m_cwtext->setText(m_cwtext->text() + QString(QChar(prosign[0])));
m_cwtext->setText(m_cwtext->toPlainText() + QString(QChar(prosign[0])));
m_cw->PlayCWNote(CWTest::GetCW(prosign[1]).c_str());
//m_cwtext->setText(m_cwtext->text() + QString(QChar(prosign[1])));
m_cwtext->setText(m_cwtext->toPlainText() + QString(QChar(prosign[1])));
}
//m_timer->changeInterval(m_charspace);
m_timer->setInterval(m_charspace);
}
else
{
if(m_charpipe.isEmpty())
m_numingroup = 0;
m_lastchar = ' ';
m_cwtext->setText(m_cwtext->toPlainText() + QString(" | "));
//m_timer->changeInterval(m_wordspace);
m_timer->setInterval(m_wordspace);
}
}/*}}}*/
void MyWidget::StartCW()/*{{{*/
{
m_timer->stop();
//gather the checked boxes...
int num_chars = QString(m_cwchars).length();
memset(m_charsInTest, 0, num_chars+1);
m_numInTest = 0;
for(int i = 0 ; i < num_chars; i++)
{
//QCheckBox *b = (QCheckBox*)m_lettergroup->find(i);
int row, column;
ConvertToPosition( i , &row, &column); // modifies row and column
//QCheckBox *b = (QCheckBox*)m_letterlayout->itemAtPosition(row, column);
QCheckBox *b = m_letters[i];
if( b == NULL )
{
std::cerr << "Checkbox #" << i << " not found in MyWidget::SelectAll()\n";
return;
}
if( b->isChecked() )
{
m_charsInTest[m_numInTest] = m_cwchars[i];
m_numInTest++;
}
}
/* for(int i = 0 ; i < 40 ; i++)
{
QCheckBox *b = (QCheckBox*)m_lettergroup->find(i);
if(b->isChecked())
{
m_charsInTest[m_numInTest] = m_cwchars[i];
m_numInTest++;
}
}*/
if(m_numInTest)
{
m_numingroup = 0;
// redundant, plus m_wpm->value() segfaults for some reason
/*int speed = m_wpm->value();
int space = m_spacing->value();
NewSpeed(speed);
NewSpacing(space);*/
m_cwtext->setText(QString(""));
m_timer->start(500);
}
}/*}}}*/
void MyWidget::NewSpeed(int speed)
{
m_cw->AdjustSpeed(speed);
// hack_value = speed;
}
void MyWidget::NewSpacing(int space)
{
m_charspace = (int)((((60000.0 / (float)space) / 7.0) / 3.0));
m_wordspace = m_charspace * 2;
}
void MyWidget::EndCW()
{
m_timer->stop();
m_charpipe = "";
}
void MyWidget::About()
{
QMessageBox::information( this
,"About CW Trainer"
,"CW Trainer 0.1.0"
"\nKoch and Farnsworth adaptable Morse Code trainer."
"\n\nBy John Wagner, N1QO and Jacob Anawalt, KD7YKO"
"\nhttp://sourceforge.net/projects/cwtrainer"
"\n\nQt4 and ALSA functionality by Aaron Decker"
"\nhttp://github.com/deckeraa/CW-Trainer"
"\nGPL - Uses code from GWFON's Koch CW Trainer"
"\nSee README and LICENSE for more information");
}
void MyWidget::ClearAll()
{
int num_chars = QString(m_cwchars).length();
for(int i = 0 ; i < num_chars; i++)
{
//QCheckBox *b = (QCheckBox*)m_lettergroup->find(i);
QCheckBox *b = m_letters[i];
if( b == NULL )
{
std::cerr << "Checkbox #" << i << " not found in MyWidget::SelectAll()\n";
return;
}
b->setChecked(false);
}
}
void MyWidget::SelectAll()
{
int num_chars = QString(m_cwchars).length();
for(int i = 0 ; i < num_chars; i++)
{
//QCheckBox *b = (QCheckBox*)m_lettergroup->find(i);
int row, column;
ConvertToPosition( i , &row, &column); // modifies row and column
//QCheckBox *b = (QCheckBox*)m_letterlayout->itemAtPosition(row, column);
QCheckBox *b = m_letters[i];
if( b == NULL )
{
std::cerr << "Checkbox #" << i << " not found in MyWidget::SelectAll()\n";
return;
}
b->setChecked(true);
}
}
void MyWidget::ReadFile()/*{{{*/
{
//QString s( QFileDialog::getOpenFileName( QString::null, "*", this ) );
QString s( QFileDialog::getOpenFileName( this, "Open File", QString::null, "*" ) );
if ( s.isEmpty() )
return;
QFile f(s);
if ( f.open(QIODevice::ReadOnly) )
{ // file opened successfully
m_charpipe = "";
QTextStream t( &f ); // use a text stream
//while ( !t.eof() ) // until end of file...
while ( !t.atEnd() ) // until end of file...
{
QString s = t.readLine(); // line of text excluding '\n'
s += " ";
m_charpipe += s;
//printf( "%3d: %s\n", n++, (const char *)s );
}
f.close();
StartCW();
}
else
QMessageBox::information( this, "CW Trainer",
"An error occurred opening the file");
}/*}}}*/
/**
* Takes a single integer and converts it to acoordinate on the grid of characters.
* The coordinate is passed back using the two reference paramaters, row and column.
*/
void MyWidget::ConvertToPosition( int i, int *row, int *column)
{
const char *ptr = m_cwchars;
int chars_in_row = QString(ptr).length() / CHARACTER_GRID_ROWS;
*row = 0;
if( i != 0) // take care of divide-by-zero
*row = i / chars_in_row;
*column = i - (*row) * chars_in_row;
}
/**
* Takes a row and a column and converts it to a single index in the grid of characters.
*/
int MyWidget::ConvertFromPosition( int row, int column )
{
const char *ptr = m_cwchars;
int chars_in_row = QString(ptr).length() / CHARACTER_GRID_ROWS;
return column + row * chars_in_row;
}