Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/3dview/fileloader.cpp
Show All 20 Lines | |||||
21 | */ | 21 | */ | ||
22 | #include <QString> | 22 | #include <QString> | ||
23 | #include <QTextStream> | 23 | #include <QTextStream> | ||
24 | #include <QVariant> | 24 | #include <QVariant> | ||
25 | #include <QVector> | 25 | #include <QVector> | ||
26 | #include <QVector4D> | 26 | #include <QVector4D> | ||
27 | #include "fileloader.h" | 27 | #include "fileloader.h" | ||
28 | 28 | | |||
29 | namespace | 29 | namespace ns | ||
laysrodrigues: ns? can you clarify the name of this namespace, because ns short for namespace doesn't sound… | |||||
30 | { | 30 | { | ||
31 | const static QString _commentChar = QStringLiteral(";"); | 31 | const static QString _commentChar = QStringLiteral(";"); | ||
32 | const static QStringList _moveCommands = {QStringLiteral("G0"), QStringLiteral("G1")}; | 32 | const static QStringList _moveCommands = {QStringLiteral("G0"), QStringLiteral("G1")}; | ||
33 | const static QString _space = QStringLiteral(" "); | 33 | const static QString _space = QStringLiteral(" "); | ||
34 | const static QString _E = QStringLiteral("E"); | 34 | const static QString _E = QStringLiteral("E"); | ||
35 | const static QString _X = QStringLiteral("X"); | 35 | const static QString _X = QStringLiteral("X"); | ||
36 | const static QString _Y = QStringLiteral("Y"); | 36 | const static QString _Y = QStringLiteral("Y"); | ||
37 | const static QString _Z = QStringLiteral("Z"); | 37 | const static QString _Z = QStringLiteral("Z"); | ||
38 | } | 38 | } | ||
39 | 39 | | |||
40 | FileLoader::FileLoader(QString &fileName, QObject *parent) : | 40 | FileLoader::FileLoader(QString &fileName, QObject *parent) : | ||
41 | QObject(parent) | 41 | QObject(parent) | ||
42 | , _file(fileName) | 42 | , _file(fileName) | ||
43 | { | 43 | { | ||
44 | } | 44 | } | ||
45 | 45 | | |||
46 | FileLoader::~FileLoader() | | |||
47 | { | | |||
48 | } | | |||
49 | | ||||
50 | void FileLoader::run() | 46 | void FileLoader::run() | ||
51 | { | 47 | { | ||
52 | QVector<QVector4D> pos; | 48 | QVector<QVector4D> pos; | ||
53 | qint64 totalSize = _file.bytesAvailable(); | 49 | qint64 totalSize = _file.bytesAvailable(); | ||
54 | qint64 stillSize = totalSize; | 50 | qint64 stillSize = totalSize; | ||
55 | 51 | | |||
56 | if (_file.open(QIODevice::ReadOnly)) { | 52 | if (_file.open(QIODevice::ReadOnly)) { | ||
57 | int lastPerc = 0; | 53 | int lastPerc = 0; | ||
58 | QTextStream in(&_file); | 54 | QTextStream in(&_file); | ||
59 | while (!in.atEnd()) { | 55 | while (!in.atEnd()) { | ||
60 | //Get each line | 56 | //Get each line | ||
61 | QString line = in.readLine(); | 57 | QString line = in.readLine(); | ||
62 | stillSize -= line.size() + 1; // +1 endl | 58 | stillSize -= line.size() + 1; // +1 endl | ||
63 | const int perc = int((totalSize - stillSize) * 100.0 / totalSize); | 59 | const int perc = int((totalSize - stillSize) * 100.0 / totalSize); | ||
64 | if (perc - lastPerc > 1) { | 60 | if (perc - lastPerc > 1) { | ||
65 | emit percentUpdate(perc); | 61 | emit percentUpdate(perc); | ||
66 | lastPerc = perc; | 62 | lastPerc = perc; | ||
67 | } | 63 | } | ||
68 | line = line.simplified(); | 64 | line = line.simplified(); | ||
69 | //Is it a comment ? Drop it | 65 | //Is it a comment ? Drop it | ||
70 | if (line.isEmpty()) { | 66 | if (line.isEmpty()) { | ||
71 | continue; | 67 | continue; | ||
72 | } | 68 | } | ||
73 | //Remove comment in the end of command | 69 | //Remove comment in the end of command | ||
74 | if (line.indexOf(_commentChar) != -1) { | 70 | if (line.indexOf(ns::_commentChar) != -1) { | ||
75 | line.resize(line.indexOf(_commentChar)); | 71 | line.resize(line.indexOf(ns::_commentChar)); | ||
76 | //Remove trailing spaces | 72 | //Remove trailing spaces | ||
77 | line = line.simplified(); | 73 | line = line.simplified(); | ||
78 | } | 74 | } | ||
79 | 75 | | |||
80 | //Split command and args | 76 | //Split command and args | ||
81 | QStringList commAndArgs = line.split(_space); | 77 | QStringList commAndArgs = line.split(ns::_space); | ||
82 | 78 | | |||
83 | if (_moveCommands.contains(commAndArgs[0])) { | 79 | if (ns::_moveCommands.contains(commAndArgs[0])) { | ||
84 | QVector4D actualPos; | 80 | QVector4D actualPos; | ||
85 | //Compute args | 81 | //Compute args | ||
86 | commAndArgs.removeFirst(); | 82 | commAndArgs.removeFirst(); | ||
87 | for (QString element : commAndArgs) { | 83 | for (QString element : commAndArgs) { | ||
88 | if (element.contains(_X)) { | 84 | if (element.contains(ns::_X)) { | ||
89 | actualPos.setX(element.remove(0, 1).toFloat() / 10); | 85 | actualPos.setX(element.remove(0, 1).toFloat() / 10); | ||
90 | } | 86 | } | ||
91 | 87 | | |||
92 | if (element.contains(_Y)) { | 88 | if (element.contains(ns::_Y)) { | ||
93 | actualPos.setY(element.remove(0, 1).toFloat() / 10); | 89 | actualPos.setY(element.remove(0, 1).toFloat() / 10); | ||
94 | } | 90 | } | ||
95 | 91 | | |||
96 | if (element.contains(_Z)) { | 92 | if (element.contains(ns::_Z)) { | ||
97 | actualPos.setZ(element.remove(0, 1).toFloat() / 10); | 93 | actualPos.setZ(element.remove(0, 1).toFloat() / 10); | ||
98 | } | 94 | } | ||
99 | 95 | | |||
100 | if (element.contains(_E)) { | 96 | if (element.contains(ns::_E)) { | ||
101 | actualPos.setW(element.remove(0, 1).toFloat() / 10); | 97 | actualPos.setW(element.remove(0, 1).toFloat() / 10); | ||
102 | } | 98 | } | ||
103 | } | 99 | } | ||
104 | 100 | | |||
105 | if (!pos.isEmpty()) { | 101 | if (!pos.isEmpty()) { | ||
106 | if (!line.contains(_X)) { | 102 | if (!line.contains(ns::_X)) { | ||
107 | actualPos.setX(pos.last().x()); | 103 | actualPos.setX(pos.last().x()); | ||
108 | } | 104 | } | ||
109 | 105 | | |||
110 | if (!line.contains(_Y)) { | 106 | if (!line.contains(ns::_Y)) { | ||
111 | actualPos.setY(pos.last().y()); | 107 | actualPos.setY(pos.last().y()); | ||
112 | } | 108 | } | ||
113 | 109 | | |||
114 | if (!line.contains(_Z)) { | 110 | if (!line.contains(ns::_Z)) { | ||
115 | actualPos.setZ(pos.last().z()); | 111 | actualPos.setZ(pos.last().z()); | ||
116 | } | 112 | } | ||
117 | 113 | | |||
118 | if (!line.contains(_E)) { | 114 | if (!line.contains(ns::_E)) { | ||
119 | actualPos.setW(pos.last().w()); | 115 | actualPos.setW(pos.last().w()); | ||
120 | } | 116 | } | ||
121 | } | 117 | } | ||
122 | 118 | | |||
123 | pos.append(actualPos); | 119 | pos.append(actualPos); | ||
124 | } | 120 | } | ||
125 | } | 121 | } | ||
126 | } | 122 | } | ||
127 | emit percentUpdate(100); | 123 | emit percentUpdate(100); | ||
128 | emit posFinished(pos); | 124 | emit posFinished(pos); | ||
129 | }; | 125 | }; |
ns? can you clarify the name of this namespace, because ns short for namespace doesn't sound clear