Skip to content

Commit

Permalink
Refactor spell checker to allow different backends
Browse files Browse the repository at this point in the history
  • Loading branch information
IgKh committed Sep 21, 2024
1 parent cfc66ce commit 8588348
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 443 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(SOURCES
katvan_previewerview.cpp
katvan_recentfiles.cpp
katvan_searchbar.cpp
katvan_spellchecker_hunspell.cpp
katvan_spellchecker.cpp
katvan_typstdriverwrapper.cpp
katvan_utils.cpp
Expand Down
15 changes: 6 additions & 9 deletions src/katvan_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <QMenu>
#include <QPainter>
#include <QRegularExpression>
#include <QScopedValueRollback>
#include <QScrollBar>
#include <QShortcut>
#include <QTextBlock>
Expand Down Expand Up @@ -88,16 +87,14 @@ class LineNumberGutter : public QWidget
Editor::Editor(QWidget* parent)
: QTextEdit(parent)
, d_theme(EditorTheme::defaultTheme())
, d_inPaletteChange(false)
, d_pendingSuggestionsPosition(-1)
{
setAcceptRichText(false);

d_spellChecker = new SpellChecker(this);
connect(d_spellChecker, &SpellChecker::suggestionsReady, this, &Editor::spellingSuggestionsReady);
connect(d_spellChecker, &SpellChecker::dictionaryChanged, this, &Editor::forceRehighlighting);
connect(SpellChecker::instance(), &SpellChecker::suggestionsReady, this, &Editor::spellingSuggestionsReady);
connect(SpellChecker::instance(), &SpellChecker::dictionaryChanged, this, &Editor::forceRehighlighting);

d_highlighter = new Highlighter(document(), d_spellChecker, d_theme);
d_highlighter = new Highlighter(document(), SpellChecker::instance(), d_theme);
d_codeModel = new CodeModel(document(), this);

d_leftLineNumberGutter = new LineNumberGutter(this);
Expand Down Expand Up @@ -379,7 +376,7 @@ void Editor::contextMenuEvent(QContextMenuEvent* event)

QAction* addToPersonalAction = new QAction(tr("Add to Personal Dictionary"));
connect(addToPersonalAction, &QAction::triggered, this, [this, misspelledWord, cursor]() {
d_spellChecker->addToPersonalDictionary(misspelledWord);
SpellChecker::instance()->addToPersonalDictionary(misspelledWord);
forceRehighlighting();
});

Expand All @@ -397,7 +394,7 @@ void Editor::contextMenuEvent(QContextMenuEvent* event)
// signal will be instantly invoked as a direct connection.
d_pendingSuggestionsWord = misspelledWord;
d_pendingSuggestionsPosition = cursor.position();
d_spellChecker->requestSuggestions(misspelledWord, cursor.position());
SpellChecker::instance()->requestSuggestions(misspelledWord, cursor.position());
}
d_contextMenu->popup(event->globalPos());
}
Expand Down Expand Up @@ -690,7 +687,7 @@ void Editor::keyPressEvent(QKeyEvent* event)
setTextBlockDirection(Qt::RightToLeft);
return;
}
#elif defined(Q_OS_MAC)
#elif defined(Q_OS_MACOS)
if (event->keyCombination().keyboardModifiers() == (Qt::MetaModifier | Qt::ShiftModifier)) {
if (event->nativeVirtualKey() == 0x38) { // kVK_Shift
d_pendingDirectionChange = Qt::LeftToRight;
Expand Down
6 changes: 0 additions & 6 deletions src/katvan_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ QT_END_NAMESPACE
namespace katvan {

class Highlighter;
class SpellChecker;
class CodeModel;

class Editor : public QTextEdit
Expand All @@ -45,8 +44,6 @@ class Editor : public QTextEdit
public:
Editor(QWidget* parent = nullptr);

SpellChecker* spellChecker() const { return d_spellChecker; }

void applySettings(const EditorSettings& settings);
void updateEditorTheme();

Expand Down Expand Up @@ -108,16 +105,13 @@ private slots:

QTimer* d_debounceTimer;
Highlighter* d_highlighter;
SpellChecker* d_spellChecker;
CodeModel* d_codeModel;

EditorSettings d_appSettings;
EditorSettings d_fileMode;
EditorSettings d_effectiveSettings;
EditorTheme d_theme;

bool d_inPaletteChange;

QPointer<QMenu> d_contextMenu;
std::optional<Qt::LayoutDirection> d_pendingDirectionChange;
QString d_pendingSuggestionsWord;
Expand Down
14 changes: 8 additions & 6 deletions src/katvan_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,9 @@ void MainWindow::restoreSpellingDictionary(const QSettings& settings)
QString dictName = settings.value(SETTING_SPELLING_DICT, QString()).toString();
QString dictPath;

SpellChecker* checker = SpellChecker::instance();
if (!dictName.isEmpty()) {
QMap<QString, QString> allDicts = SpellChecker::findDictionaries();
QMap<QString, QString> allDicts = checker->findDictionaries();
if (!allDicts.contains(dictName)) {
dictName.clear();
}
Expand All @@ -763,13 +764,14 @@ void MainWindow::restoreSpellingDictionary(const QSettings& settings)
}
}

d_editor->spellChecker()->setCurrentDictionary(dictName, dictPath);
checker->setCurrentDictionary(dictName, dictPath);
d_spellingButton->setText(dictName.isEmpty() ? tr("None") : dictName);
}

void MainWindow::changeSpellCheckingDictionary()
{
QMap<QString, QString> dicts = SpellChecker::findDictionaries();
SpellChecker* checker = SpellChecker::instance();
QMap<QString, QString> dicts = checker->findDictionaries();

QStringList dictNames = { "" };
QStringList dictLabels = { tr("None") };
Expand All @@ -778,10 +780,10 @@ void MainWindow::changeSpellCheckingDictionary()
dictNames.append(*kit);
dictLabels.append(QString("%1 - %2").arg(
*kit,
SpellChecker::dictionaryDisplayName(*kit)));
checker->dictionaryDisplayName(*kit)));
}

QString currentDict = d_editor->spellChecker()->currentDictionaryName();
QString currentDict = checker->currentDictionaryName();
int index = dictNames.indexOf(currentDict);
if (index < 0) {
index = 0;
Expand All @@ -801,7 +803,7 @@ void MainWindow::changeSpellCheckingDictionary()
}

QString selectedDictName = dictNames[dictLabels.indexOf(result)];
d_editor->spellChecker()->setCurrentDictionary(selectedDictName, dicts.value(selectedDictName));
checker->setCurrentDictionary(selectedDictName, dicts.value(selectedDictName));
d_spellingButton->setText(selectedDictName.isEmpty() ? result : selectedDictName);

QSettings settings;
Expand Down
Loading

0 comments on commit 8588348

Please sign in to comment.