~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kmenuedit/klinespellchecking.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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
#include <QMenu>
 
21
#include <KStandardAction>
 
22
#include <KActionCollection>
 
23
#include <KAction>
 
24
#include <QContextMenuEvent>
 
25
#include <sonnet/dialog.h>
 
26
#include <sonnet/backgroundchecker.h>
 
27
 
 
28
KLineSpellChecking::KLineSpellChecking(QWidget* parent)
 
29
    : KLineEdit(parent)
 
30
{
 
31
    KActionCollection *ac = new KActionCollection(this);
 
32
    m_spellAction = KStandardAction::spelling( this, SLOT( slotCheckSpelling() ), ac );
 
33
}
 
34
 
 
35
KLineSpellChecking::~KLineSpellChecking()
 
36
{
 
37
}
 
38
 
 
39
void KLineSpellChecking::slotCheckSpelling()
 
40
{
 
41
    if ( text().isEmpty() ) {
 
42
        return;
 
43
    }
 
44
    Sonnet::Dialog *spellDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), 0);
 
45
    connect(spellDialog, SIGNAL(replace( const QString&, int,const QString&)), this, SLOT(spellCheckerCorrected( const QString&, int,const QString&)));
 
46
    connect(spellDialog, SIGNAL(misspelling( const QString&, int)), this, SLOT(spellCheckerMisspelling(const QString &,int)));
 
47
    connect(spellDialog, SIGNAL(done(const QString&)), this, SLOT(slotSpellCheckDone(const QString&)));
 
48
    connect(spellDialog, SIGNAL(cancel()), this, SLOT(spellCheckerFinished()));
 
49
    connect(spellDialog, SIGNAL(stop()), this, SLOT(spellCheckerFinished()));
 
50
    spellDialog->setBuffer(text());
 
51
    spellDialog->show();
 
52
}
 
53
 
 
54
void KLineSpellChecking::spellCheckerMisspelling( const QString &_text, int pos)
 
55
{
 
56
    highLightWord( _text.length(),pos );
 
57
}
 
58
 
 
59
void KLineSpellChecking::highLightWord( unsigned int length, unsigned int pos )
 
60
{
 
61
    setSelection ( pos, length );
 
62
}
 
63
 
 
64
void KLineSpellChecking::spellCheckerCorrected( const QString &old, int pos, const QString &corr )
 
65
{
 
66
    if( old!= corr )
 
67
    {
 
68
        setSelection ( pos, old.length() );
 
69
        insert( corr );
 
70
        setSelection ( pos, corr.length() );
 
71
    }
 
72
}
 
73
 
 
74
void KLineSpellChecking::spellCheckerFinished()
 
75
{
 
76
}
 
77
 
 
78
void KLineSpellChecking::slotSpellCheckDone( const QString &s )
 
79
{
 
80
    if( s != text() )
 
81
        setText( s );
 
82
}
 
83
 
 
84
void KLineSpellChecking::contextMenuEvent(QContextMenuEvent *e)
 
85
{
 
86
    QMenu* popup = createStandardContextMenu();
 
87
 
 
88
    if ( !popup )
 
89
        return;
 
90
 
 
91
    if (echoMode() == QLineEdit::Normal &&
 
92
        !isReadOnly()) {
 
93
        popup->addSeparator();
 
94
 
 
95
        popup->addAction( m_spellAction );
 
96
        m_spellAction->setEnabled( !text().isEmpty() );
 
97
    }
 
98
    popup->exec(e->globalPos());
 
99
    delete popup;
 
100
}
 
101
 
 
102
 
 
103
 
 
104
#include "klinespellchecking.moc"
 
105
 
 
106