diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c1c2e18..ac499a2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,141 +1,151 @@ /* 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 "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include - +#include "widgets/gcodeeditorwidget.h" MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent), ui(new Ui::MainWindow), leftToolbar(new QToolBar()), profilesDialog(new ProfilesDialog(this)), - connectSettingsDialog(new ConnectSettingsDialog(this)) + connectSettingsDialog(new ConnectSettingsDialog(this)), + m_curr_editor_view(nullptr) { ui->setupUi(this); setupActions(); initWidgets(); +// Use this if trying to fix the bug with the connection +// m_curr_editor_view = ui->gcodeEditorWidget->gcodeView(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::initWidgets() { // When a new profile is added on the Profile Dialog it needs to update the profiles on connection dialog connect(profilesDialog, &ProfilesDialog::updateProfiles, connectSettingsDialog, &ConnectSettingsDialog::updateProfiles); connect(connectSettingsDialog, &ConnectSettingsDialog::startConnection, this, &MainWindow::newConnection); + connect(ui->gcodeEditorWidget, &GCodeEditorWidget::updateClientFactory, this, [=](KTextEditor::View* view){ + guiFactory()->removeClient(m_curr_editor_view); + guiFactory()->addClient(view); + m_curr_editor_view = view; + }); ui->gcodeDockWidget->setHidden(true); ui->videoDockWidget->setHidden(true); buildToolbar(); // connectSettingsDialog->setFirmwareList(core.availableFirmwarePlugins()); // profilesDialog->setBaudRates(core.serial()->validBaudRates()); } void MainWindow::buildToolbar() { leftToolbar->setOrientation(Qt::Vertical); leftToolbar->setMovable(true); leftToolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); addToolBar(Qt::LeftToolBarArea, leftToolbar); } void MainWindow::setupActions() { // Actions for the Toolbar QAction *action; action = actionCollection()->addAction(QStringLiteral("open_gcode")); 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, connectSettingsDialog, &ConnectSettingsDialog::show); action = actionCollection()->addAction(QStringLiteral("profiles")); action->setText(i18n("&Profiles")); connect(action, &QAction::triggered, profilesDialog, &ProfilesDialog::show); action = actionCollection()->addAction(QStringLiteral("3d"), ui->view3DdockWidget->toggleViewAction()); action->setText(i18n("&3D")); leftToolbar->addAction(action); action = actionCollection()->addAction(QStringLiteral("gcode"), ui->gcodeDockWidget->toggleViewAction()); action->setText(i18n("&GCode")); leftToolbar->addAction(action); +// This causes the program to crash when opening a second file, why? Apparently conflicts with +// the connect that also works with the guiFactory +// connect(action, &QAction::toggled, [=](bool b){ +// b ? guiFactory()->addClient(m_curr_editor_view) : guiFactory()->removeClient(m_curr_editor_view); +// }); action = actionCollection()->addAction(QStringLiteral("video"), ui->videoDockWidget->toggleViewAction()); action->setText(i18n("&Video")); leftToolbar->addAction(action); #ifdef Q_OS_LINUX //only set icons from theme on linux actionCollection()->action(QStringLiteral("profiles"))->setIcon(QIcon::fromTheme("emblem-favorite")); #endif //use style's standardIcon for the icons we can. actionCollection()->action(QStringLiteral("open_gcode"))->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon)); action = KStandardAction::quit(qApp, SLOT(quit()), actionCollection()); setupGUI(Default, "atelierui.rc"); } void MainWindow::openFile() { QUrl fileNameFromDialog = QFileDialog::getOpenFileUrl(this, i18n("Open GCode"), QDir::homePath(), i18n("GCode (*.gco *.gcode)")); if (!fileNameFromDialog.isEmpty()) { - fileName = fileNameFromDialog; - ui->gcodeEditorWidget->loadFile(fileName); - guiFactory()->addClient(ui->gcodeEditorWidget->gcodeView()); - ui->view3DWidget->drawModel(fileName.toString()); + ui->gcodeEditorWidget->loadFile(fileNameFromDialog); + ui->view3DWidget->drawModel(fileNameFromDialog.toString()); const int tabs = ui->tabWidget->count(); - QList files; - files.append(fileName); + m_openFiles.append(fileNameFromDialog); for(int i=0; i < tabs; ++i){ auto instance = qobject_cast(ui->tabWidget->widget(i)); - instance->setOpenFiles(files); + instance->setOpenFiles(m_openFiles); } } } void MainWindow::newConnection(const QString& port, const QMap& profile) { const int tabs = ui->tabWidget->count(); if(ui->tabWidget->count() == 1){ auto instance = qobject_cast(ui->tabWidget->currentWidget()); if(!instance->connected()){ instance->startConnection(port, profile); return; } } auto newInstanceWidget = new AtCoreInstanceWidget(); ui->tabWidget->addTab(newInstanceWidget, QString::number(tabs+1)); newInstanceWidget->startConnection(port, profile); } diff --git a/src/mainwindow.h b/src/mainwindow.h index ce8cdc3..f6fb649 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -1,53 +1,54 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Lays Rodrigues - laysrodrigues@gmail.com 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 namespace Ui { class MainWindow; } class MainWindow : public KXmlGuiWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; QStringList firmwaresList; - QUrl fileName; + QList m_openFiles; QToolBar *leftToolbar; ProfilesDialog *profilesDialog; ConnectSettingsDialog *connectSettingsDialog; + KTextEditor::View *m_curr_editor_view; void initWidgets(); void buildToolbar(); void setupActions(); void openFile(); void newConnection(const QString& port, const QMap& profile); - }; diff --git a/src/widgets/gcodeeditorwidget.cpp b/src/widgets/gcodeeditorwidget.cpp index 6fbe9bd..23387d7 100644 --- a/src/widgets/gcodeeditorwidget.cpp +++ b/src/widgets/gcodeeditorwidget.cpp @@ -1,58 +1,92 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Lays Rodrigues - laysrodrigues@gmail.com 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 "gcodeeditorwidget.h" #include #include #include GCodeEditorWidget::GCodeEditorWidget(QWidget *parent) : - QWidget(parent) + QWidget(parent), + m_tabwidget(new QTabWidget()) { m_editor = KTextEditor::Editor::instance(); + setupTabWidget(); QVBoxLayout *layout = new QVBoxLayout(); - m_view = newDoc()->createView(this); - layout->addWidget(m_view); - this->setLayout(layout); - setupInterface(); + layout->addWidget(m_tabwidget); + setLayout(layout); } - -void GCodeEditorWidget::loadFile(const QUrl &fileName) +void GCodeEditorWidget::setupTabWidget() { - auto doc = newDoc(); - doc->openUrl(fileName); - doc->setHighlightingMode(QString("G-Code")); + connect(m_tabwidget, &QTabWidget::tabCloseRequested, this, &GCodeEditorWidget::closeTab); + connect(m_tabwidget, &QTabWidget::currentChanged, this, &GCodeEditorWidget::currentIndexChanged); + m_tabwidget->setTabsClosable(true); + m_tabwidget->addTab(newView(newDoc()), i18n("New file")); } -KTextEditor::View *GCodeEditorWidget::gcodeView() const +KTextEditor::View* GCodeEditorWidget::gcodeView() const{ + + return qobject_cast(m_editor->documents().first()->views().first()); +} +void GCodeEditorWidget::loadFile(const QUrl &file) { - return m_view; + auto doc = m_editor->documents().first(); + if(!doc->isEmpty()){ + doc = newDoc(); + int t = m_tabwidget->addTab(newView(doc), file.fileName()); + m_tabwidget->setCurrentIndex(t); + } + else{ + m_tabwidget->setTabText(0, file.fileName()); + emit updateClientFactory(doc->views().first()); + } + doc->openUrl(file); + doc->setHighlightingMode(QString("G-Code")); } -void GCodeEditorWidget::setupInterface() +void GCodeEditorWidget::setupInterface(KTextEditor::View* view) { - m_interface = qobject_cast(m_view); + m_interface = qobject_cast(view); m_interface->setConfigValue("line-numbers", true); } KTextEditor::Document* GCodeEditorWidget::newDoc() { auto doc = m_editor->createDocument(this); doc->setMode("G-Code"); return doc; } +KTextEditor::View* GCodeEditorWidget::newView(KTextEditor::Document* doc){ + auto view = doc->createView(this); + setupInterface(view); + return view; +} + +void GCodeEditorWidget::closeTab(int index) +{ + m_tabwidget->removeTab(index); + if(m_tabwidget->count() == 0){ + m_tabwidget->addTab(newView(newDoc()), i18n("New file")); + } +} + +void GCodeEditorWidget::currentIndexChanged(int index){ + if(index != -1) + emit updateClientFactory(qobject_cast(m_tabwidget->currentWidget())); +} + diff --git a/src/widgets/gcodeeditorwidget.h b/src/widgets/gcodeeditorwidget.h index f7d8d48..c56170e 100644 --- a/src/widgets/gcodeeditorwidget.h +++ b/src/widgets/gcodeeditorwidget.h @@ -1,42 +1,49 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Lays Rodrigues - laysrodrigues@gmail.com 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 class GCodeEditorWidget : public QWidget { Q_OBJECT public: explicit GCodeEditorWidget(QWidget *parent = nullptr); - void loadFile(const QUrl &fileName); + void loadFile(const QUrl &file); KTextEditor::View *gcodeView() const; private: KTextEditor::Editor *m_editor; - KTextEditor::View *m_view; KTextEditor::ConfigInterface *m_interface; - QList m_documents; - void setupInterface(); + QTabWidget *m_tabwidget; + void setupTabWidget(); + void setupInterface(KTextEditor::View* view); KTextEditor::Document* newDoc(); + KTextEditor::View* newView(KTextEditor::Document* doc); + void closeTab(int index); + void currentIndexChanged(int index); + +signals: + void updateClientFactory(KTextEditor::View* view); };