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.
editcommanddialog.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include "editcommanddialog.h"
19 #include "ui_editcommanddialog.h"
20 #include "commands/command.h"
21 #include "commandkeyreceiver.h"
22 
23 #include <QFileInfo>
24 #include <QFileDialog>
25 #include <algorithm>
26 
27 EditCommandDialog::EditCommandDialog(const QStringList& groups, const QString& group, QWidget* parent) :
28  QDialog(parent),
29  ui(new Ui::EditCommandDialog),
30  m_key(static_cast<Qt::Key>(0))
31 {
32  ui->setupUi(this);
33  ui->group->addItems(groups);
34  ui->group->lineEdit()->setText(group);
36  setKeyLabel();
37 
39  this->installEventFilter(m_commandKeyReceiver);
40 }
41 
43 {
44  m_commandKeyReceiver->deleteLater();
45  delete ui;
46 }
47 
49 {
50  return ui->group->lineEdit()->text();
51 }
52 
54 {
55  return ui->description->text();
56 }
57 
58 void EditCommandDialog::setGroup(const QString& group)
59 {
60  ui->group->lineEdit()->setText(group);
61 }
62 
63 void EditCommandDialog::setDescription(const QString& description)
64 {
65  ui->description->setText(description);
66 }
67 
69 {
70  return ui->command->text();
71 }
72 
73 void EditCommandDialog::setCommand(const QString& command)
74 {
75  ui->command->setText(command);
76 }
77 
79 {
80  return ui->args->text();
81 }
82 
83 void EditCommandDialog::setArguments(const QString& arguments)
84 {
85  ui->args->setText(arguments);
86 }
87 
89 {
90  return m_key;
91 }
92 
93 Qt::KeyboardModifiers EditCommandDialog::getKeyModifiers() const
94 {
95  return m_keyModifiers;
96 }
97 
98 void EditCommandDialog::setKey(Qt::Key key, Qt::KeyboardModifiers modifiers)
99 {
100  m_key = key;
101  m_keyModifiers = modifiers;
102  setKeyAssociate();
103  setKeyLabel();
104 }
105 
107 {
108  return ui->keyAssociate->isChecked();
109 }
110 
112 {
113  ui->keyAssociate->setChecked(release);
114 }
115 
117 {
118  return ui->keyRelease->isChecked();
119 }
120 
122 {
123  ui->keyRelease->setChecked(release);
124 }
125 
127 {
128  (void) checked;
129  QString commandFileName = ui->command->text();
130  QFileInfo commandFileInfo(commandFileName);
131  QString commandFolderName = commandFileInfo.baseName();
132  QFileInfo commandDirInfo(commandFolderName);
133  QString dirStr;
134 
135  if (commandFileInfo.exists()) {
136  dirStr = commandFileName;
137  } else if (commandDirInfo.exists()) {
138  dirStr = commandFolderName;
139  } else {
140  dirStr = ".";
141  }
142 
143  QString fileName = QFileDialog::getOpenFileName(
144  this,
145  tr("Select command"),
146  dirStr,
147  tr("All (*);;Python (*.py);;Shell (*.sh *.bat);;Binary (*.bin *.exe)"), 0, QFileDialog::DontUseNativeDialog);
148 
149  if (fileName != "") {
150  ui->command->setText(fileName);
151  }
152 }
153 
155 {
156  if (checked)
157  {
158  setFocus();
159  setFocusPolicy(Qt::StrongFocus);
160  connect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
161  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
162  }
163  else
164  {
165  disconnect(m_commandKeyReceiver, SIGNAL(capturedKey(Qt::Key, Qt::KeyboardModifiers, bool)),
166  this, SLOT(commandKeyPressed(Qt::Key, Qt::KeyboardModifiers, bool)));
167  setFocusPolicy(Qt::NoFocus);
168  clearFocus();
169  }
170 }
171 
173 {
174  command.setGroup(ui->group->currentText());
175  command.setDescription(ui->description->text());
176  command.setCommand(ui->command->text());
177  command.setArgString(ui->args->text());
178  command.setAssociateKey(ui->keyAssociate->isChecked());
179  command.setKey(m_key);
181  command.setRelease(ui->keyRelease->isChecked());
182 }
183 
185 {
186  ui->group->lineEdit()->setText(command.getGroup());
187  ui->description->setText(command.getDescription());
188  ui->command->setText(command.getCommand());
189  ui->args->setText(command.getArgString());
190  ui->keyAssociate->setChecked(command.getAssociateKey());
191  m_key = command.getKey();
192  m_keyModifiers = command.getKeyModifiers();
193  setKeyAssociate();
194  setKeyLabel();
195  ui->keyRelease->setChecked(command.getRelease());
196 }
197 
199 {
200  if (m_key == 0)
201  {
202  ui->keyLabel->setText("");
203  }
204  else if (m_keyModifiers != Qt::NoModifier)
205  {
206  QString altGrStr = m_keyModifiers & Qt::GroupSwitchModifier ? "Gr " : "";
207  int maskedModifiers = (m_keyModifiers & 0x3FFFFFFF) + ((m_keyModifiers & 0x40000000)>>3);
208  ui->keyLabel->setText(altGrStr + QKeySequence(maskedModifiers, m_key).toString());
209  }
210  else
211  {
212  ui->keyLabel->setText(QKeySequence(m_key).toString());
213  }
214 }
215 
217 {
218  if (m_key == 0)
219  {
220  ui->keyAssociate->setChecked(false);
221  ui->keyAssociate->setEnabled(false);
222  }
223  else
224  {
225  ui->keyAssociate->setEnabled(true);
226  }
227 }
228 
229 void EditCommandDialog::commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
230 {
231  (void) release;
232 // qDebug("EditCommandDialog::commandKeyPressed: key: %x", m_key);
233 // qDebug("EditCommandDialog::commandKeyPressed: has modifiers: %x", QFlags<Qt::KeyboardModifier>::Int(keyModifiers));
234  m_key = key;
235  m_keyModifiers = keyModifiers;
236  setKeyAssociate();
237  setKeyLabel();
238  ui->keyCapture->setChecked(false);
239 }
void setKey(Qt::Key key, Qt::KeyboardModifiers modifiers)
void setRelease(bool release)
Definition: command.h:57
Qt::KeyboardModifiers getKeyModifiers() const
Definition: command.h:54
bool getAssociateKey() const
void setGroup(const QString &group)
CommandKeyReceiver * m_commandKeyReceiver
void setDescription(const QString &description)
const QString & getDescription() const
Definition: command.h:50
Qt::Key getKey() const
Definition: command.h:52
void toCommand(Command &command) const
void setCommand(const QString &command)
void setRelease(bool release)
void setArgString(const QString &argString)
Definition: command.h:45
Qt::KeyboardModifiers getKeyModifiers() const
QString getArguments() const
void on_showFileDialog_clicked(bool checked)
void commandKeyPressed(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
QString getCommand() const
void setKey(Qt::Key key)
Definition: command.h:51
void setArguments(const QString &arguments)
void setDescription(const QString &description)
Definition: command.h:49
EditCommandDialog(const QStringList &groups, const QString &group, QWidget *parent=0)
QString getDescription() const
QString getGroup() const
Qt::KeyboardModifiers m_keyModifiers
const QString & getArgString() const
Definition: command.h:46
void setCommand(const QString &command)
Definition: command.h:43
const QString & getGroup() const
Definition: command.h:48
void setAssociateKey(bool associate)
Definition: command.h:55
void setAssociateKey(bool associate)
Ui::EditCommandDialog * ui
void fromCommand(const Command &command)
bool getAssociateKey() const
Definition: command.h:56
void setGroup(const QString &group)
Definition: command.h:47
const QString & getCommand() const
Definition: command.h:44
void on_keyCapture_toggled(bool checked)
void setKeyModifiers(Qt::KeyboardModifiers keyModifiers)
Definition: command.h:53
Qt::Key getKey() const
bool getRelease() const
Definition: command.h:58