-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathgcodeprogramloader.cpp
89 lines (76 loc) · 2.41 KB
/
gcodeprogramloader.cpp
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
/****************************************************************************
**
** Copyright (C) 2014 Alexander Rössler
** License: LGPL version 2.1
**
** This file is part of QtQuickVcp.
**
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the GNU Lesser General Public License
** (LGPL) version 2.1 which accompanies this distribution, and is available at
** http://www.gnu.org/licenses/lgpl-2.1.html
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** Contributors:
** Alexander Rössler @ The Cool Tool GmbH <mail DOT aroessler AT gmail DOT com>
**
****************************************************************************/
#include "gcodeprogramloader.h"
namespace qtquickvcp {
GCodeProgramLoader::GCodeProgramLoader(QObject *parent) :
QObject(parent),
m_localFilePath(""),
m_localPath(""),
m_remotePath(""),
m_model(nullptr)
{
}
void GCodeProgramLoader::load()
{
if (m_model == nullptr)
{
emit loadingFailed();
return;
}
QString localFilePath = QUrl(m_localFilePath).toLocalFile();
QString localPath = QUrl(m_localPath).toLocalFile();
QString remotePath = QUrl(m_remotePath).toLocalFile();
QString remoteFilePath;
if (localFilePath.indexOf(localPath) == 0) // file is in local download path
{
remoteFilePath = QDir(remotePath).filePath(localFilePath.mid(localPath.length() + 1));
}
else
{
QFileInfo fileInfo(localFilePath);
remoteFilePath = QDir(remotePath).filePath(fileInfo.fileName());
}
QFile file(localFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
emit loadingFailed();
return;
}
int lineNumber = 0;
while (!file.atEnd()) {
file.readLine();
lineNumber++;
}
m_model->beginUpdate();
m_model->prepareFile(remoteFilePath, lineNumber);
lineNumber = 0;
file.reset();
while (!file.atEnd()) {
QByteArray line = file.readLine();
lineNumber++;
m_model->setData(remoteFilePath, lineNumber, QString(line), GCodeProgramModel::GCodeRole);
}
m_model->endUpdate();
file.close();
emit loadingFinished();
}
} // namespace qtquickvcp