Rev 363 | Rev 372 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "PasteController.h"
PasteController::PasteController(QWidget *parent) : QWidget(parent)
{
connected = false;
hotkeysEnabled = false;
xPos = yPos = zPos = 0.0;
X_Incr = X_Decr = Y_Incr = Y_Decr = Z_Incr = Z_Decr = E_Incr = false;
tmrUpdate = new QTimer(this);
connect(tmrUpdate, SIGNAL(timeout()), this, SLOT(UpdatePosition()));
QString helpString = "";
helpString.append("XY Control: Arrow Keys or H/J/K/L\n");
helpString.append("Z Control: PgUp/PgDown\n");
helpString.append("Extruder Control: Space");
labelHelp = new QLabel(helpString);
QVBoxLayout *helpLayout = new QVBoxLayout();
helpLayout->addWidget(labelHelp);
QGroupBox *helpBox = new QGroupBox("Hotkeys:");
helpBox->setLayout(helpLayout);
btnEnableHotkeys = new QPushButton("&Enable Hotkeys");
connect(btnEnableHotkeys, SIGNAL(clicked()), this, SLOT(ToggleHotkeys()));
btnInit = new QPushButton("&Initialize Printer");
btnHome = new QPushButton("&Home Printhead");
connect(btnHome, SIGNAL(clicked()), this, SLOT(ResetPosition()));
QVBoxLayout *miscBtnLayout = new QVBoxLayout();
miscBtnLayout->addWidget(btnEnableHotkeys);
miscBtnLayout->addWidget(btnInit);
miscBtnLayout->addWidget(btnHome);
QGroupBox *miscBtnBox = new QGroupBox("Preset Commands");
miscBtnBox->setLayout(miscBtnLayout);
miscBtnBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
// Icons from http://ikons.piotrkwiatkowski.co.uk/index.html
QIcon iconUp(":/External/Resources/arrow_up.png");
QIcon iconDown(":/External/Resources/arrow_down.png");
QIcon iconLeft(":/External/Resources/arrow_left.png");
QIcon iconRight(":/External/Resources/arrow_right.png");
QIcon iconAdd(":/External/Resources/upload.png");
QIcon iconRemove(":/External/Resources/download.png");
QIcon iconDelete(":/External/Resources/close.png");
btnForward = new QToolButton();
btnForward->setIcon(iconUp);
btnForward->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnForward->setAutoRaise(true);
connect(btnForward, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnBackward = new QToolButton();
btnBackward->setIcon(iconDown);
btnBackward->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnBackward->setAutoRaise(true);
connect(btnBackward, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnLeft = new QToolButton();
btnLeft->setIcon(iconLeft);
btnLeft->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnLeft->setAutoRaise(true);
connect(btnLeft, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnRight = new QToolButton();
btnRight->setIcon(iconRight);
btnRight->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnRight->setAutoRaise(true);
connect(btnRight, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnUp = new QToolButton();
btnUp->setIcon(iconAdd);
btnUp->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnUp->setAutoRaise(true);
connect(btnUp, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnDown = new QToolButton();
btnDown->setIcon(iconRemove);
btnDown->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnDown->setAutoRaise(true);
connect(btnDown, SIGNAL(clicked()), this, SLOT(UpdatePositionSingle()));
btnExtrude = new QToolButton();
btnExtrude->setIcon(iconDelete);
btnExtrude->setIconSize(QSize(BTN_ICON_SIZE,BTN_ICON_SIZE));
btnExtrude->setAutoRaise(true);
QGridLayout *btnXYELayout = new QGridLayout();
btnXYELayout->addWidget(btnForward, 0, 1, 1, 1);
btnXYELayout->addWidget(btnLeft, 1, 0, 1, 1);
btnXYELayout->addWidget(btnExtrude, 1, 1, 1, 1);
btnXYELayout->addWidget(btnRight, 1, 2, 1, 1);
btnXYELayout->addWidget(btnBackward, 2, 1, 1, 1);
QVBoxLayout *btnZLayout = new QVBoxLayout();
btnZLayout->addWidget(btnUp);
btnZLayout->addWidget(btnDown);
QHBoxLayout *btnNavLayout = new QHBoxLayout();
btnNavLayout->addLayout(btnXYELayout);
btnNavLayout->addLayout(btnZLayout);
QGroupBox *controlBox = new QGroupBox("Manual Printer Control");
controlBox->setLayout(btnNavLayout);
controlBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
labelXPos = new QLabel("X :");
labelXPos->setFont(QFont("", 14, QFont::Normal));
labelYPos = new QLabel("Y :");
labelYPos->setFont(QFont("", 14, QFont::Normal));
labelZPos = new QLabel("Z :");
labelZPos->setFont(QFont("", 14, QFont::Normal));
textXValue = new QLineEdit();
textXValue->setReadOnly(true);
textXValue->setMaximumWidth(60);
textXValue->setFont(QFont("", 10, QFont::Bold));
textXValue->setAlignment(Qt::AlignCenter);
textYValue = new QLineEdit();
textYValue->setReadOnly(true);
textYValue->setMaximumWidth(60);
textYValue->setFont(QFont("", 10, QFont::Bold));
textYValue->setAlignment(Qt::AlignCenter);
textZValue = new QLineEdit();
textZValue->setReadOnly(true);
textZValue->setMaximumWidth(60);
textZValue->setFont(QFont("", 10, QFont::Bold));
textZValue->setAlignment(Qt::AlignCenter);
UpdatePosition();
QHBoxLayout *posValueLayout = new QHBoxLayout();
posValueLayout->addWidget(labelXPos);
posValueLayout->addWidget(textXValue);
posValueLayout->addWidget(labelYPos);
posValueLayout->addWidget(textYValue);
posValueLayout->addWidget(labelZPos);
posValueLayout->addWidget(textZValue);
QGroupBox *toolheadPosBox = new QGroupBox("Toolhead Position");
toolheadPosBox->setLayout(posValueLayout);
toolheadPosBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
labelXYSteps = new QLabel("XY Axis Step (mm):");
sliderXYSteps = new QSlider(Qt::Horizontal);
textXYSteps = new QDoubleSpinBox();
textXYSteps->setMinimumWidth(60);
labelXYSpeed = new QLabel("XY Axis Speed (mm/s):");
sliderXYSpeed = new QSlider(Qt::Horizontal);
textXYSpeed = new QSpinBox();
textXYSpeed->setMinimumWidth(60);
labelZSteps = new QLabel("Z Axis Step (mm):");
sliderZSteps = new QSlider(Qt::Horizontal);
textZSteps = new QDoubleSpinBox();
textZSteps->setMinimumWidth(60);
labelZSpeed = new QLabel("Z Axis Speed (mm/s):");
sliderZSpeed = new QSlider(Qt::Horizontal);
textZSpeed = new QSpinBox();
textZSpeed->setMinimumWidth(60);
labelRepeatDelay = new QLabel("Repeat Delay (ms):");
sliderRepeatDelay = new QSlider(Qt::Horizontal);
textRepeatDelay = new QSpinBox();
textRepeatDelay->setMinimumWidth(60);
labelEForwardSteps = new QLabel("Extrude Step (mm):");
sliderEForwardSteps = new QSlider(Qt::Horizontal);
textEForwardSteps = new QSpinBox();
textEForwardSteps->setMinimumWidth(60);
labelEForwardSpeed = new QLabel("Extrude Speed (mm/s):");
sliderEForwardSpeed = new QSlider(Qt::Horizontal);
textEForwardSpeed = new QSpinBox();
textEForwardSpeed->setMinimumWidth(60);
labelEBackwardSteps = new QLabel("Retract Step (mm):");
sliderEBackwardSteps = new QSlider(Qt::Horizontal);
textEBackwardSteps = new QSpinBox();
textEBackwardSteps->setMinimumWidth(60);
labelEBackwardSpeed = new QLabel("Retract Speed: (mm/s)");
sliderEBackwardSpeed = new QSlider(Qt::Horizontal);
textEBackwardSpeed = new QSpinBox();
textEBackwardSpeed->setMinimumWidth(60);
labelEZSteps = new QLabel("Z Axis Step (mm):");
sliderEZSteps = new QSlider(Qt::Horizontal);
textEZSteps = new QDoubleSpinBox();
textEZSteps->setMinimumWidth(60);
connect(sliderXYSteps, SIGNAL(valueChanged(int)), this, SLOT(QSliderToQDoubleSpinBox(int)));
connect(textXYSteps, SIGNAL(valueChanged(double)), this, SLOT(QDoubleSpinBoxToQSlider(double)));
sliderXYSteps->setRange(0, 5000);
textXYSteps->setRange(0,50);
textXYSteps->setSingleStep(0.1);
textXYSteps->setValue(10);
connect(sliderXYSpeed, SIGNAL(valueChanged(int)), textXYSpeed, SLOT(setValue(int)));
connect(textXYSpeed, SIGNAL(valueChanged(int)), sliderXYSpeed, SLOT(setValue(int)));
sliderXYSpeed->setRange(1, 10000);
textXYSpeed->setRange(1, 10000);
textXYSpeed->setValue(4000);
connect(sliderZSteps, SIGNAL(valueChanged(int)), this, SLOT(QSliderToQDoubleSpinBox(int)));
connect(textZSteps, SIGNAL(valueChanged(double)), this, SLOT(QDoubleSpinBoxToQSlider(double)));
sliderZSteps->setRange(0, 5000);
textZSteps->setRange(0, 50);
textZSteps->setSingleStep(0.1);
textZSteps->setValue(10);
connect(sliderZSpeed, SIGNAL(valueChanged(int)), textZSpeed, SLOT(setValue(int)));
connect(textZSpeed, SIGNAL(valueChanged(int)), sliderZSpeed, SLOT(setValue(int)));
sliderZSpeed->setRange(1, 10000);
textZSpeed->setRange(1, 10000);
textZSpeed->setValue(1000);
connect(sliderRepeatDelay, SIGNAL(valueChanged(int)), textRepeatDelay, SLOT(setValue(int)));
connect(textRepeatDelay, SIGNAL(valueChanged(int)), sliderRepeatDelay, SLOT(setValue(int)));
connect(textRepeatDelay, SIGNAL(valueChanged(int)), this, SLOT(UpdateTimer(int)));
sliderRepeatDelay->setRange(20, 500);
textRepeatDelay->setRange(20, 500);
textRepeatDelay->setValue(100);
QGridLayout *xyRepeatLayout = new QGridLayout();
xyRepeatLayout->addWidget(labelXYSteps, 0, 0);
xyRepeatLayout->addWidget(sliderXYSteps, 0, 1);
xyRepeatLayout->addWidget(textXYSteps, 0, 2);
xyRepeatLayout->addWidget(labelXYSpeed, 1, 0);
xyRepeatLayout->addWidget(sliderXYSpeed, 1, 1);
xyRepeatLayout->addWidget(textXYSpeed, 1, 2);
xyRepeatLayout->addWidget(labelZSteps, 2, 0);
xyRepeatLayout->addWidget(sliderZSteps, 2, 1);
xyRepeatLayout->addWidget(textZSteps, 2, 2);
xyRepeatLayout->addWidget(labelZSpeed, 3, 0);
xyRepeatLayout->addWidget(sliderZSpeed, 3, 1);
xyRepeatLayout->addWidget(textZSpeed, 3, 2);
xyRepeatLayout->addWidget(labelRepeatDelay, 4, 0);
xyRepeatLayout->addWidget(sliderRepeatDelay, 4, 1);
xyRepeatLayout->addWidget(textRepeatDelay, 4, 2);
QGroupBox *xyRepeatBox = new QGroupBox("Movement Settings");
xyRepeatBox->setLayout(xyRepeatLayout);
connect(sliderEForwardSteps, SIGNAL(valueChanged(int)), textEForwardSteps, SLOT(setValue(int)));
connect(textEForwardSteps, SIGNAL(valueChanged(int)), sliderEForwardSteps, SLOT(setValue(int)));
sliderEForwardSteps->setRange(1, 10000);
textEForwardSteps->setRange(1, 10000);
textEForwardSteps->setValue(4000);
connect(sliderEForwardSpeed, SIGNAL(valueChanged(int)), textEForwardSpeed, SLOT(setValue(int)));
connect(textEForwardSpeed, SIGNAL(valueChanged(int)), sliderEForwardSpeed, SLOT(setValue(int)));
sliderEForwardSpeed->setRange(1, 10000);
textEForwardSpeed->setRange(1, 10000);
textEForwardSpeed->setValue(1000);
connect(sliderEBackwardSteps, SIGNAL(valueChanged(int)), textEBackwardSteps, SLOT(setValue(int)));
connect(textEBackwardSteps, SIGNAL(valueChanged(int)), sliderEBackwardSteps, SLOT(setValue(int)));
sliderEBackwardSteps->setRange(1, 10000);
textEBackwardSteps->setRange(1, 10000);
textEBackwardSteps->setValue(2000);
connect(sliderEBackwardSpeed, SIGNAL(valueChanged(int)), textEBackwardSpeed, SLOT(setValue(int)));
connect(textEBackwardSpeed, SIGNAL(valueChanged(int)), sliderEBackwardSpeed, SLOT(setValue(int)));
sliderEBackwardSpeed->setRange(1, 10000);
textEBackwardSpeed->setRange(1, 10000);
textEBackwardSpeed->setValue(2000);
connect(sliderEZSteps, SIGNAL(valueChanged(int)), this, SLOT(QSliderToQDoubleSpinBox(int)));
connect(textEZSteps, SIGNAL(valueChanged(double)), this, SLOT(QDoubleSpinBoxToQSlider(double)));
sliderEZSteps->setRange(0, 1000);
textEZSteps->setRange(0, 10);
textEZSteps->setSingleStep(0.1);
textEZSteps->setValue(2);
QGridLayout *extruderLayout = new QGridLayout();
extruderLayout->addWidget(labelEForwardSteps, 0, 0);
extruderLayout->addWidget(sliderEForwardSteps, 0, 1);
extruderLayout->addWidget(textEForwardSteps, 0, 2);
extruderLayout->addWidget(labelEForwardSpeed, 1, 0);
extruderLayout->addWidget(sliderEForwardSpeed, 1, 1);
extruderLayout->addWidget(textEForwardSpeed, 1, 2);
extruderLayout->addWidget(labelEBackwardSteps, 2, 0);
extruderLayout->addWidget(sliderEBackwardSteps, 2, 1);
extruderLayout->addWidget(textEBackwardSteps, 2, 2);
extruderLayout->addWidget(labelEBackwardSpeed, 4, 0);
extruderLayout->addWidget(sliderEBackwardSpeed, 4, 1);
extruderLayout->addWidget(textEBackwardSpeed, 4, 2);
extruderLayout->addWidget(labelEZSteps, 5, 0);
extruderLayout->addWidget(sliderEZSteps, 5, 1);
extruderLayout->addWidget(textEZSteps, 5, 2);
QGroupBox *extruderBox = new QGroupBox("Extruder Settings");
extruderBox->setLayout(extruderLayout);
QVBoxLayout *column1Layout = new QVBoxLayout();
column1Layout->addWidget(controlBox, 0, Qt::AlignTop);
column1Layout->addWidget(miscBtnBox, 0, Qt::AlignTop);
column1Layout->addWidget(helpBox, 1, Qt::AlignTop);
QVBoxLayout *column2Layout = new QVBoxLayout();
column2Layout->addWidget(toolheadPosBox);
column2Layout->addWidget(xyRepeatBox);
column2Layout->addWidget(extruderBox);
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addLayout(column1Layout);
mainLayout->addLayout(column2Layout);
setLayout(mainLayout);
setFixedSize(sizeHint());
}
PasteController::~PasteController()
{
}
void PasteController::EnableTransmit(bool en) {
connected = en;
}
void PasteController::ToggleHotkeys() {
if (hotkeysEnabled) {
qApp->removeEventFilter(this);
btnEnableHotkeys->setText("&Enable Hotkeys");
} else {
qApp->installEventFilter(this);
btnEnableHotkeys->setText("&Disable Hotkeys");
}
hotkeysEnabled = !hotkeysEnabled;
}
void PasteController::UpdatePosition() {
if (X_Incr && !X_Decr) xPos += textXYSteps->value();
if (!X_Incr && X_Decr) xPos -= textXYSteps->value();
if (Y_Incr && !Y_Decr) yPos += textXYSteps->value();
if (!Y_Incr && Y_Decr) yPos -= textXYSteps->value();
if (Z_Incr && !Z_Decr) zPos += textZSteps->value();
if (!Z_Incr && Z_Decr) zPos -= textZSteps->value();
textXValue->setText(QString::number(xPos, 'f', 2));
textYValue->setText(QString::number(yPos, 'f', 2));
textZValue->setText(QString::number(zPos, 'f', 2));
}
void PasteController::UpdatePositionSingle() {
if (QObject::sender() == btnRight) xPos += textXYSteps->value();
if (QObject::sender() == btnLeft) xPos -= textXYSteps->value();
if (QObject::sender() == btnForward) yPos += textXYSteps->value();
if (QObject::sender() == btnBackward) yPos -= textXYSteps->value();
if (QObject::sender() == btnUp) zPos += textZSteps->value();
if (QObject::sender() == btnDown) zPos -= textZSteps->value();
textXValue->setText(QString::number(xPos, 'f', 2));
textYValue->setText(QString::number(yPos, 'f', 2));
textZValue->setText(QString::number(zPos, 'f', 2));
}
void PasteController::UpdateTimer(int delay) {
tmrUpdate->setInterval(delay);
}
void PasteController::QSliderToQDoubleSpinBox(int) {
textXYSteps->setValue((double)sliderXYSteps->value()/100);
textZSteps->setValue((double)sliderZSteps->value()/100);
textEZSteps->setValue((double)sliderEZSteps->value()/100);
}
void PasteController::QDoubleSpinBoxToQSlider(double) {
sliderXYSteps->setValue(textXYSteps->value()*100);
sliderZSteps->setValue(textZSteps->value()*100);
sliderEZSteps->setValue(textEZSteps->value()*100);
}
void PasteController::ResetPosition() {
xPos = yPos = zPos = 0;
UpdatePosition();
}
bool PasteController::eventFilter(QObject *obj, QEvent *event)
{
// Only process keyboard press and release events
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyevent = static_cast<QKeyEvent*>(event);
// QKeySequence seq = keyevent->modifiers() + keyevent->key();
if (hotkeysEnabled) {
if (keyevent->isAutoRepeat()) return true;
bool keychange = false;
if (keyevent->key() == Qt::Key_Left || keyevent->key() == Qt::Key_H) {
btnLeft->setDown(true);
X_Decr = keychange = true;
}
if (keyevent->key() == Qt::Key_Right || keyevent->key() == Qt::Key_L) {
btnRight->setDown(true);
X_Incr = keychange = true;
}
if (keyevent->key() == Qt::Key_Up || keyevent->key() == Qt::Key_K) {
btnForward->setDown(true);
Y_Incr = keychange = true;
}
if (keyevent->key() == Qt::Key_Down || keyevent->key() == Qt::Key_J) {
btnBackward->setDown(true);
Y_Decr = keychange = true;
}
if (keyevent->key() == Qt::Key_PageDown) {
btnDown->setDown(true);
Z_Decr = keychange = true;
}
if (keyevent->key() == Qt::Key_PageUp) {
btnUp->setDown(true);
Z_Incr = keychange = true;
}
if (keyevent->key() == Qt::Key_Space) {
btnExtrude->setDown(true);
E_Incr = keychange = true;
}
if (X_Incr || X_Decr || Y_Incr || Y_Decr || Z_Incr || Z_Decr || E_Incr) {
UpdatePosition();
tmrUpdate->start();
}
if (keychange) return true;
}
} else if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyevent = static_cast<QKeyEvent*>(event);
// QKeySequence seq = keyevent->modifiers() + keyevent->key();
if (hotkeysEnabled) {
if (keyevent->isAutoRepeat()) return true;
bool keychange = false;
if (keyevent->key() == Qt::Key_Left || keyevent->key() == Qt::Key_H) {
btnLeft->setDown(false);
X_Decr = keychange = false;
}
if (keyevent->key() == Qt::Key_Right || keyevent->key() == Qt::Key_L) {
btnRight->setDown(false);
X_Incr = keychange = false;
}
if (keyevent->key() == Qt::Key_Up || keyevent->key() == Qt::Key_K) {
btnForward->setDown(false);
Y_Incr = keychange = false;
}
if (keyevent->key() == Qt::Key_Down || keyevent->key() == Qt::Key_J) {
btnBackward->setDown(false);
Y_Decr = keychange = false;
}
if (keyevent->key() == Qt::Key_PageDown) {
btnDown->setDown(false);
Z_Decr = keychange = false;
}
if (keyevent->key() == Qt::Key_PageUp) {
btnUp->setDown(false);
Z_Incr = keychange = false;
}
if (keyevent->key() == Qt::Key_Space) {
btnExtrude->setDown(false);
E_Incr = keychange = false;
}
if (!X_Incr && !X_Decr && !Y_Incr && !Y_Decr && !Z_Incr && !Z_Decr && !E_Incr) {
tmrUpdate->stop();
}
if (keychange) return true;
}
}
return QWidget::eventFilter(obj, event);
}