~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to part/snippet/editsnippet.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the Kate project.
2
 
 *  Based on the snippet plugin from KDevelop 4.
3
 
 *
4
 
 *  Copyright (C) 2007 Robert Gruber <rgruber@users.sourceforge.net> 
5
 
 *  Copyright (C) 2010 Milian Wolff <mail@milianw.de>
6
 
 *  Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
7
 
 *
8
 
 *  This library is free software; you can redistribute it and/or
9
 
 *  modify it under the terms of the GNU Library General Public
10
 
 *  License as published by the Free Software Foundation; either
11
 
 *  version 2 of the License, or (at your option) any later version.
12
 
 *
13
 
 *  This library is distributed in the hope that it will be useful,
14
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 *  Library General Public License for more details.
17
 
 *
18
 
 *  You should have received a copy of the GNU Library General Public License
19
 
 *  along with this library; see the file COPYING.LIB.  If not, write to
20
 
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
 
 *  Boston, MA 02110-1301, USA.
22
 
 */
23
 
 
24
 
#include "editsnippet.h"
25
 
 
26
 
#include "ui_editsnippet.h"
27
 
 
28
 
#include "snippetrepository.h"
29
 
 
30
 
#include <QToolButton>
31
 
 
32
 
#include <KLocalizedString>
33
 
#include <KPushButton>
34
 
#include <KAction>
35
 
#include <KMimeTypeTrader>
36
 
#include <KTextEditor/Document>
37
 
#include <KTextEditor/View>
38
 
#include <KToolInvocation>
39
 
#include <KMessageBox>
40
 
#include <KMessageWidget>
41
 
 
42
 
#include "snippetstore.h"
43
 
#include "snippet.h"
44
 
 
45
 
QPair<KTextEditor::View*, QToolButton*> getViewForTab(QWidget* tabWidget)
46
 
{
47
 
    QVBoxLayout* layout = new QVBoxLayout;
48
 
    tabWidget->setLayout(layout);
49
 
    KParts::ReadWritePart* part= KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadWritePart>(
50
 
                                        "text/plain", tabWidget, tabWidget);
51
 
    KTextEditor::Document* document = qobject_cast<KTextEditor::Document*>(part);
52
 
    Q_ASSERT(document);
53
 
    Q_ASSERT(document->action("file_save"));
54
 
    document->action("file_save")->setEnabled(false);
55
 
 
56
 
    KTextEditor::View* view = qobject_cast< KTextEditor::View* >( document->widget() );
57
 
    layout->addWidget(view);
58
 
 
59
 
    QHBoxLayout* hlayout = new QHBoxLayout;
60
 
    hlayout->addStretch();
61
 
 
62
 
    QToolButton* button = new QToolButton;
63
 
    button->setText(i18n("Help"));
64
 
    button->setIcon(KIcon("help-contents"));
65
 
    button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
66
 
    hlayout->addWidget(button);
67
 
    layout->addLayout(hlayout);
68
 
 
69
 
    return qMakePair(view, button);
70
 
}
71
 
 
72
 
EditSnippet::EditSnippet(SnippetRepository* repository, Snippet* snippet, QWidget* parent)
73
 
    : KDialog(parent), m_ui(new Ui::EditSnippetBase), m_repo(repository)
74
 
    , m_snippet(snippet), m_topBoxModified(false)
75
 
{
76
 
    Q_ASSERT(m_repo);
77
 
 
78
 
    setButtons(/*Reset | */Apply | Cancel | Ok);
79
 
    m_ui->setupUi(mainWidget());
80
 
 
81
 
    ///TODO: highlighting and documentation of template handler variables
82
 
    QPair<KTextEditor::View*, QToolButton*> pair = getViewForTab(m_ui->snippetTab);
83
 
    m_snippetView = pair.first;
84
 
    if (!m_repo->fileTypes().isEmpty()) {
85
 
        m_snippetView->document()->setMode(m_repo->fileTypes().first());
86
 
    }
87
 
    connect(pair.second, SIGNAL(clicked(bool)),
88
 
            this, SLOT(slotSnippetDocumentation()));
89
 
    ///TODO: highlighting and documentation of KTextEditor API
90
 
    pair = getViewForTab(m_ui->scriptTab);
91
 
    m_scriptsView = pair.first;
92
 
    m_scriptsView->document()->setMode("JavaScript");
93
 
    m_scriptsView->document()->setText(m_repo->script());
94
 
    m_scriptsView->document()->setModified(false);
95
 
    connect(pair.second, SIGNAL(clicked(bool)),
96
 
            this, SLOT(slotScriptDocumentation()));
97
 
 
98
 
    m_ui->verticalLayout->setMargin(0);
99
 
    m_ui->formLayout->setMargin(0);
100
 
 
101
 
    m_ui->snippetShortcutWidget->layout()->setMargin(0);
102
 
 
103
 
    connect(this, SIGNAL(okClicked()), this, SLOT(save()));
104
 
    connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
105
 
 
106
 
    connect(m_ui->snippetNameEdit,       SIGNAL(textEdited(QString)), this, SLOT(topBoxModified()));
107
 
    connect(m_ui->snippetNameEdit,       SIGNAL(textEdited(QString)), this, SLOT(validate()));
108
 
    connect(m_ui->snippetArgumentsEdit,  SIGNAL(textEdited(QString)), this, SLOT(topBoxModified()));
109
 
    connect(m_ui->snippetPostfixEdit,    SIGNAL(textEdited(QString)), this, SLOT(topBoxModified()));
110
 
    connect(m_ui->snippetPrefixEdit,     SIGNAL(textEdited(QString)), this, SLOT(topBoxModified()));
111
 
    connect(m_ui->snippetShortcutWidget, SIGNAL(shortcutChanged(KShortcut)), this, SLOT(topBoxModified()));
112
 
    connect(m_snippetView->document(), SIGNAL(textChanged(KTextEditor::Document*)), this, SLOT(validate()));
113
 
 
114
 
    // if we edit a snippet, add all existing data
115
 
    if ( m_snippet ) {
116
 
        setWindowTitle(i18n("Edit Snippet %1 in %2", m_snippet->text(), m_repo->text()));
117
 
 
118
 
        m_ui->snippetArgumentsEdit->setText(m_snippet->arguments());
119
 
        m_snippetView->document()->setText(m_snippet->snippet());
120
 
        m_ui->snippetNameEdit->setText(m_snippet->text());
121
 
        m_ui->snippetPostfixEdit->setText(m_snippet->postfix());
122
 
        m_ui->snippetPrefixEdit->setText(m_snippet->prefix());
123
 
        m_ui->snippetShortcutWidget->setShortcut(m_snippet->action()->shortcut());
124
 
 
125
 
        // unset modified flags
126
 
        m_snippetView->document()->setModified(false);
127
 
        m_topBoxModified = false;
128
 
    } else {
129
 
        setWindowTitle(i18n("Create New Snippet in Repository %1", m_repo->text()));
130
 
    }
131
 
 
132
 
    validate();
133
 
 
134
 
    m_ui->snippetNameEdit->setFocus();
135
 
 
136
 
    QSize initSize = sizeHint();
137
 
    initSize.setHeight( initSize.height() + 200 );
138
 
    setInitialSize(initSize);
139
 
}
140
 
 
141
 
EditSnippet::~EditSnippet()
142
 
{
143
 
    delete m_ui;
144
 
}
145
 
 
146
 
void EditSnippet::setSnippetText( const QString& text )
147
 
{
148
 
    m_snippetView->document()->setText(text);
149
 
    validate();
150
 
}
151
 
 
152
 
void EditSnippet::validate()
153
 
{
154
 
    const QString& name = m_ui->snippetNameEdit->text();
155
 
    bool valid = !name.isEmpty() && !m_snippetView->document()->isEmpty();
156
 
    if (valid) {
157
 
        // make sure the snippetname includes no spaces
158
 
        for ( int i = 0; i < name.length(); ++i ) {
159
 
            if ( name.at(i).isSpace() ) {
160
 
                valid = false;
161
 
                m_ui->messageWidget->setText(i18n("Snippet name cannot contain spaces"));
162
 
                m_ui->messageWidget->animatedShow();
163
 
                break;
164
 
            }
165
 
        }
166
 
        if (valid) {
167
 
            // hide message widget if snippet does not include spaces
168
 
            m_ui->messageWidget->animatedHide();
169
 
        }
170
 
    }
171
 
    button(Ok)->setEnabled(valid);
172
 
    button(Apply)->setEnabled(valid);
173
 
}
174
 
 
175
 
void EditSnippet::save()
176
 
{
177
 
    Q_ASSERT(!m_ui->snippetNameEdit->text().isEmpty());
178
 
 
179
 
    if ( !m_snippet ) {
180
 
        // save as new snippet
181
 
        m_snippet = new Snippet();
182
 
        m_repo->appendRow(m_snippet);
183
 
    }
184
 
    m_snippet->setArguments(m_ui->snippetArgumentsEdit->text());
185
 
    m_snippet->setSnippet(m_snippetView->document()->text());
186
 
    m_snippetView->document()->setModified(false);
187
 
    m_snippet->setText(m_ui->snippetNameEdit->text());
188
 
    m_snippet->setPostfix(m_ui->snippetPostfixEdit->text());
189
 
    m_snippet->setPrefix(m_ui->snippetPrefixEdit->text());
190
 
    m_snippet->action()->setShortcut(m_ui->snippetShortcutWidget->shortcut());
191
 
    m_repo->setScript(m_scriptsView->document()->text());
192
 
    m_scriptsView->document()->setModified(false);
193
 
    m_topBoxModified = false;
194
 
    m_repo->save();
195
 
 
196
 
    setWindowTitle(i18n("Edit Snippet %1 in %2", m_snippet->text(), m_repo->text()));
197
 
}
198
 
 
199
 
void EditSnippet::slotSnippetDocumentation()
200
 
{
201
 
    KToolInvocation::invokeHelp("kate-application-plugin-snippets", "kate");
202
 
}
203
 
 
204
 
void EditSnippet::slotScriptDocumentation()
205
 
{
206
 
    KToolInvocation::invokeHelp("dev-scripting-api", "kate");
207
 
}
208
 
 
209
 
void EditSnippet::reject()
210
 
{
211
 
    if (m_topBoxModified || m_snippetView->document()->isModified() || m_scriptsView->document()->isModified()) {
212
 
        int ret = KMessageBox::warningContinueCancel(qApp->activeWindow(),
213
 
            i18n("The snippet contains unsaved changes. Do you want to continue and lose all changes?"),
214
 
            i18n("Warning - Unsaved Changes")
215
 
        );
216
 
        if (ret == KMessageBox::Cancel) {
217
 
            return;
218
 
        }
219
 
    }
220
 
    QDialog::reject();
221
 
}
222
 
 
223
 
void EditSnippet::topBoxModified()
224
 
{
225
 
    m_topBoxModified = true;
226
 
}
227
 
 
228
 
#include "editsnippet.moc"