7#include "spellingmenu.h"
9#include "katedocument.h"
10#include "kateglobal.h"
12#include "ontheflycheck.h"
13#include "spellcheck/spellcheck.h"
15#include "katepartdebug.h"
17#include <QActionGroup>
20#include <KLocalizedString>
21#include <KTextEditor/Range>
23KateSpellingMenu::KateSpellingMenu(KTextEditor::ViewPrivate *view)
26 , m_spellingMenuAction(nullptr)
27 , m_ignoreWordAction(nullptr)
28 , m_addToDictionaryAction(nullptr)
29 , m_spellingMenu(nullptr)
30 , m_currentMisspelledRange(nullptr)
34KateSpellingMenu::~KateSpellingMenu()
36 m_currentMisspelledRange =
nullptr;
39bool KateSpellingMenu::isEnabled()
const
41 if (!m_spellingMenuAction) {
44 return m_spellingMenuAction->isEnabled();
47bool KateSpellingMenu::isVisible()
const
49 if (!m_spellingMenuAction) {
52 return m_spellingMenuAction->isVisible();
55void KateSpellingMenu::setEnabled(
bool b)
57 if (m_spellingMenuAction) {
58 m_spellingMenuAction->setEnabled(b);
62void KateSpellingMenu::setVisible(
bool b)
64 if (m_spellingMenuAction) {
65 m_spellingMenuAction->setVisible(b);
71 m_spellingMenuAction =
new KActionMenu(
i18n(
"Spelling"),
this);
72 ac->
addAction(QStringLiteral(
"spelling_suggestions"), m_spellingMenuAction);
73 m_spellingMenu = m_spellingMenuAction->
menu();
76 m_ignoreWordAction =
new QAction(
i18n(
"Ignore Word"),
this);
79 m_addToDictionaryAction =
new QAction(
i18n(
"Add to Dictionary"),
this);
82 m_dictionaryGroup =
new QActionGroup(
this);
83 QMapIterator<QString, QString> i(Sonnet::Speller().preferredDictionaries());
86 QAction *action = m_dictionaryGroup->addAction(i.key());
89 connect(m_dictionaryGroup, &QActionGroup::triggered, [
this](QAction *action) {
90 if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) {
91 const bool blockmode = m_view->blockSelection();
92 m_view->doc()->setDictionary(action->
data().
toString(), m_selectedRange, blockmode);
101 if (m_currentMisspelledRange == range) {
104 m_currentMisspelledRange = range;
109 if (range != m_currentMisspelledRange) {
113 m_currentMisspelledRange =
nullptr;
118 if (m_currentMisspelledRange == range) {
119 m_currentMisspelledRange =
nullptr;
123void KateSpellingMenu::cleanUpAfterShown()
129 if (m_currentMisspelledRangeNeedCleanUp) {
130 m_currentMisspelledRange =
nullptr;
131 m_currentMisspelledRangeNeedCleanUp =
false;
135 for (
auto act : m_menuOnTopSuggestionList) {
139 m_menuOnTopSuggestionList.clear();
143void KateSpellingMenu::prepareToBeShown(
QMenu *contextMenu)
145 Q_ASSERT(contextMenu);
147 if (!m_view->doc()->onTheFlySpellChecker()) {
152 m_selectedRange = m_view->selectionRange();
153 if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) {
155 auto imv = m_view->doc()->onTheFlySpellChecker()->installedMovingRanges(m_selectedRange);
156 for (
int i = 0; i < imv.size(); ++i) {
157 if (imv.at(i)->toRange() == m_selectedRange) {
158 m_currentMisspelledRange = imv.at(i);
159 m_currentMisspelledRangeNeedCleanUp =
true;
165 if (m_currentMisspelledRange !=
nullptr) {
167 m_selectedRange = m_currentMisspelledRange->toRange();
168 const QString &misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
169 m_spellingMenuAction->setText(
i18n(
"Spelling '%1'", misspelledWord));
171 m_currentDictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
176 for (QStringList::const_iterator i = m_currentSuggestions.cbegin(); i != m_currentSuggestions.cend() && counter > 0; ++i) {
177 const QString &suggestion = *i;
178 QAction *action =
new QAction(suggestion, contextMenu);
180 m_menuOnTopSuggestionList.append(action);
182 replaceWordBySuggestion(suggestion);
184 m_spellingMenu->addAction(action);
187 contextMenu->
insertActions(m_spellingMenuAction, m_menuOnTopSuggestionList);
189 }
else if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) {
191 m_spellingMenuAction->setText(
i18n(
"Spelling"));
197void KateSpellingMenu::populateSuggestionsMenu()
199 m_spellingMenu->clear();
201 if (m_currentMisspelledRange) {
202 m_spellingMenu->addAction(m_ignoreWordAction);
203 m_spellingMenu->addAction(m_addToDictionaryAction);
205 m_spellingMenu->addSeparator();
206 bool dictFound =
false;
207 for (
auto action : m_dictionaryGroup->actions()) {
213 m_spellingMenu->addAction(action);
215 if (!dictFound && !m_currentDictionary.isEmpty()) {
216 const QString dictName = Sonnet::Speller().availableDictionaries().key(m_currentDictionary);
217 QAction *action = m_dictionaryGroup->addAction(dictName);
218 action->
setData(m_currentDictionary);
221 m_spellingMenu->addAction(action);
224 m_spellingMenu->addSeparator();
226 for (QStringList::const_iterator i = m_currentSuggestions.cbegin(); i != m_currentSuggestions.cend() && counter > 0; ++i) {
227 const QString &suggestion = *i;
228 QAction *action =
new QAction(suggestion, m_spellingMenu);
230 replaceWordBySuggestion(suggestion);
232 m_spellingMenu->addAction(action);
236 }
else if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) {
237 for (
auto action : m_dictionaryGroup->actions()) {
239 m_spellingMenu->addAction(action);
244void KateSpellingMenu::replaceWordBySuggestion(
const QString &suggestion)
246 if (!m_currentMisspelledRange) {
250 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
251 KTextEditor::Range newRange = m_currentMisspelledRange->toRange();
254 KTextEditor::DocumentPrivate *doc = m_view->doc();
258 m_view->doc()->setDictionary(dictionary, newRange);
259 m_view->clearSelection();
262void KateSpellingMenu::addCurrentWordToDictionary()
264 if (!m_currentMisspelledRange) {
267 const QString &misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
268 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
270 m_view->doc()->clearMisspellingForWord(misspelledWord);
271 m_view->clearSelection();
274void KateSpellingMenu::ignoreCurrentWord()
276 if (!m_currentMisspelledRange) {
279 const QString &misspelledWord = m_view->doc()->text(*m_currentMisspelledRange);
280 const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(*m_currentMisspelledRange);
282 m_view->doc()->clearMisspellingForWord(misspelledWord);
283 m_view->clearSelection();
286#include "moc_spellingmenu.cpp"
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
KateSpellCheckManager * spellCheckManager()
spell check manager
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
A range that is bound to a specific Document, and maintains its position.
constexpr Cursor start() const noexcept
Get the start position of this range.
void setEnd(Cursor end) noexcept
Set the end cursor to end.
QString i18n(const char *text, const TYPE &arg...)
QVariant data() const const
void setFont(const QFont &font)
void setData(const QVariant &data)
void triggered(bool checked)
void setBold(bool enable)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
qsizetype size() const const
QString toString() const const