1
/* This file is part of the Kate project.
2
* Based on the snippet plugin from KDevelop 4.
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>
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.
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.
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.
24
#include "editrepository.h"
26
#include "snippetrepository.h"
28
#include <KLocalizedString>
30
#include <KTextEditor/EditorChooser>
31
#include <KPushButton>
35
#include "snippetstore.h"
37
EditRepository::EditRepository(SnippetRepository* repository, QWidget* parent)
38
: KDialog(parent), Ui::EditRepositoryBase(), m_repo(repository)
40
setButtons(/*Reset | */Apply | Cancel | Ok);
41
setupUi(mainWidget());
42
mainWidget()->layout()->setMargin(0);
44
connect(this, SIGNAL(okClicked()), this, SLOT(save()));
45
connect(this, SIGNAL(applyClicked()), this, SLOT(save()));
47
connect(repoNameEdit, SIGNAL(textEdited(QString)), this, SLOT(validate()));
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()));
59
// add default licenses
60
repoLicenseEdit->addItems(QStringList() << "Artistic" << "BSD" << "LGPL v2+" << "LGPL v3+");
61
repoLicenseEdit->setCurrentIndex(1); // preselect BSD
62
repoLicenseEdit->setEditable(true);
64
// if we edit a repo, add all existing data
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());
72
repoLicenseEdit->addItem(m_repo->license());
73
repoLicenseEdit->model()->sort(0);
74
index = repoLicenseEdit->findText(m_repo->license());
76
repoLicenseEdit->setCurrentIndex(index);
78
foreach ( const QString& type, m_repo->fileTypes() ) {
79
foreach( QListWidgetItem* item, repoFileTypesList->findItems(type, Qt::MatchExactly) ) {
80
item->setSelected(true);
84
setWindowTitle(i18n("Edit Snippet Repository %1", m_repo->text()));
86
setWindowTitle(i18n("Create New Snippet Repository"));
88
repoAuthorsEdit->setText(user.property(KUser::FullName).toString());
93
repoNameEdit->setFocus();
96
EditRepository::~EditRepository()
100
void EditRepository::validate()
102
bool valid = !repoNameEdit->text().isEmpty() && !repoNameEdit->text().contains('/');
103
button(Ok)->setEnabled(valid);
104
button(Apply)->setEnabled(valid);
107
void EditRepository::save()
109
Q_ASSERT(!repoNameEdit->text().isEmpty());
112
m_repo = SnippetRepository::createRepoFromName(repoNameEdit->text());
114
m_repo->setText(repoNameEdit->text());
115
m_repo->setAuthors(repoAuthorsEdit->text());
116
m_repo->setLicense(repoLicenseEdit->currentText());
117
m_repo->setCompletionNamespace(repoNamespaceEdit->text());
120
foreach( QListWidgetItem* item, repoFileTypesList->selectedItems() ) {
121
types << item->text();
123
m_repo->setFileTypes(types);
126
setWindowTitle(i18n("Edit Snippet Repository %1", m_repo->text()));
129
void EditRepository::updateFileTypes()
132
foreach( QListWidgetItem* item, repoFileTypesList->selectedItems() ) {
133
types << item->text();
135
if ( types.isEmpty() ) {
136
repoFileTypesListLabel->setText(i18n("<i>leave empty for general purpose snippets</i>"));
138
repoFileTypesListLabel->setText(types.join(", "));
142
#include "editrepository.moc"