Skip to content

Commit

Permalink
Clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
ElTh0r0 committed Jul 20, 2022
1 parent 74a1d94 commit 6a12f02
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 88 deletions.
56 changes: 22 additions & 34 deletions dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,20 @@

#include <QDebug>
#include <QMessageBox>

#include <nuspell/dictionary.hxx>
#include <nuspell/finder.hxx>

#include "ui_dialog.h"
#include "./nuspellcheck.h"
#include "./nuspellcheckdialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *pParent)
: QDialog(pParent),
ui(new Ui::Dialog) {
Dialog::Dialog(QWidget *pParent) : QDialog(pParent), ui(new Ui::Dialog) {
ui->setupUi(this);
connect(ui->buttonCheckSpelling, &QPushButton::clicked,
this, &Dialog::checkSpelling);
}


Dialog::~Dialog() {
delete ui;
connect(ui->buttonCheckSpelling, &QPushButton::clicked, this,
&Dialog::checkSpelling);
}

Dialog::~Dialog() { delete ui; }

void Dialog::checkSpelling() {
QString language = QStringLiteral("en_US");
Expand All @@ -66,7 +59,7 @@ void Dialog::checkSpelling() {

// Add application folder to search folder for dictionaries
std::filesystem::path app_path(
QString(qApp->applicationDirPath() + "/dicts/").toStdU16String());
QString(qApp->applicationDirPath() + "/dicts/").toStdU16String());
auto appdirs = std::vector<std::filesystem::path>();
appdirs.push_back(app_path);
nuspell::search_dirs_for_dicts(appdirs, dict_list);
Expand All @@ -77,8 +70,8 @@ void Dialog::checkSpelling() {
qDebug() << QString::fromUtf16(s.u16string().c_str());
}

auto dict_path = nuspell::search_dirs_for_one_dict(searchdirs,
language.toStdString());
auto dict_path =
nuspell::search_dirs_for_one_dict(searchdirs, language.toStdString());
if (std::empty(dict_path)) {
qWarning() << "Can not find the requested dictionary.";
QMessageBox::warning(this, tr("Warning"),
Expand All @@ -89,8 +82,7 @@ void Dialog::checkSpelling() {
auto dict = nuspell::Dictionary();
try {
dict.load_aff_dic(dict_path);
}
catch (const nuspell::Dictionary_Loading_Error& e) {
} catch (const nuspell::Dictionary_Loading_Error &e) {
qWarning() << e.what() << '\n';
QMessageBox::warning(this, tr("Warning"), QString::fromLatin1(e.what()));
return;
Expand Down Expand Up @@ -126,8 +118,7 @@ void Dialog::checkSpelling() {

// Workaround for better recognition of words
// punctuation etc. does not belong to words
while (!word.isEmpty() &&
!word.at(0).isLetter() &&
while (!word.isEmpty() && !word.at(0).isLetter() &&
cursor.anchor() < cursor.position()) {
int cursorPos = cursor.position();
cursor.setPosition(cursor.anchor() + 1, QTextCursor::MoveAnchor);
Expand Down Expand Up @@ -159,19 +150,18 @@ void Dialog::checkSpelling() {
ui->textEdit->setExtraSelections(esList);
QCoreApplication::processEvents();

if (spellResult == NuspellCheckDialog::AbortCheck)
break;
if (spellResult == NuspellCheckDialog::AbortCheck) break;

switch (spellResult) {
case NuspellCheckDialog::ReplaceOnce:
cursor.insertText(checkDialog->replacement());
break;
case NuspellCheckDialog::ReplaceAll:
replaceAll(cursor.position(), word, checkDialog->replacement());
break;

default:
break;
case NuspellCheckDialog::ReplaceOnce:
cursor.insertText(checkDialog->replacement());
break;
case NuspellCheckDialog::ReplaceAll:
replaceAll(cursor.position(), word, checkDialog->replacement());
break;

default:
break;
}
QCoreApplication::processEvents();
}
Expand All @@ -187,10 +177,9 @@ void Dialog::checkSpelling() {
tr("The spell check has finished."));
}


void Dialog::replaceAll(int nPos, const QString &sOld, const QString &sNew) {
QTextCursor cursor(ui->textEdit->document());
cursor.setPosition(nPos-sOld.length(), QTextCursor::MoveAnchor);
cursor.setPosition(nPos - sOld.length(), QTextCursor::MoveAnchor);

while (!cursor.atEnd()) {
QCoreApplication::processEvents();
Expand All @@ -199,8 +188,7 @@ void Dialog::replaceAll(int nPos, const QString &sOld, const QString &sNew) {

// Workaround for better recognition of words
// punctuation etc. does not belong to words
while (!word.isEmpty() &&
!word.at(0).isLetter() &&
while (!word.isEmpty() && !word.at(0).isLetter() &&
cursor.anchor() < cursor.position()) {
int cursorPos = cursor.position();
cursor.setPosition(cursor.anchor() + 1, QTextCursor::MoveAnchor);
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/

#include <QApplication>

#include "./dialog.h"

int main(int argc, char *argv[]) {
Expand Down
29 changes: 7 additions & 22 deletions nuspellcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@

NuspellCheck::NuspellCheck(nuspell::Dictionary *p_Dict,
const QString &sUserDictionary)
: m_pDict(p_Dict),
m_sUserDictionary(sUserDictionary) {
: m_pDict(p_Dict), m_sUserDictionary(sUserDictionary) {
QFile userDictonaryFile(m_sUserDictionary);
if (!m_sUserDictionary.isEmpty() && userDictonaryFile.exists()) {
if (userDictonaryFile.open(QIODevice::ReadOnly)) {
QTextStream stream(&userDictonaryFile);
for (QString word = stream.readLine();
!word.isEmpty();
for (QString word = stream.readLine(); !word.isEmpty();
word = stream.readLine())
put_word(word);
userDictonaryFile.close();
Expand All @@ -58,10 +56,7 @@ NuspellCheck::NuspellCheck(nuspell::Dictionary *p_Dict,
}
}


NuspellCheck::~NuspellCheck() {
}

NuspellCheck::~NuspellCheck() {}

auto NuspellCheck::spell(const QString &sWord) -> bool {
if (!m_UserWordsList.contains(sWord)) {
Expand All @@ -70,31 +65,21 @@ auto NuspellCheck::spell(const QString &sWord) -> bool {
return true;
}


auto NuspellCheck::suggest(const QString &sWord) -> QStringList {
auto sugs = std::vector<std::string>();
m_pDict->suggest(sWord.toStdString(), sugs);

QStringList suggestions;
std::transform(
sugs.begin(), sugs.end(), std::back_inserter(suggestions),
[](const std::string &v){
return QString::fromStdString(v);
});
sugs.begin(), sugs.end(), std::back_inserter(suggestions),
[](const std::string &v) { return QString::fromStdString(v); });

return suggestions;
}

void NuspellCheck::ignoreWord(const QString &sWord) { put_word(sWord); }

void NuspellCheck::ignoreWord(const QString &sWord) {
put_word(sWord);
}


void NuspellCheck::put_word(const QString &sWord) {
m_UserWordsList << sWord;
}

void NuspellCheck::put_word(const QString &sWord) { m_UserWordsList << sWord; }

void NuspellCheck::addToUserWordlist(const QString &sWord) {
put_word(sWord);
Expand Down
1 change: 0 additions & 1 deletion nuspellcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

#include <QString>
#include <QStringList>

#include <nuspell/dictionary.hxx>

class NuspellCheck {
Expand Down
47 changes: 18 additions & 29 deletions nuspellcheckdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,34 @@
*/

#include "./nuspellcheckdialog.h"
#include "ui_nuspellcheckdialog.h"

#include "./nuspellcheck.h"
#include "ui_nuspellcheckdialog.h"

NuspellCheckDialog::NuspellCheckDialog(NuspellCheck *pSpellChecker,
QWidget *pParent)
: QDialog(pParent),
ui(new Ui::NuspellCheckDialog) {
: QDialog(pParent), ui(new Ui::NuspellCheckDialog) {
ui->setupUi(this);
m_pSpellChecker = pSpellChecker;

connect(ui->listWidget, &QListWidget::currentTextChanged,
ui->ledtReplaceWith, &QLineEdit::setText);

connect(ui->btnAddToDict, &QPushButton::clicked,
this, &NuspellCheckDialog::addToDict);
connect(ui->btnReplaceOnce, &QPushButton::clicked,
this, &NuspellCheckDialog::replaceOnce);
connect(ui->btnReplaceAll, &QPushButton::clicked,
this, &NuspellCheckDialog::replaceAll);
connect(ui->btnIgnoreOnce, &QPushButton::clicked,
this, &NuspellCheckDialog::ignoreOnce);
connect(ui->btnIgnoreAll, &QPushButton::clicked,
this, &NuspellCheckDialog::ignoreAll);
connect(ui->btnCancel, &QPushButton::clicked,
this, &NuspellCheckDialog::reject);
}


NuspellCheckDialog::~NuspellCheckDialog() {
delete ui;
connect(ui->listWidget, &QListWidget::currentTextChanged, ui->ledtReplaceWith,
&QLineEdit::setText);

connect(ui->btnAddToDict, &QPushButton::clicked, this,
&NuspellCheckDialog::addToDict);
connect(ui->btnReplaceOnce, &QPushButton::clicked, this,
&NuspellCheckDialog::replaceOnce);
connect(ui->btnReplaceAll, &QPushButton::clicked, this,
&NuspellCheckDialog::replaceAll);
connect(ui->btnIgnoreOnce, &QPushButton::clicked, this,
&NuspellCheckDialog::ignoreOnce);
connect(ui->btnIgnoreAll, &QPushButton::clicked, this,
&NuspellCheckDialog::ignoreAll);
connect(ui->btnCancel, &QPushButton::clicked, this,
&NuspellCheckDialog::reject);
}

NuspellCheckDialog::~NuspellCheckDialog() { delete ui; }

NuspellCheckDialog::SpellCheckAction NuspellCheckDialog::checkWord(
const QString &sWord) {
Expand All @@ -85,37 +80,31 @@ NuspellCheckDialog::SpellCheckAction NuspellCheckDialog::checkWord(
return m_ReturnCode;
}


QString NuspellCheckDialog::replacement() const {
return ui->ledtReplaceWith->text();
}


void NuspellCheckDialog::ignoreOnce() {
m_ReturnCode = IgnoreOnce;
accept();
}


void NuspellCheckDialog::ignoreAll() {
m_pSpellChecker->ignoreWord(m_sUnkownWord);
m_ReturnCode = IgnoreAll;
accept();
}


void NuspellCheckDialog::replaceOnce() {
m_ReturnCode = ReplaceOnce;
accept();
}


void NuspellCheckDialog::replaceAll() {
m_ReturnCode = ReplaceAll;
accept();
}


void NuspellCheckDialog::addToDict() {
m_pSpellChecker->addToUserWordlist(m_sUnkownWord);
m_ReturnCode = AddToDict;
Expand Down
10 changes: 8 additions & 2 deletions nuspellcheckdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ class NuspellCheckDialog : public QDialog {
Q_OBJECT

public:
enum SpellCheckAction {AbortCheck, IgnoreOnce, IgnoreAll,
ReplaceOnce, ReplaceAll, AddToDict};
enum SpellCheckAction {
AbortCheck,
IgnoreOnce,
IgnoreAll,
ReplaceOnce,
ReplaceAll,
AddToDict
};

explicit NuspellCheckDialog(NuspellCheck *pSpellChecker,
QWidget *pParent = 0);
Expand Down

0 comments on commit 6a12f02

Please sign in to comment.