~kdevelop/kdevplatform/master

« back to all changes in this revision

Viewing changes to plugins/filetemplates/templatepreviewtoolview.cpp

  • Committer: Friedrich W. H. Kossebau
  • Date: 2017-08-13 21:54:31 UTC
  • Revision ID: git-v1:8ce76bea4df0831deb2fb243af33cc90d3cc8043
Wipe master branch and point in README to new location

Summary: also add util script for moving over existing personal branches

Reviewers: #kdevelop, apol, kfunk

Reviewed By: #kdevelop, apol, kfunk

Subscribers: kfunk, kdevelop-devel

Differential Revision: https://phabricator.kde.org/D7244

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of KDevelop
3
 
 * Copyright 2012 Miha Čančula <miha@noughmad.eu>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
19
 
 */
20
 
 
21
 
#include "templatepreviewtoolview.h"
22
 
 
23
 
#include "ui_templatepreviewtoolview.h"
24
 
 
25
 
#include "filetemplatesplugin.h"
26
 
#include "templatepreview.h"
27
 
 
28
 
#include <interfaces/icore.h>
29
 
#include <interfaces/idocumentcontroller.h>
30
 
#include <language/codegen/templaterenderer.h>
31
 
 
32
 
#include <KTextEditor/Document>
33
 
#include <KLocalizedString>
34
 
 
35
 
using namespace KDevelop;
36
 
 
37
 
TemplatePreviewToolView::TemplatePreviewToolView(FileTemplatesPlugin* plugin, QWidget* parent)
38
 
: QWidget(parent)
39
 
, ui(new Ui::TemplatePreviewToolView)
40
 
, m_original(nullptr)
41
 
, m_plugin(plugin)
42
 
{
43
 
    ui->setupUi(this);
44
 
    setWindowIcon(QIcon::fromTheme(QStringLiteral("document-preview"), windowIcon()));
45
 
 
46
 
    ui->messageWidget->hide();
47
 
    ui->emptyLinesPolicyComboBox->setCurrentIndex(1);
48
 
 
49
 
    IDocumentController* dc = ICore::self()->documentController();
50
 
    if (dc->activeDocument())
51
 
    {
52
 
        m_original = dc->activeDocument()->textDocument();
53
 
    }
54
 
 
55
 
    if (m_original)
56
 
    {
57
 
        documentActivated(dc->activeDocument());
58
 
    }
59
 
 
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();
65
 
 
66
 
    connect(dc, &IDocumentController::documentActivated,
67
 
            this, &TemplatePreviewToolView::documentActivated);
68
 
    connect(dc, &IDocumentController::documentClosed,
69
 
            this, &TemplatePreviewToolView::documentClosed);
70
 
}
71
 
 
72
 
TemplatePreviewToolView::~TemplatePreviewToolView()
73
 
{
74
 
    delete ui;
75
 
}
76
 
 
77
 
void TemplatePreviewToolView::documentActivated(KDevelop::IDocument* document)
78
 
{
79
 
    documentChanged(document->textDocument());
80
 
}
81
 
 
82
 
void TemplatePreviewToolView::documentChanged(KTextEditor::Document* document)
83
 
{
84
 
    if (!isVisible()) {
85
 
        return;
86
 
    }
87
 
 
88
 
 
89
 
    if (m_original) {
90
 
        disconnect(m_original, &KTextEditor::Document::textChanged,
91
 
                   this, &TemplatePreviewToolView::documentChanged);
92
 
    }
93
 
    m_original = document;
94
 
 
95
 
    FileTemplatesPlugin::TemplateType type = FileTemplatesPlugin::NoTemplate;
96
 
    if (m_original) {
97
 
        connect(m_original, &KTextEditor::Document::textChanged,
98
 
                this, &TemplatePreviewToolView::documentChanged);
99
 
        type = m_plugin->determineTemplateType(document->url());
100
 
    }
101
 
 
102
 
    switch (type) {
103
 
        case FileTemplatesPlugin::NoTemplate:
104
 
            ui->messageWidget->setMessageType(KMessageWidget::Information);
105
 
            if (m_original) {
106
 
                ui->messageWidget->setText(xi18n("The active text document is not a <application>KDevelop</application> template"));
107
 
            } else {
108
 
                ui->messageWidget->setText(i18n("No active text document."));
109
 
            }
110
 
            ui->messageWidget->animatedShow();
111
 
            ui->preview->setText(QString());
112
 
            break;
113
 
 
114
 
        case FileTemplatesPlugin::FileTemplate:
115
 
            ui->classRadioButton->setChecked(true);
116
 
            sourceTextChanged(m_original->text());
117
 
            break;
118
 
 
119
 
        case FileTemplatesPlugin::ProjectTemplate:
120
 
            ui->projectRadioButton->setChecked(true);
121
 
            sourceTextChanged(m_original->text());
122
 
            break;
123
 
    }
124
 
}
125
 
 
126
 
void TemplatePreviewToolView::showEvent(QShowEvent*)
127
 
{
128
 
    IDocument* doc = ICore::self()->documentController()->activeDocument();
129
 
    documentChanged(doc ? doc->textDocument() : nullptr);
130
 
}
131
 
 
132
 
void TemplatePreviewToolView::documentClosed(IDocument* document)
133
 
{
134
 
    m_original = nullptr;
135
 
 
136
 
    if (document && document->textDocument() == m_original) {
137
 
        documentChanged(nullptr);
138
 
    }
139
 
}
140
 
 
141
 
void TemplatePreviewToolView::sourceTextChanged(const QString& text)
142
 
{
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();
148
 
    } else {
149
 
        ui->messageWidget->animatedHide();
150
 
    }
151
 
 
152
 
    if (m_original) {
153
 
        ui->preview->document()->setMode(m_original->mode());
154
 
    }
155
 
}
156
 
 
157
 
void TemplatePreviewToolView::selectedRendererChanged()
158
 
{
159
 
    if (ui->classRadioButton->isChecked())
160
 
    {
161
 
        TemplateRenderer::EmptyLinesPolicy policy = TemplateRenderer::KeepEmptyLines;
162
 
        switch (ui->emptyLinesPolicyComboBox->currentIndex())
163
 
        {
164
 
            case 0:
165
 
                policy = TemplateRenderer::KeepEmptyLines;
166
 
                break;
167
 
 
168
 
            case 1:
169
 
                policy = TemplateRenderer::TrimEmptyLines;
170
 
                break;
171
 
 
172
 
            case 2:
173
 
                policy = TemplateRenderer::RemoveEmptyLines;
174
 
                break;
175
 
        }
176
 
        m_policy = policy;
177
 
    }
178
 
    documentChanged(m_original);
179
 
}
180