• 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
katesnippetglobal.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) 2012 Christoph Cullmann <cullmann@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 #include "katesnippetglobal.h"
23 
24 #include <klocale.h>
25 #include <kpluginfactory.h>
26 #include <kaboutdata.h>
27 #include <kpluginloader.h>
28 #include <ktexteditor/view.h>
29 #include <ktexteditor/document.h>
30 #include <ktexteditor/codecompletioninterface.h>
31 #include <QMenu>
32 
33 #include <KActionCollection>
34 #include <KAction>
35 #include <KToolBar>
36 
37 #include <KTextEditor/HighlightInterface>
38 
39 #include "snippetview.h"
40 #include "snippetcompletionmodel.h"
41 #include "snippetstore.h"
42 
43 #include "snippet.h"
44 #include "snippetrepository.h"
45 #include "snippetcompletionitem.h"
46 #include "editsnippet.h"
47 
48 KateSnippetGlobal::KateSnippetGlobal(QObject *parent, const QVariantList &)
49  : QObject(parent)
50 {
51  SnippetStore::init(this);
52  m_model = new SnippetCompletionModel;
53 }
54 
55 KateSnippetGlobal::~KateSnippetGlobal ()
56 {
57  delete m_model;
58  delete SnippetStore::self();
59 }
60 
61 void KateSnippetGlobal::showDialog (KateView *view)
62 {
63  KDialog dialog;
64  dialog.setCaption("Snippets");
65  dialog.setButtons(KDialog::Ok);
66  dialog.setDefaultButton(KDialog::Ok);
67 
68  QWidget *mainWidget = new QWidget (&dialog);
69  dialog.setMainWidget(mainWidget);
70  QVBoxLayout *layout = new QVBoxLayout(mainWidget);
71 
72  KToolBar *topToolbar = new KToolBar (&dialog, "snippetsToolBar");
73  topToolbar->setToolButtonStyle (Qt::ToolButtonIconOnly);
74  layout->addWidget(topToolbar);
75 
76  QWidget* widget = snippetWidget ();
77  layout->addWidget(widget);
78 
79  // add actions
80  topToolbar->addActions (widget->actions());
81 
85  m_activeViewForDialog = view;
86  dialog.exec();
87  m_activeViewForDialog = 0;
88 }
89 
90 QWidget *KateSnippetGlobal::snippetWidget ()
91 {
92  return new SnippetView (this, 0);
93 }
94 
95 void KateSnippetGlobal::insertSnippet(Snippet* snippet)
96 {
97  // query active view, always prefer that!
98  KTextEditor::View *view = 0;
99  KTextEditor::MdiContainer *iface = qobject_cast<KTextEditor::MdiContainer*>(KateGlobal::self()->container());
100  if (iface && iface->activeView())
101  view = iface->activeView();
102 
103  // fallback to stuff set for dialog
104  if (!view)
105  view = m_activeViewForDialog;
106 
107  // no view => nothing to do
108  if (!view)
109  return;
110 
111  // try to insert snippet
112  SnippetCompletionItem item(snippet, static_cast<SnippetRepository*>(snippet->parent()));
113  KTextEditor::Range range = view->selectionRange();
114  if ( !range.isValid() ) {
115  range = KTextEditor::Range(view->cursorPosition(), view->cursorPosition());
116  }
117  item.execute(view, range);
118 
119  // set focus to view
120  view->setFocus ();
121 }
122 
123 void KateSnippetGlobal::insertSnippetFromActionData()
124 {
125  KAction* action = dynamic_cast<KAction*>(sender());
126  Q_ASSERT(action);
127  Snippet* snippet = action->data().value<Snippet*>();
128  Q_ASSERT(snippet);
129  insertSnippet(snippet);
130 }
131 
132 void KateSnippetGlobal::createSnippet (KateView *view)
133 {
134  // invalid range? skip to do anything, it will fail!
135  if (!view->selectionRange().isValid())
136  return;
137 
138  // get mode
139  QString mode = view->doc()->highlightingModeAt(view->selectionRange().start());
140  if ( mode.isEmpty() )
141  mode = view->doc()->mode();
142 
143  // try to look for a fitting repo
144  SnippetRepository* match = 0;
145  for ( int i = 0; i < SnippetStore::self()->rowCount(); ++i ) {
146  SnippetRepository* repo = dynamic_cast<SnippetRepository*>( SnippetStore::self()->item(i) );
147  if ( repo && repo->fileTypes().count() == 1 && repo->fileTypes().first() == mode ) {
148  match = repo;
149  break;
150  }
151  }
152  bool created = !match;
153  if ( created ) {
154  match = SnippetRepository::createRepoFromName(
155  i18nc("Autogenerated repository name for a programming language",
156  "%1 snippets", mode)
157  );
158  match->setFileTypes(QStringList() << mode);
159  }
160 
161  EditSnippet dlg(match, 0, view);
162  dlg.setSnippetText(view->selectionText());
163  int status = dlg.exec();
164  if ( created && status != KDialog::Accepted ) {
165  // cleanup
166  match->remove();
167  }
168 }
169 
170 #include "katesnippetglobal.moc"
KateSnippetGlobal::KateSnippetGlobal
KateSnippetGlobal(QObject *parent, const QVariantList &args=QVariantList())
Definition: katesnippetglobal.cpp:48
QWidget
KateGlobal::container
QObject * container()
Get the currently associated Container object.
Definition: kateglobal.cpp:519
KateSnippetGlobal::insertSnippet
void insertSnippet(Snippet *snippet)
Inserts the given snippet into the currently active view.
Definition: katesnippetglobal.cpp:95
Kate::Script::i18nc
QScriptValue i18nc(QScriptContext *context, QScriptEngine *engine)
i18nc("context", "text", arguments [optional])
Definition: katescripthelpers.cpp:210
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
QObject::sender
QObject * sender() const
SnippetView
This class gets embedded into the right tool view by the KateSnippetGlobal.
Definition: snippetview.h:39
KateDocument::highlightingModeAt
virtual QString highlightingModeAt(const KTextEditor::Cursor &position)
Definition: katedocument.cpp:5419
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KDialog
KateSnippetGlobal::showDialog
void showDialog(KateView *view)
Show the snippet dialog, used by most simple apps using just KatePart.
Definition: katesnippetglobal.cpp:61
snippetview.h
SnippetStore::init
static void init(KateSnippetGlobal *plugin)
Initialize the SnippetStore.
Definition: snippetstore.cpp:61
KateView::selectionRange
virtual const KTextEditor::Range & selectionRange() const
Definition: kateview.cpp:2815
KateDocument::mode
virtual QString mode() const
Return the name of the currently used mode.
Definition: katedocument.cpp:1468
SnippetRepository::setFileTypes
void setFileTypes(const QStringList &filetypes)
Sets the valid filetypes for the snippets contained in this repository.
Definition: snippetrepository.cpp:108
KateSnippetGlobal::insertSnippetFromActionData
void insertSnippetFromActionData()
Definition: katesnippetglobal.cpp:123
SnippetCompletionItem
Definition: snippetcompletionitem.h:41
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
katesnippetglobal.h
KateSnippetGlobal::snippetWidget
QWidget * snippetWidget()
Create a new snippet widget, to allow to manage and insert snippets.
Definition: katesnippetglobal.cpp:90
QObject
EditSnippet::setSnippetText
void setSnippetText(const QString &text)
Definition: editsnippet.cpp:146
SnippetCompletionItem::execute
void execute(KTextEditor::View *view, const KTextEditor::Range &word)
Definition: snippetcompletionitem.cpp:92
QString::isEmpty
bool isEmpty() const
QStandardItem::parent
QStandardItem * parent() const
QVBoxLayout
QList::first
T & first()
QString
SnippetRepository
Each object of this type represents a repository of snippets.
Definition: snippetrepository.h:52
Snippet
One object of this class represents a single snippet.
Definition: snippet.h:42
QStringList
KateView
Definition: kateview.h:77
SnippetCompletionModel
Definition: snippetcompletionmodel.h:39
QStandardItemModel::item
QStandardItem * item(int row, int column) const
editsnippet.h
SnippetRepository::remove
void remove()
Remove this repository from the disk.
Definition: snippetrepository.cpp:156
snippetcompletionmodel.h
SnippetStore::self
static SnippetStore * self()
Retuns the SnippetStore.
Definition: snippetstore.cpp:67
KAction
QStandardItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
KateView::selectionText
virtual QString selectionText() const
Definition: kateview.cpp:2041
EditSnippet
This dialog is used to create/edit snippets in a given repository.
Definition: editsnippet.h:49
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:553
KateSnippetGlobal::createSnippet
void createSnippet(KateView *view)
Create snippet for given view, e.g.
Definition: katesnippetglobal.cpp:132
snippetstore.h
snippetcompletionitem.h
QWidget::actions
QList< QAction * > actions() const
KateSnippetGlobal::~KateSnippetGlobal
~KateSnippetGlobal()
Definition: katesnippetglobal.cpp:55
SnippetRepository::fileTypes
QStringList fileTypes() const
The valid filetypes for the snippets contained in this repository.
Definition: snippetrepository.cpp:103
snippetrepository.h
snippet.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 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