• 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
  • spellcheck
spellingmenu.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2009-2010 by Michel Ludwig <michel.ludwig@kdemail.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "spellingmenu.h"
22 #include "spellingmenu.moc"
23 
24 #include "katedocument.h"
25 #include "kateglobal.h"
26 #include "kateview.h"
27 #include "spellcheck/spellcheck.h"
28 
29 #include <kdebug.h>
30 
31 KateSpellingMenu::KateSpellingMenu(KateView *view)
32  : QObject(view),
33  m_view (view),
34  m_spellingMenuAction(NULL),
35  m_ignoreWordAction(NULL),
36  m_addToDictionaryAction(NULL),
37  m_spellingMenu(NULL),
38  m_currentMisspelledRange(NULL), // we have to use 'm_currentMisspelledRange'
39  // as QSignalMapper doesn't work with pairs of objects;
40  // it just points to the object pointed to by either
41  // 'm_currentMouseMisspelledRange' or 'm_currentCaretMisspelledRange'
42  m_currentMouseMisspelledRange(NULL),
43  m_currentCaretMisspelledRange(NULL),
44  m_useMouseForMisspelledRange(false),
45  m_suggestionsSignalMapper(new QSignalMapper(this))
46 {
47  connect(m_suggestionsSignalMapper, SIGNAL(mapped(QString)),
48  this, SLOT(replaceWordBySuggestion(QString)));
49 }
50 
51 KateSpellingMenu::~KateSpellingMenu()
52 {
53  m_currentMisspelledRange = NULL; // it shouldn't be accessed anymore as it could
54  // point to a non-existing object (bug 226724)
55  // (for example, when it pointed to m_currentCaretMisspelledRange
56  // and that range got deleted after the caret had left)
57  m_currentCaretMisspelledRange = NULL;
58  m_currentMouseMisspelledRange = NULL;
59 }
60 
61 bool KateSpellingMenu::isEnabled() const
62 {
63  if(!m_spellingMenuAction) {
64  return false;
65  }
66  return m_spellingMenuAction->isEnabled();
67 }
68 
69 void KateSpellingMenu::setEnabled(bool b)
70 {
71  if(m_spellingMenuAction) {
72  m_spellingMenuAction->setEnabled(b);
73  }
74  if(m_ignoreWordAction) {
75  m_ignoreWordAction->setEnabled(b);
76  }
77  if(m_addToDictionaryAction) {
78  m_addToDictionaryAction->setEnabled(b);
79  }
80 }
81 
82 void KateSpellingMenu::setVisible(bool b)
83 {
84  if(m_spellingMenuAction) {
85  m_spellingMenuAction->setVisible(b);
86  }
87  if(m_ignoreWordAction) {
88  m_ignoreWordAction->setVisible(b);
89  }
90  if(m_addToDictionaryAction) {
91  m_addToDictionaryAction->setVisible(b);
92  }
93 }
94 
95 void KateSpellingMenu::createActions(KActionCollection *ac)
96 {
97  m_spellingMenuAction = new KActionMenu(i18n("Spelling"), this);
98  ac->addAction("spelling_suggestions", m_spellingMenuAction);
99  m_spellingMenu = m_spellingMenuAction->menu();
100  connect(m_spellingMenu, SIGNAL(aboutToShow()), this, SLOT(populateSuggestionsMenu()));
101 
102  m_ignoreWordAction = new KAction(i18n("Ignore Word"), this);
103  connect(m_ignoreWordAction, SIGNAL(triggered()), this, SLOT(ignoreCurrentWord()));
104 
105  m_addToDictionaryAction = new KAction(i18n("Add to Dictionary"), this);
106  connect(m_addToDictionaryAction, SIGNAL(triggered()), this, SLOT(addCurrentWordToDictionary()));
107 
108  setEnabled(false);
109  setVisible(false);
110 }
111 
112 void KateSpellingMenu::caretEnteredMisspelledRange(KTextEditor::MovingRange *range)
113 {
114  if(m_currentCaretMisspelledRange == range) {
115  return;
116  }
117  m_currentCaretMisspelledRange = NULL;
118  setEnabled(true);
119  m_currentCaretMisspelledRange = range;
120 }
121 
122 void KateSpellingMenu::caretExitedMisspelledRange(KTextEditor::MovingRange *range)
123 {
124  if(range != m_currentCaretMisspelledRange) { // order of 'exit' and 'entered' signals
125  return; // was wrong
126  }
127  setEnabled(false);
128  m_currentCaretMisspelledRange = NULL;
129 }
130 
131 void KateSpellingMenu::mouseEnteredMisspelledRange(KTextEditor::MovingRange *range)
132 {
133  if(m_currentMouseMisspelledRange == range) {
134  return;
135  }
136  m_currentMouseMisspelledRange = range;
137 }
138 
139 void KateSpellingMenu::mouseExitedMisspelledRange(KTextEditor::MovingRange *range)
140 {
141  if(range != m_currentMouseMisspelledRange) { // order of 'exit' and 'entered' signals
142  return; // was wrong
143  }
144  m_currentMouseMisspelledRange = NULL;
145 }
146 
147 void KateSpellingMenu::rangeDeleted(KTextEditor::MovingRange *range)
148 {
149  if(m_currentCaretMisspelledRange == range) {
150  m_currentCaretMisspelledRange = NULL;
151  }
152  if(m_currentMouseMisspelledRange == range) {
153  m_currentMouseMisspelledRange = NULL;
154  }
155  if(m_currentMisspelledRange == range) {
156  m_currentMisspelledRange = NULL;
157  }
158  setEnabled(m_currentCaretMisspelledRange != NULL);
159 }
160 
161 void KateSpellingMenu::setUseMouseForMisspelledRange(bool b)
162 {
163  m_useMouseForMisspelledRange = b;
164  if(m_useMouseForMisspelledRange) {
165  setEnabled(m_currentMouseMisspelledRange != NULL);
166  }
167  else if(!m_useMouseForMisspelledRange) {
168  setEnabled(m_currentCaretMisspelledRange != NULL);
169  }
170 }
171 
172 void KateSpellingMenu::populateSuggestionsMenu()
173 {
174  m_spellingMenu->clear();
175  if((m_useMouseForMisspelledRange && !m_currentMouseMisspelledRange)
176  || (!m_useMouseForMisspelledRange && !m_currentCaretMisspelledRange)) {
177  return;
178  }
179  m_currentMisspelledRange = (m_useMouseForMisspelledRange ? m_currentMouseMisspelledRange
180  : m_currentCaretMisspelledRange);
181  m_spellingMenu->addAction(m_ignoreWordAction);
182  m_spellingMenu->addAction(m_addToDictionaryAction);
183  m_spellingMenu->addSeparator();
184  const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
185  const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
186  m_currentSuggestions = KateGlobal::self()->spellCheckManager()->suggestions(misspelledWord, dictionary);
187 
188  int counter = 0;
189  for(QStringList::iterator i = m_currentSuggestions.begin(); i != m_currentSuggestions.end() && counter < 10; ++i) {
190  const QString& suggestion = *i;
191  KAction *action = new KAction(suggestion, m_spellingMenu);
192  connect(action, SIGNAL(triggered()), m_suggestionsSignalMapper, SLOT(map()));
193  m_suggestionsSignalMapper->setMapping(action, suggestion);
194  m_spellingMenu->addAction(action);
195  ++counter;
196  }
197 }
198 
199 void KateSpellingMenu::replaceWordBySuggestion(const QString& suggestion)
200 {
201  KateDocument *doc = m_view->doc();
202  KateGlobal::self()->spellCheckManager()->replaceCharactersEncodedIfNecessary(suggestion, doc, *m_currentMisspelledRange);
203 }
204 
205 void KateSpellingMenu::addCurrentWordToDictionary()
206 {
207  if(!m_currentMisspelledRange) {
208  return;
209  }
210  const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
211  const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
212  KateGlobal::self()->spellCheckManager()->addToDictionary(misspelledWord, dictionary);
213  m_view->doc()->clearMisspellingForWord(misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here!
214 }
215 
216 void KateSpellingMenu::ignoreCurrentWord()
217 {
218  if(!m_currentMisspelledRange) {
219  return;
220  }
221  const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
222  const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
223  KateGlobal::self()->spellCheckManager()->ignoreWord(misspelledWord, dictionary);
224  m_view->doc()->clearMisspellingForWord(misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here!
225 }
226 
227 // kate: space-indent on; indent-width 2; replace-tabs on;
KateGlobal::spellCheckManager
KateSpellCheckManager * spellCheckManager()
spell check manager
Definition: kateglobal.h:345
kateview.h
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
KateDocument::dictionaryForMisspelledRange
QString dictionaryForMisspelledRange(const KTextEditor::Range &range) const
Definition: katedocument.cpp:5172
KateSpellingMenu::addCurrentWordToDictionary
void addCurrentWordToDictionary()
Definition: spellingmenu.cpp:205
KateSpellingMenu::m_currentMisspelledRange
KTextEditor::MovingRange * m_currentMisspelledRange
Definition: spellingmenu.h:68
KateSpellingMenu::createActions
void createActions(KActionCollection *ac)
Definition: spellingmenu.cpp:95
KateSpellingMenu::rangeDeleted
void rangeDeleted(KTextEditor::MovingRange *range)
Definition: spellingmenu.cpp:147
KateDocument::clearMisspellingForWord
void clearMisspellingForWord(const QString &word)
Definition: katedocument.cpp:5181
katedocument.h
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KateSpellingMenu::m_addToDictionaryAction
KAction * m_addToDictionaryAction
Definition: spellingmenu.h:66
KateSpellingMenu::m_currentMouseMisspelledRange
KTextEditor::MovingRange * m_currentMouseMisspelledRange
Definition: spellingmenu.h:69
KateSpellingMenu::setEnabled
void setEnabled(bool b)
Definition: spellingmenu.cpp:69
spellcheck.h
KateSpellingMenu::m_suggestionsSignalMapper
QSignalMapper * m_suggestionsSignalMapper
Definition: spellingmenu.h:73
KateSpellingMenu::populateSuggestionsMenu
void populateSuggestionsMenu()
Definition: spellingmenu.cpp:172
KateSpellingMenu::m_spellingMenu
KMenu * m_spellingMenu
Definition: spellingmenu.h:67
KateSpellingMenu::KateSpellingMenu
KateSpellingMenu(KateView *view)
Definition: spellingmenu.cpp:31
QSignalMapper::setMapping
void setMapping(QObject *sender, int id)
KateSpellingMenu::isEnabled
bool isEnabled() const
Definition: spellingmenu.cpp:61
QObject
kateglobal.h
KateSpellingMenu::setVisible
void setVisible(bool b)
Definition: spellingmenu.cpp:82
KateSpellCheckManager::addToDictionary
void addToDictionary(const QString &word, const QString &dictionary)
Definition: spellcheck.cpp:61
KateSpellingMenu::ignoreCurrentWord
void ignoreCurrentWord()
Definition: spellingmenu.cpp:216
KateSpellingMenu::m_useMouseForMisspelledRange
bool m_useMouseForMisspelledRange
Definition: spellingmenu.h:71
QString
KateSpellingMenu::m_currentSuggestions
QStringList m_currentSuggestions
Definition: spellingmenu.h:72
KateSpellingMenu::caretEnteredMisspelledRange
void caretEnteredMisspelledRange(KTextEditor::MovingRange *range)
Definition: spellingmenu.cpp:112
KateSpellCheckManager::suggestions
QStringList suggestions(const QString &word, const QString &dictionary)
Definition: spellcheck.cpp:47
QList::iterator
KateSpellingMenu::m_view
KateView * m_view
Definition: spellingmenu.h:64
KateView
Definition: kateview.h:77
QList::end
iterator end()
KateSpellingMenu::m_currentCaretMisspelledRange
KTextEditor::MovingRange * m_currentCaretMisspelledRange
Definition: spellingmenu.h:70
KateDocument
Definition: katedocument.h:74
KateSpellingMenu::mouseExitedMisspelledRange
void mouseExitedMisspelledRange(KTextEditor::MovingRange *range)
Definition: spellingmenu.cpp:139
KateSpellCheckManager::ignoreWord
void ignoreWord(const QString &word, const QString &dictionary)
Definition: spellcheck.cpp:54
KateSpellingMenu::m_spellingMenuAction
KActionMenu * m_spellingMenuAction
Definition: spellingmenu.h:65
KActionMenu
spellingmenu.h
KateSpellingMenu::m_ignoreWordAction
KAction * m_ignoreWordAction
Definition: spellingmenu.h:66
KateSpellingMenu::caretExitedMisspelledRange
void caretExitedMisspelledRange(KTextEditor::MovingRange *range)
Definition: spellingmenu.cpp:122
KateSpellingMenu::setUseMouseForMisspelledRange
void setUseMouseForMisspelledRange(bool b)
This method has to be called before the menu is shown in response to a context menu event...
Definition: spellingmenu.cpp:161
KAction
KateSpellingMenu::replaceWordBySuggestion
void replaceWordBySuggestion(const QString &suggestion)
Definition: spellingmenu.cpp:199
KateDocument::text
virtual QString text(const KTextEditor::Range &range, bool blockwise=false) const
Definition: katedocument.cpp:337
KateSpellingMenu::mouseEnteredMisspelledRange
void mouseEnteredMisspelledRange(KTextEditor::MovingRange *range)
Definition: spellingmenu.cpp:131
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:553
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KateSpellingMenu::~KateSpellingMenu
virtual ~KateSpellingMenu()
Definition: spellingmenu.cpp:51
QSignalMapper
QList::begin
iterator begin()
KateSpellCheckManager::replaceCharactersEncodedIfNecessary
void replaceCharactersEncodedIfNecessary(const QString &newWord, KateDocument *doc, const KTextEditor::Range &replacementRange)
Definition: spellcheck.cpp:239
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