diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -157,11 +157,16 @@ }; auto *gcodeEditor = new GCodeEditorWidget(this); - connect(gcodeEditor, &GCodeEditorWidget::updateClientFactory, this, [this](KTextEditor::View* view){ - guiFactory()->removeClient(m_currEditorView); - guiFactory()->addClient(view); - m_currEditorView = view; - }); + connect(gcodeEditor, &GCodeEditorWidget::updateClientFactory, this, [this](KTextEditor::View* view){ + // if (m_currEditorView != view && view) { + guiFactory()->removeClient(m_currEditorView); + guiFactory()->addClient(view); + m_currEditorView = view; + // } + }); + connect(gcodeEditor, &GCodeEditorWidget::fileClosed, this, [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")), gcodeEditor); setupButton("video", i18n("&Video"), QIcon::fromTheme("camera-web", QIcon(":/icon/video")), new VideoMonitorWidget(this)); @@ -218,7 +223,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)); @@ -236,4 +243,4 @@ { return palette().text().color().value() >= QColor(Qt::lightGray).value() ? \ QString("dark") : QString("light"); -} \ No newline at end of file +} 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 @@ -43,7 +44,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,29 @@ 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)) { + auto doc = m_editor->documents().at(urlDoc[file]); + doc->documentReload(); + m_tabwidget->setCurrentIndex(m_tabwidget->indexOf(urlTab[doc->url()])); + return; } + auto doc = newDoc(); + int t = m_tabwidget->addTab(newView(doc), file.fileName()); + m_tabwidget->setCurrentIndex(t); doc->openUrl(file); doc->setHighlightingMode(QString("G-Code")); + urlTab.insert(doc->url(), m_tabwidget->widget(t)); + urlDoc.insert(doc->url(),m_editor->documents().count()-1); + emit updateClientFactory(doc->views().first()); } void GCodeEditorWidget::setupInterface(const KTextEditor::View* view) @@ -77,13 +82,20 @@ 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]); + qDebug() << "CLOSING:" << "Tab:" << index << "Doc:" << urlDoc[url] << "URL:" << url; + if(doc->closeUrl()) { + m_tabwidget->removeTab(index); + urlTab.remove(url); + urlDoc.remove(url); + emit fileClosed(url); } } void GCodeEditorWidget::currentIndexChanged(int index){ - if(index != -1) + if(index != -1) { emit updateClientFactory(qobject_cast(m_tabwidget->currentWidget())); + qDebug() << "New Index:" << index; + } }