diff --git a/src/core/atcore.h b/src/core/atcore.h --- a/src/core/atcore.h +++ b/src/core/atcore.h @@ -382,15 +382,15 @@ * @param arg the distance to move the axis or the place to move to depending on printer mode * @sa home(), home(uchar axis), move(QLatin1Char axis, int arg) */ - Q_INVOKABLE void move(AtCore::AXES axis, int arg); + Q_INVOKABLE void move(AtCore::AXES axis, double arg); /** * @brief move an axis of the printer * @param axis the axis to move AXES (X Y Z E ) * @param arg the distance to move the axis or the place to move to depending on printer mode * @sa home(), home(uchar axis), move(AtCore::AXES, int arg) */ - Q_INVOKABLE void move(QLatin1Char axis, int arg); + Q_INVOKABLE void move(QLatin1Char axis, double arg); /** * @brief Set the bed temperature diff --git a/src/core/atcore.cpp b/src/core/atcore.cpp --- a/src/core/atcore.cpp +++ b/src/core/atcore.cpp @@ -612,15 +612,15 @@ pushCommand(GCode::toCommand(GCode::M221, QString::number(speed))); } -void AtCore::move(AtCore::AXES axis, int arg) +void AtCore::move(AtCore::AXES axis, double arg) { const auto axisAsString = QMetaEnum::fromType().valueToKey(axis); move(QLatin1Char(axisAsString[0]), arg); } -void AtCore::move(QLatin1Char axis, int arg) +void AtCore::move(QLatin1Char axis, double arg) { - pushCommand(GCode::toCommand(GCode::G1, QStringLiteral("%1 %2").arg(axis).arg(QString::number(arg)))); + pushCommand(GCode::toCommand(GCode::G1, QStringLiteral("%1 %2").arg(axis).arg(QString::number(arg, 'g', 3)))); } int AtCore::extruderCount() const diff --git a/src/widgets/axiscontrol.h b/src/widgets/axiscontrol.h --- a/src/widgets/axiscontrol.h +++ b/src/widgets/axiscontrol.h @@ -28,36 +28,38 @@ { Q_OBJECT public: - PieButton(QLatin1Char &axis, int value, int size, int angle); + PieButton(QLatin1Char &axis, double 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); + void clicked(QLatin1Char axis, double value); private: QLatin1Char _axis; - int _value; + double _value; QPalette _palette; }; class ATCOREWIDGETS_EXPORT RectButton : public QObject, public QGraphicsRectItem { Q_OBJECT public: - RectButton(QLatin1Char &axis, int value, int size); + RectButton(QLatin1Char &axis, double value, int size); void setPalette(QPalette palette); + void setSelected(bool selected); protected: void mousePressEvent(QGraphicsSceneMouseEvent *); void hoverEnterEvent(QGraphicsSceneHoverEvent *); void hoverLeaveEvent(QGraphicsSceneHoverEvent *); signals: - void clicked(QLatin1Char axis, int value); + void clicked(QLatin1Char axis, double value); private: + bool _selected; QLatin1Char _axis; - int _value; + double _value; QPalette _palette; }; @@ -72,10 +74,14 @@ Q_OBJECT public: - explicit AxisControl(const QList &movementValues = {1, 10, 25}, QWidget *parent = nullptr); + explicit AxisControl(const QList &movementValues = {0.1, 1, 10}, QWidget *parent = nullptr); private: - void setLabels(QGraphicsItem *item, QLatin1Char &axis, int value); + void createScene(); + void setLabels(QGraphicsItem *item, QLatin1Char &axis, double value); + void setValues(const QList &movementValues); + QList _mValues; + double _setScale; protected: void resizeEvent(QResizeEvent *); @@ -86,6 +92,5 @@ * @param axis: Axis to move * @param value: Amount to move */ - void clicked(QLatin1Char axis, int value); - + void clicked(QLatin1Char axis, double value); }; diff --git a/src/widgets/axiscontrol.cpp b/src/widgets/axiscontrol.cpp --- a/src/widgets/axiscontrol.cpp +++ b/src/widgets/axiscontrol.cpp @@ -19,7 +19,7 @@ #include "axiscontrol.h" #include -PieButton::PieButton(QLatin1Char &axis, int value, int size, int angle) : _axis(axis), _value(value) +PieButton::PieButton(QLatin1Char &axis, double value, int size, int angle) : _axis(axis), _value(value) { const int delta = 16; // Qt Docs: angle is 16th of a degree. setBrush(_palette.button()); @@ -51,16 +51,23 @@ setBrush(_palette.button()); } -RectButton::RectButton(QLatin1Char &axis, int value, int size) : _axis(axis), _value(value) +RectButton::RectButton(QLatin1Char &axis, double value, int size) : _axis(axis), _value(value) { setBrush(_palette.button()); - setRect(QRect(QPoint(0, 0), QPoint(size, size))); + if (axis == QLatin1Char('*')) { + setRect(QRect(QPoint(0, 0), QPoint(size * 2, size))); + } else { + setRect(QRect(QPoint(0, 0), QPoint(size, size))); + } + setAcceptHoverEvents(true); setZValue(size * -1); - if (axis != QLatin1Char('E')) { - setToolTip(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); - } else { + if (axis == QLatin1Char('E')) { setToolTip(tr("Extrude %1 Units").arg(value)); + } else if (axis == QLatin1Char('*')) { + setToolTip(tr("Set Movement Scale to %1x Units").arg(value)); + } else { + setToolTip(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); } } @@ -76,12 +83,26 @@ void RectButton::hoverEnterEvent(QGraphicsSceneHoverEvent *) { - setBrush(_palette.highlight()); + if (!_selected) { + setBrush(_palette.highlight()); + } } void RectButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { - setBrush(_palette.button()); + if (!_selected) { + setBrush(_palette.button()); + } +} + +void RectButton::setSelected(bool selected) +{ + _selected = selected; + if (_selected) { + setBrush(_palette.highlight()); + } else { + setBrush(_palette.button()); + } } /* About the Magic Numbers I don't have experience programming with QGraphicsScene, @@ -91,36 +112,42 @@ in the scene. If you have a better solution, please share with us. Lays Rodrigues - Jan/2017 */ -AxisControl::AxisControl(const QList &movementValues, QWidget *parent) : +AxisControl::AxisControl(const QList &movementValues, QWidget *parent) : QGraphicsView(parent) { setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); + _mValues = movementValues; + _setScale = 1; + createScene(); +} +void AxisControl::createScene() +{ setScene(new QGraphicsScene()); - const int listSize = movementValues.size(); - int maxValue = *std::max_element(movementValues.begin(), movementValues.end()); - QList lessList = movementValues; + const int listSize = _mValues.size(); + double maxValue = *std::max_element(_mValues.begin(), _mValues.end()); + QList lessList = _mValues; std::sort(lessList.begin(), lessList.end(), std::less()); - QList greaterList = movementValues; - std::sort(greaterList.begin(), greaterList.end(), std::greater()); + QList greaterList = _mValues; + std::sort(greaterList.begin(), greaterList.end(), std::greater()); - auto createPie = [ this, maxValue ](QLatin1Char & axis, int value, int size, int angle) { + auto createPie = [ this, maxValue ](QLatin1Char & axis, double 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 (abs(value) == maxValue) { + if (QString::number(abs(value)) == QString::number(maxValue)) { setLabels(pie, axis, value); } scene()->addItem(pie); }; - auto createRect = [ this, maxValue ](QLatin1Char & axis, int value, int size, int xPos, int yPos) { + auto createRect = [ this, maxValue ](QLatin1Char & axis, double 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 (abs(value) == maxValue) { + if (QString::number(abs(value)) == QString::number(maxValue)) { setLabels(z, axis, value); } scene()->addItem(z); @@ -131,7 +158,8 @@ auto ychar = QLatin1Char('Y'); auto zchar = QLatin1Char('Z'); auto echar = QLatin1Char('E'); - for (const int &value : lessList) { + auto scaleChar = QLatin1Char('*'); + for (const double &value : lessList) { createPie(xchar, value, currPieSize, -45); // Left createPie(xchar, value * -1, currPieSize, 135); // Right createPie(ychar, value, currPieSize, 45); // Top @@ -144,13 +172,13 @@ int yPos = -(listSize * 25); //Align with the origin // Z+ - for (const int &value : greaterList) { + for (const double &value : greaterList) { createRect(zchar, value, currSize, xPos, yPos); yPos += currSize; } // Z- - for (const int &value : lessList) { + for (const double &value : lessList) { createRect(zchar, -value, currSize, xPos, yPos); yPos += currSize; } @@ -160,25 +188,52 @@ yPos = -(listSize * 25); //Align with the origin // E- - for (const int &value : greaterList) { + for (const double &value : greaterList) { createRect(echar, -value, currSize, xPos, yPos); yPos += currSize; } // E+ - for (const int &value : lessList) { + for (const double &value : lessList) { createRect(echar, value, currSize, xPos, yPos); yPos += currSize; } - setSceneRect(scene()->itemsBoundingRect()); + + //Scale selector + xPos = -scene()->width() / 2 - 10; + yPos = -scene()->height() / 2 + fontMetrics().height(); + currSize = fontMetrics().width(QStringLiteral("W")); + + for (const double &value : QList {0.01, 0.1, 1, 10}) { + auto z = new RectButton(scaleChar, value, currSize); + z->setPalette(this->palette()); + z->setPos(xPos, yPos); + z->setSelected(false); + auto lb = new QGraphicsTextItem(QString::number(value)); + lb->setX(xPos - 4); + lb->setY(yPos - fontMetrics().height() / 3); + scene()->addItem(lb); + connect(z, &RectButton::clicked, this, [this, value] { + _setScale = value; + setValues(QList {0.1 * value, 1 * value, 10 * value}); + }); + if (QString::number(value) == QString::number(0.01)) { + setLabels(z, scaleChar, value); + } + if (QString::number(value) == QString::number(_setScale)) { + z->setSelected(true); + } + scene()->addItem(z); + yPos += currSize; + } } void AxisControl::resizeEvent(QResizeEvent *) { fitInView(sceneRect(), Qt::KeepAspectRatio); } -void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char &axis, int value) +void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char &axis, double value) { auto *lb = new QGraphicsSimpleTextItem(); lb->setBrush(palette().buttonText()); @@ -189,7 +244,11 @@ lb->setText((value < 0) ? QStringLiteral("-") + axis : QStringLiteral(" ") + axis); } - if (axis.toLatin1() == 'X') { + if (axis.toLatin1() == '*') { + lb->setY(-scene()->height() / 2); + lb->setX(item->x()); + lb->setText(tr("Scale")); + } else 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); @@ -204,7 +263,6 @@ lb->setY(item->y() - item->boundingRect().height()); } } else { - lb->setX(item->x() + lb->boundingRect().width() / fontMetrics().width(lb->text())); #ifndef Q_OS_WIN @@ -215,3 +273,9 @@ } scene()->addItem(lb); } + +void AxisControl::setValues(const QList &movementValues) +{ + _mValues = movementValues; + createScene(); +}