diff --git a/testclient/mainwindow.h b/testclient/mainwindow.h --- a/testclient/mainwindow.h +++ b/testclient/mainwindow.h @@ -65,7 +65,12 @@ private slots: //ButtonEvents - + /** + * @brief axisControlClicked Used to catch the axis control. + * @param axis The Axis clicked on (X |Y |Z) + * @param value Distance Value + */ + void axisControlClicked(QChar axis, int value); /** * @brief the printing progress has changed * @param progress: the new progress @@ -138,11 +143,6 @@ void printerSpeedPBClicked(); /** - * @brief movementModeChanged The radio buttons for absoulte/relative have been toggled - */ - void movementModeChanged(const bool &checked); - - /** * @brief lowRatePB has been clicked */ void flowRatePBClicked(); diff --git a/testclient/mainwindow.cpp b/testclient/mainwindow.cpp --- a/testclient/mainwindow.cpp +++ b/testclient/mainwindow.cpp @@ -30,6 +30,7 @@ #include "mainwindow.h" #include "seriallayer.h" #include "gcodecommands.h" +#include "widgets/axiscontrol.h" Q_LOGGING_CATEGORY(TESTCLIENT_MAINWINDOW, "org.kde.atelier.core"); @@ -51,6 +52,9 @@ ui->pluginCB->addItem(tr("Autodetect")); ui->pluginCB->addItems(core->availablePlugins()); + AxisControl *axisControl = new AxisControl; + ui->moveDockContents->layout()->addWidget(axisControl); + addLog(tr("Attempting to locate Serial Ports")); populateCBs(); @@ -87,13 +91,13 @@ connect(ui->printPB, &QPushButton::clicked, this, &MainWindow::printPBClicked); connect(ui->printerSpeedPB, &QPushButton::clicked, this, &MainWindow::printerSpeedPBClicked); connect(ui->flowRatePB, &QPushButton::clicked, this, &MainWindow::flowRatePBClicked); - connect(ui->absoluteRB, &QRadioButton::toggled, this, &MainWindow::movementModeChanged); connect(ui->showMessagePB, &QPushButton::clicked, this, &MainWindow::showMessage); connect(ui->pluginCB, &QComboBox::currentTextChanged, this, &MainWindow::pluginCBChanged); connect(core, &AtCore::stateChanged, this, &MainWindow::printerStateChanged); connect(this, &MainWindow::printFile, core, &AtCore::print); connect(ui->stopPB, &QPushButton::clicked, core, &AtCore::stop); connect(ui->emergencyStopPB, &QPushButton::clicked, core, &AtCore::emergencyStop); + connect(axisControl, &AxisControl::clicked, this, &MainWindow::axisControlClicked); //We love solid, but we need a release :/ QTimer *timer = new QTimer(); @@ -449,16 +453,6 @@ core->setPrinterSpeed(ui->printerSpeedSB->value()); } -void MainWindow::movementModeChanged(const bool &checked) -{ - if (checked) { - core->setAbsolutePosition(); - ui->mvAxisSB->setMinimum(0); - } else { - core->setRelativePosition(); - ui->mvAxisSB->setMinimum(-200); - } -} void MainWindow::printerStateChanged(PrinterState state) { QString stateString; @@ -574,4 +568,9 @@ ui->printDock->setTitleBarWidget(new QWidget()); } } - +void MainWindow::axisControlClicked(QChar axis, int value) +{ + core->setRelativePosition(); + core->pushCommand(GCode::toCommand(GCode::G1, QStringLiteral("%1%2").arg(axis, QString::number(value)))); + core->setAbsolutePosition(); +} diff --git a/testclient/mainwindow.ui b/testclient/mainwindow.ui --- a/testclient/mainwindow.ui +++ b/testclient/mainwindow.ui @@ -637,7 +637,7 @@ 1 - + @@ -744,34 +744,6 @@ - - - - - - Axis move - - - - - - - Absol&ute - - - true - - - - - - - Relati&ve - - - - - diff --git a/testclient/widgets/CMakeLists.txt b/testclient/widgets/CMakeLists.txt --- a/testclient/widgets/CMakeLists.txt +++ b/testclient/widgets/CMakeLists.txt @@ -1,5 +1,6 @@ set(widgets_SRCS plotwidget.cpp + axiscontrol.cpp ) add_library(AtCoreTestWidgets STATIC ${widgets_SRCS}) diff --git a/testclient/widgets/axiscontrol.h b/testclient/widgets/axiscontrol.h new file mode 100644 --- /dev/null +++ b/testclient/widgets/axiscontrol.h @@ -0,0 +1,84 @@ +/* Atelier KDE Printer Host for 3D Printing + Copyright (C) <2016> + Author: Lays Rodrigues - laysrodriguessilva@gmail.com + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#pragma once + +#include +#include +#include + +/* Usage: + * + * Create a instance of PrinterHotendPositionVisualController and + * connect the clicked signal, it will give you the axis and value + * that was clicked. + */ + +class PieButton : public QObject, public QGraphicsEllipseItem +{ + Q_OBJECT +public: + PieButton(QLatin1Char axis, int value, int size, int angle); + void setPalette(QPalette palette); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *); + void hoverEnterEvent(QGraphicsSceneHoverEvent *); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *); +signals: + void clicked(QLatin1Char axis, int value); +private: + QLatin1Char _axis; + int _value; + QPalette _palette; +}; + +class RectButton : public QObject, public QGraphicsRectItem +{ + Q_OBJECT + +public: + RectButton(QLatin1Char axis, int value, int size); + void setPalette(QPalette palette); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *); + void hoverEnterEvent(QGraphicsSceneHoverEvent *); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *); +signals: + void clicked(QLatin1Char axis, int value); +private: + QLatin1Char _axis; + int _value; + QPalette _palette; +}; + +class AxisControl : public QGraphicsView +{ + Q_OBJECT + +public: + explicit AxisControl(QWidget *parent = 0); + +private: + void setLabels(QGraphicsItem *item, QLatin1Char axis, int value); + +protected: + void resizeEvent(QResizeEvent *); + +signals: + void clicked(QLatin1Char axis, int value); + +}; diff --git a/testclient/widgets/axiscontrol.cpp b/testclient/widgets/axiscontrol.cpp new file mode 100644 --- /dev/null +++ b/testclient/widgets/axiscontrol.cpp @@ -0,0 +1,187 @@ +/* Atelier KDE Printer Host for 3D Printing + Copyright (C) <2016> + Author: Lays Rodrigues - laysrodriguessilva@gmail.com + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "axiscontrol.h" + +PieButton::PieButton(QLatin1Char axis, int value, int size, int angle) : _axis(axis), _value(value) +{ + const int delta = 16; // Qt Docs: angle is 16th of a degree. + setBrush(_palette.button()); + setStartAngle(angle * delta); + setSpanAngle(90 * delta); + setRect(QRect(QPoint(size * -1, size * -1), QPoint(size, size))); + setZValue(size * -1); + setAcceptHoverEvents(true); + setToolTip(QStringLiteral("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); +} + +void PieButton::setPalette(QPalette palette) +{ + _palette = palette; +} + +void PieButton::mousePressEvent(QGraphicsSceneMouseEvent *) +{ + emit clicked(_axis, _value); +} + +void PieButton::hoverEnterEvent(QGraphicsSceneHoverEvent *) +{ + setBrush(_palette.highlight()); +} + +void PieButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) +{ + setBrush(_palette.button()); +} + +RectButton::RectButton(QLatin1Char axis, int value, int size) : _axis(axis), _value(value) +{ + setBrush(_palette.button()); + setRect(QRect(QPoint(0, 0), QPoint(size, size))); + setAcceptHoverEvents(true); + setZValue(size * -1); + setToolTip(QStringLiteral("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); +} + +void RectButton::setPalette(QPalette palette) +{ + _palette = palette; +} + +void RectButton::mousePressEvent(QGraphicsSceneMouseEvent *) +{ + emit clicked(_axis, _value); +} + +void RectButton::hoverEnterEvent(QGraphicsSceneHoverEvent *) +{ + setBrush(_palette.highlight()); +} + +void RectButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) +{ + setBrush(_palette.button()); +} +/* About the Magic Numbers + I don't have experience programming with QGraphicsScene, + Tomaz is helping me, but until we have a better solution, all the values + that are dividing or multiplying the items is based only in tests and errors. + Those values was choosen because it fit better on the alignment of the items + in the scene. If you have a better solution, please share with us. + Lays Rodrigues - Jan/2017 +*/ +AxisControl::AxisControl(QWidget *parent) : + QGraphicsView(parent) +{ + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); + + setScene(new QGraphicsScene()); + + auto createPie = [ = ](QLatin1Char axis, int value, int size, int angle) { + auto pie = new PieButton(axis, value, size, angle); + pie->setPalette(this->palette()); + connect(pie, &PieButton::clicked, this, &AxisControl::clicked); + if (value == 25 || value == -25) { + setLabels(pie, axis, value); + } + scene()->addItem(pie); + }; + + auto createRect = [ = ](QLatin1Char axis, int value, int size, int xPos, int yPos) { + auto z = new RectButton(axis, value, size); + z->setPalette(this->palette()); + z->setPos(xPos, yPos); + connect(z, &RectButton::clicked, this, &AxisControl::clicked); + if (value == 25 || value == -25) { + setLabels(z, axis, value); + } + scene()->addItem(z); + }; + + int currPieSize = 25; + for (auto value : { + 1, 10, 25 + }) { + createPie(QLatin1Char('X'), value, currPieSize, -45); // Left + createPie(QLatin1Char('X'), value * -1, currPieSize, 135); // Right + createPie(QLatin1Char('Y'), value, currPieSize, 45); // Top + createPie(QLatin1Char('Y'), value * -1, currPieSize, 225); // Bottom + currPieSize += 25; + } + + int currZSize = 25; + int xPos = sceneRect().width() - 50; + int yPos = -75; //Align with the origin of the scene 3 * 25 + for (auto value : { + 25, 10, 1 + }) { + createRect(QLatin1Char('Z'), value, currZSize, xPos, yPos); + yPos += currZSize; + } + for (auto value : { + -1, -10, -25 + }) { + createRect(QLatin1Char('Z'), value, currZSize, xPos, yPos); + yPos += currZSize; + } + setSceneRect(scene()->itemsBoundingRect()); +} + +void AxisControl::resizeEvent(QResizeEvent *) +{ + fitInView(sceneRect(), Qt::KeepAspectRatio); +} + +void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char axis, int value) +{ + auto *lb = new QGraphicsSimpleTextItem(); + + lb->setBrush(palette().buttonText()); + + if (this->logicalDpiX() <= 96) { + lb->setText((value < 0) ? QStringLiteral(" -") + axis : QStringLiteral(" ") + axis); + } else { + lb->setText((value < 0) ? QStringLiteral("-") + axis : QStringLiteral(" ") + axis); + } + + if (axis.toLatin1() == 'X') { + lb->setY(item->y() - lb->boundingRect().width()); + if (value < 0) { + lb->setX(item->x() - item->boundingRect().width() / 1.2 - lb->boundingRect().width() / 2); + } else { + lb->setX(item->x() + item->boundingRect().width() / 1.2 - lb->boundingRect().width() / 2); + } + } else if (axis.toLatin1() == 'Y') { + lb->setX(item->x() - lb->boundingRect().width() / 2); + if (value < 0) { + lb->setY(item->y() + item->boundingRect().height() / 1.5); + } else { + lb->setY(item->y() - item->boundingRect().height()); + } + } else { + + if (value < 0) { + lb->setX(item->x() + lb->boundingRect().width() / fontMetrics().width(lb->text())); + lb->setY(item->y() - lb->boundingRect().height() / fontMetrics().xHeight()); + } else { + lb->setX(item->x() + lb->boundingRect().width() / fontMetrics().width(lb->text())); + lb->setY(item->y() - lb->boundingRect().height() / fontMetrics().xHeight()); + } + } + scene()->addItem(lb); +}