Frontend to the famous GRBL.

mainwindow.cpp 15KB

    /* Part of x2grbl * * Copyright Johann Wilhelm <johann.wilhelm@9mal6.de> 2015 * * see Readme.md for detailed license and usage information! */ #include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QMessageBox> #include <QGraphicsScene> #include "outputdeviceqpicture.h" #include <QGLWidget> #include <QSettings> #include "grbloptionsdialog.h" #include <QMessageBox> #include <QBuffer> #include "probingoptionsdialog.h" #include "millingoptionsdialog.h" #include "hpglparser.h" #include "eaglebrdparser.h" #include "autolevelproxy.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->graphicsView->setScene(new QGraphicsScene()); ui->graphicsView->scene()->addItem(&PictureItem); ui->graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); ui->graphicsView->setTransform(QTransform::fromScale(1,-1)); ui->dimensionsGroup->setEnabled(false); ui->execGroup->setEnabled(false); connect(&Grbl, SIGNAL(Connecting()), this, SLOT(GrblConnecting())); connect(&Grbl, SIGNAL(Connected()), this, SLOT(GrblConnected())); connect(&Grbl, SIGNAL(Disconnected()), this, SLOT(GrblDisconnected())); connect(&Grbl, SIGNAL(Failed()), this, SLOT(GrblFailed())); connect(&Grbl, SIGNAL(Finished()), this, SLOT(GrblFinished())); connect(&Grbl, SIGNAL(ProgressUpdate(float)), this, SLOT(GrblProgressUpdate(float))); readSettings(); Scale=1; } void MainWindow::closeEvent(QCloseEvent *event) { writeSettings(); event->accept(); } void MainWindow::writeSettings() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("2.5d Grbl-Commander")); settings.beginGroup("MainWindow"); settings.setValue("size", size()); settings.setValue("pos", pos()); settings.endGroup(); } void MainWindow::readSettings() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("2.5d Grbl-Commander")); settings.beginGroup("MainWindow"); resize(settings.value("size", QSize(400, 400)).toSize()); move(settings.value("pos", QPoint(200, 200)).toPoint()); settings.endGroup(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_actionFit_Preview_triggered() { ui->graphicsView->scene()->invalidate(); ui->graphicsView->fitInView(&PictureItem, Qt::KeepAspectRatio); ui->graphicsView->scene()->invalidate(); } void MainWindow::on_actionScale_triggered() { Scale++; ui->graphicsView->scale(Scale, Scale); } void MainWindow::on_actionScale_2_triggered() { Scale--; ui->graphicsView->scale(Scale, Scale); } void MainWindow::on_runButton_clicked() { ui->progressBar->setValue(0); if (Grbl.isConnected()) { if (ui->probeZ->isChecked()) { AutoLeveler.setOutputDevice(Grbl); AutoLeveler.ProbeGrid(ui->minX->text().toDouble(), ui->maxX->text().toDouble(),ui->minY->text().toDouble(),ui->maxY->text().toDouble()); connect(&Grbl, SIGNAL(ExecutionFinished()), SLOT(GrblProbingFinished())); Grbl.emitFinished(true); } else { Interpreter.Run(Grbl, Parser); } } else { AutoLevelProxy autoLeveler(nullptr); Interpreter.Run(autoLeveler, Parser); } } void MainWindow::GrblProbingFinished() { disconnect(&Grbl, SIGNAL(ExecutionFinished()), this, SLOT(GrblProbingFinished())); if (QMessageBox::question(this, tr("Start Milling"), tr("Probing finished!\nStart milling?"), QMessageBox::Yes|QMessageBox::No )==QMessageBox::Yes) { Interpreter.Run(AutoLeveler, Parser); } } void MainWindow::GrblConnecting() { ui->progressBar->setValue(0); setStatusTip(tr("Connecting to Grbl...")); } void MainWindow::GrblConnected() { ui->runButton->setEnabled(true); ui->progressBar->setValue(0); ui->actionConnect->setChecked(Grbl.isConnected()); ui->actionConnect->setEnabled(true); setStatusTip(tr("Grbl connected.")); QStringList InitCmds; QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("2.5d Grbl-Commander")); settings.beginGroup("Grbl"); if (settings.contains(QString("GrblInitCommands"))) { InitCmds=settings.value("GrblInitCommands").toString().split(QRegExp("[\r\n]"),QString::SkipEmptyParts); } settings.endGroup(); GCode Cmd; Cmd.Code=CodeType_RAW; for (int i=0;i<InitCmds.count();i++) { Cmd.Raw=InitCmds[i]; Grbl.Run(Cmd); } } void MainWindow::GrblDisconnected() { ui->runButton->setEnabled(false); ui->progressBar->setValue(0); ui->actionConnect->setChecked(Grbl.isConnected()); setStatusTip(tr("Grbl not connected.")); } void MainWindow::GrblFailed() { ui->progressBar->setValue(0); ui->actionConnect->setChecked(Grbl.isConnected()); ui->actionConnect->setEnabled(true); setStatusTip(tr("Command failed!")); } void MainWindow::GrblFinished() { ui->progressBar->setValue(100); setStatusTip(tr("Finished!")); } void MainWindow::GrblProgressUpdate(float Progress) { ui->progressBar->setValue(Progress*100); } void MainWindow::on_actionOptions_triggered() { GrblOptionsDialog Dialog; Dialog.exec(); } void MainWindow::on_actionConnect_triggered(bool checked) { if (checked) { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("2.5d Grbl-Commander")); settings.beginGroup("Grbl"); if (settings.contains(QString("GrblPort"))) { ui->actionConnect->setEnabled(false); Grbl.tryConnect(settings.value("GrblPort").toString()); ui->actionConnect->setChecked(Grbl.isConnected()); } else { this->setStatusTip(tr("Please configure Grbl!")); ui->actionConnect->setChecked(false); } settings.endGroup(); } else Grbl.Disconnect(); } void MainWindow::on_actionProbing_triggered() { ProbingOptionsDialog Dialog; Dialog.exec(); } void MainWindow::on_actionMilling_triggered() { MillingOptionsDialog Dialog; Dialog.exec(); } void MainWindow::on_selectInputFileButton_clicked() { QString InputFile=QFileDialog::getOpenFileName(this, tr("G-Code Input File"), QString(), QString("*.nc;*.tap")); if (InputFile.length()) { QFile Input(InputFile); Input.open(QIODevice::ReadOnly); if (Input.isOpen()) { ui->inputFileEdit->setText(InputFile); if (!Parser.open(Input)) { QMessageBox::warning(this, tr("G-Code Input File"), tr("Parse Error!").arg(InputFile)); PictureItem.clear(); ui->minX->setText(QString("")); ui->maxX->setText(QString("")); ui->minY->setText(QString("")); ui->maxY->setText(QString("")); ui->execGroup->setEnabled(false); } else { OutputDeviceQPicture Picture; //Interpreter.Run(Picture, Parser); AutoLeveler.setOutputDevice(Picture); Interpreter.Run(AutoLeveler, Parser); PictureItem.setPicture(Picture.picture()); ui->minX->setText(QString("%1").arg(Picture.minX)); ui->minY->setText(QString("%1").arg(Picture.minY)); ui->maxX->setText(QString("%1").arg(Picture.maxX)); ui->maxY->setText(QString("%1").arg(Picture.maxY)); ui->graphicsView->scene()->invalidate(); ui->graphicsView->fitInView(&PictureItem, Qt::KeepAspectRatio); ui->graphicsView->scene()->invalidate(); ui->execGroup->setEnabled(true); } Input.close(); } else { QMessageBox::warning(this, tr("G-Code Input File"), tr("Could not open file %1!").arg(InputFile)); } } } void MainWindow::on_selectHPGLInputFileButton_clicked() { QString InputFile=QFileDialog::getOpenFileName(this, tr("HPGL Input File"), QString(), QString("*.hpgl")); if (InputFile.length()) { QFile Input(InputFile); Input.open(QIODevice::ReadOnly); if (Input.isOpen()) { ui->inputFileEdit->setText(InputFile); HpglParser hpglParser; if (!hpglParser.open(Input)) { PictureItem.clear(); ui->minX->setText(QString("")); ui->maxX->setText(QString("")); ui->minY->setText(QString("")); ui->maxY->setText(QString("")); ui->execGroup->setEnabled(false); QMessageBox::warning(this, tr("HPGL Input File"), tr("Parse Error!").arg(InputFile)); } else { hpglParser.InitParser(Parser); OutputDeviceQPicture Picture; //Interpreter.Run(Picture, Parser); AutoLeveler.setOutputDevice(Picture); if (!Interpreter.Run(AutoLeveler, Parser)) { //if (!Interpreter.Run(Picture, Parser)) { PictureItem.clear(); ui->minX->setText(QString("")); ui->maxX->setText(QString("")); ui->minY->setText(QString("")); ui->maxY->setText(QString("")); ui->execGroup->setEnabled(false); QMessageBox::warning(this, tr("Fatal!"), tr("HPGL Interpreter produced illegal GCode!")); } else { PictureItem.setPicture(Picture.picture()); ui->minX->setText(QString("%1").arg(Picture.minX)); ui->minY->setText(QString("%1").arg(Picture.minY)); ui->maxX->setText(QString("%1").arg(Picture.maxX)); ui->maxY->setText(QString("%1").arg(Picture.maxY)); ui->graphicsView->scene()->invalidate(); ui->graphicsView->fitInView(&PictureItem, Qt::KeepAspectRatio); ui->graphicsView->scene()->invalidate(); ui->execGroup->setEnabled(true); } } } else { QMessageBox::warning(this, tr("HPGL Input File"), tr("Could not open file %1!").arg(InputFile)); } } } void MainWindow::on_selectEagleBrdFileButton_clicked() { QString InputFile=QFileDialog::getOpenFileName(this, tr("Eagle-BRD Input File"), QString(), QString("*.brd")); if (InputFile.length()) { QFile Input(InputFile); Input.open(QIODevice::ReadOnly); if (Input.isOpen()) { ui->inputFileEdit->setText(InputFile); EagleBrdParser eagleParser; if (!eagleParser.open(Input)) { PictureItem.clear(); ui->minX->setText(QString("")); ui->maxX->setText(QString("")); ui->minY->setText(QString("")); ui->maxY->setText(QString("")); ui->execGroup->setEnabled(false); QMessageBox::warning(this, tr("Eagle-BRD Input File"), tr("Parse Error!").arg(InputFile)); } else { if (eagleParser.SelectLayers()) { eagleParser.InitParser(Parser); OutputDeviceQPicture Picture; //Interpreter.Run(Picture, Parser); //AutoLeveler.setOutputDevice(Picture); //if (!Interpreter.Run(AutoLeveler, Parser)) { if (!Interpreter.Run(Picture, Parser)) { PictureItem.clear(); ui->minX->setText(QString("")); ui->maxX->setText(QString("")); ui->minY->setText(QString("")); ui->maxY->setText(QString("")); ui->execGroup->setEnabled(false); QMessageBox::warning(this, tr("Fatal!"), tr("Eagle Interpreter produced illegal GCode!")); } else { PictureItem.setPicture(Picture.picture()); ui->minX->setText(QString("%1").arg(Picture.minX)); ui->minY->setText(QString("%1").arg(Picture.minY)); ui->maxX->setText(QString("%1").arg(Picture.maxX)); ui->maxY->setText(QString("%1").arg(Picture.maxY)); ui->graphicsView->scene()->invalidate(); ui->graphicsView->fitInView(&PictureItem, Qt::KeepAspectRatio); ui->graphicsView->scene()->invalidate(); ui->execGroup->setEnabled(true); } } } } else { QMessageBox::warning(this, tr("HPGL Input File"), tr("Could not open file %1!").arg(InputFile)); } } } void MainWindow::on_ManualHome_clicked() { Grbl.goHome(); } void MainWindow::on_ManualXMinus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["X"]=QString("%1").arg(-ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualXPlus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["X"]=QString("%1").arg(ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualYMinus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["Y"]=QString("%1").arg(-ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualYPlus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["Y"]=QString("%1").arg(ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualZPlus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["Z"]=QString("%1").arg(ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualZMinus_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; //to Relative Cmd.Cmd=91; Grbl.Run(Cmd); Cmd.Cmd=0; Cmd.Parameters["Z"]=QString("%1").arg(-ui->ManualStep->value()); Grbl.Run(Cmd); Cmd.Parameters.clear(); //back to Absolute Cmd.Cmd=90; Grbl.Run(Cmd); } void MainWindow::on_ManualDefineOrigin_clicked() { GCode Cmd; Cmd.Code=CodeType_GCode; Cmd.Cmd=10; Cmd.Parameters["L"]=QString("20"); Cmd.Parameters["P"]=QString("1"); Cmd.Parameters["X"]=QString("0"); Cmd.Parameters["Y"]=QString("0"); Cmd.Parameters["Z"]=QString("0"); Grbl.Run(Cmd); }