utils.h 2.59 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3 4
#ifndef UTILS_H
#define UTILS_H

#include <atomic>
5
#include <iostream>
6
#include <typeinfo>
Thiago Santini's avatar
Thiago Santini committed
7

8
#include <QCameraViewfinderSettings>
Thiago Santini's avatar
Thiago Santini committed
9
#include <QDateTime>
10 11
#include <QDebug>
#include <QElapsedTimer>
Thiago Santini's avatar
Thiago Santini committed
12 13
#include <QFile>
#include <QMutex>
14
#include <QTextStream>
15

Thiago Santini's avatar
Thiago Santini committed
16
#include <QCameraInfo>
17 18
Q_DECLARE_METATYPE(QCameraInfo)

Thiago Santini's avatar
Thiago Santini committed
19
#include <QSettings>
20
#include <QSoundEffect>
21
#include <QWidget>
Thiago Santini's avatar
Thiago Santini committed
22

Thiago Santini's avatar
Thiago Santini committed
23 24
#include "LogWidget.h"

Thiago Santini's avatar
Thiago Santini committed
25
typedef qint64 Timestamp;
26
extern const Timestamp maxTimestamp;
27
Q_DECLARE_METATYPE(Timestamp)
Thiago Santini's avatar
Thiago Santini committed
28

29 30 31
// useful common tokens for data
class Token {
public:
32 33 34 35
    static const char Delimiter;
    static const char Newline;
    static const char JournalEntry;
    static const char HeaderEntry;
Thiago Santini's avatar
Thiago Santini committed
36 37 38 39
    static const char MarkerIdDelimiter;
    static const char MarkerValueDelimiter;
    static const char MarkerEnd;
    static const int Precision;
40 41
};

Thiago Santini's avatar
Thiago Santini committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
template <typename T>
inline QString journalField(const T& v)
{
    return QString("%1%2").arg(v).arg(Token::Delimiter);
}
template <>
inline QString journalField<double>(const double& v)
{
    return QString("%1%2").arg(v, 0, 'g', Token::Precision).arg(Token::Delimiter);
}
template <>
inline QString journalField<float>(const float& v)
{
    return journalField(static_cast<double>(v));
}

58 59 60 61 62 63 64 65 66 67 68
template <typename T>
inline void checkDelimitiers()
{
    int dSize = T().toQString().split(Token::Delimiter).size();
    int hSize = T().header().split(Token::Delimiter).size();
    if (dSize != hSize) {
        QString err = QString("%1: Data delimiters (%2) don't match header delimiters (%3)").arg(typeid(T).name()).arg(dSize).arg(hSize);
        qFatal(err.toStdString().c_str());
    }
}

Thiago Santini's avatar
Thiago Santini committed
69 70 71 72
/*
 * Utility functions
 */

Thiago Santini's avatar
Thiago Santini committed
73 74
QDebug
operator<<(QDebug dbg, const QCameraViewfinderSettings& p);
Thiago Santini's avatar
Thiago Santini committed
75 76 77

void logInitBanner();
void logExitBanner();
78
void logMessages(QtMsgType type, const QMessageLogContext& context, const QString& msg);
Thiago Santini's avatar
Thiago Santini committed
79 80 81 82 83 84

QString toQString(QVideoFrame::PixelFormat format);
QString toQString(QCameraViewfinderSettings setting);

QString iniStr(QString str);

85
template <typename T>
86 87
T ns2ms(const T v) { return 1.0e-6 * v; }

88
void delay(const int& thMs);
Thiago Santini's avatar
Thiago Santini committed
89

90 91
template <typename T>
void set(const QSettings* settings, const QString key, T& v)
Thiago Santini's avatar
Thiago Santini committed
92 93 94 95 96 97 98 99
{
    if (!settings)
        return;
    QVariant variant = settings->value(key);
    if (variant.isValid())
        v = qvariant_cast<T>(variant);
}

100
void loadSoundEffect(const QString& fileName, const QString& exeDir, QSoundEffect& effect);
101

102 103 104 105 106 107 108 109 110
template <typename T>
double median(std::vector<T> v)
{
    int size = static_cast<int>(v.size());
    std::sort(v.begin(), v.end());
    int mid = size / 2;
    return size % 2 == 0 ? (v[mid] + v[mid - 1]) / 2 : v[mid];
}

Thiago Santini's avatar
Thiago Santini committed
111
#endif // UTILS_H