Changeset View
Changeset View
Standalone View
Standalone View
plugins/textediting/spellcheck/SpellCheckMenu.cpp
Show All 21 Lines | |||||
22 | #include "SpellCheck.h" | 22 | #include "SpellCheck.h" | ||
23 | #include "SpellCheckDebug.h" | 23 | #include "SpellCheckDebug.h" | ||
24 | 24 | | |||
25 | #include <kactionmenu.h> | 25 | #include <kactionmenu.h> | ||
26 | #include <klocalizedstring.h> | 26 | #include <klocalizedstring.h> | ||
27 | 27 | | |||
28 | #include <QMenu> | 28 | #include <QMenu> | ||
29 | #include <QAction> | 29 | #include <QAction> | ||
30 | #include <QSignalMapper> | | |||
31 | 30 | | |||
32 | SpellCheckMenu::SpellCheckMenu(const Sonnet::Speller &speller, SpellCheck *spellCheck) | 31 | SpellCheckMenu::SpellCheckMenu(const Sonnet::Speller &speller, SpellCheck *spellCheck) | ||
33 | : QObject(spellCheck), | 32 | : QObject(spellCheck), | ||
34 | m_spellCheck(spellCheck), | 33 | m_spellCheck(spellCheck), | ||
35 | m_speller(speller), | 34 | m_speller(speller), | ||
36 | m_suggestionsMenuAction(0), | 35 | m_suggestionsMenuAction(0), | ||
37 | m_ignoreWordAction(0), | 36 | m_ignoreWordAction(0), | ||
38 | m_addToDictionaryAction(0), | 37 | m_addToDictionaryAction(0), | ||
39 | m_suggestionsMenu(0), | 38 | m_suggestionsMenu(0), | ||
40 | m_suggestionsSignalMapper(new QSignalMapper(this)), | | |||
41 | m_currentMisspelledPosition(-1) | 39 | m_currentMisspelledPosition(-1) | ||
42 | { | 40 | { | ||
43 | m_suggestionsMenuAction = new KActionMenu(i18n("Spelling"), this); | 41 | m_suggestionsMenuAction = new KActionMenu(i18n("Spelling"), this); | ||
44 | m_suggestionsMenu = m_suggestionsMenuAction->menu(); | 42 | m_suggestionsMenu = m_suggestionsMenuAction->menu(); | ||
45 | connect(m_suggestionsMenu, SIGNAL(aboutToShow()), this, SLOT(createSuggestionsMenu())); | 43 | connect(m_suggestionsMenu, SIGNAL(aboutToShow()), this, SLOT(createSuggestionsMenu())); | ||
46 | 44 | | |||
47 | m_addToDictionaryAction = new QAction(i18n("Add to Dictionary"), this); | 45 | m_addToDictionaryAction = new QAction(i18n("Add to Dictionary"), this); | ||
48 | connect(m_addToDictionaryAction, SIGNAL(triggered()), this, SLOT(addWordToDictionary())); | 46 | connect(m_addToDictionaryAction, SIGNAL(triggered()), this, SLOT(addWordToDictionary())); | ||
49 | 47 | | |||
50 | // disabling this as if it calls the speller it's only changed in a local copy | 48 | // disabling this as if it calls the speller it's only changed in a local copy | ||
51 | // see addWordToDictionary for how it should be done, except background checker | 49 | // see addWordToDictionary for how it should be done, except background checker | ||
52 | // doesn't have suche a method for ignoreWord | 50 | // doesn't have suche a method for ignoreWord | ||
53 | // Only option left is to personally ignore words | 51 | // Only option left is to personally ignore words | ||
54 | 52 | | |||
55 | // m_ignoreWordAction = new QAction(i18n("Ignore Word"), this); | 53 | // m_ignoreWordAction = new QAction(i18n("Ignore Word"), this); | ||
56 | // connect(m_ignoreWordAction, SIGNAL(triggered()), this, SLOT(ignoreWord())); | 54 | // connect(m_ignoreWordAction, SIGNAL(triggered()), this, SLOT(ignoreWord())); | ||
57 | 55 | | |||
58 | connect(m_suggestionsSignalMapper, SIGNAL(mapped(QString)), | | |||
59 | this, SLOT(replaceWord(QString))); | | |||
60 | | ||||
61 | setEnabled(false); | 56 | setEnabled(false); | ||
62 | setVisible(false); | 57 | setVisible(false); | ||
63 | } | 58 | } | ||
64 | 59 | | |||
65 | SpellCheckMenu::~SpellCheckMenu() | 60 | SpellCheckMenu::~SpellCheckMenu() | ||
66 | { | 61 | { | ||
67 | 62 | | |||
68 | } | 63 | } | ||
Show All 11 Lines | 71 | { | |||
80 | m_suggestionsMenu->addAction(m_ignoreWordAction); | 75 | m_suggestionsMenu->addAction(m_ignoreWordAction); | ||
81 | m_suggestionsMenu->addAction(m_addToDictionaryAction); | 76 | m_suggestionsMenu->addAction(m_addToDictionaryAction); | ||
82 | m_suggestionsMenu->addSeparator(); | 77 | m_suggestionsMenu->addSeparator(); | ||
83 | if (!m_currentMisspelled.isEmpty()) { | 78 | if (!m_currentMisspelled.isEmpty()) { | ||
84 | m_suggestions = m_speller.suggest(m_currentMisspelled); | 79 | m_suggestions = m_speller.suggest(m_currentMisspelled); | ||
85 | for (int i = 0; i < m_suggestions.count(); ++i) { | 80 | for (int i = 0; i < m_suggestions.count(); ++i) { | ||
86 | const QString &suggestion = m_suggestions.at(i); | 81 | const QString &suggestion = m_suggestions.at(i); | ||
87 | QAction *action = new QAction(suggestion, m_suggestionsMenu); | 82 | QAction *action = new QAction(suggestion, m_suggestionsMenu); | ||
88 | connect(action, SIGNAL(triggered()), m_suggestionsSignalMapper, SLOT(map())); | 83 | connect(action, &QAction::triggered, [this, suggestion] { replaceWord(suggestion); }); | ||
dfaure: You're capturing `suggestion` by reference, and it's a reference itself.
Are you 100% sure that… | |||||
89 | m_suggestionsSignalMapper->setMapping(action, suggestion); | | |||
90 | m_suggestionsMenu->addAction(action); | 84 | m_suggestionsMenu->addAction(action); | ||
91 | } | 85 | } | ||
92 | } | 86 | } | ||
93 | } | 87 | } | ||
94 | 88 | | |||
95 | void SpellCheckMenu::ignoreWord() | 89 | void SpellCheckMenu::ignoreWord() | ||
96 | { | 90 | { | ||
97 | if (m_currentMisspelled.isEmpty() || m_currentMisspelledPosition < 0) | 91 | if (m_currentMisspelled.isEmpty() || m_currentMisspelledPosition < 0) | ||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |
You're capturing suggestion by reference, and it's a reference itself.
Are you 100% sure that this will point to the m_suggestions list and not to the local variable?
OK I did some googling and found that C++11 was unclear about that, C++14 clarified it to be OK.
Still, I would just make a copy, so the next reader doesn't have to spend time googling for edge cases ;)