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

kget

  • sources
  • kde-4.14
  • kdenetwork
  • kget
  • ui
  • linkview
kget_sortfilterproxymodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "kget_sortfilterproxymodel.h"
21 
22 #include <QtCore/QStringList>
23 
24 static const QString ARCHIVES = QString("/x-7z-compressed,/x-ace,/x-archive,/x-arj,/x-bzip,/x-bzip-compressed-tar,/x-compressed-tar,/x-deb/,/x-rar,/x-tar,/x-rpm,/x-tarz,/zip");
25 static const QString WEB_CONTENT = QString("/html,/x-asp,/xhtml+xml,/x-php,");
26 
27 KGetSortFilterProxyModel::KGetSortFilterProxyModel(int column, QObject *parent)
28  : QSortFilterProxyModel(parent),
29  m_filterType(NoFilter),
30  m_filterMode(Contain),
31  m_column(column),
32  m_showWebContent(false)
33 {
34  m_mimeTypes.insert(NoFilter, "");
35  m_mimeTypes.insert(VideoFiles, "video/");
36  m_mimeTypes.insert(AudioFiles, "audio/");
37  m_mimeTypes.insert(CompressedFiles, "archive/");
38  m_mimeTypes.insert(ImageFiles, "image/");
39 }
40 
41 KGetSortFilterProxyModel::~KGetSortFilterProxyModel()
42 {
43 }
44 
45 bool KGetSortFilterProxyModel::showWebContent() const
46 {
47  return m_showWebContent;
48 }
49 
50 void KGetSortFilterProxyModel::setFilterType(int filterType)
51 {
52  m_filterType = filterType;
53  invalidateFilter();
54 }
55 
56 void KGetSortFilterProxyModel::setFilterMode(int filterMode)
57 {
58  switch (filterMode)
59  {
60  case DoesNotContain:
61  m_filterMode = DoesNotContain;
62  break;
63  case Contain:
64  default:
65  m_filterMode = Contain;
66  }
67 
68  invalidateFilter();
69 }
70 
71 void KGetSortFilterProxyModel::setShowWebContent(bool show)
72 {
73  m_showWebContent = show;
74  invalidateFilter();
75 }
76 
77 void KGetSortFilterProxyModel::setShowWebContent(int show)
78 {
79  m_showWebContent = show;
80  invalidateFilter();
81 }
82 
83 void KGetSortFilterProxyModel::setFilterColumn(int column)
84 {
85  m_column = column;
86  invalidateFilter();
87 }
88 
89 bool KGetSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
90 {
91  const QModelIndex index = sourceModel()->index(sourceRow, 1, sourceParent);
92  if (!index.isValid() || index.data(Qt::UserRole).toString().isEmpty())
93  {
94  return false;
95  }
96 
97  const QString meta = index.data(Qt::UserRole).toString();
98  const QString text = columnText(sourceRow, sourceParent);
99  bool show = false;
100 
101 
102  //do not show entries if their text is empty when not using NoFilter and m_showWebContent
103  if (!text.isEmpty() && (m_filterType != NoFilter))
104  {
105  show = meta.startsWith(m_mimeTypes[m_filterType]);
106 
107  if (m_filterType == CompressedFiles)
108  {
109  show = ARCHIVES.contains(meta.mid(meta.indexOf('/')));
110  }
111  }
112  else if (m_filterType == NoFilter)
113  {
114  if (m_showWebContent)
115  {
116  show = true;
117  }
118  else
119  {
120  show = !text.isEmpty() && !WEB_CONTENT.contains(meta.mid(meta.indexOf('/')));
121  }
122  }
123 
124  if (show) {
125  show = acceptText(text);
126  }
127 
128  return show;
129 }
130 
131 QString KGetSortFilterProxyModel::columnText(int row, const QModelIndex &sourceParent) const
132 {
133  const QModelIndex index = sourceModel()->index(row, m_column, sourceParent);
134  return (index.isValid() ? index.data(Qt::DisplayRole).toString() : QString());
135 }
136 
137 bool KGetSortFilterProxyModel::acceptText(const QString &text) const
138 {
139  //look if the text-filter matches
140  const QRegExp re = filterRegExp();
141  bool accept = (re.indexIn(text) != -1) ? true : false;
142  if ((m_filterMode == DoesNotContain) && !re.isEmpty()) {
143  accept = !accept;
144  }
145 
146  return accept;
147 }
QSortFilterProxyModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QModelIndex
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QHash::insert
iterator insert(const Key &key, const T &value)
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
KGetSortFilterProxyModel::filterAcceptsRow
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Definition: kget_sortfilterproxymodel.cpp:89
KGetSortFilterProxyModel::CompressedFiles
Definition: kget_sortfilterproxymodel.h:37
QRegExp::isEmpty
bool isEmpty() const
KGetSortFilterProxyModel::AudioFiles
Definition: kget_sortfilterproxymodel.h:36
KGetSortFilterProxyModel::VideoFiles
Definition: kget_sortfilterproxymodel.h:35
KGetSortFilterProxyModel::Contain
Definition: kget_sortfilterproxymodel.h:42
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QModelIndex::isValid
bool isValid() const
QObject
WEB_CONTENT
static const QString WEB_CONTENT
Definition: kget_sortfilterproxymodel.cpp:25
QString::isEmpty
bool isEmpty() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QSortFilterProxyModel::invalidateFilter
void invalidateFilter()
KGetSortFilterProxyModel::NoFilter
Definition: kget_sortfilterproxymodel.h:34
KGetSortFilterProxyModel::ImageFiles
Definition: kget_sortfilterproxymodel.h:38
QString
KGetSortFilterProxyModel::~KGetSortFilterProxyModel
virtual ~KGetSortFilterProxyModel()
Definition: kget_sortfilterproxymodel.cpp:41
KGetSortFilterProxyModel::showWebContent
bool showWebContent() const
Definition: kget_sortfilterproxymodel.cpp:45
KGetSortFilterProxyModel::setShowWebContent
void setShowWebContent(bool show)
Definition: kget_sortfilterproxymodel.cpp:71
ARCHIVES
static const QString ARCHIVES
Definition: kget_sortfilterproxymodel.cpp:24
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QSortFilterProxyModel
KGetSortFilterProxyModel::setFilterType
void setFilterType(int filterType)
Definition: kget_sortfilterproxymodel.cpp:50
QAbstractProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
QString::mid
QString mid(int position, int n) const
QModelIndex::data
QVariant data(int role) const
KGetSortFilterProxyModel::setFilterMode
void setFilterMode(int filterMode)
Definition: kget_sortfilterproxymodel.cpp:56
KGetSortFilterProxyModel::setFilterColumn
void setFilterColumn(int column)
Definition: kget_sortfilterproxymodel.cpp:83
kget_sortfilterproxymodel.h
KGetSortFilterProxyModel::DoesNotContain
Definition: kget_sortfilterproxymodel.h:43
QVariant::toString
QString toString() const
KGetSortFilterProxyModel::KGetSortFilterProxyModel
KGetSortFilterProxyModel(int sortColumn, QObject *parent=0)
Definition: kget_sortfilterproxymodel.cpp:27
QSortFilterProxyModel::filterRegExp
QRegExp filterRegExp() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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