File: logqso.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 6
file content (340 lines) | stat: -rw-r--r-- 9,762 bytes parent folder | download | duplicates (3)
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
#include "logqso.h"

#include <QString>
#include <QSettings>
#include <QStandardPaths>
#include <QDir>
#include <QDebug>
#include <QUdpSocket>

#include "logbook/adif.h"
#include "MessageBox.hpp"
#include "Configuration.hpp"
#include "Bands.hpp"
#include "MaidenheadLocatorValidator.hpp"
#include "DriftingDateTime.h"

#include "ui_logqso.h"
#include "moc_logqso.cpp"

LogQSO::LogQSO(QString const& programTitle, QSettings * settings
               , Configuration const * config, QWidget *parent)
  : QDialog {parent, Qt::WindowStaysOnTopHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint}
  , ui(new Ui::LogQSO)
  , m_settings (settings)
  , m_config {config}
{
  ui->setupUi(this);
  setWindowTitle(programTitle + " - Log QSO");
  ui->grid->setValidator (new MaidenheadLocatorValidator {this});

  auto b = ui->buttonBox->button(QDialogButtonBox::Save);
  if(b){
      b->setText("Add to Log");
  }
}

LogQSO::~LogQSO ()
{
}

bool LogQSO::acceptText(QString text){
    auto w = focusWidget();
    if(!w){
        return false;
    }

    auto name = QString(w->metaObject()->className());
    if(name != "QLineEdit"){
        return false;
    }

    auto l = static_cast<QLineEdit*>(w);
    if(l->text().isEmpty()){
        // set
        l->setText(text);
    } else {
        // append
        l->setText(QString("%1 %2").arg(l->text()).arg(text));
    }

    return true;
}

QString LogQSO::currentCall(){
    return ui->call->text().trimmed();
}

void LogQSO::on_start_now_button_pressed(){
  ui->start_date_time->setDateTime(DriftingDateTime::currentDateTimeUtc());
}

void LogQSO::on_end_now_button_pressed(){
  ui->end_date_time->setDateTime(DriftingDateTime::currentDateTimeUtc());
}

void LogQSO::on_add_new_field_button_pressed(){
    createAdditionalField();
}

void LogQSO::createAdditionalField(QString key, QString value){
    QLineEdit * l = new QLineEdit(this);
    if(!value.isEmpty()){
        l->setText(value);
    }

    QComboBox * c = new QComboBox(this);
    c->insertItems(0, ADIF_FIELDS);
    c->insertItem(0, "");
    c->setEditable(true);
    c->setAutoCompletion(true);
    c->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
    c->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    connect(c, &QComboBox::currentTextChanged, this, [this, l](const QString &text){
       l->setProperty("fieldKey", QVariant(text));
    });
    c->setCurrentText(key);

    auto layout = static_cast<QFormLayout*>(ui->additionalFields->layout());
    layout->addRow(c, l);

    // set tab ordering
    if(m_additionalFieldsControls.isEmpty()){
        setTabOrder(ui->cbComments, c);
    } else {
        setTabOrder(m_additionalFieldsControls.last(), c);
    }
    setTabOrder(c, l);
    setTabOrder(l, ui->add_new_field_button);
    c->setFocus();

    m_additionalFieldsControls.append(l);
    ui->additionalFields->setVisible(true);
    ui->additionalFields->adjustSize();

    // update the window layout
    updateGeometry();
}

QMap<QString, QVariant> LogQSO::collectAdditionalFields(){
    QMap<QString, QVariant> additionalFields;
    foreach(auto field, m_additionalFieldsControls){
        auto key = field->property("fieldKey").toString();
        if(key.isEmpty()){
            continue;
        }
        additionalFields[key] = QVariant(field->text());
    }
    return additionalFields;
}

void LogQSO::resetAdditionalFields(){
    ui->additionalFields->setVisible(false);

    if(!m_additionalFieldsControls.isEmpty()){
        auto layout = static_cast<QFormLayout*>(ui->additionalFields->layout());

#if QT_VERSION >= 0x050800
        for(int i = 0, count = layout->rowCount(); i < count; i++){
            layout->removeRow(0);
        }
#else
        QLayoutItem *child;
        while((child = layout->takeAt(0)) != 0){
            delete child;
        }
#endif

        m_additionalFieldsControls.clear();
    }

    setTabOrder(ui->cbComments, ui->add_new_field_button);
    updateGeometry();
}

void LogQSO::loadSettings ()
{
  m_settings->beginGroup ("LogQSO");
  restoreGeometry (m_settings->value ("geometry", saveGeometry ()).toByteArray ());
  ui->cbComments->setChecked (m_settings->value ("SaveComments", false).toBool ());
  m_comments = m_settings->value ("LogComments", "").toString();

  resetAdditionalFields();
  auto additionalFields = m_settings->value("AdditionalFields", {}).toStringList();
  QSet<QString> additionalFieldsSet;
  foreach(auto key, additionalFields){
      if(key.isEmpty()){
          continue;
      }
      if(additionalFieldsSet.contains(key)){
          continue;
      }

      createAdditionalField(key);
      additionalFieldsSet.insert(key);
  }

  m_settings->endGroup ();
}

void LogQSO::storeSettings () const
{
  m_settings->beginGroup ("LogQSO");
  m_settings->setValue ("geometry", saveGeometry ());
  m_settings->setValue ("SaveComments", ui->cbComments->isChecked ());
  m_settings->setValue ("LogComments", m_comments);

  auto additionalFields = QStringList{};
  foreach(auto field, m_additionalFieldsControls){
      auto key = field->property("fieldKey").toString();
      if(key.isEmpty()){
          continue;
      }
      additionalFields.append(key);
  }
  m_settings->setValue ("AdditionalFields", additionalFields);

  m_settings->endGroup ();
}

void LogQSO::initLogQSO(QString const& hisCall, QString const& hisGrid, QString mode,
                        QString const& rptSent, QString const& rptRcvd,
                        QDateTime const& dateTimeOn, QDateTime const& dateTimeOff,
                        Radio::Frequency dialFreq, QString const& myCall, QString const& myGrid,
                        bool toDATA, bool dBtoComments, bool bFox, QString const& opCall, QString const& comments)
{
  if(!isHidden()) return;

  loadSettings();

  ui->call->setFocus();
  ui->call->setText(hisCall);
  ui->grid->setText(hisGrid);
  ui->name->setText("");
  ui->comments->setText("");

  if (ui->cbComments->isChecked ()) ui->comments->setText(m_comments);

  if(dBtoComments) {
    QString t=mode;
    if(rptSent!="") t+="  Sent: " + rptSent;
    if(rptRcvd!="") t+="  Rcvd: " + rptRcvd;
    ui->comments->setText(t);
  }

  if(!comments.isEmpty()){
    ui->comments->setText(comments);
  }

  if(toDATA) mode="DATA";

  ui->mode->setText(mode);
  ui->sent->setText(rptSent);
  ui->rcvd->setText(rptRcvd);
  ui->start_date_time->setDateTime (dateTimeOn);
  ui->end_date_time->setDateTime (dateTimeOff);

  m_dialFreq=dialFreq;
  m_myCall=myCall;
  m_myGrid=myGrid;

  ui->band->setText (m_config->bands ()->find (dialFreq));
  ui->loggedOperator->setText(opCall);

  if(bFox) {
    accept();
  } else {
    show ();
  }
}

void LogQSO::accept()
{
  QString hisCall,hisGrid,mode,submode,rptSent,rptRcvd,dateOn,dateOff,timeOn,timeOff,band,operator_call;
  QString comments,name;

  hisCall=ui->call->text().toUpper();
  hisGrid=ui->grid->text().toUpper();
  mode = ui->mode->text().toUpper();
  if(mode == "JS8"){
    mode="MFSK";
    submode="JS8";
  }
  rptSent=ui->sent->text();
  rptRcvd=ui->rcvd->text();
  m_dateTimeOn = ui->start_date_time->dateTime ();
  m_dateTimeOff = ui->end_date_time->dateTime ();
  band=ui->band->text();
  name=ui->name->text();
  comments=ui->comments->text();
  m_comments=comments;
  QString strDialFreq(QString::number(m_dialFreq / 1.e6,'f',6));
  operator_call = ui->loggedOperator->text();
  //Log this QSO to ADIF file "js8call_log.adi"
  QString filename = "js8call_log.adi";  // TODO allow user to set
  ADIF adifile;
  auto adifilePath = QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}.absoluteFilePath (filename);
  adifile.init(adifilePath);

  auto additionalFields = collectAdditionalFields();

  QByteArray ADIF {adifile.QSOToADIF (hisCall, hisGrid, mode, submode, rptSent, rptRcvd, m_dateTimeOn, m_dateTimeOff, band
                                      , comments, name, strDialFreq, m_myCall, m_myGrid, operator_call, additionalFields)};

  if (!adifile.addQSOToFile (ADIF))
  {
    MessageBox::warning_message (this, tr ("Log file error"),
                                 tr ("Cannot open \"%1\"").arg (adifilePath));
  }

  //Log this QSO to file "js8call.log"
  static QFile f {QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}.absoluteFilePath ("js8call.log")};
  if(!f.open(QIODevice::Text | QIODevice::Append)) {
    MessageBox::warning_message (this, tr ("Log file error"),
                                 tr ("Cannot open \"%1\" for append").arg (f.fileName ()),
                                 tr ("Error: %1").arg (f.errorString ()));
  } else {
    QStringList logEntryItems = {
      m_dateTimeOn.date().toString("yyyy-MM-dd"),
      m_dateTimeOn.time().toString("hh:mm:ss"),
      m_dateTimeOff.date().toString("yyyy-MM-dd"),
      m_dateTimeOff.time().toString("hh:mm:ss"),
      hisCall,
      hisGrid,
      strDialFreq,
      (mode == "MFSK" ? "JS8" : mode),
      rptSent,
      rptRcvd,
      comments,
      name
    };

    if(!additionalFields.isEmpty()){
        foreach(auto value, additionalFields.values()){
            logEntryItems.append(value.toString());
        }
    }

    QTextStream out(&f);
    out << logEntryItems.join(",") << endl;
    out.flush();
    flushFileBuffer(f);
    f.close();
  }

  Q_EMIT acceptQSO (m_dateTimeOff, hisCall, hisGrid, m_dialFreq, mode, submode, rptSent, rptRcvd, comments, name,m_dateTimeOn, operator_call, m_myCall, m_myGrid, ADIF, additionalFields);

  //Clean up and finish logging
  ui->call->clear();

  QDialog::accept();
}

// closeEvent is only called from the system menu close widget for a
// modeless dialog so we use the hideEvent override to store the
// window settings
void LogQSO::hideEvent (QHideEvent * e)
{
  storeSettings ();
  QDialog::hideEvent (e);
}