diff --git a/src/atelierui.rc b/src/atelierui.rc --- a/src/atelierui.rc +++ b/src/atelierui.rc @@ -8,27 +8,24 @@ + - - Run - - - + diff --git a/src/dialogs/CMakeLists.txt b/src/dialogs/CMakeLists.txt --- a/src/dialogs/CMakeLists.txt +++ b/src/dialogs/CMakeLists.txt @@ -1,6 +1,5 @@ set(dialogs_SRCS profilesdialog.cpp - connectsettingsdialog.cpp choosefiledialog.cpp ) add_library(AtelierDialogs STATIC ${dialogs_SRCS}) diff --git a/src/dialogs/connectsettingsdialog.h b/src/dialogs/connectsettingsdialog.h deleted file mode 100644 --- a/src/dialogs/connectsettingsdialog.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Atelier KDE Printer Host for 3D Printing - Copyright (C) <2016> - Author: Lays Rodrigues - laysrodrigues@gmail.com - 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 - -class ConnectSettingsDialog : public QDialog -{ - Q_OBJECT - -public: - explicit ConnectSettingsDialog(QWidget *parent = nullptr); - -private: - void initDisplay(); - void initData(); - void updateSerialPort(const QStringList &ports); - AtCore *atcore; - QSettings settings; - QMap profileData(); - void accept(); - QComboBox *serialPortCB; - QComboBox *deviceProfileCB; - QDialogButtonBox *buttonBox; -signals: - void startConnection(const QString port, const QMap data); -}; diff --git a/src/dialogs/connectsettingsdialog.cpp b/src/dialogs/connectsettingsdialog.cpp deleted file mode 100644 --- a/src/dialogs/connectsettingsdialog.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* Atelier KDE Printer Host for 3D Printing - Copyright (C) <2016> - Author: Lays Rodrigues - laysrodrigues@gmail.com - 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 "connectsettingsdialog.h" -#include -#include -#include -#include -#include - -ConnectSettingsDialog::ConnectSettingsDialog(QWidget *parent) : - QDialog(parent), - atcore(new AtCore) -{ - initDisplay(); - initData(); - setWindowTitle(i18n("Connect to Printer")); - atcore->setSerialTimerInterval(100); - connect(atcore, &AtCore::portsChanged, this, &ConnectSettingsDialog::updateSerialPort); - connect(buttonBox, &QDialogButtonBox::accepted, this, &ConnectSettingsDialog::accept); - connect(buttonBox, &QDialogButtonBox::rejected, this, &ConnectSettingsDialog::close); -} - -void ConnectSettingsDialog::initDisplay() -{ - serialPortCB = new QComboBox; - serialPortCB->setEditable(true); - QLabel *deviceLabel = new QLabel(i18n("Device")); - QHBoxLayout *deviceLayout = new QHBoxLayout; - deviceLayout->addWidget(deviceLabel); - deviceLayout->addWidget(serialPortCB); - - deviceProfileCB = new QComboBox; - QHBoxLayout *profileLayout = new QHBoxLayout; - QLabel *profileLabel = new QLabel(i18n("Profile")); - profileLayout->addWidget(profileLabel); - profileLayout->addWidget(deviceProfileCB); - - buttonBox = new QDialogButtonBox(); - buttonBox->addButton(i18n("Connect"),QDialogButtonBox::AcceptRole); - buttonBox->addButton(QDialogButtonBox::Cancel); - - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addLayout(deviceLayout); - mainLayout->addLayout(profileLayout); - mainLayout->addWidget(buttonBox); - - setLayout(mainLayout); -} - -void ConnectSettingsDialog::initData() -{ - settings.beginGroup("GeneralSettings"); - QStringList profiles = settings.childGroups(); - settings.endGroup(); - deviceProfileCB->addItems(profiles); -} - -void ConnectSettingsDialog::updateSerialPort(const QStringList &ports) -{ - serialPortCB->clear(); - if(!ports.isEmpty()) { - serialPortCB->addItems(ports); - } -} - -QMap ConnectSettingsDialog::profileData() -{ - QString profile = deviceProfileCB->currentText(); - settings.beginGroup("GeneralSettings"); - settings.beginGroup(profile); - QMap data; - data["bps"] = settings.value(QStringLiteral("bps"), QStringLiteral("115200")); - data["bedTemp"] = settings.value(QStringLiteral("maximumTemperatureBed"), QStringLiteral("0")); - data["hotendTemp"] = settings.value(QStringLiteral("maximumTemperatureExtruder"), QStringLiteral("0")); - data["firmware"] = settings.value(QStringLiteral("firmware"),QStringLiteral("Auto-Detect")); - data["postPause"] = settings.value(QStringLiteral("postPause"),QStringLiteral("")); - data["name"] = profile; - settings.endGroup(); - settings.endGroup(); - return data; -} - -void ConnectSettingsDialog::accept() -{ - if (deviceProfileCB->currentText().isEmpty()) { - QMessageBox::critical(this, i18n("Error"), i18n("Please, create a profile to connect on Settings!")); - return; - } - if (serialPortCB->currentText().isEmpty()) { - QMessageBox::critical(this, i18n("Error"), i18n("Please, connect a serial device to continue!")); - return; - } - emit startConnection(serialPortCB->currentText(), profileData()); - close(); -} diff --git a/src/mainwindow.h b/src/mainwindow.h --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -60,11 +60,13 @@ QTabWidget *m_instances; void setupLateralArea(); + void newAtCoreInstance(); void initWidgets(); void setupActions(); void openFile(); - void newConnection(const QString& port, const QMap& profile); + void atCoreInstanceNameChange(const QString &name); signals: void extruderCountChanged(int count); + void profilesChanged(); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -18,7 +18,6 @@ */ #include "mainwindow.h" #include "ui_mainwindow.h" -#include #include #include #include @@ -44,6 +43,16 @@ ui->setupUi(this); initWidgets(); setupActions(); + connect(m_instances, &QTabWidget::tabCloseRequested, [this] (int index){ + QWidget *tempWidget= m_instances->widget(index); + delete tempWidget; + + if(m_instances->count() == 1) { + m_instances->setTabsClosable(false); + m_instances->setMovable(false); + } + }); + } MainWindow::~MainWindow() @@ -53,10 +62,9 @@ void MainWindow::initWidgets() { - auto newInstanceWidget = new AtCoreInstanceWidget(); - m_instances->addTab(newInstanceWidget, i18n("Connect your printer")); setupLateralArea(); + newAtCoreInstance(); // View: // Sidebar, Sidevar Controls, Printer Tabs. @@ -69,13 +77,25 @@ centralLayout->addWidget(splitter); ui->centralwidget->setLayout(centralLayout); } - +void MainWindow::newAtCoreInstance() +{ + auto newInstanceWidget = new AtCoreInstanceWidget(); + QString name = QString::number(m_instances->addTab(newInstanceWidget, i18n("Connect a printer"))); + newInstanceWidget->setObjectName(name); + connect(this, &MainWindow::profilesChanged, newInstanceWidget, &AtCoreInstanceWidget::updateProfileData); + connect(newInstanceWidget, &AtCoreInstanceWidget::connectionChanged, this, &MainWindow::atCoreInstanceNameChange); + + if(m_instances->count() > 1) { + m_instances->setTabsClosable(true); + m_instances->setMovable(true); + } +} // Move to LateralArea. void MainWindow::setupLateralArea() { m_lateral.m_toolBar = new QWidget(); m_lateral.m_stack = new QStackedWidget(); - auto *buttonLayout = new QVBoxLayout(); + auto buttonLayout = new QVBoxLayout(); auto setupButton = [this, buttonLayout](const QString& key, const QString& text, const QIcon& icon, QWidget *w) { auto *btn = new QPushButton(m_lateral.m_toolBar); @@ -117,21 +137,16 @@ action->setText(i18n("&Open GCode")); connect(action, &QAction::triggered, this, &MainWindow::openFile); - action = actionCollection()->addAction(QStringLiteral("connect")); - action->setText(i18n("&Connect")); - connect(action, &QAction::triggered, [ & ]{ - std::unique_ptr csd(new ConnectSettingsDialog); - connect(csd.get(), &ConnectSettingsDialog::startConnection, [this](const QString& port, const QMap& data) { - newConnection(port, data); - }); - csd->exec(); - }); + action = actionCollection()->addAction(QStringLiteral("new_instance")); + action->setText(i18n("&New Connection")); + connect(action, &QAction::triggered, this, &MainWindow::newAtCoreInstance); action = actionCollection()->addAction(QStringLiteral("profiles")); action->setText(i18n("&Profiles")); connect(action, &QAction::triggered, [this] { std::unique_ptr pd(new ProfilesDialog); pd->exec(); + emit(profilesChanged()); }); #ifdef Q_OS_LINUX @@ -165,18 +180,8 @@ } } } -void MainWindow::newConnection(const QString& port, const QMap& profile) + +void MainWindow::atCoreInstanceNameChange(const QString &name) { - const int tabs = m_instances->count(); - if(tabs == 1){ - auto instance = qobject_cast(m_instances->currentWidget()); - if(!instance->connected()){ - instance->startConnection(port, profile); - m_instances->setTabText(m_instances->currentIndex(), profile["name"].toString()); - return; - } - } - auto newInstanceWidget = new AtCoreInstanceWidget(); - m_instances->addTab(newInstanceWidget, profile["name"].toString()); - newInstanceWidget->startConnection(port, profile); + m_instances->setTabText(sender()->objectName().toInt(),name); } diff --git a/src/widgets/atcoreinstancewidget.h b/src/widgets/atcoreinstancewidget.h --- a/src/widgets/atcoreinstancewidget.h +++ b/src/widgets/atcoreinstancewidget.h @@ -17,12 +17,14 @@ */ #pragma once -#include -#include -#include #include +#include #include - +#include +#include +#include +#include +#include namespace Ui { class AtCoreInstanceWidget; @@ -49,14 +51,23 @@ bool connected(); void setOpenFiles(const QList& files); +public slots: + void updateProfileData(); + private: Ui::AtCoreInstanceWidget* ui; AtCore m_core; QToolBar *m_mainToolBar; + QComboBox *m_comboPort; + QComboBox *m_comboProfile; + QPushButton *m_connectButton; + QToolBar *m_connectToolBar; + QWidget *m_connectWidget; QToolBar *m_toolBar; QMap profileData; QList m_files; QAction *m_printAction; + QSettings m_settings; void initConnectsToAtCore(); void printFile(const QUrl& fileName); void pausePrint(); @@ -71,7 +82,11 @@ void buildToolbar(); void buildMainToolbar(); void print(); + void updateSerialPort(const QStringList &ports); + void buildConnectionToolbar(); + void connectButtonClicked(); signals: void extruderCountChanged(int count); void disableDisconnect(bool b); + void connectionChanged(QString name); }; diff --git a/src/widgets/atcoreinstancewidget.cpp b/src/widgets/atcoreinstancewidget.cpp --- a/src/widgets/atcoreinstancewidget.cpp +++ b/src/widgets/atcoreinstancewidget.cpp @@ -36,12 +36,16 @@ ui->printProgressWidget->setVisible(false); buildMainToolbar(); buildToolbar(); + buildConnectionToolbar(); enableControls(false); + updateProfileData(); initConnectsToAtCore(); + m_mainToolBar->setHidden(true); } AtCoreInstanceWidget::~AtCoreInstanceWidget() { + m_core.closeConnection(); delete ui; } @@ -96,13 +100,6 @@ m_mainToolBar = new QToolBar(); m_mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - auto disconnectAction = new QAction(style()->standardIcon(QStyle::SP_DialogCloseButton), i18n("Disconnect")); - connect(this, &AtCoreInstanceWidget::disableDisconnect, disconnectAction, &QAction::setDisabled); - connect(disconnectAction, &QAction::triggered, [this](){ - m_core.closeConnection(); - }); - m_mainToolBar->addAction(disconnectAction); - m_printAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay),i18n("Print")); connect(m_printAction, &QAction::triggered, [ this ](){ if(m_core.state() == AtCore::BUSY) { @@ -130,20 +127,91 @@ m_mainToolBar->addAction(disableMotorsAction); ui->mainToolBarLayout->addWidget(m_mainToolBar); - ui->mainToolBarLayout->addStretch(); } -void AtCoreInstanceWidget::startConnection(const QString& serialPort, const QMap& profiles){ - m_core.initSerial(serialPort, profiles["bps"].toInt()); - if(m_core.state() == AtCore::CONNECTING){ - QString fw = profiles["firmware"].toString(); - if( fw != QString("Auto-Detect")){ - m_core.loadFirmwarePlugin(fw); + +void AtCoreInstanceWidget::buildConnectionToolbar() +{ + m_connectToolBar = new QToolBar(); + m_comboPort = new QComboBox; + m_comboPort->setEditable(true); + QLabel *deviceLabel = new QLabel(i18n("Device")); + QHBoxLayout *deviceLayout = new QHBoxLayout; + deviceLayout->addWidget(deviceLabel); + deviceLayout->addWidget(m_comboPort,100); + + m_comboProfile = new QComboBox; + m_comboProfile->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); + QHBoxLayout *profileLayout = new QHBoxLayout; + QLabel *profileLabel = new QLabel(i18n("Profile")); + profileLayout->addWidget(profileLabel); + profileLayout->addWidget(m_comboProfile,100); + + QHBoxLayout *connectLayout = new QHBoxLayout; + connectLayout->addLayout(deviceLayout,50); + connectLayout->addLayout(profileLayout,50); + + m_connectWidget = new QWidget(); + m_connectWidget->setLayout(connectLayout); + m_connectToolBar->addWidget(m_connectWidget); + + m_connectButton = new QPushButton(i18n("Connect")); + m_connectButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); + connect(this, &AtCoreInstanceWidget::disableDisconnect, m_connectButton, &QPushButton::setDisabled); + connect(m_connectButton, &QPushButton::clicked, this, &AtCoreInstanceWidget::connectButtonClicked); + ui->mainToolBarLayout->addWidget(m_connectToolBar); + ui->mainToolBarLayout->addWidget(m_connectButton); + +} + +void AtCoreInstanceWidget::connectButtonClicked() +{ + if(m_core.state() == AtCore::DISCONNECTED) { + if (m_comboProfile->currentText().isEmpty()) { + QMessageBox::critical(this, i18n("Error"), i18n("Please, create a profile to connect on Settings!")); + return; + } + + if (m_comboPort->currentText().isEmpty()) { + QMessageBox::critical(this, i18n("Error"), i18n("Please, connect a serial device to continue!")); + return; + } + + //Get profile data before connecting. + QString profile = m_comboProfile->currentText(); + m_settings.beginGroup("GeneralSettings"); + m_settings.beginGroup(profile); + QMap data; + data["bps"] = m_settings.value(QStringLiteral("bps"), QStringLiteral("115200")); + data["bedTemp"] = m_settings.value(QStringLiteral("maximumTemperatureBed"), QStringLiteral("0")); + data["hotendTemp"] = m_settings.value(QStringLiteral("maximumTemperatureExtruder"), QStringLiteral("0")); + data["firmware"] = m_settings.value(QStringLiteral("firmware"),QStringLiteral("Auto-Detect")); + data["postPause"] = m_settings.value(QStringLiteral("postPause"),QStringLiteral("")); + data["name"] = profile; + m_settings.endGroup(); + m_settings.endGroup(); + + //then connect + m_core.initSerial(m_comboPort->currentText(), data["bps"].toInt()); + if(m_core.state() == AtCore::CONNECTING){ + profileData = data; + QString fw = profileData["firmware"].toString(); + if( fw != QString("Auto-Detect")){ + m_core.loadFirmwarePlugin(fw); + } + emit(connectionChanged(profileData["name"].toString())); } + } else { + m_core.closeConnection(); + emit(connectionChanged(i18n("Connect a Printer"))); } } void AtCoreInstanceWidget::initConnectsToAtCore() { + m_core.setSerialTimerInterval(100); + // Handle device changes + connect(&m_core, &AtCore::portsChanged, this, &AtCoreInstanceWidget::updateSerialPort); + // Handle AtCore status change connect(&m_core, &AtCore::stateChanged, this, &AtCoreInstanceWidget::handlePrinterStatusChanged); @@ -240,6 +308,10 @@ static QString stateString; switch (newState) { case AtCore::CONNECTING: { + m_core.setSerialTimerInterval(0); + m_connectButton->setText(i18n("Disconnect")); + m_connectToolBar->setHidden(true); + m_mainToolBar->setHidden(false); stateString = i18n("Connecting..."); connect(&m_core, &AtCore::receivedMessage, this, &AtCoreInstanceWidget::checkReceivedCommand); connect(m_core.serial(), &SerialLayer::pushedCommand, this, &AtCoreInstanceWidget::checkPushedCommands); @@ -256,6 +328,10 @@ disconnect(&m_core, &AtCore::receivedMessage, this, &AtCoreInstanceWidget::checkReceivedCommand); disconnect(m_core.serial(), &SerialLayer::pushedCommand, this, &AtCoreInstanceWidget::checkPushedCommands); ui->logWidget->addLog(i18n("Serial disconnected")); + m_core.setSerialTimerInterval(100); + m_connectButton->setText(i18n("Connect")); + m_connectToolBar->setHidden(false); + m_mainToolBar->setHidden(true); enableControls(false); } break; case AtCore::STARTPRINT: { @@ -368,3 +444,18 @@ { m_files = files; } + +void AtCoreInstanceWidget::updateSerialPort(const QStringList &ports) +{ + m_comboPort->clear(); + m_comboPort->addItems(ports); +} + +void AtCoreInstanceWidget::updateProfileData() +{ + m_settings.beginGroup("GeneralSettings"); + QStringList profiles = m_settings.childGroups(); + m_settings.endGroup(); + m_comboProfile->clear(); + m_comboProfile->addItems(profiles); +}