~ubuntu-branches/ubuntu/quantal/kdepim/quantal

« back to all changes in this revision

Viewing changes to libksieve/ksieveui/sievetextedit.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Kolberg
  • Date: 2011-09-03 13:01:43 UTC
  • mfrom: (0.2.14)
  • Revision ID: package-import@ubuntu.com-20110903130143-5mmx3goibh8sgt9t
Tags: 4:4.7.1-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2011 Laurent Montel <montel@kde.org>
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "sievetextedit.h"
 
20
#include "sievesyntaxhighlighter.h"
 
21
#include <kglobalsettings.h>
 
22
#include <QCompleter>
 
23
#include <QStringListModel>
 
24
#include <QKeyEvent>
 
25
#include <QAbstractItemView>
 
26
#include <QScrollBar>
 
27
 
 
28
using namespace KSieveUi;
 
29
 
 
30
SieveTextEdit::SieveTextEdit( QWidget *parent )
 
31
  :KTextEdit( parent )
 
32
{
 
33
  setFocus();
 
34
  setAcceptRichText( false );
 
35
  setCheckSpellingEnabled( false );
 
36
  setWordWrapMode ( QTextOption::NoWrap );
 
37
  setFont( KGlobalSettings::fixedFont() );
 
38
  (void) new SieveSyntaxHighlighter( document() );
 
39
  initCompleter();
 
40
}
 
41
 
 
42
SieveTextEdit::~SieveTextEdit()
 
43
{
 
44
}
 
45
 
 
46
 
 
47
void SieveTextEdit::initCompleter()
 
48
{
 
49
  QStringList listWord;
 
50
 
 
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" );
 
56
  
 
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 );
 
61
  
 
62
  m_completer->setWidget( this );
 
63
  m_completer->setCompletionMode( QCompleter::PopupCompletion );
 
64
  
 
65
  connect( m_completer, SIGNAL(activated(QString)), this, SLOT(slotInsertCompletion(QString)) );
 
66
}
 
67
 
 
68
void SieveTextEdit::slotInsertCompletion( const QString& completion )
 
69
{
 
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));
 
75
  setTextCursor(tc);
 
76
 
 
77
}
 
78
 
 
79
void SieveTextEdit::keyPressEvent(QKeyEvent* e)
 
80
{
 
81
  if( m_completer->popup()->isVisible() ) {
 
82
    switch (e->key()) {
 
83
    case Qt::Key_Enter:
 
84
    case Qt::Key_Return:
 
85
    case Qt::Key_Escape:
 
86
    case Qt::Key_Tab:
 
87
    case Qt::Key_Backtab:
 
88
      e->ignore();
 
89
      return; // let the completer do default behavior
 
90
    default:
 
91
      break;
 
92
    }
 
93
  }
 
94
  KTextEdit::keyPressEvent(e);
 
95
  QString text = wordUnderCursor();
 
96
  if( text.length() < 2 ) // min 2 char for completion
 
97
    return;
 
98
 
 
99
  m_completer->setCompletionPrefix( text );
 
100
 
 
101
  QRect cr = cursorRect();
 
102
  cr.setWidth( m_completer->popup()->sizeHintForColumn(0)
 
103
               + m_completer->popup()->verticalScrollBar()->sizeHint().width() );
 
104
  m_completer->complete( cr );
 
105
}
 
106
 
 
107
QString SieveTextEdit::wordUnderCursor()
 
108
{
 
109
    static QString eow = QLatin1String( "~!@#$%^&*()+{}|\"<>,./;'[]\\-= " ); // everything without ':', '?' and '_'
 
110
    QTextCursor tc = textCursor();
 
111
 
 
112
    tc.anchor();
 
113
    while( 1 ) {
 
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) ) )
 
118
            break;
 
119
        tc.movePosition( QTextCursor::Left, QTextCursor::KeepAnchor );
 
120
    }
 
121
    return tc.selectedText();
 
122
}
 
123
 
 
124
#include "sievetextedit.moc"