diff --git a/src/widgets/axiscontrol.h b/src/widgets/axiscontrol.h --- a/src/widgets/axiscontrol.h +++ b/src/widgets/axiscontrol.h @@ -1,7 +1,8 @@ -/* Atelier KDE Printer Host for 3D Printing - Copyright (C) <2016> - Author: Lays Rodrigues - lays.rodrigues@kde.org - Chris Rizzitello - rizzitello@kde.org +/* AtCore Test Client + Copyright (C) <2016 - 2018> + + Authors: + Chris Rizzitello 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 @@ -17,75 +18,40 @@ along with this program. If not, see . */ #pragma once - -#include -#include -#include +#include +#include +#include #include "atcorewidgets_export.h" -class ATCOREWIDGETS_EXPORT 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 ATCOREWIDGETS_EXPORT 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; -}; - /** * @brief AxisControl is a Widget to generate axis relative movements. * * Usage: * Create a instance of AxisControl and connect the clicked signal, it will give you the axis and value that was clicked. */ -class ATCOREWIDGETS_EXPORT AxisControl : public QGraphicsView +class ATCOREWIDGETS_EXPORT AxisControl : public QWidget { Q_OBJECT public: - explicit AxisControl(const QList &movementValues = {1, 10, 25}, QWidget *parent = nullptr); - -private: - void setLabels(QGraphicsItem *item, QLatin1Char &axis, int value); - -protected: - void resizeEvent(QResizeEvent *); + /** + * @brief Create a new AxisControl + * @param parent + */ + AxisControl(QWidget *parent = nullptr); + ~AxisControl() = default; signals: /** * @brief User has clicked to move an axis. * @param axis: Axis to move * @param value: Amount to move */ - void clicked(QLatin1Char axis, int value); + void clicked(const QLatin1Char &axis, double value); +private: + QPushButton *makeButton(const QLatin1Char &axis, const QSize &iconSize, const QString &themeIcon, const QString &fallbackText); + QWidget *makeSimpleAxis(const QLatin1Char &axis, const QSize &iconSize); + QDoubleSpinBox *sbValue = nullptr; }; diff --git a/src/widgets/axiscontrol.cpp b/src/widgets/axiscontrol.cpp --- a/src/widgets/axiscontrol.cpp +++ b/src/widgets/axiscontrol.cpp @@ -1,7 +1,8 @@ -/* Atelier KDE Printer Host for 3D Printing - Copyright (C) <2016> - Author: Lays Rodrigues - lays.rodrigues@kde.org - Chris Rizzitello - rizzitello@kde.org +/* AtCore Test Client + Copyright (C) <2016 - 2018> + + Authors: + Chris Rizzitello 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 @@ -17,201 +18,92 @@ along with this program. If not, see . */ #include "axiscontrol.h" -#include +#include +#include +#include -PieButton::PieButton(QLatin1Char &axis, int value, int size, int angle) : _axis(axis), _value(value) +AxisControl::AxisControl(QWidget *parent) : + QWidget(parent) + , sbValue(new QDoubleSpinBox) { - 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(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); -} + auto mainLayout = new QVBoxLayout; + auto newLabel = new QLabel(tr("Units to Move Axis")); + sbValue->setDecimals(3); + sbValue->setMaximum(100.0); + sbValue->setValue(1); -void PieButton::setPalette(QPalette palette) -{ - _palette = palette; -} + auto layout = new QHBoxLayout(); + layout->addWidget(newLabel); + layout->addWidget(sbValue); -void PieButton::mousePressEvent(QGraphicsSceneMouseEvent *) -{ - emit clicked(_axis, _value); -} + auto newWidget = new QWidget(); + newWidget->setLayout(layout); + newWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + mainLayout->addWidget(newWidget); -void PieButton::hoverEnterEvent(QGraphicsSceneHoverEvent *) -{ - setBrush(_palette.highlight()); -} + QSize iconSize = QSize(fontMetrics().height(), fontMetrics().height()); + auto glayout = new QGridLayout(); + newLabel = new QLabel(QStringLiteral("X/Y")); + newLabel->setAlignment(Qt::AlignCenter); + glayout->addWidget(newLabel, 2, 1); -void PieButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) -{ - setBrush(_palette.button()); -} + //Y-Axis + auto newButton = makeButton(QLatin1Char('Y'), iconSize, QStringLiteral("arrow-up"), QStringLiteral("↑")); + glayout->addWidget(newButton, 1, 1); -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); - if (axis != QLatin1Char('E')) { - setToolTip(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); - } else { - setToolTip(tr("Extrude %1 Units").arg(value)); - } -} + newButton = makeButton(QLatin1Char('Y'), iconSize, QStringLiteral("arrow-down"), QStringLiteral("↓")); + glayout->addWidget(newButton, 3, 1); -void RectButton::setPalette(QPalette palette) -{ - _palette = palette; -} + //X-Axis + newButton = makeButton(QLatin1Char('X'), iconSize, QStringLiteral("arrow-left"), QStringLiteral("←")); + glayout->addWidget(newButton, 2, 0); -void RectButton::mousePressEvent(QGraphicsSceneMouseEvent *) -{ - emit clicked(_axis, _value); -} + newButton = makeButton(QLatin1Char('X'), iconSize, QStringLiteral("arrow-right"), QStringLiteral("→")); + glayout->addWidget(newButton, 2, 3); -void RectButton::hoverEnterEvent(QGraphicsSceneHoverEvent *) -{ - setBrush(_palette.highlight()); -} + auto bottomLayout = new QHBoxLayout(); + bottomLayout->addItem(glayout); -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 chosen 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(const QList &movementValues, QWidget *parent) : - QGraphicsView(parent) -{ - setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); - - setScene(new QGraphicsScene()); - - const int listSize = movementValues.size(); - int maxValue = *std::max_element(movementValues.begin(), movementValues.end()); - QList lessList = movementValues; - std::sort(lessList.begin(), lessList.end(), std::less()); - QList greaterList = movementValues; - std::sort(greaterList.begin(), greaterList.end(), std::greater()); - - auto createPie = [ this, maxValue ](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 (abs(value) == maxValue) { - setLabels(pie, axis, value); - } - scene()->addItem(pie); - }; - - auto createRect = [ this, maxValue ](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 (abs(value) == maxValue) { - setLabels(z, axis, value); - } - scene()->addItem(z); - }; - - int currPieSize = 25; - auto xchar = QLatin1Char('X'); - auto ychar = QLatin1Char('Y'); - auto zchar = QLatin1Char('Z'); - auto echar = QLatin1Char('E'); - for (const int &value : lessList) { - createPie(xchar, value, currPieSize, -45); // Left - createPie(xchar, value * -1, currPieSize, 135); // Right - createPie(ychar, value, currPieSize, 45); // Top - createPie(ychar, value * -1, currPieSize, 225); // Bottom - currPieSize += 25; - } + newWidget = makeSimpleAxis(QLatin1Char('Z'), iconSize); + bottomLayout->addWidget(newWidget); - int currSize = 25; - int xPos = sceneRect().width() - 50; - int yPos = -(listSize * 25); //Align with the origin + newWidget = makeSimpleAxis(QLatin1Char('E'), iconSize); + bottomLayout->addWidget(newWidget); - // Z+ - for (const int &value : greaterList) { - createRect(zchar, value, currSize, xPos, yPos); - yPos += currSize; - } - - // Z- - for (const int &value : lessList) { - createRect(zchar, -value, currSize, xPos, yPos); - yPos += currSize; - } + mainLayout->addItem(bottomLayout); + setLayout(mainLayout); - currSize = 25; - xPos = sceneRect().width() - 50; - yPos = -(listSize * 25); //Align with the origin - - // E- - for (const int &value : greaterList) { - createRect(echar, -value, currSize, xPos, yPos); - yPos += currSize; - } - - // E+ - for (const int &value : lessList) { - createRect(echar, value, currSize, xPos, yPos); - yPos += currSize; - } - setSceneRect(scene()->itemsBoundingRect()); } -void AxisControl::resizeEvent(QResizeEvent *) +QPushButton *AxisControl::makeButton(const QLatin1Char &axis, const QSize &iconSize, const QString &themeIcon, const QString &fallbackText) { - fitInView(sceneRect(), Qt::KeepAspectRatio); + auto button = new QPushButton(QIcon::fromTheme(themeIcon), QString()); + button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + if (button->icon().isNull()) { + button->setText(fallbackText); + } else { + button->setIconSize(iconSize); + } + connect(button, &QPushButton::clicked, this, [this, axis] { + emit clicked(axis, sbValue->value()); + }); + return button; } -void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char &axis, int value) +QWidget *AxisControl::makeSimpleAxis(const QLatin1Char &axis, const QSize &iconSize) { - auto *lb = new QGraphicsSimpleTextItem(); - lb->setBrush(palette().buttonText()); + auto vLayout = new QVBoxLayout; + auto button = makeButton(axis, iconSize, QStringLiteral("arrow-up"), QStringLiteral("↑")); + vLayout->addWidget(button); - 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 { + auto label = new QLabel(QString(axis)); + label->setAlignment(Qt::AlignCenter); + vLayout->addWidget(label); - lb->setX(item->x() + lb->boundingRect().width() / fontMetrics().width(lb->text())); + button = makeButton(axis, iconSize, QStringLiteral("arrow-down"), QStringLiteral("↓")); + vLayout->addWidget(button); -#ifndef Q_OS_WIN - lb->setY(item->y() - lb->boundingRect().height() / fontMetrics().xHeight()); -#else - lb->setY(item->y() - lb->boundingRect().height() / fontMetrics().height()); -#endif - } - scene()->addItem(lb); + auto widget = new QWidget(); + widget->setLayout(vLayout); + return widget; }