• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • snippet
editrepository.cpp
Go to the documentation of this file.
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"
QWidget
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
SnippetRepository::setCompletionNamespace
void setCompletionNamespace(const QString &completionNamespace)
Sets the code completion namespace for this repository.
Definition: snippetrepository.cpp:132
editrepository.h
SnippetRepository::createRepoFromName
static SnippetRepository * createRepoFromName(const QString &name)
Creates a snippet repository for the given name and adds it to the SnippetStore.
Definition: snippetrepository.cpp:73
QListWidgetItem
KDialog
QStringList::join
QString join(const QString &separator) const
SnippetRepository::license
QString license() const
The license for the snippets contained in this repository.
Definition: snippetrepository.cpp:117
QStandardItem::text
QString text() const
SnippetRepository::setFileTypes
void setFileTypes(const QStringList &filetypes)
Sets the valid filetypes for the snippets contained in this repository.
Definition: snippetrepository.cpp:108
EditRepository::~EditRepository
virtual ~EditRepository()
Definition: editrepository.cpp:96
SnippetRepository::save
void save()
Save this repository to disk.
Definition: snippetrepository.cpp:172
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
SnippetRepository::authors
QString authors() const
The author(s) of the snippets contained in this repository.
Definition: snippetrepository.cpp:93
SnippetRepository::completionNamespace
QString completionNamespace() const
The namespace associated with this repository.
Definition: snippetrepository.cpp:127
QString
SnippetRepository
Each object of this type represents a repository of snippets.
Definition: snippetrepository.h:52
QStringList
SnippetRepository::setAuthors
void setAuthors(const QString &authors)
Sets the author(s) of the snippets contained in this repository.
Definition: snippetrepository.cpp:98
QStandardItem::setText
void setText(const QString &text)
QItemSelection
EditRepository::EditRepository
EditRepository(SnippetRepository *repo, QWidget *parent=0)
repo set to 0 when you want to create a new repository.
Definition: editrepository.cpp:37
snippetstore.h
QListWidgetItem::setSelected
void setSelected(bool select)
QListWidgetItem::text
QString text() const
SnippetRepository::fileTypes
QStringList fileTypes() const
The valid filetypes for the snippets contained in this repository.
Definition: snippetrepository.cpp:103
snippetrepository.h
SnippetRepository::setLicense
void setLicense(const QString &license)
Sets the license for the snippets contained in this repository.
Definition: snippetrepository.cpp:122
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal