~ubuntu-branches/ubuntu/wily/kmenuedit/wily

« back to all changes in this revision

Viewing changes to klinespellchecking.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-10-14 13:48:27 UTC
  • Revision ID: package-import@ubuntu.com-20141014134827-c4gvv92bbv7zww8j
Tags: upstream-5.1.0.1
ImportĀ upstreamĀ versionĀ 5.1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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::contextMenuEvent(QContextMenuEvent *e)
 
87
{
 
88
    QMenu* popup = 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