File: editstyledialog.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (129 lines) | stat: -rw-r--r-- 4,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* This file is part of KDevelop
 *  Copyright (C) 2008 Cédric Pasteur <cedric.pasteur@free.fr>

   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 the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

 */

#include "editstyledialog.h"

#include <interfaces/isourceformatter.h>

#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KTextEditor/Editor>
#include <KTextEditor/ConfigInterface>
#include <KLocalizedString>

#include <QPushButton>
#include <QVBoxLayout>
#include <QUrl>
#include <QDialogButtonBox>


using namespace KDevelop;

EditStyleDialog::EditStyleDialog(ISourceFormatter* formatter, const QMimeType& mime,
                                 const SourceFormatterStyle& style, QWidget* parent)
    : QDialog(parent)
    , m_sourceFormatter(formatter)
    , m_mimeType(mime)
    , m_style(style)
{
    m_content = new QWidget();
    m_ui.setupUi(m_content);

    auto* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_content);

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    auto okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &EditStyleDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &EditStyleDialog::reject);
    mainLayout->addWidget(buttonBox);

    m_settingsWidget = m_sourceFormatter->editStyleWidget(mime);
    init();

    if (m_settingsWidget) {
        m_settingsWidget->load(style);
    }
}

EditStyleDialog::~EditStyleDialog()
{
}

void EditStyleDialog::init()
{
    // add plugin settings widget
    if (m_settingsWidget) {
        auto* layout = new QVBoxLayout(m_ui.settingsWidgetParent);
        layout->setContentsMargins(0, 0, 0, 0);
        layout->addWidget(m_settingsWidget);
        m_ui.settingsWidgetParent->setLayout(layout);
        connect(m_settingsWidget, &SettingsWidget::previewTextChanged,
                this, &EditStyleDialog::updatePreviewText);
    }

    m_document = KTextEditor::Editor::instance()->createDocument(this);
    m_document->setReadWrite(false);
    m_document->setHighlightingMode(m_style.modeForMimetype(m_mimeType));

    m_view = m_document->createView(m_ui.textEditor);
    auto* layout2 = new QVBoxLayout(m_ui.textEditor);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addWidget(m_view);
    m_ui.textEditor->setLayout(layout2);
    m_view->setStatusBarEnabled(false);
    m_view->show();

    KTextEditor::ConfigInterface* iface =
        qobject_cast<KTextEditor::ConfigInterface*>(m_view);
    if (iface) {
        iface->setConfigValue(QStringLiteral("dynamic-word-wrap"), false);
        iface->setConfigValue(QStringLiteral("icon-bar"), false);
        iface->setConfigValue(QStringLiteral("scrollbar-minimap"), false);
    }

    if (m_sourceFormatter) {
        QString text = m_sourceFormatter->previewText(m_style, m_mimeType);
        updatePreviewText(text);
    }
}

void EditStyleDialog::updatePreviewText(const QString &text)
{
    m_document->setReadWrite(true);
    m_style.setContent(content());
    if (m_sourceFormatter) {
        m_document->setText(m_sourceFormatter->formatSourceWithStyle(m_style, text, QUrl(), m_mimeType));
    } else {
        m_document->setText(i18n("No Source Formatter available"));
    }

    m_view->setCursorPosition(KTextEditor::Cursor(0, 0));
    m_document->setReadWrite(false);
}

QString EditStyleDialog::content()
{
    if (m_settingsWidget) {
        return m_settingsWidget->save();
    }
    return QString();
}