1
/* Copyright (C) 2011 Laurent Montel <montel@kde.org>
3
* This library is free software; you can redistribute it and/or
4
* modify it under the terms of the GNU Library General Public
5
* License as published by the Free Software Foundation; either
6
* version 2 of the License, or (at your option) any later version.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Library General Public License for more details.
13
* You should have received a copy of the GNU Library General Public License
14
* along with this library; see the file COPYING.LIB. If not, write to
15
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
* Boston, MA 02110-1301, USA.
19
#include "sievetextedit.h"
20
#include "sievesyntaxhighlighter.h"
21
#include <kglobalsettings.h>
23
#include <QStringListModel>
25
#include <QAbstractItemView>
28
using namespace KSieveUi;
30
SieveTextEdit::SieveTextEdit( QWidget *parent )
34
setAcceptRichText( false );
35
setCheckSpellingEnabled( false );
36
setWordWrapMode ( QTextOption::NoWrap );
37
setFont( KGlobalSettings::fixedFont() );
38
(void) new SieveSyntaxHighlighter( document() );
42
SieveTextEdit::~SieveTextEdit()
47
void SieveTextEdit::initCompleter()
51
listWord<< QLatin1String( "require" )<<QLatin1String( "stop" );
52
listWord << QLatin1String( ":contains" )<<QLatin1String( ":matches" )<<QLatin1String( ":is" )<<QLatin1String( ":over" )<<QLatin1String( ":under" );
53
listWord << QLatin1String( "if" )<<QLatin1String( "elsif" )<<QLatin1String( "else" );
54
listWord << QLatin1String( "keep" )<<QLatin1String( "reject" )<<QLatin1String( "discard" )<<QLatin1String( "redirect" )<<QLatin1String( "fileinto" );
55
listWord << QLatin1String( "address" )<<QLatin1String( "allof" )<<QLatin1String( "anyof" )<<QLatin1String( "exists" )<<QLatin1String( "false" )<<QLatin1String( "header" )<<QLatin1String("not" )<<QLatin1String( "size" )<<QLatin1String( "true" );
57
m_completer = new QCompleter( this );
58
m_completer->setModel( new QStringListModel( listWord, m_completer ) );
59
m_completer->setModelSorting( QCompleter::CaseSensitivelySortedModel );
60
m_completer->setCaseSensitivity( Qt::CaseInsensitive );
62
m_completer->setWidget( this );
63
m_completer->setCompletionMode( QCompleter::PopupCompletion );
65
connect( m_completer, SIGNAL(activated(QString)), this, SLOT(slotInsertCompletion(QString)) );
68
void SieveTextEdit::slotInsertCompletion( const QString& completion )
70
QTextCursor tc = textCursor();
71
int extra = completion.length() - m_completer->completionPrefix().length();
72
tc.movePosition(QTextCursor::Left);
73
tc.movePosition(QTextCursor::EndOfWord);
74
tc.insertText(completion.right(extra));
79
void SieveTextEdit::keyPressEvent(QKeyEvent* e)
81
if( m_completer->popup()->isVisible() ) {
89
return; // let the completer do default behavior
94
KTextEdit::keyPressEvent(e);
95
QString text = wordUnderCursor();
96
if( text.length() < 2 ) // min 2 char for completion
99
m_completer->setCompletionPrefix( text );
101
QRect cr = cursorRect();
102
cr.setWidth( m_completer->popup()->sizeHintForColumn(0)
103
+ m_completer->popup()->verticalScrollBar()->sizeHint().width() );
104
m_completer->complete( cr );
107
QString SieveTextEdit::wordUnderCursor()
109
static QString eow = QLatin1String( "~!@#$%^&*()+{}|\"<>,./;'[]\\-= " ); // everything without ':', '?' and '_'
110
QTextCursor tc = textCursor();
114
// vHanda: I don't understand why the cursor seems to give a pos 1 past the last char instead
115
// of just the last char.
116
int pos = tc.position() - 1;
117
if( pos < 0 || eow.contains( document()->characterAt(pos) ) )
119
tc.movePosition( QTextCursor::Left, QTextCursor::KeepAnchor );
121
return tc.selectedText();
124
#include "sievetextedit.moc"