SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
cwkeyergui.cpp
Go to the documentation of this file.
1 // Copyright (C) 2016 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include <QKeyEvent>
20 #include <QDebug>
21 
22 #include "gui/cwkeyergui.h"
23 #include "ui_cwkeyergui.h"
24 #include "dsp/cwkeyer.h"
25 #include "util/simpleserializer.h"
26 #include "util/messagequeue.h"
27 #include "commandkeyreceiver.h"
28 #include "mainwindow.h"
29 
30 CWKeyerGUI::CWKeyerGUI(QWidget* parent) :
31  QWidget(parent),
32  ui(new Ui::CWKeyerGUI),
33  m_cwKeyer(nullptr),
34  m_doApplySettings(true),
35  m_keyScope(NoKeyScope)
36 {
37  ui->setupUi(this);
40  this->installEventFilter(m_commandKeyReceiver);
41 }
42 
44 {
45  this->releaseKeyboard(); // just in case
46  m_commandKeyReceiver->deleteLater();
47  delete ui;
48 }
49 
51 {
52  m_cwKeyer = cwKeyer;
53  setSettings(cwKeyer->getSettings());
55 }
56 
58 {
61  applySettings(true);
62 }
63 
64 QByteArray CWKeyerGUI::serialize() const
65 {
66  return m_settings.serialize();
67 }
68 
69 bool CWKeyerGUI::deserialize(const QByteArray& data)
70 {
71  if (m_settings.deserialize(data))
72  {
74  applySettings(true);
75  return true;
76  }
77  else
78  {
80  return false;
81  }
82 }
83 
84 // === SLOTS ==================================================================
85 
87 {
88  (void) checked;
89  ui->cwTextEdit->clear();
90  m_settings.m_text = "";
91  applySettings();
92 }
93 
95 {
96  m_settings.m_text = ui->cwTextEdit->text();
97  applySettings();
98 }
99 
101 {
102  ui->cwSpeedText->setText(QString("%1").arg(value));
103  m_settings.m_wpm = value;
104  applySettings();
105 }
106 
108 {
109  //ui->playDots->setEnabled(!checked); // release other source inputs
110  ui->playDashes->setEnabled(!checked);
111  ui->playText->setEnabled(!checked);
112  ui->keyboardKeyer->setEnabled(!checked);
114  applySettings();
115 }
116 
118 {
119  ui->playDots->setEnabled(!checked); // release other source inputs
120  //ui->playDashes->setEnabled(!checked);
121  ui->playText->setEnabled(!checked);
122  ui->keyboardKeyer->setEnabled(!checked);
124  applySettings();
125 }
126 
128 {
129  ui->playDots->setEnabled(!checked); // release other source inputs
130  ui->playDashes->setEnabled(!checked);
131  //ui->playText->setEnabled(!checked);
132  ui->keyboardKeyer->setEnabled(!checked);
133 
134  if (checked) {
135  ui->playStop->setChecked(true);
136  } else {
137  ui->playStop->setChecked(false);
138  }
139 
141  applySettings();
142 }
143 
145 {
146  m_settings.m_loop = checked;
147  applySettings();
148 }
149 
151 {
152  if (checked) {
153  m_cwKeyer->resetText();
154  } else {
155  m_cwKeyer->stopText();
156  }
157 }
158 
160 {
161  m_settings.m_keyboardIambic = !checked;
162  applySettings();
163 }
164 
166 {
167  if (checked && ui->keyDashCapture->isChecked())
168  {
169  ui->keyDotCapture->setChecked(false);
170  ui->keyDashCapture->setChecked(false);
171  return;
172  }
173 
174  if (checked)
175  {
177  setFocus();
178  setFocusPolicy(Qt::StrongFocus);
179  connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
180  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
181  }
182  else
183  {
185  disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
186  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
187  setFocusPolicy(Qt::NoFocus);
188  clearFocus();
189  }
190 }
191 
193 {
194  if (checked && ui->keyDotCapture->isChecked())
195  {
196  ui->keyDotCapture->setChecked(false);
197  ui->keyDashCapture->setChecked(false);
198  return;
199  }
200 
201  if (checked)
202  {
205  setFocus();
206  setFocusPolicy(Qt::StrongFocus);
207  connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
208  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
209  }
210  else
211  {
214  disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
215  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
216  setFocusPolicy(Qt::NoFocus);
217  clearFocus();
218  }
219 }
220 
221 void CWKeyerGUI::commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
222 {
223  (void) release;
224  // qDebug("CWKeyerGUI::commandKeyPressed: key scope: %d", (int) m_keyScope);
225  // qDebug("CWKeyerGUI::commandKeyPressed: key: %x", key);
226  // qDebug("CWKeyerGUI::commandKeyPressed: has modifiers: %x", QFlags<Qt::KeyboardModifier>::Int(keyModifiers));
227 
228  if (m_keyScope == DotKeyScope)
229  {
230  setKeyLabel(ui->keyDotLabel, key, keyModifiers);
231  ui->keyDotCapture->setChecked(false);
232  m_settings.m_dotKey = key;
233  m_settings.m_dotKeyModifiers = keyModifiers;
234  applySettings();
235  }
236  else if (m_keyScope == DashKeyScope)
237  {
238  setKeyLabel(ui->keyDashLabel, key, keyModifiers);
239  ui->keyDashCapture->setChecked(false);
240  m_settings.m_dashKey = key;
241  m_settings.m_dashKeyModifiers = keyModifiers;
242  applySettings();
243  }
244 
246 }
247 
249 {
250  qDebug("CWKeyerGUI::on_keyboardKeyer_toggled: %s", checked ? "true" : "false");
251  ui->playDots->setEnabled(!checked); // block or release other source inputs
252  ui->playDashes->setEnabled(!checked);
253  ui->playText->setEnabled(!checked);
255  applySettings();
256 
257  if (checked) {
258  MainWindow::getInstance()->commandKeysConnect(this, SLOT(keyboardKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
259  } else {
260  MainWindow::getInstance()->commandKeysDisconnect(this, SLOT(keyboardKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
261  }
262 }
263 
264 void CWKeyerGUI::keyboardKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
265 {
266  const CWKeyerSettings& settings = m_cwKeyer->getSettings();
267 
268  if ((key == settings.m_dotKey) && (keyModifiers == settings.m_dotKeyModifiers))
269  {
270  qDebug("CWKeyerGUI::keyboardKeyPressed: dot %s", release ? "released" : "pressed");
271  if (release) {
273  } else {
275  }
276  }
277  else if ((key == settings.m_dashKey) && (keyModifiers == settings.m_dashKeyModifiers))
278  {
279  qDebug("CWKeyerGUI::keyboardKeyPressed: dash %s", release ? "released" : "pressed");
280  if (release) {
282  } else {
284  }
285  }
286 }
287 
288 // === End SLOTS ==============================================================
289 
291 {
293  {
295  m_cwKeyer->getInputMessageQueue()->push(message);
296  }
297 }
298 
300 {
301  blockApplySettings(true);
302 
303  ui->playLoopCW->setChecked(m_settings.m_loop);
304 
306  ui->playDots->setChecked(m_settings.m_mode == CWKeyerSettings::CWDots);
307 
309  ui->playDashes->setChecked(m_settings.m_mode == CWKeyerSettings::CWDashes);
310 
312  ui->playText->setChecked(m_settings.m_mode == CWKeyerSettings::CWText);
313 
315  ui->keyboardKeyer->setChecked(m_settings.m_mode == CWKeyerSettings::CWKeyboard);
316 
317  ui->cwTextEdit->setText(m_settings.m_text);
318  ui->cwSpeed->setValue(m_settings.m_wpm);
319  ui->cwSpeedText->setText(QString("%1").arg(m_settings.m_wpm));
320  ui->keyingStyle->setChecked(!m_settings.m_keyboardIambic);
321 
324 
325  blockApplySettings(false);
326 }
327 
328 void CWKeyerGUI::setKeyLabel(QLabel *label, Qt::Key key, Qt::KeyboardModifiers keyModifiers)
329 {
330  if (key == 0)
331  {
332  label->setText("");
333  }
334  else if (keyModifiers != Qt::NoModifier)
335  {
336  QString altGrStr = keyModifiers & Qt::GroupSwitchModifier ? "Gr " : "";
337  int maskedModifiers = (keyModifiers & 0x3FFFFFFF) + ((keyModifiers & 0x40000000)>>3);
338  label->setText(altGrStr + QKeySequence(maskedModifiers, key).toString());
339  }
340  else
341  {
342  label->setText(QKeySequence(key).toString());
343  }
344 }
345 
347 {
348  m_doApplySettings = !block;
349 }
static MainWindow * getInstance()
Definition: mainwindow.h:73
void commandKeysConnect(QObject *object, const char *slot)
void push(Message *message, bool emitSignal=true)
Push message onto queue.
void applySettings(bool force=false)
Definition: cwkeyergui.cpp:290
void resetText()
Definition: cwkeyer.h:114
QByteArray serialize() const
Definition: cwkeyergui.cpp:64
void on_keyboardKeyer_toggled(bool checked)
Definition: cwkeyergui.cpp:248
void keyboardKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
Definition: cwkeyergui.cpp:264
void setKeyboardDashes()
Definition: cwkeyer.cpp:430
static MsgConfigureCWKeyer * create(const CWKeyerSettings &settings, bool force)
Definition: cwkeyer.h:63
void commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
Definition: cwkeyergui.cpp:221
void on_playDashes_toggled(bool checked)
Definition: cwkeyergui.cpp:117
void setKeyboardDots()
Definition: cwkeyer.cpp:423
void setCWKeyer(CWKeyer *cwKeyer)
Definition: cwkeyergui.cpp:50
void setSettings(const CWKeyerSettings &settings)
Definition: cwkeyergui.h:51
void setKeyLabel(QLabel *label, Qt::Key key, Qt::KeyboardModifiers keyModifiers)
Definition: cwkeyergui.cpp:328
void on_playStop_toggled(bool checked)
Definition: cwkeyergui.cpp:150
void on_cwSpeed_valueChanged(int value)
Definition: cwkeyergui.cpp:100
void on_cwTextEdit_editingFinished()
Definition: cwkeyergui.cpp:94
bool m_doApplySettings
Definition: cwkeyergui.h:66
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
Definition: fixed.h:2401
MessageQueue * getInputMessageQueue()
Get the queue for asynchronous inbound communication.
Definition: cwkeyer.h:104
void on_keyDotCapture_toggled(bool checked)
Definition: cwkeyergui.cpp:165
void setKeyboardSilence()
Definition: cwkeyer.cpp:437
CWKeyer * m_cwKeyer
Definition: cwkeyergui.h:64
bool deserialize(const QByteArray &data)
void on_playLoopCW_toggled(bool checked)
Definition: cwkeyergui.cpp:144
void on_playText_toggled(bool checked)
Definition: cwkeyergui.cpp:127
void on_cwTextClear_clicked(bool checked)
Definition: cwkeyergui.cpp:86
CommandKeyReceiver * m_commandKeyReceiver
Definition: cwkeyergui.h:67
CWKeyerSettings m_settings
Definition: cwkeyergui.h:65
void resetToDefaults()
Definition: cwkeyergui.cpp:57
void setRelease(bool release)
void on_keyDashCapture_toggled(bool checked)
Definition: cwkeyergui.cpp:192
Qt::KeyboardModifiers m_dotKeyModifiers
Qt::KeyboardModifiers m_dashKeyModifiers
void commandKeysDisconnect(QObject *object, const char *slot)
void displaySettings()
Definition: cwkeyergui.cpp:299
KeyScope m_keyScope
Definition: cwkeyergui.h:68
void on_playDots_toggled(bool checked)
Definition: cwkeyergui.cpp:107
void blockApplySettings(bool block)
Definition: cwkeyergui.cpp:346
Ui::CWKeyerGUI * ui
Definition: cwkeyergui.h:62
const CWKeyerSettings & getSettings() const
Definition: cwkeyer.h:107
void stopText()
Definition: cwkeyer.h:115
void on_keyingStyle_toggled(bool checked)
Definition: cwkeyergui.cpp:159
QByteArray serialize() const
bool deserialize(const QByteArray &data)
Definition: cwkeyergui.cpp:69
CWKeyerGUI(QWidget *parent=nullptr)
Definition: cwkeyergui.cpp:30