2
* This file is part of KDevelop
3
* Copyright 2012 Miha Čančula <miha@noughmad.eu>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "templatepreviewtoolview.h"
23
#include "ui_templatepreviewtoolview.h"
25
#include "filetemplatesplugin.h"
26
#include "templatepreview.h"
28
#include <interfaces/icore.h>
29
#include <interfaces/idocumentcontroller.h>
30
#include <language/codegen/templaterenderer.h>
32
#include <KTextEditor/Document>
33
#include <KLocalizedString>
35
using namespace KDevelop;
37
TemplatePreviewToolView::TemplatePreviewToolView(FileTemplatesPlugin* plugin, QWidget* parent)
39
, ui(new Ui::TemplatePreviewToolView)
44
setWindowIcon(QIcon::fromTheme(QStringLiteral("document-preview"), windowIcon()));
46
ui->messageWidget->hide();
47
ui->emptyLinesPolicyComboBox->setCurrentIndex(1);
49
IDocumentController* dc = ICore::self()->documentController();
50
if (dc->activeDocument())
52
m_original = dc->activeDocument()->textDocument();
57
documentActivated(dc->activeDocument());
60
connect(ui->projectRadioButton, &QRadioButton::toggled,
61
this, &TemplatePreviewToolView::selectedRendererChanged);
62
connect(ui->emptyLinesPolicyComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
63
this, &TemplatePreviewToolView::selectedRendererChanged);
64
selectedRendererChanged();
66
connect(dc, &IDocumentController::documentActivated,
67
this, &TemplatePreviewToolView::documentActivated);
68
connect(dc, &IDocumentController::documentClosed,
69
this, &TemplatePreviewToolView::documentClosed);
72
TemplatePreviewToolView::~TemplatePreviewToolView()
77
void TemplatePreviewToolView::documentActivated(KDevelop::IDocument* document)
79
documentChanged(document->textDocument());
82
void TemplatePreviewToolView::documentChanged(KTextEditor::Document* document)
90
disconnect(m_original, &KTextEditor::Document::textChanged,
91
this, &TemplatePreviewToolView::documentChanged);
93
m_original = document;
95
FileTemplatesPlugin::TemplateType type = FileTemplatesPlugin::NoTemplate;
97
connect(m_original, &KTextEditor::Document::textChanged,
98
this, &TemplatePreviewToolView::documentChanged);
99
type = m_plugin->determineTemplateType(document->url());
103
case FileTemplatesPlugin::NoTemplate:
104
ui->messageWidget->setMessageType(KMessageWidget::Information);
106
ui->messageWidget->setText(xi18n("The active text document is not a <application>KDevelop</application> template"));
108
ui->messageWidget->setText(i18n("No active text document."));
110
ui->messageWidget->animatedShow();
111
ui->preview->setText(QString());
114
case FileTemplatesPlugin::FileTemplate:
115
ui->classRadioButton->setChecked(true);
116
sourceTextChanged(m_original->text());
119
case FileTemplatesPlugin::ProjectTemplate:
120
ui->projectRadioButton->setChecked(true);
121
sourceTextChanged(m_original->text());
126
void TemplatePreviewToolView::showEvent(QShowEvent*)
128
IDocument* doc = ICore::self()->documentController()->activeDocument();
129
documentChanged(doc ? doc->textDocument() : nullptr);
132
void TemplatePreviewToolView::documentClosed(IDocument* document)
134
m_original = nullptr;
136
if (document && document->textDocument() == m_original) {
137
documentChanged(nullptr);
141
void TemplatePreviewToolView::sourceTextChanged(const QString& text)
143
QString errorString = ui->preview->setText(text, ui->projectRadioButton->isChecked(), m_policy);
144
if (!errorString.isEmpty()) {
145
ui->messageWidget->setMessageType(KMessageWidget::Error);
146
ui->messageWidget->setText(errorString);
147
ui->messageWidget->animatedShow();
149
ui->messageWidget->animatedHide();
153
ui->preview->document()->setMode(m_original->mode());
157
void TemplatePreviewToolView::selectedRendererChanged()
159
if (ui->classRadioButton->isChecked())
161
TemplateRenderer::EmptyLinesPolicy policy = TemplateRenderer::KeepEmptyLines;
162
switch (ui->emptyLinesPolicyComboBox->currentIndex())
165
policy = TemplateRenderer::KeepEmptyLines;
169
policy = TemplateRenderer::TrimEmptyLines;
173
policy = TemplateRenderer::RemoveEmptyLines;
178
documentChanged(m_original);