1 /*
2  * Copyright (C) Pedram Pourang (aka Tsu Jan) 2019 <tsujan2000@gmail.com>
3  *
4  * FeatherPad is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * FeatherPad is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
18  */
19 
20 #include "spellDialog.h"
21 #include "ui_spellDialog.h"
22 #include "spellChecker.h"
23 
24 #include <QScreen>
25 #include <QWindow>
26 
27 namespace FeatherPad {
28 
SpellDialog(SpellChecker * spellChecker,const QString & word,bool correction,QWidget * parent)29 SpellDialog::SpellDialog (SpellChecker *spellChecker, const QString& word,
30                           bool correction, QWidget *parent)
31     : QDialog (parent), ui (new Ui::SpellDialog)
32 {
33     ui->setupUi (this);
34     setWindowModality (Qt::WindowModal);
35 
36     QWidget::setTabOrder (ui->replace, ui->listWidget);
37     QWidget::setTabOrder (ui->listWidget, ui->ignoreOnce);
38     QWidget::setTabOrder (ui->ignoreOnce, ui->ignoreAll);
39     QWidget::setTabOrder (ui->ignoreAll, ui->correctOnce);
40     QWidget::setTabOrder (ui->correctOnce, ui->correctAll);
41     QWidget::setTabOrder (ui->correctAll, ui->addToDict);
42 
43     ui->ignoreOnce->setShortcut (QKeySequence (Qt::Key_F3));
44     ui->ignoreOnce->setToolTip (QKeySequence (Qt::Key_F3).toString (QKeySequence::NativeText));
45     ui->ignoreAll->setShortcut (QKeySequence (Qt::Key_F4));
46     ui->ignoreAll->setToolTip (QKeySequence (Qt::Key_F4).toString (QKeySequence::NativeText));
47     ui->correctOnce->setShortcut (QKeySequence (Qt::Key_F5));
48     ui->correctOnce->setToolTip (QKeySequence (Qt::Key_F5).toString (QKeySequence::NativeText));
49     ui->correctAll->setShortcut (QKeySequence (Qt::Key_F6));
50     ui->correctAll->setToolTip (QKeySequence (Qt::Key_F6).toString (QKeySequence::NativeText));
51     ui->addToDict->setShortcut (QKeySequence (Qt::Key_F7));
52     ui->addToDict->setToolTip (QKeySequence (Qt::Key_F7).toString (QKeySequence::NativeText));
53 
54     if (!correction)
55     {
56         ui->correctOnce->setEnabled (false);
57         ui->correctAll->setEnabled (false);
58     }
59 
60     spellChecker_ = spellChecker;
61 
62     connect (ui->correctOnce, &QAbstractButton::clicked, [this] {emit spellChecked (SpellAction::CorrectOnce);});
63     connect (ui->ignoreOnce, &QAbstractButton::clicked, [this] {emit spellChecked (SpellAction::IgnoreOnce);});
64     connect (ui->correctAll, &QAbstractButton::clicked, [this] {emit spellChecked (SpellAction::CorrectAll);});
65     connect (ui->ignoreAll, &QAbstractButton::clicked, [this] {emit spellChecked (SpellAction::IgnoreAll);});
66 
67     connect (ui->addToDict, &QAbstractButton::clicked, [this] {emit spellChecked (SpellAction::AddToDict);});
68 
69     connect (ui->listWidget, &QListWidget::currentTextChanged, ui->replace, &QLineEdit::setText);
70 
71     checkWord (word);
72 
73     if (parent != nullptr)
74     {
75         if (QWindow *win = parent->windowHandle())
76         {
77             if (QScreen *sc = win->screen())
78             {
79                 QSize ag = sc->availableGeometry().size()
80                            - (parent->window()->frameGeometry().size() - parent->window()->geometry().size());
81                 resize (size().boundedTo (ag));
82             }
83         }
84     }
85 }
86 /*************************/
~SpellDialog()87 SpellDialog::~SpellDialog()
88 {
89     delete ui; ui = nullptr;
90 }
91 /*************************/
replacement() const92 QString SpellDialog::replacement() const
93 {
94     return ui->replace->text();
95 }
96 /*************************/
checkWord(const QString & word)97 void SpellDialog::checkWord (const QString &word)
98 {
99     if (word.isEmpty()) return;
100     ui->replace->clear();
101     ui->listWidget->clear();
102 
103     ui->misspelledLabel->setText (QString ("<b>%1</b>").arg (word));
104 
105     QStringList suggestions = spellChecker_->suggest (word);
106     ui->listWidget->addItems (suggestions);
107     if (!suggestions.isEmpty())
108         ui->listWidget->setCurrentRow (0, QItemSelectionModel::Select);
109 }
110 
111 }
112