aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/unipentrace.cpp
blob: 74c1b23ef96c91ce79d243bd26c04344955c9517 (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
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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "unipentrace.h"
#include <QRectF>
#include <QDir>
#include <QFile>
#include <QDebug>

namespace QtVirtualKeyboard {

UnipenTrace::UnipenTrace(const QVariantMap &traceCaptureDeviceInfo,
                         const QVariantMap &traceScreenInfo,
                         QObject *parent) :
    QObject(parent)
{
    m_lines.append(QLatin1String(".VERSION 1.0"));
    m_lines.append(QLatin1String(".HIERARCHY CHARACTER"));
    m_lines.append(QLatin1String(".COORD X Y T"));
    m_lines.append(QLatin1String(".SEGMENT CHARACTER"));
    const QRectF boundingBox = traceScreenInfo["boundingBox"].toRectF();
    if (!boundingBox.isEmpty()) {
        m_lines.append(QStringLiteral(".X_DIM %1").arg(qRound(boundingBox.right())));
        m_lines.append(QStringLiteral(".Y_DIM %1").arg(qRound(boundingBox.bottom())));
    }
    bool ok = false;
    int dpi = traceCaptureDeviceInfo["dpi"].toInt(&ok);
    if (ok) {
        m_lines.append(QStringLiteral(".X_POINTS_PER_INCH %1").arg(dpi));
        m_lines.append(QStringLiteral(".Y_POINTS_PER_INCH %1").arg(dpi));
    }
    ok = false;
    int sampleRate = traceCaptureDeviceInfo["sampleRate"].toInt(&ok);
    if (ok)
        m_lines.append(QStringLiteral(".POINTS_PER_SECOND %1").arg(sampleRate));
}

void UnipenTrace::record(const QList<Trace *> &traceList)
{
    qlonglong t0 = 0;
    for (const Trace *trace : qAsConst(traceList)) {
        const QVariantList &points = trace->points();
        const bool hasTime = trace->channels().contains("t");
        const QVariantList timeData = hasTime ? trace->channelData("t") : QVariantList();
        QVariantList::ConstIterator t = timeData.constBegin();
        if (t0 == 0 && hasTime)
            t0 = t->toLongLong();

        m_lines.append(QLatin1String(".PEN_DOWN"));

        for (const QVariant &point : points) {
            const QPointF pt(point.toPointF());
            QString pointStr(QStringLiteral("%1 %2 ").arg(qRound(pt.x())).arg(qRound(pt.y())));
            if (hasTime) {
                pointStr.append(QString::number(t->toLongLong() - t0));
                t++;
            } else {
                pointStr.append(QLatin1String("0"));
            }
            m_lines.append(pointStr);
        }

        m_lines.append(QLatin1String(".PEN_UP"));
    }
}

void UnipenTrace::save(uint unicode, uint confidence)
{
    if (m_directory.isEmpty())
        return;

    QString fileName;
    QDir fileDir(m_directory);
    if (!fileDir.exists())
        fileDir.mkpath(m_directory);
    if (fileDir.exists()) {
        int fileIndex = 0;
        do {
            fileName = fileDir.absoluteFilePath(QStringLiteral("%1_%2_%3.txt").arg(unicode).arg(confidence, 3, 10, QLatin1Char('0')).arg(fileIndex++));
        } while (QFileInfo::exists(fileName));
    }

    QString dataStr(m_lines.join('\n'));
    dataStr.append('\n');
    QFile file(fileName);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
        file.write(dataStr.toUtf8().constData());
    } else {
        qWarning() << "Cannot open file for writing" << fileName;
    }
}

QString UnipenTrace::directory() const
{
    return m_directory;
}

void UnipenTrace::setDirectory(const QString &directory)
{
    m_directory = directory;
}

}