File indexing completed on 2024-05-19 05:51:03
0001 /* Atelier KDE Printer Host for 3D Printing 0002 Copyright (C) <2016> 0003 Author: Lays Rodrigues - lays.rodrigues@kde.org 0004 Chris Rizzitello - rizzitello@kde.org 0005 0006 This program is free software: you can redistribute it and/or modify 0007 it under the terms of the GNU General Public License as published by 0008 the Free Software Foundation, either version 3 of the License, or 0009 (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 #include "gcodeeditorwidget.h" 0020 #include <KLocalizedString> 0021 #include <QDropEvent> 0022 #include <QLabel> 0023 #include <QMimeData> 0024 #include <QVBoxLayout> 0025 0026 GCodeEditorWidget::GCodeEditorWidget(QWidget *parent) 0027 : QWidget(parent) 0028 , m_tabwidget(new QTabWidget(this)) 0029 { 0030 setAcceptDrops(true); 0031 m_editor = KTextEditor::Editor::instance(); 0032 setupTabWidget(); 0033 auto layout = new QVBoxLayout(); 0034 layout->addWidget(m_tabwidget); 0035 setLayout(layout); 0036 } 0037 0038 void GCodeEditorWidget::setupTabWidget() 0039 { 0040 connect(m_tabwidget, &QTabWidget::tabCloseRequested, this, &GCodeEditorWidget::closeTab); 0041 connect(m_tabwidget, &QTabWidget::currentChanged, this, &GCodeEditorWidget::currentIndexChanged); 0042 m_tabwidget->setTabsClosable(true); 0043 } 0044 0045 void GCodeEditorWidget::loadFile(const QUrl &file) 0046 { 0047 // if the file is loaded then reload the document. 0048 if (urlDoc.contains(file)) { 0049 urlDoc[file]->documentReload(); 0050 m_tabwidget->setCurrentIndex(m_tabwidget->indexOf(urlTab[file])); 0051 return; 0052 } 0053 auto doc = newDoc(file); 0054 int t = m_tabwidget->addTab(newView(doc), file.fileName()); 0055 urlDoc[doc->url()] = doc; 0056 urlTab[doc->url()] = m_tabwidget->widget(t); 0057 // connect our new document's modified state changed signal. 0058 connect(doc, &KTextEditor::Document::modifiedChanged, this, [this, t](const KTextEditor::Document *document) { 0059 QString filename = document->url().fileName(QUrl::FullyDecoded); 0060 if (document->isModified()) { 0061 filename.append(" *"); 0062 } 0063 m_tabwidget->setTabText(t, filename); 0064 }); 0065 m_tabwidget->setCurrentIndex(t); 0066 } 0067 0068 void GCodeEditorWidget::setupInterface(const KTextEditor::View *view) 0069 { 0070 m_interface = qobject_cast<KTextEditor::ConfigInterface *>(view); 0071 m_interface->setConfigValue("line-numbers", true); 0072 m_interface->setConfigValue("dynamic-word-wrap", false); 0073 m_interface->setConfigValue("modification-markers", true); 0074 m_interface->setConfigValue("scrollbar-minimap", false); 0075 } 0076 0077 KTextEditor::Document *GCodeEditorWidget::newDoc(const QUrl &file) 0078 { 0079 KTextEditor::Document *doc = m_editor->createDocument(this); 0080 doc->setMode("G-Code"); 0081 doc->openUrl(file); 0082 doc->setHighlightingMode(QString("G-Code")); 0083 return doc; 0084 } 0085 0086 KTextEditor::View *GCodeEditorWidget::newView(KTextEditor::Document *doc) 0087 { 0088 auto view = doc->createView(this); 0089 // Connection is a hack using undocumented parts of KTextEditor::View. 0090 // One day this may break, KTextEditor::View needs this added correctly as a real slot to the API. 0091 // Hopefully we can get that added and use it in the future. 0092 // This must be the older style connect string or it will not work. 0093 connect(view, SIGNAL(dropEventPass(QDropEvent *)), this, SLOT(dropCatch(QDropEvent *))); 0094 setupInterface(view); 0095 return view; 0096 } 0097 0098 void GCodeEditorWidget::closeTab(int index) 0099 { 0100 QUrl url = urlTab.key(m_tabwidget->widget(index)); 0101 auto doc = urlDoc[url]; 0102 if (doc->closeUrl()) { 0103 m_tabwidget->removeTab(index); 0104 urlTab.remove(url); 0105 urlDoc.remove(url); 0106 emit fileClosed(url); 0107 } 0108 } 0109 0110 void GCodeEditorWidget::currentIndexChanged(int index) 0111 { 0112 emit currentFileChanged(urlTab.key(m_tabwidget->widget(index))); 0113 emit updateClientFactory(qobject_cast<KTextEditor::View *>(m_tabwidget->widget(index))); 0114 } 0115 0116 void GCodeEditorWidget::dropCatch(QDropEvent *event) 0117 { 0118 if (event->mimeData()->hasUrls()) { 0119 emit droppedUrls(event->mimeData()->urls()); 0120 } 0121 } 0122 0123 QVector<QUrl> GCodeEditorWidget::modifiedFiles() 0124 { 0125 QVector<QUrl> modList; 0126 for (auto const &doc : m_editor->documents()) { 0127 if (doc->isModified()) { 0128 modList.append(doc->url()); 0129 } 0130 } 0131 return modList; 0132 } 0133 0134 bool GCodeEditorWidget::saveFile(const QUrl &url) 0135 { 0136 if (!urlDoc.contains(url)) { 0137 return false; 0138 } 0139 KTextEditor::Document *doc = urlDoc[url]; 0140 return doc->save(); 0141 }