• 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
snippetfilterproxymodel.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 "snippetfilterproxymodel.h"
23 
24 #include "snippetstore.h"
25 #include "snippet.h"
26 
27 SnippetFilterProxyModel::SnippetFilterProxyModel(QObject *parent)
28  : QSortFilterProxyModel(parent)
29 {
30  connect(SnippetStore::self(),
31  SIGNAL(dataChanged(QModelIndex,QModelIndex)),
32  this,
33  SLOT(dataChanged(QModelIndex,QModelIndex)));
34 }
35 
36 
37 SnippetFilterProxyModel::~SnippetFilterProxyModel()
38 {
39 }
40 
41 QVariant SnippetFilterProxyModel::data(const QModelIndex& index, int role) const
42 {
43  if (role == Qt::DisplayRole && index.parent().isValid()) {
44  // in the view, also show prefix, postfix and arguments
45  Snippet* snippet = dynamic_cast<Snippet*>( SnippetStore::self()->itemFromIndex(mapToSource(index)) );
46  if (snippet) {
47  QString ret = snippet->prefix() + ' ' + snippet->text() + snippet->arguments() + ' ' + snippet->postfix();
48  return ret.trimmed();
49  }
50  }
51  return QSortFilterProxyModel::data(index, role);
52 }
53 
54 void SnippetFilterProxyModel::changeFilter(const QString& filter)
55 {
56  filter_ = filter;
57  clear();
58 }
59 
60 bool SnippetFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
61 {
62  if (filter_.isEmpty()) {
63  // No filtering needed...
64  return true;
65  }
66 
67  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
68 
69  QStandardItem* item = SnippetStore::self()->itemFromIndex( index );
70  if (!item)
71  return false;
72 
73  Snippet* snippet = dynamic_cast<Snippet*>( item );
74  if (snippet) {
75  if ( snippet->text().contains( filter_) )
76  return true;
77  else
78  return false;
79  }
80 
81  // if it's not a snippet; allow it...
82  return true;
83 }
84 
85 void SnippetFilterProxyModel::dataChanged(const QModelIndex& /*topLeft*/, const QModelIndex& /*bottomRight*/)
86 {
87  // If we don't do this, the model will contain strange QModelIndex elements after a
88  // sync of a repository. Stangely this only happens on Linux. When running under Windows
89  // everything's ok, evan without this hack.
90  // By letting the proxy reevaluate the items, these elements will be removed.
91 
93  clear();
94 }
95 
96 #include "snippetfilterproxymodel.moc"
QSortFilterProxyModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QModelIndex
snippetfilterproxymodel.h
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
SnippetFilterProxyModel::~SnippetFilterProxyModel
~SnippetFilterProxyModel()
Definition: snippetfilterproxymodel.cpp:37
QSortFilterProxyModel::clear
void clear()
SnippetFilterProxyModel::SnippetFilterProxyModel
SnippetFilterProxyModel(QObject *parent)
Definition: snippetfilterproxymodel.cpp:27
QStandardItem::text
QString text() const
SnippetFilterProxyModel::changeFilter
void changeFilter(const QString &filter)
Definition: snippetfilterproxymodel.cpp:54
QModelIndex::isValid
bool isValid() const
QObject
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
SnippetFilterProxyModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: snippetfilterproxymodel.cpp:41
QString
QModelIndex::parent
QModelIndex parent() const
Snippet
One object of this class represents a single snippet.
Definition: snippet.h:42
Snippet::prefix
QString prefix() const
Returns the display prefix of this snippet.
Definition: snippet.cpp:57
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QSortFilterProxyModel
QAbstractProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QStandardItemModel::itemFromIndex
QStandardItem * itemFromIndex(const QModelIndex &index) const
SnippetStore::self
static SnippetStore * self()
Retuns the SnippetStore.
Definition: snippetstore.cpp:67
QStandardItem
Snippet::arguments
QString arguments() const
Returns the display arguments of this snippet.
Definition: snippet.cpp:77
snippetstore.h
Snippet::postfix
QString postfix() const
Returns the display postfix of this snippet.
Definition: snippet.cpp:67
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QSortFilterProxyModel::data
virtual QVariant data(const QModelIndex &index, int role) const
snippet.h
QVariant
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