Skip to content

Commit

Permalink
Text drop handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denvi committed Nov 1, 2015
1 parent f6e0712 commit 22694c3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 55 deletions.
Binary file modified bin/grblControl.exe
Binary file not shown.
117 changes: 62 additions & 55 deletions frmmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ void frmMain::closeEvent(QCloseEvent *ce)

void frmMain::dragEnterEvent(QDragEnterEvent *dee)
{
if (dee->mimeData()->hasFormat("text/plain")) dee->acceptProposedAction();
if (dee->mimeData()->hasFormat("text/plain") && !m_heightMapMode) dee->acceptProposedAction();
else if (dee->mimeData()->hasFormat("text/uri-list") && dee->mimeData()->urls().count() == 1) {
QString fileName = dee->mimeData()->urls().at(0).toLocalFile();

Expand All @@ -1228,13 +1228,21 @@ void frmMain::dropEvent(QDropEvent *de)
if (!m_heightMapMode) {
if (!saveChanges(false)) return;

addRecentFile(fileName);
updateRecentFilesMenu();

loadFile(fileName);
// Load dropped g-code file
if (!fileName.isEmpty()) {
addRecentFile(fileName);
updateRecentFilesMenu();
loadFile(fileName);
// Load dropped text
} else {
m_programFileName.clear();
m_fileChanged = true;
loadFile(de->mimeData()->text().split("\n"));
}
} else {
if (!saveChanges(true)) return;

// Load dropped heightmap file
addRecentHeightmap(fileName);
updateRecentFilesMenu();
loadHeightMap(fileName);
Expand Down Expand Up @@ -1286,15 +1294,8 @@ void frmMain::resetHeightmap()
m_heightMapChanged = false;
}

void frmMain::loadFile(QString fileName)
void frmMain::loadFile(QList<QString> data)
{
QFile file(fileName);

if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, this->windowTitle(), tr("Can't open file:\n") + fileName);
return;
}

QTime time;
time.start();

Expand All @@ -1321,12 +1322,6 @@ void frmMain::loadFile(QString fileName)
QByteArray headerState = ui->tblProgram->horizontalHeader()->saveState();
ui->tblProgram->setModel(NULL);

// Set filename
m_programFileName = fileName;

// Prepare text stream
QTextStream textStream(&file);

// Prepare parser
GcodeParser gp;
gp.setTraverseSpeed(m_rapidSpeed);
Expand All @@ -1337,45 +1332,32 @@ void frmMain::loadFile(QString fileName)
// Block parser updates on table changes
m_programLoading = true;

// // Test memory leakage
// for (int i = 0; i < 1; i++) {

// textStream.seek(0);

// clearTable();
// gp.reset();

QString command;
QString stripped;
QList<QString> args;

while (!textStream.atEnd())
{
command = textStream.readLine();
QString command;
QString stripped;
QList<QString> args;

// Trim & split command
stripped = GcodePreprocessorUtils::removeComment(command);
args = GcodePreprocessorUtils::splitCommand(stripped);
while (!data.isEmpty())
{
command = data.takeFirst();

PointSegment *ps = gp.addCommand(args);
//Quantum line (if disable pointsegment check some points will have NAN number on raspberry)
// code alignment?
if (ps && (std::isnan(ps->point()->x()) || std::isnan(ps->point()->y()) || std::isnan(ps->point()->z())))
qDebug() << "nan point segment added:" << *ps->point();
// Trim & split command
stripped = GcodePreprocessorUtils::removeComment(command);
args = GcodePreprocessorUtils::splitCommand(stripped);

m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 1, 1), command);
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 2), tr("In queue"));
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 4), gp.getCommandNumber());
// Store splitted args to speed up future parser updates
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 5), QVariant(args));
PointSegment *ps = gp.addCommand(args);
// Quantum line (if disable pointsegment check some points will have NAN number on raspberry)
// code alignment?
if (ps && (std::isnan(ps->point()->x()) || std::isnan(ps->point()->y()) || std::isnan(ps->point()->z())))
qDebug() << "nan point segment added:" << *ps->point();

// Test args
// qDebug() << m_programModel.data(m_programModel.index(m_programModel.rowCount() - 2, 5)).toStringList();
}
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 1, 1), command);
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 2), tr("In queue"));
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 4), gp.getCommandNumber());
// Store splitted args to speed up future parser updates
m_programModel.setData(m_programModel.index(m_programModel.rowCount() - 2, 5), QVariant(args));
}

// m_viewParser.reset();
updateProgramEstimatedTime(m_viewParser.getLinesFromParser(&gp, m_arcPrecision));
// }
updateProgramEstimatedTime(m_viewParser.getLinesFromParser(&gp, m_arcPrecision));

qDebug() << "model filled:" << time.elapsed();
time.start();
Expand All @@ -1400,11 +1382,36 @@ void frmMain::loadFile(QString fileName)
updateControlsState();
}

void frmMain::loadFile(QString fileName)
{
QFile file(fileName);

if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, this->windowTitle(), tr("Can't open file:\n") + fileName);
return;
}

// Set filename
m_programFileName = fileName;

// Prepare text stream
QTextStream textStream(&file);

// Read lines
QList<QString> data;
while (!textStream.atEnd()) data.append(textStream.readLine());

// Load lines
loadFile(data);
}

QTime frmMain::updateProgramEstimatedTime(QList<LineSegment*> lines)
{
double time = 0;

foreach (LineSegment *ls, lines) {
for (int i = 0; i < lines.count(); i++) {
LineSegment *ls = lines[i];
// foreach (LineSegment *ls, lines) {
double length = (ls->getEnd() - ls->getStart()).length();

if (!std::isnan(length) && !std::isnan(ls->getSpeed()) && ls->getSpeed() != 0) time +=
Expand All @@ -1415,7 +1422,7 @@ QTime frmMain::updateProgramEstimatedTime(QList<LineSegment*> lines)
// ? (ls->getSpeed() * ui->txtFeed->value() / 100) : ls->getSpeed())
// << time;

if (std::isnan(length)) qDebug() << "length nan:" << ls->getStart() << ls->getEnd();
if (std::isnan(length)) qDebug() << "length nan:" << i << ls->getLineNumber() << ls->getStart() << ls->getEnd();
if (std::isnan(ls->getSpeed())) qDebug() << "speed nan:" << ls->getSpeed();
}

Expand Down
1 change: 1 addition & 0 deletions frmmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ private slots:
bool m_cellChanged;

void loadFile(QString fileName);
void loadFile(QList<QString> data);
void clearTable();
void loadSettings();
void saveSettings();
Expand Down
3 changes: 3 additions & 0 deletions gcodepreprocessorutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(PointSegme
end = m * end;
center = m * center;

// Check center
if (std::isnan(center.length())) return QList<QVector3D>();

// Calculate radius if necessary.
if (radius == 0) {
radius = sqrt(pow((double)(start.x() - center.x()), 2.0) + pow((double)(end.y() - center.y()), 2.0));
Expand Down
2 changes: 2 additions & 0 deletions pointsegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PointSegment::PointSegment(QObject *parent) : QObject(parent)
m_lineNumber = -1;
m_arcProperties = NULL;
m_speed = 0;
m_plane = XY;
}

PointSegment::PointSegment(PointSegment *ps) : PointSegment(ps->point(), ps->getLineNumber())
Expand All @@ -35,6 +36,7 @@ PointSegment::PointSegment(PointSegment *ps) : PointSegment(ps->point(), ps->get
this->setArcCenter(ps->center());
this->setRadius(ps->getRadius());
this->setIsClockwise(ps->isClockwise());
this->setPlane(ps->plane());
}
}

Expand Down

0 comments on commit 22694c3

Please sign in to comment.