File: applychangeswidget.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 (215 lines) | stat: -rw-r--r-- 6,059 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright 2008 Aleix Pol <aleixpol@gmail.com>
 * Copyright 2009 Ramón Zarazúa <killerfox512+kde@gmail.com>
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "applychangeswidget.h"

#include <KTextEditor/Document>

#include <KMimeTypeTrader>

#include <QAction>
#include <QDialogButtonBox>
#include <QDir>
#include <QLabel>
#include <QMimeType>
#include <QMimeDatabase>
#include <QPushButton>
#include <QSplitter>
#include <QTemporaryFile>
#include <QTabWidget>
#include <QVBoxLayout>

#include "coderepresentation.h"
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>

namespace KDevelop {
class ApplyChangesWidgetPrivate
{
public:

    explicit ApplyChangesWidgetPrivate(ApplyChangesWidget* p)
        : parent(p)
        , m_index(0) {}
    ~ApplyChangesWidgetPrivate()
    {
        qDeleteAll(m_temps);
    }

    void createEditPart(const KDevelop::IndexedString& url);

    ApplyChangesWidget* const parent;
    int m_index;
    QList<KParts::ReadWritePart*> m_editParts;
    QList<QTemporaryFile*> m_temps;
    QList<IndexedString> m_files;
    QTabWidget* m_documentTabs;
    QLabel* m_info;
};

ApplyChangesWidget::ApplyChangesWidget(QWidget* parent)
    : QDialog(parent)
    , d_ptr(new ApplyChangesWidgetPrivate(this))
{
    Q_D(ApplyChangesWidget);

    setSizeGripEnabled(true);

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    auto mainLayout = new QVBoxLayout(this);
    auto okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &ApplyChangesWidget::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &ApplyChangesWidget::reject);

    auto* w = new QWidget(this);
    d->m_info = new QLabel(w);
    d->m_documentTabs = new QTabWidget(w);
    connect(d->m_documentTabs, &QTabWidget::currentChanged,
            this, &ApplyChangesWidget::indexChanged);

    auto* l = new QVBoxLayout(w);
    l->addWidget(d->m_info);
    l->addWidget(d->m_documentTabs);

    mainLayout->addWidget(w);
    mainLayout->addWidget(buttonBox);

    resize(QSize(800, 400));
}

ApplyChangesWidget::~ApplyChangesWidget() = default;

bool ApplyChangesWidget::hasDocuments() const
{
    Q_D(const ApplyChangesWidget);

    return d->m_editParts.size() > 0;
}

KTextEditor::Document* ApplyChangesWidget::document() const
{
    Q_D(const ApplyChangesWidget);

    return qobject_cast<KTextEditor::Document*>(d->m_editParts.value(d->m_index));
}

void ApplyChangesWidget::setInformation(const QString& info)
{
    Q_D(ApplyChangesWidget);

    d->m_info->setText(info);
}

void ApplyChangesWidget::addDocuments(const IndexedString& original)
{
    Q_D(ApplyChangesWidget);

    int idx = d->m_files.indexOf(original);
    if (idx < 0) {
        auto* w = new QWidget;
        d->m_documentTabs->addTab(w, original.str());
        d->m_documentTabs->setCurrentWidget(w);

        d->m_files.insert(d->m_index, original);
        d->createEditPart(original);
    } else {
        d->m_index = idx;
    }
}

bool ApplyChangesWidget::applyAllChanges()
{
    Q_D(ApplyChangesWidget);

    /// @todo implement safeguard in case a file saving fails

    bool ret = true;
    for (int i = 0; i < d->m_files.size(); ++i)
        if (d->m_editParts[i]->saveAs(d->m_files[i].toUrl())) {
            IDocument* doc = ICore::self()->documentController()->documentForUrl(d->m_files[i].toUrl());
            if (doc && doc->state() == IDocument::Dirty)
                doc->reload();
        } else
            ret = false;

    return ret;
}
}

namespace KDevelop {
void ApplyChangesWidgetPrivate::createEditPart(const IndexedString& file)
{
    QWidget* widget = m_documentTabs->currentWidget();
    Q_ASSERT(widget);

    auto* m = new QVBoxLayout(widget);
    auto* v = new QSplitter(widget);
    m->addWidget(v);

    QUrl url = file.toUrl();

    QMimeType mimetype = QMimeDatabase().mimeTypeForUrl(url);

    KParts::ReadWritePart* part = KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadWritePart>(
        mimetype.name(), widget, widget);
    auto* document = qobject_cast<KTextEditor::Document*>(part);
    Q_ASSERT(document);

    Q_ASSERT(document->action("file_save"));
    document->action("file_save")->setEnabled(false);

    m_editParts.insert(m_index, part);

    //Open the best code representation, even if it is artificial
    CodeRepresentation::Ptr repr = createCodeRepresentation(file);
    if (!repr->fileExists()) {
        const QString templateName = QDir::tempPath() + QLatin1Char('/') +
                                     url.fileName().split(QLatin1Char('.')).last();
        auto* temp(new QTemporaryFile(templateName));
        temp->open();
        temp->write(repr->text().toUtf8());
        temp->close();

        url = QUrl::fromLocalFile(temp->fileName());

        m_temps << temp;
    }
    m_editParts[m_index]->openUrl(url);

    v->addWidget(m_editParts[m_index]->widget());
    v->setSizes(QList<int> {400, 100});
}

void ApplyChangesWidget::indexChanged(int newIndex)
{
    Q_D(ApplyChangesWidget);

    Q_ASSERT(newIndex != -1);
    d->m_index = newIndex;
}

void ApplyChangesWidget::updateDiffView(int index)
{
    Q_D(ApplyChangesWidget);

    d->m_index = index == -1 ? d->m_index : index;
}
}