diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -213,10 +213,12 @@ m_gcodeEditor = new GCodeEditorWidget(this); connect(m_gcodeEditor, &GCodeEditorWidget::updateClientFactory, this, &MainWindow::updateClientFactory); + connect(m_gcodeEditor, &GCodeEditorWidget::fileClosed, [this](const QUrl & file) { + m_openFiles.removeAll(file); + }); auto *viewer3D = new Viewer3D(this); connect(viewer3D, &Viewer3D::droppedUrls, this, &MainWindow::processDropEvent); - setupButton("3d", i18n("&3D"), QIcon::fromTheme("draw-cuboid", QIcon(QString(":/%1/3d").arg(m_theme))), viewer3D); setupButton("gcode", i18n("&GCode"), QIcon::fromTheme("accessories-text-editor", QIcon(":/icon/edit")), m_gcodeEditor); setupButton("video", i18n("&Video"), QIcon::fromTheme("camera-web", QIcon(":/icon/video")), new VideoMonitorWidget(this)); @@ -283,7 +285,9 @@ m_lateral.get("3d")->drawModel(fileName.toString()); const int tabs = m_instances->count(); - m_openFiles.append(fileName); + if (!m_openFiles.contains(fileName)) { + m_openFiles.append(fileName); + } for (int i = 0; i < tabs; ++i) { auto instance = qobject_cast(m_instances->widget(i)); diff --git a/src/widgets/gcodeeditorwidget.h b/src/widgets/gcodeeditorwidget.h --- a/src/widgets/gcodeeditorwidget.h +++ b/src/widgets/gcodeeditorwidget.h @@ -1,6 +1,7 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Lays Rodrigues - lays.rodrigues@kde.org + 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 @@ -30,10 +31,11 @@ public: explicit GCodeEditorWidget(QWidget *parent = nullptr); - KTextEditor::View *gcodeView() const; void loadFile(const QUrl &file); private: + QMap urlDoc; + QMap urlTab; KTextEditor::ConfigInterface *m_interface; KTextEditor::Document *newDoc(); KTextEditor::Editor *m_editor; @@ -46,4 +48,5 @@ signals: void updateClientFactory(KTextEditor::View *view); + void fileClosed(const QUrl &file); }; diff --git a/src/widgets/gcodeeditorwidget.cpp b/src/widgets/gcodeeditorwidget.cpp --- a/src/widgets/gcodeeditorwidget.cpp +++ b/src/widgets/gcodeeditorwidget.cpp @@ -1,6 +1,7 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Lays Rodrigues - lays.rodrigues@kde.org + 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 @@ -36,27 +37,24 @@ 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 -{ - return qobject_cast(m_editor->documents().first()->views().first()); } void GCodeEditorWidget::loadFile(const QUrl &file) { - 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()); + //if the file is loaded then reload the document. + if (urlDoc.contains(file)) { + m_editor->documents().at(urlDoc[file])->documentReload(); + m_tabwidget->setCurrentIndex(m_tabwidget->indexOf(urlTab[file])); + return; } + auto doc = newDoc(); + int t = m_tabwidget->addTab(newView(doc), file.fileName()); doc->openUrl(file); doc->setHighlightingMode(QString("G-Code")); + urlTab[doc->url()] = m_tabwidget->widget(t); + urlDoc[doc->url()] = m_editor->documents().count() - 1; + m_tabwidget->setCurrentIndex(t); + qDebug() << "LOAD " << doc->url() << "DOC:" << m_editor->documents().count() - 1 << "Tab:" << t << m_tabwidget->widget(t); } void GCodeEditorWidget::setupInterface(const KTextEditor::View *view) @@ -81,15 +79,23 @@ void GCodeEditorWidget::closeTab(int index) { - m_tabwidget->removeTab(index); - if (!m_tabwidget->count()) { - m_tabwidget->addTab(newView(newDoc()), i18n("New file")); + QUrl url = urlTab.key(m_tabwidget->widget(index)); + auto doc = m_editor->documents().at(urlDoc[url]); + if (doc->closeUrl()) { + qDebug() << "Closing:" << url << "Tab:" << index << m_tabwidget->tabText(index); + m_tabwidget->removeTab(index); + urlTab.remove(url); + urlDoc.remove(url); + emit fileClosed(url); + qDebug() << "NEWTAB:" << m_tabwidget->tabText(index); } } void GCodeEditorWidget::currentIndexChanged(int index) { if (index != -1) { emit updateClientFactory(qobject_cast(m_tabwidget->currentWidget())); + } else { + emit(updateClientFactory(nullptr)); } }