swift
modelfilter.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QtGlobal>
7 
13 #include "misc/countrylist.h"
14 #include "misc/identifierlist.h"
15 #include "misc/logmessage.h"
20 #include "misc/network/userlist.h"
24 #include "misc/statusmessagelist.h"
25 
26 using namespace swift::misc;
27 
28 namespace swift::gui::models
29 {
30  template <class ContainerType>
31  bool IModelFilter<ContainerType>::stringMatchesFilterExpression(const QString &value, const QString &filter,
32  Qt::CaseSensitivity cs) const
33  {
34  QString v = value.trimmed();
35  QString f = filter.trimmed();
36 
37  if (v.isEmpty() && f.isEmpty()) { return true; }
38  if (v.isEmpty()) { return false; }
39 
40  // no wildcard, just string matching
41  if (!filter.contains('*')) { return (v.indexOf(f, 0, cs) == 0) && (v.length() == f.length()); }
42 
43  const QString filterNoWildcard = stripWildcard(f);
44 
45  // included?
46  if (f.startsWith('*') && f.endsWith('*')) { return v.contains(filterNoWildcard, cs); }
47 
48  // ends with
49  if (f.endsWith('*')) { return v.startsWith(filterNoWildcard, cs); }
50 
51  // starting with
52  if (f.startsWith('*')) { return v.endsWith(filterNoWildcard, cs); }
53 
54  // wildcard in middle
55  if (f.contains('*'))
56  {
57  const QStringList parts = v.split('*');
58  if (parts.size() < 2) { return false; }
59  const bool s = v.startsWith(parts.front(), cs) && v.endsWith(parts.back());
60  return s;
61  }
62 
63  // should never happen
64  CLogMessage(this).error(u"Illegal search pattern : '%1'") << f;
65  return false;
66  }
67 
68  template <class ContainerType>
69  QString IModelFilter<ContainerType>::stripWildcard(const QString &value) const
70  {
71  QString sw(value);
72  return sw.remove('*');
73  }
74 
75  // Forward instantiations
79 
86 
91 
95 
96 } // namespace swift::gui::models
Model filter interface.
Definition: modelfilter.h:21
Class for emitting a log message.
Definition: logmessage.h:27
Derived & error(const char16_t(&format)[N])
Set the severity to error, providing a format string.
Models to be used with views, mainly QTableView.
Free functions in swift::misc.