Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 21 additions & 105 deletions mainwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
#include <QDesktopServices>
#include <QFileDialog>
#include <QDebug>

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

#include <QStandardPaths>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
Expand Down Expand Up @@ -44,22 +40,17 @@ MainWindow::MainWindow(QWidget *parent) :
connect( g2m, SIGNAL( signalError(QString) ), ui->stderror, SLOT( setPlainText(QString) ) );

connect(ui->gcode, SIGNAL(textChanged()), this, SLOT(changedGcode()));
connect(ui->gcode, SIGNAL(cursorPositionChanged()), this, SLOT(onChangedPosition()));
connect(ui->command, SIGNAL(textChanged()), this, SLOT(changedCommand()));

connect(ui->action_AutoZoom, SIGNAL(triggered()), this, SLOT(toggleAutoZoom()));

//connect(ui->command, SIGNAL(keyPressed(QKeyEvent *)), view, SLOT(keyPressEvent(QKeyEvent *)));

connect(ui->action_Donate, SIGNAL(triggered(bool)), this, SLOT(helpDonate()));
connect(ui->action_Issues, SIGNAL(triggered(bool)), this, SLOT(helpIssues()));
connect(ui->action_Chat, SIGNAL(triggered(bool)), this, SLOT(helpChat()));

// qt does not understand ~/ so path must be absolute or it will create ~/ on PWD
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;

home_dir = homedir;
home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, QString(), QStandardPaths::LocateDirectory);
openFile = "";

loadSettings();
Expand All @@ -76,24 +67,30 @@ void MainWindow::toggleAutoZoom() {
view->setAutoZoom(ui->action_AutoZoom->isChecked());
}

void MainWindow::showFullScreen()
{
if( ui->action_showFullScreen->isChecked() )
showMaximized();
else
showNormal();
}

void MainWindow::changedCommand()
{
QString str;

openFile = "";
bBigFile = bMoreBig = bFileMode = bFirstCallDone = false;
fileSize = filePos = 0;
bFileMode = false;
connect(ui->gcode, SIGNAL(textChanged()), this, SLOT(changedGcode()));
disconnect(ui->gcode, SIGNAL(cursorPositionChanged()), this, SLOT(onChangedPosition()));
str = "GCoder :- ";
setWindowTitle(str);
parseCommand();
}

void MainWindow::changedGcode() {

if(bFileMode)
return;
if(bFileMode)
return;

if (ui->gcode->toPlainText().isEmpty())
{
Expand Down Expand Up @@ -172,7 +169,8 @@ void MainWindow::loadSettings()

if (settings->value("maximized", isMaximized() ).toBool())
showMaximized();

ui->action_showFullScreen->setChecked(settings->value("maximized", isMaximized() ).toBool());

view->setAutoZoom(settings->value("autoZoom", view->autoZoom()).toBool());
ui->action_AutoZoom->setChecked(settings->value("autoZoom", view->autoZoom()).toBool());

Expand Down Expand Up @@ -224,7 +222,7 @@ void MainWindow::saveSettings() {

void MainWindow::closeEvent(QCloseEvent * event)
{
qDebug() << "MainWindow::closeEvent";
// qDebug() << "MainWindow::closeEvent";
saveSettings();
// ui.viewer->close();

Expand All @@ -239,7 +237,7 @@ void MainWindow::closeEvent(QCloseEvent * event)
void MainWindow::onOpenFile()
{
QString filename;
QString path = home_dir + "/machinekit";
QString path = home_dir + "machinekit";

filename = QFileDialog::getOpenFileName(this, tr("Open GCode"), path, tr("GCode Files (*.ngc *.nc);; All files (*.*)"));
if(filename.length())
Expand Down Expand Up @@ -286,35 +284,21 @@ QString str;

if (file.open(QFile::ReadOnly | QFile::Text))
{
fileSize = file.size();
str = "Loading file " + filename;
ui->statusbar->showMessage(str, 5000);
ui->gcode->clear();
ui->gcode->clear();

QTextStream ts(&file);

while( (!ts.atEnd()) && (ts.pos() < CHUNK_SIZE) )
while( !ts.atEnd())
{
str = ts.readLine();
if(str.length())
if(str.length()) // don't want blank lines
{
str = str + "\n";
ui->gcode->appendNewPlainText(str);
}
}

filePos = ts.pos();
if(file.size() > MAX_SIZE)
bBigFile = true;

if(fileSize >= filePos)
bMoreBig = true;
else
{
bBigFile = bMoreBig = false;
fileSize = filePos = 0;
}

file.close();

str = "GCoder :- " + filename;
Expand All @@ -324,8 +308,6 @@ QString str;

openFile = filename;
bFileMode = true;
if(bMoreBig)
connect(ui->gcode, SIGNAL(cursorPositionChanged()), this, SLOT(onChangedPosition()));
}
else
{
Expand All @@ -334,78 +316,13 @@ QString str;
}
}

void MainWindow::onChangedPosition()
{
if(!bBigFile || !bMoreBig)
return;

if( (ui->gcode->textCursor().blockNumber() + 1) > ((ui->gcode->document()->blockCount()) - 100)
&& bFirstCallDone)
reOpenInBrowser();
else
bFirstCallDone = true;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// called when further chunks of a large file are added to the text edit widget

int MainWindow::reOpenInBrowser()
{
QString str, str2;
QFile file(openFile);

if (file.open(QFile::ReadOnly | QFile::Text))
{
if(file.size() != fileSize)
{
str = "Error - file size has changed since last load - " + openFile ;
ui->statusbar->showMessage(str, 5000);
return -1;
}
QTextStream ts(&file);
ts.seek(filePos);

while( (!ts.atEnd()) && (ts.pos() < (filePos + ADD_SIZE) ) )
{
str = ts.readLine();
if(str.length())
{
str = str + "\n";
ui->gcode->appendNewPlainText(str);
}
}

filePos = ts.pos();
if(fileSize > filePos)
bMoreBig = true;
else
{
bBigFile = bMoreBig = false;
fileSize = filePos = 0;
disconnect(ui->gcode, SIGNAL(cursorPositionChanged()), this, SLOT(onChangedPosition()));
}
file.close();
return 0;
}
else
{
str = "Error reOpening file " + openFile;
ui->statusbar->showMessage(str, 10000);
return -1;
}
}



////////////////////////////////////////////////////////////////////////////////////////////////////

void MainWindow::onSaveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save G Code (As)"), openFile, tr("GCode Files (*.ngc *.nc);; All files (*.*)"));
saveInBrowser(fileName);
// bModified = false;
}


Expand All @@ -421,7 +338,6 @@ QString str;
out << ui->gcode->toPlainText();
file.close();

//ui->gcode->setReadOnly(true);
str = "GCoder:- " + filename;
setWindowTitle(str);
return 0;
Expand Down
41 changes: 16 additions & 25 deletions mainwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ namespace Ui {
class MainWindow;
}

// max file size of 25KB
#define MAX_SIZE 25000
// render initial 3K chunk
#define CHUNK_SIZE 3000
// add in 1K bits dynamically
#define ADD_SIZE 1000

class MainWindow : public QMainWindow
{
Q_OBJECT
Expand All @@ -41,40 +34,38 @@ class MainWindow : public QMainWindow
public slots:
// load the last command string from the settings
// used during startup
void loadSettingsCommand();

void changedGcode();
void changedCommand();
virtual void loadSettingsCommand();

void onOpenFile();
void onSaveAs();
int onSettings();
void onChangedPosition();
virtual void changedGcode();
virtual void changedCommand();

void appendCanonLine(g2m::canonLine*);
virtual void onOpenFile();
virtual void onSaveAs();
virtual int onSettings();

void toggleAutoZoom();
virtual void appendCanonLine(g2m::canonLine*);

void helpDonate();
void helpIssues();
void helpChat();
virtual void toggleAutoZoom();
virtual void showFullScreen();

virtual void helpDonate();
virtual void helpIssues();
virtual void helpChat();

protected:
void loadSettings();
void saveSettings();

private:
private: // functions
int openInViewer(QString filename);
void openInBrowser(QString filename);
int reOpenInBrowser();
int saveInBrowser(QString& filename);

void closeEvent(QCloseEvent *) Q_DECL_OVERRIDE;

private:
private: // data
QString home_dir, openFile;
int fileSize, filePos;
bool bMoreBig, bBigFile, bFileMode, bFirstCallDone;
bool bFileMode;

QString rs274;
QString tooltable;
Expand Down
21 changes: 3 additions & 18 deletions mainwin.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<property name="title">
<string>&amp;View</string>
</property>
<addaction name="action_showNormal"/>
<addaction name="action_showFullScreen"/>
</widget>
<widget class="QMenu" name="menu_Help">
Expand Down Expand Up @@ -179,7 +178,6 @@
</attribute>
<addaction name="separator"/>
<addaction name="action_showFullScreen"/>
<addaction name="action_showNormal"/>
<addaction name="separator"/>
<addaction name="action_AutoZoom"/>
</widget>
Expand Down Expand Up @@ -228,6 +226,9 @@
</property>
</action>
<action name="action_showFullScreen">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Fullscreen</string>
</property>
Expand Down Expand Up @@ -351,22 +352,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>action_showNormal</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>showNormal()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>299</y>
</hint>
</hints>
</connection>
<connection>
<sender>action_Open</sender>
<signal>triggered()</signal>
Expand Down
2 changes: 1 addition & 1 deletion settings_dlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QDir dir;
else if(buttonNumber == 2)
{
if(tooltable.isEmpty())
pathStr = home_dir + "/machinekit/configs";
pathStr = home_dir + "machinekit/configs";
else
pathStr = dir.absoluteFilePath(tooltable);
}
Expand Down