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

« back to all changes in this revision

Viewing changes to part/snippet/editrepository.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 "editrepository.h"
25
 
 
26
 
#include "snippetrepository.h"
27
 
 
28
 
#include <KLocalizedString>
29
 
 
30
 
#include <KTextEditor/EditorChooser>
31
 
#include <KPushButton>
32
 
 
33
 
#include <KUser>
34
 
 
35
 
#include "snippetstore.h"
36
 
 
37
 
EditRepository::EditRepository(SnippetRepository* repository, QWidget* parent)
38
 
    : KDialog(parent), Ui::EditRepositoryBase(), m_repo(repository)
39
 
{
40
 
    setButtons(/*Reset | */Apply | Cancel | Ok);
41
 
    setupUi(mainWidget());
42
 
    mainWidget()->layout()->setMargin(0);
43
 
 
44
 
    connect(this, SIGNAL(okClicked()), this, SLOT(save()));
45
 
    connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
46
 
 
47
 
    connect(repoNameEdit, SIGNAL(textEdited(QString)), this, SLOT(validate()));
48
 
 
49
 
    // fill list of available modes
50
 
    KTextEditor::Document *document = KTextEditor::EditorChooser::editor()->createDocument(0);
51
 
    repoFileTypesList->addItems(document->highlightingModes());
52
 
    repoFileTypesList->sortItems();
53
 
    repoFileTypesList->setSelectionMode(QAbstractItemView::ExtendedSelection);
54
 
    connect(repoFileTypesList->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
55
 
            this, SLOT(updateFileTypes()));
56
 
 
57
 
    delete document;
58
 
 
59
 
    // add default licenses
60
 
    repoLicenseEdit->addItems(QStringList() << "Artistic" << "BSD" << "LGPL v2+" << "LGPL v3+");
61
 
    repoLicenseEdit->setCurrentIndex(1); // preselect BSD
62
 
    repoLicenseEdit->setEditable(true);
63
 
 
64
 
    // if we edit a repo, add all existing data
65
 
    if ( m_repo ) {
66
 
        repoNameEdit->setText(m_repo->text());
67
 
        repoAuthorsEdit->setText(m_repo->authors());
68
 
        repoNamespaceEdit->setText(m_repo->completionNamespace());
69
 
        if ( !m_repo->license().isEmpty() ) {
70
 
            int index = repoLicenseEdit->findText(m_repo->license());
71
 
            if ( index == -1 ) {
72
 
                repoLicenseEdit->addItem(m_repo->license());
73
 
                repoLicenseEdit->model()->sort(0);
74
 
                index = repoLicenseEdit->findText(m_repo->license());
75
 
            }
76
 
            repoLicenseEdit->setCurrentIndex(index);
77
 
        }
78
 
        foreach ( const QString& type, m_repo->fileTypes() ) {
79
 
            foreach( QListWidgetItem* item, repoFileTypesList->findItems(type, Qt::MatchExactly) ) {
80
 
                item->setSelected(true);
81
 
            }
82
 
        }
83
 
 
84
 
        setWindowTitle(i18n("Edit Snippet Repository %1", m_repo->text()));
85
 
    } else {
86
 
        setWindowTitle(i18n("Create New Snippet Repository"));
87
 
        KUser user;
88
 
        repoAuthorsEdit->setText(user.property(KUser::FullName).toString());
89
 
    }
90
 
 
91
 
    validate();
92
 
    updateFileTypes();
93
 
    repoNameEdit->setFocus();
94
 
}
95
 
 
96
 
EditRepository::~EditRepository()
97
 
{
98
 
}
99
 
 
100
 
void EditRepository::validate()
101
 
{
102
 
    bool valid = !repoNameEdit->text().isEmpty() && !repoNameEdit->text().contains('/');
103
 
    button(Ok)->setEnabled(valid);
104
 
    button(Apply)->setEnabled(valid);
105
 
}
106
 
 
107
 
void EditRepository::save()
108
 
{
109
 
    Q_ASSERT(!repoNameEdit->text().isEmpty());
110
 
    if ( !m_repo ) {
111
 
        // save as new repo
112
 
        m_repo = SnippetRepository::createRepoFromName(repoNameEdit->text());
113
 
    }
114
 
    m_repo->setText(repoNameEdit->text());
115
 
    m_repo->setAuthors(repoAuthorsEdit->text());
116
 
    m_repo->setLicense(repoLicenseEdit->currentText());
117
 
    m_repo->setCompletionNamespace(repoNamespaceEdit->text());
118
 
 
119
 
    QStringList types;
120
 
    foreach( QListWidgetItem* item, repoFileTypesList->selectedItems() ) {
121
 
        types << item->text();
122
 
    }
123
 
    m_repo->setFileTypes(types);
124
 
    m_repo->save();
125
 
 
126
 
    setWindowTitle(i18n("Edit Snippet Repository %1", m_repo->text()));
127
 
}
128
 
 
129
 
void EditRepository::updateFileTypes()
130
 
{
131
 
    QStringList types;
132
 
    foreach( QListWidgetItem* item, repoFileTypesList->selectedItems() ) {
133
 
        types << item->text();
134
 
    }
135
 
    if ( types.isEmpty() ) {
136
 
        repoFileTypesListLabel->setText(i18n("<i>leave empty for general purpose snippets</i>"));
137
 
    } else {
138
 
        repoFileTypesListLabel->setText(types.join(", "));
139
 
    }
140
 
}
141
 
 
142
 
#include "editrepository.moc"