diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -6,17 +6,22 @@ ${CMAKE_CURRENT_BINARY_DIR}/atcore_default_folders.h ) +find_package(Qt5 REQUIRED COMPONENTS + Quick +) + set(AtCoreLib_SRCS atcore.cpp seriallayer.cpp gcodecommands.cpp ifirmware.cpp temperature.cpp printthread.cpp + machineinfo.cpp ) add_library(AtCore SHARED ${AtCoreLib_SRCS}) -target_link_libraries(AtCore Qt5::Core Qt5::SerialPort) +target_link_libraries(AtCore Qt5::Core Qt5::SerialPort Qt5::Quick) # # @@ -33,13 +38,15 @@ EXPORT_NAME AtCore ) + ecm_generate_headers(ATCORE_CamelCase_HEADERS HEADER_NAMES AtCore GCodeCommands IFirmware SerialLayer Temperature + MachineInfo REQUIRED_HEADERS ATCORE_HEADERS ) diff --git a/src/core/machineinfo.h b/src/core/machineinfo.h new file mode 100644 --- /dev/null +++ b/src/core/machineinfo.h @@ -0,0 +1,148 @@ +/* AtCore + Copyright (C) <2019> + + Authors: + Chris Rizzitello + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#pragma once +#include "atcore_export.h" + +#include +#include +#include + +class ATCORE_EXPORT MachineInfo : public QObject +{ + Q_OBJECT +public: + /** + * @brief KEYS enum Possible keys for the printer settings + */ + enum class KEY { + NAME = 0, //!< Profile Name + BAUDRATE, //!< Machine BAUD Rate + FIRMWARE, //! &profile) const; + + /** + * @brief Store a key to an existing profile, Sending a key of Key::NAME will rename the profile + * @param profileName: profile to write into + * @param key: The key you will write to + * @param value: The value you will store. + * @return true if successful + * @sa storeProfile() + */ + Q_INVOKABLE bool storeKey(const QString &profileName, const MachineInfo::KEY key, const QVariant &value) const; + + /** + * @brief copyies a profile and optionally deletes the src profile + * @param srcProfile: profiles Current Name + * @param destProfile: profiles New Name + * @param rmSrc: delete srcProfile (defalut false) + * @return true if successful + */ + Q_INVOKABLE bool copyProfile(const QString &srcProfile, const QString &destProfile, bool rmSrc = false) const; + /** + * @brief Remove a full profile + * @param profileName: name of the profile you want to remove. + * @return true if successful. + */ + Q_INVOKABLE bool removeProfile(const QString &profileName) const; + + /** + * @brief Get a list of all the profile names + * @return QStringList containing the stored profile names. + */ + Q_INVOKABLE QStringList profileNames() const; + +signals: + /** + * @brief A profile has changed + */ + void profilesChanged() const; + +private: + MachineInfo *operator = (MachineInfo &other) = delete; + MachineInfo(const MachineInfo &other) = delete; + explicit MachineInfo(QObject *parent = nullptr); + ~MachineInfo() = default; + + /** + * @brief used to hold MachineInfo::KEY name and defaultValues. + */ + struct keyInfo { + QString name; //!< Key name used in the settings file + QVariant defaultValue; //!< Defaut Value for the key + }; + /** + * @brief Map of MachineInfo::KEY , KeyString and DefaultValue + */ + static const QMap decoderMap; + + /** + * @brief m_settings our settings object. + */ + + QSettings *m_settings = nullptr; +}; diff --git a/src/core/machineinfo.cpp b/src/core/machineinfo.cpp new file mode 100644 --- /dev/null +++ b/src/core/machineinfo.cpp @@ -0,0 +1,176 @@ +/* AtCore + Copyright (C) <2019> + + Authors: + Chris Rizzitello + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "machineinfo.h" + +#include +#include + +Q_LOGGING_CATEGORY(MACHINE_INFO, "org.kde.atelier.core.machineInfo") + +const QMap MachineInfo::decoderMap = { + {KEY::NAME, {QStringLiteral("Name"), QStringLiteral("ProfileName")}}, + {KEY::BAUDRATE, {QStringLiteral("bps"), 115200}}, + {KEY::FIRMWARE, {QStringLiteral("firmware"), QStringLiteral("Auto-Detect")}}, + {KEY::MAXBEDTEMP, {QStringLiteral("maximumTemperatureBed"), 0}}, + {KEY::MAXEXTTEMP, {QStringLiteral("maximumTemperatureExtruder"), 0}}, + {KEY::POSTPAUSE, {QStringLiteral("postPause"), QString()}}, + {KEY::ISCARTESIAN, {QStringLiteral("isCartesian"), false}}, + {KEY::XMAX, {QStringLiteral("dimensionX"), 200}}, + {KEY::YMAX, {QStringLiteral("dimensionY"), 200}}, + {KEY::ZMAX, {QStringLiteral("dimensionZ"), 180}}, + {KEY::AUTOTEMPREPORT, {QStringLiteral("autoReportTemp"), false}} +}; + +MachineInfo *MachineInfo::instance() +{ + static MachineInfo m; + return &m; +} + +MachineInfo::MachineInfo(QObject *parent) : + QObject(parent) + , m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral("atcore"), QStringLiteral("profiles"), this)) +{ +} + +QObject *MachineInfo::qmlSingletonRegister(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(scriptEngine) + engine->setObjectOwnership(instance(), QQmlEngine::CppOwnership); + return instance(); +} + +QVariantMap MachineInfo::readProfile(const QString &profileName) const +{ + m_settings->sync(); + m_settings->beginGroup(profileName); + QVariantMap data{{decoderMap[KEY::NAME].name, m_settings->group()}}; + for (int i = 1 ; i <= 10; i++) { + data.insert(decoderMap[MachineInfo::KEY(i)].name, m_settings->value(decoderMap[MachineInfo::KEY(i)].name, decoderMap[MachineInfo::KEY(i)].defaultValue)); + } + m_settings->endGroup(); + return data; +} + +QVariant MachineInfo::readKey(const QString &profileName, MachineInfo::KEY key) const +{ + if (profileName.isEmpty()) { + return decoderMap[key].defaultValue; + } + + if (key == KEY::NAME) { + return profileName; + } + + m_settings->sync(); + return m_settings->value(QStringLiteral("%1/%2").arg(profileName, decoderMap[key].name), decoderMap[key].defaultValue); +} + +bool MachineInfo::storeKey(const QString &profileName, const MachineInfo::KEY key, const QVariant &value) const +{ + if (key == KEY::NAME) { + //copyProfile emits profilesChanged + return copyProfile(profileName, value.toString(), true); + } + + if (!profileNames().contains(profileName)) { + qCWarning(MACHINE_INFO) << "Keysave: Profile does not exsits, create profile before attempting to store keys."; + return false; + } + + if (readKey(profileName, key) == value) { + return false; + } + + m_settings->beginGroup(profileName); + m_settings->setValue(decoderMap[key].name, value); + m_settings->endGroup(); + m_settings->sync(); + emit profilesChanged(); + return true; +} + +bool MachineInfo::copyProfile(const QString &srcProfile, const QString &destProfile, bool rmSrc) const +{ + if (srcProfile.isEmpty() || destProfile.isEmpty()) { + qCWarning(MACHINE_INFO) << "Profile Copy Error: Source or Destination Profile Is Empty."; + return false; + } + + if (!profileNames().contains(srcProfile)) { + qCWarning(MACHINE_INFO) << "Profile Copy Error: Source Not Found."; + return false; + } + + if (srcProfile == destProfile) { + qCWarning(MACHINE_INFO) << "Profile Copy Error: Source is Destination."; + return false; + } + QVariantMap newProfile = readProfile(srcProfile); + m_settings->beginGroup(destProfile); + for (int i = 1 ; i <= 10; i++) { + m_settings->setValue(decoderMap[MachineInfo::KEY(i)].name, newProfile[decoderMap[MachineInfo::KEY(i)].name]); + } + m_settings->endGroup(); + m_settings->sync(); + if (rmSrc) { + removeProfile(srcProfile); + //removeProfile emits profilesChanged + } else { + emit profilesChanged(); + } + return true; +} + +bool MachineInfo::removeProfile(const QString &profileName) const +{ + if (!profileNames().contains(profileName)) { + qCWarning(MACHINE_INFO) << "Profile Remove Error: Profile not found."; + return false; + } + m_settings->beginGroup(profileName); + m_settings->remove(QString()); + m_settings->endGroup(); + m_settings->sync(); + emit profilesChanged(); + return true; +} + +void MachineInfo::storeProfile(const QMap &profile) const +{ + m_settings->beginGroup(profile[KEY::NAME].toString()); + for (int i = 1 ; i <= 10; i++) { + m_settings->setValue(decoderMap[MachineInfo::KEY(i)].name, profile[MachineInfo::KEY(i)]); + } + m_settings->endGroup(); + m_settings->sync(); + emit profilesChanged(); +} + +QStringList MachineInfo::profileNames() const +{ + m_settings->sync(); + return m_settings->childGroups(); +} + diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -10,6 +10,7 @@ sdwidget.cpp statuswidget.cpp temperaturewidget.cpp + profilemanager.cpp ) add_library(AtCoreWidgets ${widgetsLIB_SRCS}) @@ -50,6 +51,7 @@ SdWidget StatusWidget TemperatureWidget + ProfileManager REQUIRED_HEADERS ATCOREWIDGETS_HEADERS ) diff --git a/src/widgets/printwidget.h b/src/widgets/printwidget.h --- a/src/widgets/printwidget.h +++ b/src/widgets/printwidget.h @@ -38,12 +38,6 @@ */ PrintWidget(bool showAllControls = true, QWidget *parent = nullptr); - /** - * @brief Get post pause string - * @return The Post Pause string. - */ - QString postPauseCommand() const; - /** * @brief set Post Pause string text. * @param text: text to set to. diff --git a/src/widgets/printwidget.cpp b/src/widgets/printwidget.cpp --- a/src/widgets/printwidget.cpp +++ b/src/widgets/printwidget.cpp @@ -43,16 +43,6 @@ hBoxLayout->addWidget(buttonPrint); hBoxLayout->addWidget(newButton); mainLayout->addLayout(hBoxLayout); - - newLabel = new QLabel(tr("On Pause:"), this); - - linePostPause = new QLineEdit(this); - linePostPause->setPlaceholderText(QStringLiteral("G91,G0 Z1,G90,G1 X0 Y195")); - - hBoxLayout = new QHBoxLayout; - hBoxLayout->addWidget(newLabel); - hBoxLayout->addWidget(linePostPause); - mainLayout->addLayout(hBoxLayout); } newLabel = new QLabel(tr("Printer Speed"), this); @@ -109,11 +99,6 @@ setLayout(mainLayout); } -QString PrintWidget::postPauseCommand() const -{ - return linePostPause->text(); -} - void PrintWidget::setPrintText(const QString &text) { buttonPrint->setText(text); diff --git a/src/widgets/profilemanager.h b/src/widgets/profilemanager.h new file mode 100644 --- /dev/null +++ b/src/widgets/profilemanager.h @@ -0,0 +1,75 @@ +/* AtCore - Widgets + Copyright (C) <2019> + Author: Chris Rizzitello - rizzitello@kde.org + + 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 +#include +#include +#include +#include "atcorewidgets_export.h" + +class ATCOREWIDGETS_EXPORT ProfileManager : public QWidget +{ + Q_OBJECT +public: + ProfileManager(QWidget *parent = nullptr); + +private: + void onCbProfileEditingFinished(); + void onRadioCartesianToggled(bool checked); + QStringList detectFWPlugins() ; + QStringList firmwaresInPath(const QString &path); + void loadProfile(const QString &profileName); + + QCheckBox *checkAutoTempReport = nullptr; + QComboBox *cbBaud = nullptr; + QComboBox *cbFirmware = nullptr; + QComboBox *cbProfile = nullptr; + QRadioButton *radioCartesian = nullptr; + QRadioButton *radioDelta = nullptr; + QLabel *lblX = nullptr; + QLabel *lblZ = nullptr; + QLineEdit *lineName = nullptr; + QLineEdit *linePostPause = nullptr; + QSpinBox *sbMaxBedTemp = nullptr; + QSpinBox *sbMaxExtTemp = nullptr; + QSpinBox *sbMaxX = nullptr; + QSpinBox *sbMaxY = nullptr; + QSpinBox *sbMaxZ = nullptr; + QWidget *axisY = nullptr; +}; + +namespace SERIAL +{ +static const QStringList BAUDS = { + QStringLiteral("9600"), + QStringLiteral("14400"), + QStringLiteral("19200"), + QStringLiteral("28800"), + QStringLiteral("38400"), + QStringLiteral("57600"), + QStringLiteral("76800"), + QStringLiteral("115200"), + QStringLiteral("230400"), + QStringLiteral("250000"), + QStringLiteral("500000"), + QStringLiteral("1000000") +}; +} diff --git a/src/widgets/profilemanager.cpp b/src/widgets/profilemanager.cpp new file mode 100644 --- /dev/null +++ b/src/widgets/profilemanager.cpp @@ -0,0 +1,336 @@ +/* AtCore - Widgets + Copyright (C) <2019> + Author: Chris Rizzitello - rizzitello@kde.org + + 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 "profilemanager.h" +#include "atcore_default_folders.h" +#include "machineinfo.h" + +#include +#include +#include +#include +#include +#include + +ProfileManager::ProfileManager(QWidget *parent) : + QWidget(parent) +{ + auto newLabel = new QLabel(tr("Profile:")); + cbProfile = new QComboBox(); + cbProfile->setEditable(true); + cbProfile->setAutoCompletion(true); + cbProfile->addItems(MachineInfo::instance()->profileNames()); + connect(MachineInfo::instance(), &MachineInfo::profilesChanged, this, [this] { + int index = cbProfile->currentIndex(); + cbProfile->clear(); + cbProfile->addItems(MachineInfo::instance()->profileNames()); + cbProfile->setCurrentIndex(std::min(index, cbProfile->count() - 1)); + }); + + connect(cbProfile, QOverload::of(&QComboBox::currentIndexChanged), this, [this] { + if (MachineInfo::instance()->profileNames().contains(cbProfile->currentText())) + { + loadProfile(cbProfile->currentText()); + } + }); + + connect(cbProfile->lineEdit(), &QLineEdit::editingFinished, this, &ProfileManager::onCbProfileEditingFinished); + + auto newHLayout = new QHBoxLayout(); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(cbProfile, 75); + + auto newButton = new QToolButton(); + newButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete"), style()->standardIcon(QStyle::SP_DialogDiscardButton))); + newButton->setToolTip(tr("Delete Current Profile")); + newButton->setIconSize(QSize(fontMetrics().height(), fontMetrics().height())); + connect(newButton, &QToolButton::clicked, this, [this] { + if (!cbProfile->currentText().isEmpty()) + { + MachineInfo::instance()->removeProfile(cbProfile->currentText()); + } + }); + + auto mainLayout = new QVBoxLayout(); + newHLayout->addWidget(newButton); + mainLayout->addLayout(newHLayout); + auto profileLayout = new QVBoxLayout(); + auto boxLayout = new QVBoxLayout(); + + newLabel = new QLabel(tr("Name")); + lineName = new QLineEdit(); + connect(lineName, &QLineEdit::editingFinished, this, [this] { + QString itemName = lineName->text(); + if (MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::NAME, itemName)) + { + loadProfile(itemName); + } + }); + newHLayout = new QHBoxLayout; + newHLayout->addWidget(newLabel); + newHLayout->addWidget(lineName); + profileLayout->addLayout(newHLayout); + + newLabel = new QLabel(tr("Printer Type:")); + radioDelta = new QRadioButton(tr("Delta")); + radioCartesian = new QRadioButton(tr("Cartesian")); + radioCartesian->setChecked(true); + connect(radioCartesian, &QRadioButton::toggled, this, &ProfileManager::onRadioCartesianToggled); + + newHLayout = new QHBoxLayout; + newHLayout->addWidget(newLabel); + newHLayout->addWidget(radioCartesian); + newHLayout->addWidget(radioDelta); + boxLayout->addLayout(newHLayout); + + lblX = new QLabel(tr("Maximum X")); + sbMaxX = new QSpinBox(); + sbMaxX->setMaximum(std::numeric_limits::max()); + sbMaxX->setSuffix(QStringLiteral("mm")); + connect(sbMaxX, QOverload::of(&QSpinBox::valueChanged), this, [this](int value) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::XMAX, value); + }); + + newHLayout = new QHBoxLayout; + newHLayout->addWidget(lblX); + newHLayout->addWidget(sbMaxX); + boxLayout->addLayout(newHLayout); + + newLabel = new QLabel(tr("Maximum Y")); + sbMaxY = new QSpinBox(); + sbMaxY->setMaximum(std::numeric_limits::max()); + sbMaxY->setSuffix(QStringLiteral("mm")); + connect(sbMaxY, QOverload::of(&QSpinBox::valueChanged), this, [this](int value) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::YMAX, value); + }); + + newHLayout = new QHBoxLayout; + newHLayout->setContentsMargins(0, 0, 0, 0); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(sbMaxY); + axisY = new QWidget(this); + axisY->setLayout(newHLayout); + boxLayout->addWidget(axisY); + + lblZ = new QLabel(tr("Maximum Z")); + sbMaxZ = new QSpinBox(); + sbMaxZ->setMaximum(std::numeric_limits::max()); + sbMaxZ->setSuffix(QStringLiteral("mm")); + connect(sbMaxZ, QOverload::of(&QSpinBox::valueChanged), this, [this](int value) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::ZMAX, value); + }); + + newHLayout = new QHBoxLayout; + newHLayout->addWidget(lblZ); + newHLayout->addWidget(sbMaxZ); + boxLayout->addLayout(newHLayout); + + auto groupBox = new QGroupBox(tr("Mechanics")); + groupBox->setLayout(boxLayout); + profileLayout->addWidget(groupBox); + + boxLayout = new QVBoxLayout; + newLabel = new QLabel(tr("Bed Maximum")); + sbMaxBedTemp = new QSpinBox(); + sbMaxBedTemp->setMaximum(999); + sbMaxBedTemp->setSuffix(QStringLiteral(" ºC")); + connect(sbMaxBedTemp, QOverload::of(&QSpinBox::valueChanged), this, [this](int value) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::MAXBEDTEMP, value); + }); + + newHLayout = new QHBoxLayout; + newHLayout->addWidget(newLabel); + newHLayout->addWidget(sbMaxBedTemp); + boxLayout->addLayout(newHLayout); + + sbMaxExtTemp = new QSpinBox(); + sbMaxExtTemp->setMaximum(999); + newLabel = new QLabel(tr("Extruder Maximum")); + sbMaxExtTemp->setSuffix(QStringLiteral(" ºC")); + connect(sbMaxExtTemp, QOverload::of(&QSpinBox::valueChanged), this, [this](int value) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::MAXEXTTEMP, value); + }); + + newHLayout = new QHBoxLayout(); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(sbMaxExtTemp); + boxLayout->addLayout(newHLayout); + + checkAutoTempReport = new QCheckBox(tr("Auto Temperature Report")); + checkAutoTempReport->setLayoutDirection(Qt::RightToLeft); + connect(checkAutoTempReport, &QCheckBox::toggled, this, [this](bool checked) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::AUTOTEMPREPORT, checked); + }); + boxLayout->addWidget(checkAutoTempReport, 0, Qt::AlignRight); + + groupBox = new QGroupBox(tr("Temperature")); + groupBox->setLayout(boxLayout); + profileLayout->addWidget(groupBox); + + boxLayout = new QVBoxLayout(); + cbBaud = new QComboBox(); + cbBaud->addItems(SERIAL::BAUDS); + cbBaud->setCurrentText(QStringLiteral("115200")); + connect(cbBaud, &QComboBox::currentTextChanged, this, [this](const QString & newText) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::BAUDRATE, newText); + }); + + newLabel = new QLabel(tr("Bit Rate")); + newHLayout = new QHBoxLayout(); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(cbBaud); + boxLayout->addLayout(newHLayout); + + newLabel = new QLabel(tr("Firmware")); + cbFirmware = new QComboBox(); + cbFirmware->addItem(QStringLiteral("Auto-Detect")); + cbFirmware->addItems(detectFWPlugins()); + connect(cbFirmware, &QComboBox::currentTextChanged, this, [this](const QString & newText) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::FIRMWARE, newText); + }); + + newHLayout = new QHBoxLayout(); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(cbFirmware); + boxLayout->addLayout(newHLayout); + + linePostPause = new QLineEdit(); + connect(linePostPause, &QLineEdit::editingFinished, this, [this] { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::POSTPAUSE, linePostPause->text()); + }); + + newLabel = new QLabel(tr("PostPause")); + newHLayout = new QHBoxLayout(); + newHLayout->addWidget(newLabel); + newHLayout->addWidget(linePostPause); + boxLayout->addLayout(newHLayout); + + groupBox = new QGroupBox(tr("Advanced")); + groupBox->setLayout(boxLayout); + profileLayout->addWidget(groupBox); + groupBox = new QGroupBox(tr("Profile")); + groupBox->setLayout(profileLayout); + mainLayout->addWidget(groupBox); + setLayout(mainLayout); + loadProfile(cbProfile->currentText()); +} + +void ProfileManager::onCbProfileEditingFinished() +{ + if (MachineInfo::instance()->profileNames().contains(cbProfile->currentText())) { + loadProfile(cbProfile->currentText()); + return; + } + QMap newProfile = { + {MachineInfo::KEY::NAME, cbProfile->currentText()}, + {MachineInfo::KEY::FIRMWARE, cbFirmware->currentText()}, + {MachineInfo::KEY::BAUDRATE, cbBaud->currentText()}, + {MachineInfo::KEY::POSTPAUSE, linePostPause->text()}, + {MachineInfo::KEY::ISCARTESIAN, radioCartesian->isChecked()}, + {MachineInfo::KEY::XMAX, sbMaxX->value()}, + {MachineInfo::KEY::YMAX, sbMaxY->value()}, + {MachineInfo::KEY::ZMAX, sbMaxZ->value()}, + {MachineInfo::KEY::AUTOTEMPREPORT, checkAutoTempReport->isChecked()}, + {MachineInfo::KEY::MAXBEDTEMP, sbMaxBedTemp->value()}, + {MachineInfo::KEY::MAXEXTTEMP, sbMaxExtTemp->value()} + }; + MachineInfo::instance()->storeProfile(newProfile); + loadProfile(newProfile[MachineInfo::KEY::NAME].toString()); +} + +void ProfileManager::onRadioCartesianToggled(bool checked) +{ + axisY->setVisible(checked); + if (checked) { + lblX->setText(tr("Maximum X")); + lblZ->setText(tr("Maximum Z")); + } else { + lblX->setText(tr("Radius")); + lblZ->setText(tr("Height")); + } + if (MachineInfo::instance()->profileNames().contains(cbProfile->currentText())) { + MachineInfo::instance()->storeKey(cbProfile->currentText(), MachineInfo::KEY::ISCARTESIAN, checked); + } +} + +void ProfileManager::loadProfile(const QString &profileName) +{ + if (profileName.isEmpty()) { + return; + } + blockSignals(true); + lineName->setText(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::NAME).toString()); + radioCartesian->setChecked(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::ISCARTESIAN).toBool()); + radioDelta->setChecked(!MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::ISCARTESIAN).toBool()); + sbMaxX->setValue(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::XMAX).toInt()); + sbMaxY->setValue(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::YMAX).toInt()); + sbMaxZ->setValue(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::ZMAX).toInt()); + checkAutoTempReport->setChecked(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::AUTOTEMPREPORT).toBool()); + sbMaxBedTemp->setValue(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::MAXBEDTEMP).toInt()); + sbMaxExtTemp->setValue(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::MAXEXTTEMP).toInt()); + cbFirmware->setCurrentText(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::FIRMWARE).toString()); + cbBaud->setCurrentText(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::BAUDRATE).toString()); + linePostPause->setText(MachineInfo::instance()->readKey(profileName, MachineInfo::KEY::POSTPAUSE).toString()); + cbProfile->setCurrentText(profileName); + cbProfile->setCurrentIndex(MachineInfo::instance()->profileNames().indexOf(profileName)); + blockSignals(false); +} + +QStringList ProfileManager::detectFWPlugins() +{ + QStringList firmwares; + QStringList paths = AtCoreDirectories::pluginDir; + //Add our runtime paths + const QString &p(qApp->applicationDirPath()); + paths.prepend(p + QStringLiteral("/../Plugins/AtCore")); + paths.prepend(p + QStringLiteral("/AtCore")); + paths.prepend(p + QStringLiteral("/plugins")); + for (const QString &path : qAsConst(paths)) { + firmwares = firmwaresInPath(path); + if (!firmwares.isEmpty()) { + //use path where plugins were detected. + break; + } + } + return firmwares; +} + +QStringList ProfileManager::firmwaresInPath(const QString &path) +{ + QStringList firmwares; + QStringList files = QDir(path).entryList(QDir::Files); + for (QString file : files) { +#if defined(Q_OS_WIN) + if (file.endsWith(QStringLiteral(".dll"))) +#elif defined(Q_OS_MAC) + if (file.endsWith(QStringLiteral(".dylib"))) +#else + if (file.endsWith(QStringLiteral(".so"))) +#endif + file = file.split(QChar::fromLatin1('.')).at(0); + else { + continue; + } + if (file.startsWith(QStringLiteral("lib"))) { + file = file.remove(QStringLiteral("lib")); + } + file = file.toLower().simplified(); + firmwares.append(file); + } + return firmwares; +} diff --git a/testclient/mainwindow.h b/testclient/mainwindow.h --- a/testclient/mainwindow.h +++ b/testclient/mainwindow.h @@ -30,6 +30,7 @@ #include "movementwidget.h" #include "plotwidget.h" #include "printwidget.h" +#include "profilemanager.h" #include "sdwidget.h" #include "statuswidget.h" #include "temperaturewidget.h" @@ -164,8 +165,7 @@ void makeConnectDock(); QDockWidget *connectDock = nullptr; QComboBox *comboPort = nullptr; - QComboBox *comboBAUD = nullptr; - QComboBox *comboPlugin = nullptr; + QComboBox *comboProfile = nullptr; QPushButton *buttonConnect = nullptr; QCheckBox *cbReset = nullptr; QTimer *connectionTimer = nullptr; @@ -181,4 +181,8 @@ void makeSdDock(); QDockWidget *sdDock = nullptr; SdWidget *sdWidget = nullptr; + + void makeProfileDock(); + QDockWidget *profileDock = nullptr; + ProfileManager *profileManager = nullptr; }; diff --git a/testclient/mainwindow.cpp b/testclient/mainwindow.cpp --- a/testclient/mainwindow.cpp +++ b/testclient/mainwindow.cpp @@ -28,6 +28,7 @@ #include #include "mainwindow.h" +#include "machineinfo.h" #include "seriallayer.h" #include "gcodecommands.h" #include "about.h" @@ -55,7 +56,16 @@ connect(core, &AtCore::portsChanged, this, &MainWindow::locateSerialPort); connect(core, &AtCore::sdCardFileListChanged, sdWidget, &SdWidget::updateFilelist); connect(core, &AtCore::autoTemperatureReportChanged, this, &MainWindow::updateAutoTemperatureReport); + comboPort->setFocus(Qt::OtherFocusReason); + + if (comboProfile->count() == 0) { + QMessageBox::information(this, tr("AtCore First Run"), tr("No Profiles Detected, the Profile Manager to create one.")); + profileDock->setVisible(true); + move(profileDock->geometry().center()); + profileDock->move(geometry().center()); + profileDock->activateWindow(); + } } void MainWindow::initMenu() @@ -106,6 +116,7 @@ makeMoveDock(); makeTempControlsDock(); makeSdDock(); + makeProfileDock(); setDangeriousDocksDisabled(true); @@ -118,6 +129,9 @@ tabifyDockWidget(connectDock, printDock); tabifyDockWidget(connectDock, commandDock); connectDock->raise(); + + tabifyDockWidget(logDock, profileDock); + logDock->raise(); setCentralWidget(nullptr); //More Gui stuff @@ -256,32 +270,35 @@ hBoxLayout->addWidget(comboPort, 75); mainLayout->addLayout(hBoxLayout); - newLabel = new QLabel(tr("Baud Rate:")); - comboBAUD = new QComboBox; - comboBAUD->addItems(core->portSpeeds()); - comboBAUD->setCurrentIndex(9); + comboProfile = new QComboBox(); + comboProfile->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + comboProfile->addItems(MachineInfo::instance()->profileNames()); - hBoxLayout = new QHBoxLayout; - hBoxLayout->addWidget(newLabel); - hBoxLayout->addWidget(comboBAUD, 75); - mainLayout->addLayout(hBoxLayout); + connect(MachineInfo::instance(), &MachineInfo::profilesChanged, this, [this] { + int index = comboProfile->currentIndex(); + comboProfile->clear(); + comboProfile->addItems(MachineInfo::instance()->profileNames()); + comboProfile->setCurrentIndex(std::min(index, comboProfile->count() - 1)); + }); - newLabel = new QLabel(tr("Use Plugin:")); - comboPlugin = new QComboBox; - comboPlugin->addItem(tr("Autodetect")); - comboPlugin->addItems(core->availableFirmwarePlugins()); - - hBoxLayout = new QHBoxLayout; - hBoxLayout->addWidget(newLabel); - hBoxLayout->addWidget(comboPlugin, 75); - mainLayout->addLayout(hBoxLayout); + newLabel = new QLabel(tr("Profile:")); + auto profileLayout = new QHBoxLayout(); + profileLayout->addWidget(newLabel); + profileLayout->addWidget(comboProfile); + mainLayout->addLayout(profileLayout); cbReset = new QCheckBox(tr("Attempt to stop Reset on connect")); - cbReset->setHidden(true); + if (MachineInfo::instance()->profileNames().isEmpty()) { + cbReset->setHidden(true); + } else { + cbReset->setHidden(MachineInfo::instance()->readKey( + comboProfile->currentText(), MachineInfo::KEY::FIRMWARE).toString().contains(QStringLiteral("Auto-Detect"))); + } mainLayout->addWidget(cbReset); - connect(comboPlugin, &QComboBox::currentTextChanged, this, [this](const QString & currentText) { - cbReset->setHidden(!core->availableFirmwarePlugins().contains(currentText)); + connect(comboProfile, &QComboBox::currentTextChanged, this, [this](const QString & currentText) { + cbReset->setHidden(MachineInfo::instance()->readKey( + currentText, MachineInfo::KEY::FIRMWARE).toString().contains(QStringLiteral("Auto-Detect"))); }); buttonConnect = new QPushButton(tr("Connect")); @@ -398,6 +415,17 @@ addDockWidget(Qt::LeftDockWidgetArea, sdDock); } +void MainWindow::makeProfileDock() +{ + profileManager = new ProfileManager(); + profileDock = new QDockWidget(tr("Profile Manager"), this); + profileDock->setWidget(profileManager); + menuView->insertAction(nullptr, profileDock->toggleViewAction()); + addDockWidget(Qt::RightDockWidgetArea, profileDock); + profileDock->setFloating(true); + profileDock->setVisible(false); +} + void MainWindow::closeEvent(QCloseEvent *event) { core->close(); @@ -458,15 +486,15 @@ void MainWindow::connectPBClicked() { if (core->state() == AtCore::DISCONNECTED) { - if (core->newConnection(comboPort->currentText(), comboBAUD->currentText().toInt(), comboPlugin->currentText(), cbReset->isChecked())) { + int baud = MachineInfo::instance()->readKey(comboProfile->currentText(), MachineInfo::KEY::BAUDRATE).toInt(); + QString plugin = MachineInfo::instance()->readKey(comboProfile->currentText(), MachineInfo::KEY::FIRMWARE).toString(); + if (core->newConnection(comboPort->currentText(), baud, plugin, cbReset->isChecked())) { connect(core, &AtCore::receivedMessage, logWidget, &LogWidget::appendRLog); connect(core, &AtCore::pushedCommand, logWidget, &LogWidget::appendSLog); logWidget->appendLog(tr("Serial connected")); - if (core->availableFirmwarePlugins().contains(comboPlugin->currentText())) { - if (cbReset->isChecked()) { - //Wait a few seconds after connect to avoid the normal errors - QTimer::singleShot(5000, core, &AtCore::sdCardPrintStatus); - } + if ((!plugin.contains(QStringLiteral("Auto-Detect"))) && cbReset->isChecked()) { + //Wait a few seconds after connect to avoid the normal errors + QTimer::singleShot(5000, core, &AtCore::sdCardPrintStatus); } } } else { @@ -488,8 +516,8 @@ break; case AtCore::CONNECTING: - QMessageBox::information(this, tr("Error"), tr(" A Firmware Plugin was not loaded!\n Please send the command M115 and let us know what your firmware returns, so we can improve our firmware detection. We have loaded the most common plugin \"repetier\" for you. You may try to print again after this message")); - comboPlugin->setCurrentText(QStringLiteral("repetier")); + QMessageBox::information(this, tr("Error"), tr(" A Firmware Plugin was not loaded!\n Please send the command M115 and let us know what your firmware returns, so we can improve our firmware detection. Edit your profile to use \"marlin\" and try again.")); + //comboPlugin->setCurrentText(QStringLiteral("marlin")); break; case AtCore::IDLE: @@ -503,7 +531,7 @@ break; case AtCore::BUSY: - core->pause(printWidget->postPauseCommand()); + core->pause(MachineInfo::instance()->readKey(comboProfile->currentText(), MachineInfo::KEY::POSTPAUSE).toString()); break; case AtCore::PAUSE: @@ -592,6 +620,7 @@ delete tempControlsDock->titleBarWidget(); delete printDock->titleBarWidget(); delete sdDock->titleBarWidget(); + delete profileDock->titleBarWidget(); } else { connectDock->setTitleBarWidget(new QWidget()); logDock->setTitleBarWidget(new QWidget()); @@ -601,6 +630,7 @@ tempControlsDock->setTitleBarWidget(new QWidget()); printDock->setTitleBarWidget(new QWidget()); sdDock->setTitleBarWidget(new QWidget()); + profileDock->setTitleBarWidget(new QWidget()); } } @@ -623,8 +653,7 @@ void MainWindow::setConnectionWidgetsEnabled(bool enabled) { - comboBAUD->setEnabled(enabled); - comboPlugin->setEnabled(enabled); + comboProfile->setEnabled(enabled); comboPort->setEnabled(enabled); }