Better wording
[kdepim.git] / templateparser / templatestextedit.cpp
blob81ac16c209face5114069bf601af3dbe93b500ae
1 /* Copyright (C) 2011, 2012 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 "templatestextedit.h"
20 #include "templatessyntaxhighlighter.h"
22 #include <KGlobalSettings>
24 #include <QCompleter>
25 #include <QKeyEvent>
26 #include <QScrollBar>
27 #include <QStringListModel>
29 using namespace TemplateParser;
31 TemplatesTextEdit::TemplatesTextEdit( QWidget *parent )
32 :KTextEdit( parent )
34 setFocus();
35 const QFont f = KGlobalSettings::fixedFont();
36 setFont( f );
38 setWordWrapMode ( QTextOption::NoWrap );
39 (void) new TemplatesSyntaxHighlighter( document() );
40 initCompleter();
43 TemplatesTextEdit::~TemplatesTextEdit()
47 void TemplatesTextEdit::initCompleter()
49 QStringList listWord;
50 listWord << QLatin1String( "%QUOTE" )
51 << QLatin1String( "%FORCEDPLAIN" )
52 << QLatin1String( "%FORCEDHTML" )
53 << QLatin1String( "%QHEADERS" )
54 << QLatin1String( "%HEADERS" )
55 << QLatin1String( "%TEXT" )
56 << QLatin1String( "%OTEXTSIZE" )
57 << QLatin1String( "%OTEXT" )
58 << QLatin1String( "%OADDRESSEESADDR" )
59 << QLatin1String( "%CCADDR" )
60 << QLatin1String( "%CCNAME" )
61 << QLatin1String( "%CCFNAME" )
62 << QLatin1String( "%CCLNAME" )
63 << QLatin1String( "%TOADDR" )
64 << QLatin1String( "%TONAME" )
65 << QLatin1String( "%TOFNAME" )
66 << QLatin1String( "%TOLNAME" )
67 << QLatin1String( "%TOLIST" )
68 << QLatin1String( "%FROMADDR" )
69 << QLatin1String( "%FROMNAME" )
70 << QLatin1String( "%FROMFNAME" )
71 << QLatin1String( "%FROMLNAME" )
72 << QLatin1String( "%FULLSUBJECT" )
73 << QLatin1String( "%FULLSUBJ" )
74 << QLatin1String( "%MSGID" )
75 << QLatin1String( "%HEADER( " )
76 << QLatin1String( "%OCCADDR" )
77 << QLatin1String( "%OCCNAME" )
78 << QLatin1String( "%OCCFNAME" )
79 << QLatin1String( "%OCCLNAME" )
80 << QLatin1String( "%OTOADDR" )
81 << QLatin1String( "%OTONAME" )
82 << QLatin1String( "%OTOFNAME" )
83 << QLatin1String( "%OTOLNAME" )
84 << QLatin1String( "%OTOLIST" )
85 << QLatin1String( "%OTO" )
86 << QLatin1String( "%OFROMADDR" )
87 << QLatin1String( "%OFROMNAME" )
88 << QLatin1String( "%OFROMFNAME" )
89 << QLatin1String( "%OFROMLNAME" )
90 << QLatin1String( "%OFULLSUBJECT" )
91 << QLatin1String( "%OFULLSUBJ" )
92 << QLatin1String( "%OMSGID" )
93 << QLatin1String( "%DATEEN" )
94 << QLatin1String( "%DATESHORT" )
95 << QLatin1String( "%DATE" )
96 << QLatin1String( "%DOW" )
97 << QLatin1String( "%TIMELONGEN" )
98 << QLatin1String( "%TIMELONG" )
99 << QLatin1String( "%TIME" )
100 << QLatin1String( "%ODATEEN" )
101 << QLatin1String( "%ODATESHORT" )
102 << QLatin1String( "%ODATE" )
103 << QLatin1String( "%ODOW" )
104 << QLatin1String( "%OTIMELONGEN" )
105 << QLatin1String( "%OTIMELONG" )
106 << QLatin1String( "%OTIME" )
107 << QLatin1String( "%BLANK" )
108 << QLatin1String( "%NOP" )
109 << QLatin1String( "%CLEAR" )
110 << QLatin1String( "%DEBUGOFF" )
111 << QLatin1String( "%DEBUG" )
112 << QLatin1String( "%CURSOR" )
113 << QLatin1String( "%SIGNATURE" );
115 listWord << QLatin1String( "%REM=\"\"%-" )
116 << QLatin1String( "%INSERT=\"\"" )
117 << QLatin1String( "%SYSTEM=\"\"" )
118 << QLatin1String( "%PUT=\"\"" )
119 << QLatin1String( "%QUOTEPIPE=\"\"" )
120 << QLatin1String( "%MSGPIPE=\"\"" )
121 << QLatin1String( "%BODYPIPE=\"\"" )
122 << QLatin1String( "%CLEARPIPE=\"\"" )
123 << QLatin1String( "%TEXTPIPE=\"\"" )
124 << QLatin1String( "%OHEADER=\"\"" )
125 << QLatin1String( "%HEADER=\".*\"" );
127 m_completer = new QCompleter( this );
128 m_completer->setModel( new QStringListModel( listWord, m_completer ) );
129 m_completer->setModelSorting( QCompleter::CaseSensitivelySortedModel );
130 m_completer->setCaseSensitivity( Qt::CaseInsensitive );
132 m_completer->setWidget( this );
133 m_completer->setCompletionMode( QCompleter::PopupCompletion );
135 connect( m_completer, SIGNAL(activated(QString)), this, SLOT(slotInsertCompletion(QString)) );
138 void TemplatesTextEdit::slotInsertCompletion( const QString &completion )
140 QTextCursor tc = textCursor();
141 int extra = completion.length() - m_completer->completionPrefix().length();
142 tc.movePosition( QTextCursor::Left );
143 tc.movePosition( QTextCursor::EndOfWord );
144 tc.insertText( completion.right( extra ) );
145 setTextCursor( tc );
149 void TemplatesTextEdit::keyPressEvent( QKeyEvent *e )
151 if( m_completer->popup()->isVisible() ) {
152 switch ( e->key() ) {
153 case Qt::Key_Enter:
154 case Qt::Key_Return:
155 case Qt::Key_Escape:
156 case Qt::Key_Tab:
157 case Qt::Key_Backtab:
158 e->ignore();
159 return; // let the completer do default behavior
160 default:
161 break;
164 KTextEdit::keyPressEvent( e );
165 const QString text = wordUnderCursor();
166 if( text.length() < 2 ) {
167 // min 2 char for completion
168 return;
171 m_completer->setCompletionPrefix( text );
173 QRect cr = cursorRect();
174 cr.setWidth( m_completer->popup()->sizeHintForColumn( 0 ) +
175 m_completer->popup()->verticalScrollBar()->sizeHint().width() );
176 m_completer->complete( cr );
179 QString TemplatesTextEdit::wordUnderCursor()
181 static QString eow =
182 QLatin1String( "~!@#$^&*()+{}|\"<>,./;'[]\\-= " ); // everything without ':', '?' and '_'
183 QTextCursor tc = textCursor();
185 tc.anchor();
186 while ( 1 ) {
187 // vHanda: I don't understand why the cursor seems to give a pos 1 past
188 // the last char instead of just the last char.
189 int pos = tc.position() - 1;
190 if ( pos < 0 || eow.contains( document()->characterAt(pos) ) ) {
191 break;
193 tc.movePosition( QTextCursor::Left, QTextCursor::KeepAnchor );
195 return tc.selectedText();
198 #include "templatestextedit.moc"