summaryrefslogtreecommitdiff
path: root/qmap/main.cpp
blob: 196b6f4b8167e4a3821b64407afea910006fb97e (plain)
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
#include <fftw3.h>
#ifdef QT5
#include <QtWidgets>
#else
#include <QtGui>
#endif
#include <QApplication>
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QStandardPaths>

#include "revision_utils.hpp"
#include "mainwindow.h"
#include "runtime_paths.h"

extern "C" {
  // Fortran procedures we need
  void four2a_ (_Complex float *, int * nfft, int * ndim, int * isign, int * iform, int len);

  void _gfortran_set_args(int argc, char *argv[]);
  void _gfortran_set_convert(int conv);
  void ftninit_(void);
  void fftbig_(float dd[], int* nfft);
}

int main(int argc, char *argv[])
{
  QCoreApplication::setApplicationName ("QMAP");
  QString dataDir = QStandardPaths::writableLocation (QStandardPaths::AppLocalDataLocation);
  if (dataDir.isEmpty ())
    {
      dataDir = QDir::home ().absoluteFilePath (".qmap");
    }

  QApplication::setAttribute (Qt::AA_EnableHighDpiScaling);

  QApplication a {argc, argv};

  // Override programs executable basename as application name.
  a.setApplicationName ("QMAP");
  a.setApplicationVersion ("0.7");

  dataDir = qmapDataDir();
  QFileInfo dataDirInfo {dataDir};
  if (!dataDirInfo.exists() || !dataDirInfo.isDir() || !dataDirInfo.isWritable()
      || !QDir::setCurrent(dataDir)) {
    QString message {"Unable to use QMAP working directory: " + dataDir};
    qWarning() << message;
    QMessageBox::critical(nullptr, QObject::tr("QMAP Startup Error"), message);
    return 1;
  }

  // QMAP C++ and Fortran code still use relative opens for runtime files.
  // Start from the writable data directory before Fortran initializes them.
  _gfortran_set_args(argc, argv);
  _gfortran_set_convert(0);
  ftninit_();
  // switch off as we share an Info.plist file with WSJT-X
  a.setAttribute (Qt::AA_DontUseNativeMenuBar);
  MainWindow w;
  w.show ();
  QObject::connect (&a, &QApplication::lastWindowClosed, &a, &QApplication::quit);
  auto result = a.exec ();

  // clean up lazily initialized FFTW3 resources
  {
    int nfft {-1};
    int ndim {1};
    int isign {1};
    int iform {1};
    // free FFT plan resources
    four2a_ (nullptr, &nfft, &ndim, &isign, &iform, 0);
    fftbig_(nullptr, &nfft);
  }
  fftwf_forget_wisdom ();
  fftwf_cleanup ();
  return result;
}