File indexing completed on 2025-01-26 05:19:14

0001 /* Atelier KDE Printer Host for 3D Printing
0002     Copyright (C) <2017-2018>
0003     Author: Patrick José Pereira - patrickjp@kde.org
0004             Kevin Ottens - ervin@kde.org
0005 
0006     This program is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU General Public License as
0008     published by the Free Software Foundation; either version 3 of
0009     the License or any later version accepted by the membership of
0010     KDE e.V. (or its successor approved by the membership of KDE
0011     e.V.), which shall act as a proxy defined in Section 14 of
0012     version 3 of the license.
0013 
0014     This program is distributed in the hope that it will be useful,
0015     but WITHOUT ANY WARRANTY; without even the implied warranty of
0016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017     GNU General Public License for more details.
0018 
0019     You should have received a copy of the GNU General Public License
0020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 #include "fileloader.h"
0023 #include <QString>
0024 #include <QTextStream>
0025 #include <QVariant>
0026 #include <QVector4D>
0027 #include <QVector>
0028 
0029 const QString FileLoader::_commentChar = QStringLiteral(";");
0030 const QStringList FileLoader::_moveCommands = {QStringLiteral("G0"), QStringLiteral("G1")};
0031 const QString FileLoader::_space = QStringLiteral(" ");
0032 const QString FileLoader::_E = QStringLiteral("E");
0033 const QString FileLoader::_X = QStringLiteral("X");
0034 const QString FileLoader::_Y = QStringLiteral("Y");
0035 const QString FileLoader::_Z = QStringLiteral("Z");
0036 
0037 FileLoader::FileLoader(QString &fileName, QObject *parent)
0038     : QObject(parent)
0039     , _file(fileName)
0040 {
0041 }
0042 
0043 void FileLoader::run()
0044 {
0045     QVector<QVector4D> pos;
0046     qint64 totalSize = _file.bytesAvailable();
0047     qint64 stillSize = totalSize;
0048 
0049     if (_file.open(QIODevice::ReadOnly)) {
0050         int lastPerc = 0;
0051         QTextStream in(&_file);
0052         while (!in.atEnd()) {
0053             // Get each line
0054             QString line = in.readLine();
0055             stillSize -= line.size() + 1; // +1 endl
0056             const int perc = int((totalSize - stillSize) * 100.0 / totalSize);
0057             if (perc - lastPerc > 1) {
0058                 emit percentUpdate(perc);
0059                 lastPerc = perc;
0060             }
0061             line = line.simplified();
0062             // Is it a comment ? Drop it
0063             if (line.isEmpty()) {
0064                 continue;
0065             }
0066             // Remove comment in the end of command
0067             if (line.indexOf(_commentChar) != -1) {
0068                 line.resize(line.indexOf(_commentChar));
0069                 // Remove trailing spaces
0070                 line = line.simplified();
0071             }
0072 
0073             // Split command and args
0074             QStringList commAndArgs = line.split(_space);
0075 
0076             if (_moveCommands.contains(commAndArgs[0])) {
0077                 QVector4D actualPos;
0078                 // Compute args
0079                 commAndArgs.removeFirst();
0080                 for (QString element : commAndArgs) {
0081                     if (element.contains(_X)) {
0082                         actualPos.setX(element.remove(0, 1).toFloat() / 10);
0083                     }
0084 
0085                     if (element.contains(_Y)) {
0086                         actualPos.setY(element.remove(0, 1).toFloat() / 10);
0087                     }
0088 
0089                     if (element.contains(_Z)) {
0090                         actualPos.setZ(element.remove(0, 1).toFloat() / 10);
0091                     }
0092 
0093                     if (element.contains(_E)) {
0094                         actualPos.setW(element.remove(0, 1).toFloat() / 10);
0095                     }
0096                 }
0097 
0098                 if (!pos.isEmpty()) {
0099                     if (!line.contains(_X)) {
0100                         actualPos.setX(pos.last().x());
0101                     }
0102 
0103                     if (!line.contains(_Y)) {
0104                         actualPos.setY(pos.last().y());
0105                     }
0106 
0107                     if (!line.contains(_Z)) {
0108                         actualPos.setZ(pos.last().z());
0109                     }
0110 
0111                     if (!line.contains(_E)) {
0112                         actualPos.setW(pos.last().w());
0113                     }
0114                 }
0115 
0116                 pos.append(actualPos);
0117             }
0118         }
0119     }
0120     emit percentUpdate(100);
0121     emit posFinished(pos);
0122 };