diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -185,6 +185,9 @@ 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); + }); setupButton("3d", i18n("&3D"), QIcon::fromTheme("draw-cuboid", QIcon(QString(":/%1/3d").arg(m_theme))), new Viewer3D(this)); 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)); @@ -241,7 +244,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 - 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 @@ -31,7 +32,6 @@ public: explicit GCodeEditorWidget(QWidget *parent = nullptr); void loadFile(const QUrl &file); - KTextEditor::View *gcodeView() const; private: KTextEditor::Editor *m_editor; @@ -43,7 +43,10 @@ KTextEditor::View* newView(KTextEditor::Document* doc); void closeTab(int index); void currentIndexChanged(int index); + QMap urlTab; + QMap urlDoc; 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 - 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 @@ -35,25 +36,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) @@ -77,13 +77,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); +// QWidget *widget = m_tabwidget->widget(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) + if(index != -1) { emit updateClientFactory(qobject_cast(m_tabwidget->currentWidget())); + } else { + emit (updateClientFactory(nullptr)); + } }