Kate
spellingmenu.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2009-2010 by Michel Ludwig <michel.ludwig@kdemail.net> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "spellingmenu.h" 00022 #include "spellingmenu.moc" 00023 00024 #include "katedocument.h" 00025 #include "kateglobal.h" 00026 #include "kateview.h" 00027 #include "spellcheck/spellcheck.h" 00028 00029 #include <kdebug.h> 00030 00031 KateSpellingMenu::KateSpellingMenu(KateView *view) 00032 : QObject(view), 00033 m_view (view), 00034 m_spellingMenuAction(NULL), 00035 m_ignoreWordAction(NULL), 00036 m_addToDictionaryAction(NULL), 00037 m_spellingMenu(NULL), 00038 m_currentMisspelledRange(NULL), // we have to use 'm_currentMisspelledRange' 00039 // as QSignalMapper doesn't work with pairs of objects; 00040 // it just points to the object pointed to by either 00041 // 'm_currentMouseMisspelledRange' or 'm_currentCaretMisspelledRange' 00042 m_currentMouseMisspelledRange(NULL), 00043 m_currentCaretMisspelledRange(NULL), 00044 m_useMouseForMisspelledRange(false), 00045 m_suggestionsSignalMapper(new QSignalMapper(this)) 00046 { 00047 connect(m_suggestionsSignalMapper, SIGNAL(mapped(const QString&)), 00048 this, SLOT(replaceWordBySuggestion(const QString&))); 00049 } 00050 00051 KateSpellingMenu::~KateSpellingMenu() 00052 { 00053 m_currentMisspelledRange = NULL; // it shouldn't be accessed anymore as it could 00054 // point to a non-existing object (bug 226724) 00055 // (for example, when it pointed to m_currentCaretMisspelledRange 00056 // and that range got deleted after the caret had left) 00057 m_currentCaretMisspelledRange = NULL; 00058 m_currentMouseMisspelledRange = NULL; 00059 } 00060 00061 bool KateSpellingMenu::isEnabled() const 00062 { 00063 if(!m_spellingMenuAction) { 00064 return false; 00065 } 00066 return m_spellingMenuAction->isEnabled(); 00067 } 00068 00069 void KateSpellingMenu::setEnabled(bool b) 00070 { 00071 if(m_spellingMenuAction) { 00072 m_spellingMenuAction->setEnabled(b); 00073 } 00074 if(m_ignoreWordAction) { 00075 m_ignoreWordAction->setEnabled(b); 00076 } 00077 if(m_addToDictionaryAction) { 00078 m_addToDictionaryAction->setEnabled(b); 00079 } 00080 } 00081 00082 void KateSpellingMenu::setVisible(bool b) 00083 { 00084 if(m_spellingMenuAction) { 00085 m_spellingMenuAction->setVisible(b); 00086 } 00087 if(m_ignoreWordAction) { 00088 m_ignoreWordAction->setVisible(b); 00089 } 00090 if(m_addToDictionaryAction) { 00091 m_addToDictionaryAction->setVisible(b); 00092 } 00093 } 00094 00095 void KateSpellingMenu::createActions(KActionCollection *ac) 00096 { 00097 m_spellingMenuAction = new KActionMenu(i18n("Spelling"), this); 00098 ac->addAction("spelling_suggestions", m_spellingMenuAction); 00099 m_spellingMenu = m_spellingMenuAction->menu(); 00100 connect(m_spellingMenu, SIGNAL(aboutToShow()), this, SLOT(populateSuggestionsMenu())); 00101 00102 m_ignoreWordAction = new KAction(i18n("Ignore Word"), this); 00103 connect(m_ignoreWordAction, SIGNAL(triggered()), this, SLOT(ignoreCurrentWord())); 00104 00105 m_addToDictionaryAction = new KAction(i18n("Add to Dictionary"), this); 00106 connect(m_addToDictionaryAction, SIGNAL(triggered()), this, SLOT(addCurrentWordToDictionary())); 00107 00108 setEnabled(false); 00109 setVisible(false); 00110 } 00111 00112 void KateSpellingMenu::caretEnteredMisspelledRange(KTextEditor::MovingRange *range) 00113 { 00114 if(m_currentCaretMisspelledRange == range) { 00115 return; 00116 } 00117 m_currentCaretMisspelledRange = NULL; 00118 setEnabled(true); 00119 m_currentCaretMisspelledRange = range; 00120 } 00121 00122 void KateSpellingMenu::caretExitedMisspelledRange(KTextEditor::MovingRange *range) 00123 { 00124 if(range != m_currentCaretMisspelledRange) { // order of 'exit' and 'entered' signals 00125 return; // was wrong 00126 } 00127 setEnabled(false); 00128 m_currentCaretMisspelledRange = NULL; 00129 } 00130 00131 void KateSpellingMenu::mouseEnteredMisspelledRange(KTextEditor::MovingRange *range) 00132 { 00133 if(m_currentMouseMisspelledRange == range) { 00134 return; 00135 } 00136 m_currentMouseMisspelledRange = range; 00137 } 00138 00139 void KateSpellingMenu::mouseExitedMisspelledRange(KTextEditor::MovingRange *range) 00140 { 00141 if(range != m_currentMouseMisspelledRange) { // order of 'exit' and 'entered' signals 00142 return; // was wrong 00143 } 00144 m_currentMouseMisspelledRange = NULL; 00145 } 00146 00147 void KateSpellingMenu::rangeDeleted(KTextEditor::MovingRange *range) 00148 { 00149 if(m_currentCaretMisspelledRange == range) { 00150 m_currentCaretMisspelledRange = NULL; 00151 } 00152 if(m_currentMouseMisspelledRange == range) { 00153 m_currentMouseMisspelledRange = NULL; 00154 } 00155 if(m_currentMisspelledRange == range) { 00156 m_currentMisspelledRange = NULL; 00157 } 00158 setEnabled(m_currentCaretMisspelledRange != NULL); 00159 } 00160 00161 void KateSpellingMenu::setUseMouseForMisspelledRange(bool b) 00162 { 00163 m_useMouseForMisspelledRange = b; 00164 if(m_useMouseForMisspelledRange) { 00165 setEnabled(m_currentMouseMisspelledRange != NULL); 00166 } 00167 else if(!m_useMouseForMisspelledRange) { 00168 setEnabled(m_currentCaretMisspelledRange != NULL); 00169 } 00170 } 00171 00172 void KateSpellingMenu::populateSuggestionsMenu() 00173 { 00174 m_spellingMenu->clear(); 00175 if((m_useMouseForMisspelledRange && !m_currentMouseMisspelledRange) 00176 || (!m_useMouseForMisspelledRange && !m_currentCaretMisspelledRange)) { 00177 return; 00178 } 00179 m_currentMisspelledRange = (m_useMouseForMisspelledRange ? m_currentMouseMisspelledRange 00180 : m_currentCaretMisspelledRange); 00181 m_spellingMenu->addAction(m_ignoreWordAction); 00182 m_spellingMenu->addAction(m_addToDictionaryAction); 00183 m_spellingMenu->addSeparator(); 00184 const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange); 00185 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange); 00186 m_currentSuggestions = KateGlobal::self()->spellCheckManager()->suggestions(misspelledWord, dictionary); 00187 00188 int counter = 0; 00189 for(QStringList::iterator i = m_currentSuggestions.begin(); i != m_currentSuggestions.end() && counter < 10; ++i) { 00190 const QString& suggestion = *i; 00191 KAction *action = new KAction(suggestion, m_spellingMenu); 00192 connect(action, SIGNAL(triggered()), m_suggestionsSignalMapper, SLOT(map())); 00193 m_suggestionsSignalMapper->setMapping(action, suggestion); 00194 m_spellingMenu->addAction(action); 00195 ++counter; 00196 } 00197 } 00198 00199 void KateSpellingMenu::replaceWordBySuggestion(const QString& suggestion) 00200 { 00201 KateDocument *doc = m_view->doc(); 00202 KateGlobal::self()->spellCheckManager()->replaceCharactersEncodedIfNecessary(suggestion, doc, *m_currentMisspelledRange); 00203 } 00204 00205 void KateSpellingMenu::addCurrentWordToDictionary() 00206 { 00207 if(!m_currentMisspelledRange) { 00208 return; 00209 } 00210 const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange); 00211 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange); 00212 KateGlobal::self()->spellCheckManager()->addToDictionary(misspelledWord, dictionary); 00213 m_view->doc()->clearMisspellingForWord(misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here! 00214 } 00215 00216 void KateSpellingMenu::ignoreCurrentWord() 00217 { 00218 if(!m_currentMisspelledRange) { 00219 return; 00220 } 00221 const QString& misspelledWord = m_view->doc()->text(*m_currentMisspelledRange); 00222 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange); 00223 KateGlobal::self()->spellCheckManager()->ignoreWord(misspelledWord, dictionary); 00224 m_view->doc()->clearMisspellingForWord(misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here! 00225 } 00226 00227 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference