Sonnet

languagefilter.cpp
1 /* This file is part of the KDE libraries
2 
3  SPDX-FileCopyrightText: 2009 Jakub Stachowski <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include <QString>
9 
10 #include "guesslanguage.h"
11 #include "languagefilter_p.h"
12 #include "loader_p.h"
13 #include "settingsimpl_p.h"
14 #include "speller.h"
15 
16 namespace Sonnet
17 {
18 #define MIN_RELIABILITY 0.1
19 #define MAX_ITEMS 5
20 
21 class LanguageFilterPrivate
22 {
23 public:
24  LanguageFilterPrivate(AbstractTokenizer *s)
25  : source(s)
26  {
27  gl.setLimits(MAX_ITEMS, MIN_RELIABILITY);
28  }
29 
30  ~LanguageFilterPrivate()
31  {
32  delete source;
33  }
34 
35  QString mainLanguage() const;
36 
37  AbstractTokenizer *source = nullptr;
38  Token lastToken;
39 
40  mutable QString lastLanguage;
41  mutable QString cachedMainLanguage;
42  QString prevLanguage;
43 
44  GuessLanguage gl;
45  Speller sp;
46 };
47 
48 QString LanguageFilterPrivate::mainLanguage() const
49 {
50  if (cachedMainLanguage.isNull()) {
51  cachedMainLanguage = gl.identify(source->buffer(), QStringList(Loader::openLoader()->settings()->defaultLanguage()));
52  }
53  return cachedMainLanguage;
54 }
55 
56 /* -----------------------------------------------------------------*/
57 
58 LanguageFilter::LanguageFilter(AbstractTokenizer *source)
59  : d(new LanguageFilterPrivate(source))
60 {
61  d->prevLanguage = Loader::openLoader()->settings()->defaultLanguage();
62 }
63 
64 LanguageFilter::LanguageFilter(const LanguageFilter &other)
65  : d(new LanguageFilterPrivate(other.d->source))
66 {
67  d->lastToken = other.d->lastToken;
68  d->lastLanguage = other.d->lastLanguage;
69  d->cachedMainLanguage = other.d->cachedMainLanguage;
70  d->prevLanguage = other.d->prevLanguage;
71 }
72 
73 LanguageFilter::~LanguageFilter()
74 {
75  delete d;
76 }
77 
78 bool LanguageFilter::hasNext() const
79 {
80  return d->source->hasNext();
81 }
82 
83 void LanguageFilter::setBuffer(const QString &buffer)
84 {
85  d->cachedMainLanguage = QString();
86  d->source->setBuffer(buffer);
87 }
88 
89 Token LanguageFilter::next()
90 {
91  d->lastToken = d->source->next();
92  d->prevLanguage = d->lastLanguage;
93  d->lastLanguage = QString();
94  return d->lastToken;
95 }
96 
97 QString LanguageFilter::language() const
98 {
99  if (d->lastLanguage.isNull()) {
100  d->lastLanguage = d->gl.identify(d->lastToken.toString(), QStringList() << d->prevLanguage << Loader::openLoader()->settings()->defaultLanguage());
101  }
102  const QStringList available = d->sp.availableLanguages();
103 
104  // FIXME: do something a little more smart here
105  if (!available.contains(d->lastLanguage)) {
106  for (const QString &lang : available) {
107  if (lang.startsWith(d->lastLanguage)) {
108  d->lastLanguage = lang;
109  break;
110  }
111  }
112  }
113 
114  return d->lastLanguage;
115 }
116 
117 bool LanguageFilter::isSpellcheckable() const
118 {
119  const QString &lastlang = language();
120  if (lastlang.isEmpty()) {
121  return false;
122  }
123 
124  if (d->sp.availableLanguages().contains(lastlang)) {
125  return true;
126  }
127 
128  return false;
129 }
130 
131 QString LanguageFilter::buffer() const
132 {
133  return d->source->buffer();
134 }
135 
136 void LanguageFilter::replace(int position, int len, const QString &newWord)
137 {
138  d->source->replace(position, len, newWord);
139  // FIXME: fix lastToken
140  // uncache language for current token - it may have changed
141  d->lastLanguage = QString();
142 }
143 }
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & replace(int position, int n, QChar after)
The sonnet namespace.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:14:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.