• 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
snippetstore.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 "snippetstore.h"
25 
26 #include "katesnippetglobal.h"
27 #include "snippetrepository.h"
28 
29 #include <KStandardDirs>
30 #include <KDebug>
31 
32 #include <ktexteditor/editor.h>
33 #include <ktexteditor/templateinterface2.h>
34 
35 SnippetStore* SnippetStore::m_self = 0;
36 
37 SnippetStore::SnippetStore(KateSnippetGlobal* plugin)
38  : m_plugin(plugin), m_scriptregistrar(0)
39 {
40  m_self = this;
41 
42  const QStringList list = KGlobal::dirs()->findAllResources("data",
43  "ktexteditor_snippets/data/*.xml", KStandardDirs::NoDuplicates)
44  << KGlobal::dirs()->findAllResources("data",
45  "ktexteditor_snippets/ghns/*.xml", KStandardDirs::NoDuplicates);
46 
47  foreach(const QString& file, list ) {
48  SnippetRepository* repo = new SnippetRepository(file);
49  appendRow(repo);
50  }
51 
52  m_scriptregistrar = KateGlobal::self();
53 }
54 
55 SnippetStore::~SnippetStore()
56 {
57  invisibleRootItem()->removeRows( 0, invisibleRootItem()->rowCount() );
58  m_self = 0;
59 }
60 
61 void SnippetStore::init(KateSnippetGlobal* plugin)
62 {
63  Q_ASSERT(!SnippetStore::self());
64  new SnippetStore(plugin);
65 }
66 
67 SnippetStore* SnippetStore::self()
68 {
69  return m_self;
70 }
71 
72 Qt::ItemFlags SnippetStore::flags(const QModelIndex & index) const
73 {
74  Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
75  if ( !index.parent().isValid() ) {
76  flags |= Qt::ItemIsUserCheckable;
77  }
78  return flags;
79 }
80 
81 KConfigGroup SnippetStore::getConfig()
82 {
86  return KateGlobal::self()->sessionConfig()->group("Snippets");
87 }
88 
89 bool SnippetStore::setData(const QModelIndex& index, const QVariant& value, int role)
90 {
91  if ( role == Qt::EditRole && value.toString().isEmpty() ) {
92  // don't allow empty names
93  return false;
94  }
95  const bool success = QStandardItemModel::setData(index, value, role);
96  if ( !success || role != Qt::EditRole ) {
97  return success;
98  }
99 
100  // when we edited something, save the repository
101 
102  QStandardItem* repoItem = 0;
103  if ( index.parent().isValid() ) {
104  repoItem = itemFromIndex(index.parent());
105  } else {
106  repoItem = itemFromIndex(index);
107  }
108 
109  SnippetRepository* repo = dynamic_cast<SnippetRepository*>(repoItem);
110  if ( repo ) {
111  repo->save();
112  }
113  return true;
114 }
115 
116 SnippetRepository* SnippetStore::repositoryForFile(const QString& file)
117 {
118  for ( int i = 0; i < rowCount(); ++i ) {
119  if ( SnippetRepository* repo = dynamic_cast<SnippetRepository*>(item(i)) ) {
120  if ( repo->file() == file ) {
121  return repo;
122  }
123  }
124  }
125  return 0;
126 }
127 
128 void SnippetStore::unregisterScript(KTextEditor::TemplateScript* script)
129 {
130  if ( m_scriptregistrar ) {
131  m_scriptregistrar->unregisterTemplateScript(script);
132  }
133 }
134 
135 KTextEditor::TemplateScript* SnippetStore::registerScript(const QString& script)
136 {
137  if ( m_scriptregistrar ) {
138  return m_scriptregistrar->registerTemplateScript(this, script);
139  }
140  return 0;
141 }
142 
143 
144 #include "snippetstore.moc"
QModelIndex
SnippetStore::~SnippetStore
virtual ~SnippetStore()
Definition: snippetstore.cpp:55
SnippetStore::unregisterScript
void unregisterScript(KTextEditor::TemplateScript *token)
Unregister script identified by token.
Definition: snippetstore.cpp:128
QStandardItemModel::invisibleRootItem
QStandardItem * invisibleRootItem() const
KateGlobal::sessionConfig
KSharedConfig::Ptr sessionConfig
property to tell the editor to use a given session config for session related configuration instead o...
Definition: kateglobal.h:97
SnippetStore::registerScript
KTextEditor::TemplateScript * registerScript(const QString &script)
Register script to make it available in snippets.
Definition: snippetstore.cpp:135
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
QStandardItemModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
SnippetStore::init
static void init(KateSnippetGlobal *plugin)
Initialize the SnippetStore.
Definition: snippetstore.cpp:61
QModelIndex::isValid
bool isValid() const
katesnippetglobal.h
SnippetRepository::save
void save()
Save this repository to disk.
Definition: snippetrepository.cpp:172
QStandardItem::appendRow
void appendRow(const QList< QStandardItem * > &items)
SnippetStore::repositoryForFile
SnippetRepository * repositoryForFile(const QString &file)
Returns the repository for the given file if there is any.
Definition: snippetstore.cpp:116
QString::isEmpty
bool isEmpty() const
SnippetStore
This class is implemented as singelton.
Definition: snippetstore.h:44
QStandardItem::removeRows
void removeRows(int row, int count)
QString
SnippetRepository
Each object of this type represents a repository of snippets.
Definition: snippetrepository.h:52
QModelIndex::parent
QModelIndex parent() const
QStringList
QStandardItemModel::item
QStandardItem * item(int row, int column) const
SnippetRepository::SnippetRepository
SnippetRepository(const QString &file)
Creates a new SnippetRepository.
Definition: snippetrepository.cpp:52
KateSnippetGlobal
This is the main class of KDevelop's snippet plugin.
Definition: katesnippetglobal.h:39
SnippetRepository::file
const QString & file() const
The path to this repository's file.
Definition: snippetrepository.cpp:88
QStandardItemModel::itemFromIndex
QStandardItem * itemFromIndex(const QModelIndex &index) const
SnippetStore::self
static SnippetStore * self()
Retuns the SnippetStore.
Definition: snippetstore.cpp:67
SnippetStore::getConfig
KConfigGroup getConfig()
Definition: snippetstore.cpp:81
QStandardItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
SnippetStore::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: snippetstore.cpp:89
QStandardItem
snippetstore.h
QVariant::toString
QString toString() const
snippetrepository.h
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:59 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