Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/gcodeeditorwidget.cpp
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | |||||
42 | void GCodeEditorWidget::loadFile(const QUrl &file) | 42 | void GCodeEditorWidget::loadFile(const QUrl &file) | ||
43 | { | 43 | { | ||
44 | //if the file is loaded then reload the document. | 44 | //if the file is loaded then reload the document. | ||
45 | if (urlDoc.contains(file)) { | 45 | if (urlDoc.contains(file)) { | ||
46 | urlDoc[file]->documentReload(); | 46 | urlDoc[file]->documentReload(); | ||
47 | m_tabwidget->setCurrentIndex(m_tabwidget->indexOf(urlTab[file])); | 47 | m_tabwidget->setCurrentIndex(m_tabwidget->indexOf(urlTab[file])); | ||
48 | return; | 48 | return; | ||
49 | } | 49 | } | ||
50 | auto doc = newDoc(); | 50 | auto doc = newDoc(file); | ||
51 | int t = m_tabwidget->addTab(newView(doc), file.fileName()); | 51 | int t = m_tabwidget->addTab(newView(doc), file.fileName()); | ||
52 | doc->openUrl(file); | | |||
53 | doc->setHighlightingMode(QString("G-Code")); | | |||
54 | urlTab[doc->url()] = m_tabwidget->widget(t); | | |||
55 | urlDoc[doc->url()] = doc; | 52 | urlDoc[doc->url()] = doc; | ||
56 | m_tabwidget->setCurrentIndex(t); | 53 | urlTab[doc->url()] = m_tabwidget->widget(t); | ||
57 | //connect our new document's modified state changed signal. | 54 | //connect our new document's modified state changed signal. | ||
58 | connect(doc, &KTextEditor::Document::modifiedChanged, [this, t](const KTextEditor::Document * document) { | 55 | connect(doc, &KTextEditor::Document::modifiedChanged, [this, t](const KTextEditor::Document * document) { | ||
59 | QString filename = document->url().fileName(QUrl::FullyDecoded); | 56 | QString filename = document->url().fileName(QUrl::FullyDecoded); | ||
60 | if (document->isModified()) { | 57 | if (document->isModified()) { | ||
61 | filename.append(" *"); | 58 | filename.append(" *"); | ||
62 | } | 59 | } | ||
63 | m_tabwidget->setTabText(t, filename); | 60 | m_tabwidget->setTabText(t, filename); | ||
64 | }); | 61 | }); | ||
62 | m_tabwidget->setCurrentIndex(t); | ||||
65 | } | 63 | } | ||
66 | 64 | | |||
67 | void GCodeEditorWidget::setupInterface(const KTextEditor::View *view) | 65 | void GCodeEditorWidget::setupInterface(const KTextEditor::View *view) | ||
68 | { | 66 | { | ||
69 | m_interface = qobject_cast<KTextEditor::ConfigInterface *>(view); | 67 | m_interface = qobject_cast<KTextEditor::ConfigInterface *>(view); | ||
70 | m_interface->setConfigValue("line-numbers", true); | 68 | m_interface->setConfigValue("line-numbers", true); | ||
71 | } | 69 | } | ||
72 | 70 | | |||
73 | KTextEditor::Document *GCodeEditorWidget::newDoc() | 71 | KTextEditor::Document *GCodeEditorWidget::newDoc(const QUrl &file) | ||
74 | { | 72 | { | ||
75 | KTextEditor::Document *doc = m_editor->createDocument(this); | 73 | KTextEditor::Document *doc = m_editor->createDocument(this); | ||
76 | doc->setMode("G-Code"); | 74 | doc->setMode("G-Code"); | ||
75 | doc->openUrl(file); | ||||
76 | doc->setHighlightingMode(QString("G-Code")); | ||||
77 | return doc; | 77 | return doc; | ||
78 | } | 78 | } | ||
79 | 79 | | |||
80 | KTextEditor::View *GCodeEditorWidget::newView(KTextEditor::Document *doc) | 80 | KTextEditor::View *GCodeEditorWidget::newView(KTextEditor::Document *doc) | ||
81 | { | 81 | { | ||
82 | auto view = doc->createView(this); | 82 | auto view = doc->createView(this); | ||
83 | setupInterface(view); | 83 | setupInterface(view); | ||
84 | return view; | 84 | return view; | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | void GCodeEditorWidget::closeTab(int index) | 87 | void GCodeEditorWidget::closeTab(int index) | ||
88 | { | 88 | { | ||
89 | QUrl url = urlTab.key(m_tabwidget->widget(index)); | 89 | QUrl url = urlTab.key(m_tabwidget->widget(index)); | ||
90 | auto doc = urlDoc[url]; | 90 | auto doc = urlDoc[url]; | ||
91 | if (doc->closeUrl()) { | 91 | if (doc->closeUrl()) { | ||
92 | m_tabwidget->removeTab(index); | 92 | m_tabwidget->removeTab(index); | ||
93 | urlTab.remove(url); | 93 | urlTab.remove(url); | ||
94 | urlDoc.remove(url); | 94 | urlDoc.remove(url); | ||
95 | emit fileClosed(url); | 95 | emit fileClosed(url); | ||
96 | } | 96 | } | ||
97 | } | 97 | } | ||
98 | 98 | | |||
99 | void GCodeEditorWidget::currentIndexChanged(int index) | 99 | void GCodeEditorWidget::currentIndexChanged(int index) | ||
100 | { | 100 | { | ||
101 | if (index != -1) { | 101 | emit currentFileChanged(urlTab.key(m_tabwidget->widget(index))); | ||
102 | emit updateClientFactory(qobject_cast<KTextEditor::View *>(m_tabwidget->currentWidget())); | 102 | emit updateClientFactory(qobject_cast<KTextEditor::View *>(m_tabwidget->widget(index))); | ||
tcanabrava: the order of the emits matter?
if not one can save the pointers and the url and only call emit… | |||||
103 | } else { | | |||
104 | emit(updateClientFactory(nullptr)); | | |||
105 | } | | |||
106 | } | 103 | } |
the order of the emits matter?
if not one can save the pointers and the url and only call emit once per method.