1 | /* |
2 | * Copyright (C) 2008 Montel Laurent <montel@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | */ |
19 | #include "klinespellchecking.h" |
20 | |
21 | #include <QMenu> |
22 | #include <QContextMenuEvent> |
23 | |
24 | #include <KStandardAction> |
25 | #include <KActionCollection> |
26 | #include <KAction> |
27 | #include <sonnet/dialog.h> |
28 | #include <sonnet/backgroundchecker.h> |
29 | |
30 | KLineSpellChecking::KLineSpellChecking(QWidget* parent) |
31 | : KLineEdit(parent) |
32 | { |
33 | KActionCollection *ac = new KActionCollection(this); |
34 | m_spellAction = KStandardAction::spelling( this, SLOT(slotCheckSpelling()), ac ); |
35 | } |
36 | |
37 | KLineSpellChecking::~KLineSpellChecking() |
38 | { |
39 | } |
40 | |
41 | void KLineSpellChecking::slotCheckSpelling() |
42 | { |
43 | if ( text().isEmpty() ) { |
44 | return; |
45 | } |
46 | Sonnet::Dialog *spellDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), 0); |
47 | connect(spellDialog, SIGNAL(replace(QString,int,QString)), this, SLOT(spellCheckerCorrected(QString,int,QString))); |
48 | connect(spellDialog, SIGNAL(misspelling(QString,int)), this, SLOT(spellCheckerMisspelling(QString,int))); |
49 | connect(spellDialog, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString))); |
50 | connect(spellDialog, SIGNAL(cancel()), this, SLOT(spellCheckerFinished())); |
51 | connect(spellDialog, SIGNAL(stop()), this, SLOT(spellCheckerFinished())); |
52 | spellDialog->setBuffer(text()); |
53 | spellDialog->show(); |
54 | } |
55 | |
56 | void KLineSpellChecking::spellCheckerMisspelling( const QString &_text, int pos) |
57 | { |
58 | highLightWord( _text.length(),pos ); |
59 | } |
60 | |
61 | void KLineSpellChecking::highLightWord( unsigned int length, unsigned int pos ) |
62 | { |
63 | setSelection ( pos, length ); |
64 | } |
65 | |
66 | void KLineSpellChecking::spellCheckerCorrected( const QString &old, int pos, const QString &corr ) |
67 | { |
68 | if( old!= corr ) |
69 | { |
70 | setSelection ( pos, old.length() ); |
71 | insert( corr ); |
72 | setSelection ( pos, corr.length() ); |
73 | } |
74 | } |
75 | |
76 | void KLineSpellChecking::spellCheckerFinished() |
77 | { |
78 | } |
79 | |
80 | void KLineSpellChecking::slotSpellCheckDone( const QString &s ) |
81 | { |
82 | if( s != text() ) |
83 | setText( s ); |
84 | } |
85 | |
86 | void KLineSpellChecking::(QContextMenuEvent *e) |
87 | { |
88 | QMenu* = createStandardContextMenu(); |
89 | |
90 | if ( !popup ) |
91 | return; |
92 | |
93 | if (echoMode() == QLineEdit::Normal && |
94 | !isReadOnly()) { |
95 | popup->addSeparator(); |
96 | |
97 | popup->addAction( m_spellAction ); |
98 | m_spellAction->setEnabled( !text().isEmpty() ); |
99 | } |
100 | popup->exec(e->globalPos()); |
101 | delete popup; |
102 | } |
103 | |
104 | |
105 | |
106 | #include "klinespellchecking.moc" |
107 | |
108 | |
109 | |