File indexing completed on 2024-05-12 04:41:11

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2016-2018, 2020 Chris Rizzitello <rizzitello@kde.org>
0004     SPDX-FileCopyrightText: 2018 Patrick José Pereira <patrickjp@kde.org>
0005     SPDX-FileCopyrightText: 2018 Lays Rodrigues <lays.rodrigues@kde.org>
0006 */
0007 
0008 #include "axiscontrol.h"
0009 #include <QComboBox>
0010 #include <QDoubleSpinBox>
0011 #include <QGridLayout>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 #include <QString>
0016 
0017 AxisControl::AxisControl(QWidget *parent)
0018     : QWidget(parent)
0019     , sbValue(new QDoubleSpinBox)
0020 {
0021     auto mainLayout = new QVBoxLayout;
0022     auto newLabel = new QLabel(tr("Move Axis"), this);
0023     sbValue->setSuffix(QStringLiteral(" mm"));
0024     sbValue->setDecimals(3);
0025     sbValue->setMaximum(100.0);
0026     sbValue->setValue(1);
0027 
0028     auto comboUnits = new QComboBox(this);
0029     comboUnits->addItems(QStringList {QStringLiteral("Metric"), QStringLiteral("Imperial")});
0030 
0031     connect(comboUnits, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int selection) {
0032         if (selection == 0) {
0033             sbValue->setSuffix(QStringLiteral(" mm"));
0034         } else {
0035             sbValue->setSuffix(QStringLiteral(" in"));
0036         }
0037         Q_EMIT unitsChanged(selection);
0038     });
0039 
0040     auto layout = new QHBoxLayout();
0041     layout->addWidget(newLabel);
0042     layout->addWidget(sbValue);
0043     layout->addWidget(comboUnits);
0044 
0045     auto newWidget = new QWidget(this);
0046     newWidget->setLayout(layout);
0047     newWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0048     mainLayout->addWidget(newWidget);
0049 
0050     QSize iconSize = QSize(fontMetrics().height(), fontMetrics().height());
0051     auto glayout = new QGridLayout();
0052     newLabel = new QLabel(QStringLiteral("X/Y"), this);
0053     newLabel->setAlignment(Qt::AlignCenter);
0054     glayout->addWidget(newLabel, 2, 1);
0055 
0056     // Y-Axis
0057     glayout->addWidget(makeButton(QLatin1Char('Y'), 1, iconSize, QStringLiteral("arrow-up"), QStringLiteral("↑")), 1, 1);
0058     glayout->addWidget(makeButton(QLatin1Char('Y'), -1, iconSize, QStringLiteral("arrow-down"), QStringLiteral("↓")), 3, 1);
0059 
0060     // X-Axis
0061     glayout->addWidget(makeButton(QLatin1Char('X'), -1, iconSize, QStringLiteral("arrow-left"), QStringLiteral("←")), 2, 0);
0062     glayout->addWidget(makeButton(QLatin1Char('X'), 1, iconSize, QStringLiteral("arrow-right"), QStringLiteral("→")), 2, 3);
0063 
0064     auto bottomLayout = new QHBoxLayout();
0065     bottomLayout->addItem(glayout);
0066 
0067     newWidget = makeSimpleAxis(QLatin1Char('Z'), iconSize);
0068     bottomLayout->addWidget(newWidget);
0069 
0070     newWidget = makeSimpleAxis(QLatin1Char('E'), iconSize);
0071     bottomLayout->addWidget(newWidget);
0072 
0073     mainLayout->addItem(bottomLayout);
0074     setLayout(mainLayout);
0075 }
0076 
0077 QPushButton *AxisControl::makeButton(const QLatin1Char axis, int multiplier, const QSize &iconSize, const QString &themeIcon, const QString &fallbackText)
0078 {
0079     auto button = new QPushButton(QIcon::fromTheme(themeIcon), QString(), this);
0080     button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0081     if (button->icon().isNull()) {
0082         button->setText(fallbackText);
0083     } else {
0084         button->setIconSize(iconSize);
0085     }
0086 
0087     connect(button, &QPushButton::clicked, this, [this, axis, multiplier] { Q_EMIT clicked(axis, sbValue->value() * multiplier); });
0088     return button;
0089 }
0090 
0091 QWidget *AxisControl::makeSimpleAxis(const QLatin1Char axis, const QSize &iconSize)
0092 {
0093     int multiplier = 1;
0094     if (axis == QLatin1Char('E')) {
0095         multiplier = -1;
0096     }
0097 
0098     auto vLayout = new QVBoxLayout;
0099 
0100     vLayout->addWidget(makeButton(axis, multiplier, iconSize, QStringLiteral("arrow-up"), QStringLiteral("↑")));
0101 
0102     auto label = new QLabel(QString(axis), this);
0103     label->setAlignment(Qt::AlignCenter);
0104     vLayout->addWidget(label);
0105 
0106     multiplier *= -1;
0107     vLayout->addWidget(makeButton(axis, multiplier, iconSize, QStringLiteral("arrow-down"), QStringLiteral("↓")));
0108 
0109     auto widget = new QWidget(this);
0110     widget->setLayout(vLayout);
0111     return widget;
0112 }