• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDE3Support

  • kde3support
  • kdeui
k3textedit.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "k3textedit.h"
21
22#include <QtGui/QApplication>
23#include <QtGui/QClipboard>
24#include <Qt3Support/Q3PopupMenu>
25
26#include <k3syntaxhighlighter.h>
27#include <k3spell.h>
28#include <kcursor.h>
29#include <kglobalsettings.h>
30#include <kstandardshortcut.h>
31#include <kicon.h>
32#include <klocale.h>
33#include <QKeyEvent>
34
35class K3TextEdit::K3TextEditPrivate
36{
37public:
38 K3TextEditPrivate()
39 : customPalette( false ),
40 checkSpellingEnabled( false ),
41 highlighter( 0 ),
42 spell( 0 )
43 {}
44 ~K3TextEditPrivate() {
45 delete highlighter;
46 delete spell;
47 }
48
49 bool customPalette;
50 bool checkSpellingEnabled;
51 K3DictSpellingHighlighter *highlighter;
52 K3Spell *spell;
53};
54
55K3TextEdit::K3TextEdit( const QString& text, const QString& context,
56 QWidget *parent, const char *name )
57 : Q3TextEdit ( text, context, parent, name )
58{
59 d = new K3TextEditPrivate();
60 KCursor::setAutoHideCursor( this, true, false );
61}
62
63K3TextEdit::K3TextEdit( QWidget *parent, const char *name )
64 : Q3TextEdit ( parent, name )
65{
66 d = new K3TextEditPrivate();
67 KCursor::setAutoHideCursor( this, true, false );
68}
69
70K3TextEdit::~K3TextEdit()
71{
72 delete d;
73}
74
75void K3TextEdit::keyPressEvent( QKeyEvent *e )
76{
77 int key = e->key();
78
79 if ( KStandardShortcut::copy().contains( key ) ) {
80 copy();
81 e->accept();
82 return;
83 }
84 else if ( KStandardShortcut::paste().contains( key ) ) {
85 paste();
86 e->accept();
87 return;
88 }
89 else if ( KStandardShortcut::cut().contains( key ) ) {
90 cut();
91 e->accept();
92 return;
93 }
94 else if ( KStandardShortcut::undo().contains( key ) ) {
95 undo();
96 e->accept();
97 return;
98 }
99 else if ( KStandardShortcut::redo().contains( key ) ) {
100 redo();
101 e->accept();
102 return;
103 }
104 else if ( KStandardShortcut::deleteWordBack().contains( key ) )
105 {
106 deleteWordBack();
107 e->accept();
108 return;
109 }
110 else if ( KStandardShortcut::deleteWordForward().contains( key ) )
111 {
112 deleteWordForward();
113 e->accept();
114 return;
115 }
116 else if ( KStandardShortcut::backwardWord().contains( key ) )
117 {
118 CursorAction action = MoveWordBackward;
119 int para, index;
120 getCursorPosition( &para, & index );
121 if (text(para).isRightToLeft())
122 action = MoveWordForward;
123 moveCursor(action, false );
124 e->accept();
125 return;
126 }
127 else if ( KStandardShortcut::forwardWord().contains( key ) )
128 {
129 CursorAction action = MoveWordForward;
130 int para, index;
131 getCursorPosition( &para, & index );
132 if (text(para).isRightToLeft())
133 action = MoveWordBackward;
134 moveCursor( action, false );
135 e->accept();
136 return;
137 }
138 else if ( KStandardShortcut::next().contains( key ) )
139 {
140 moveCursor( MovePgDown, false );
141 e->accept();
142 return;
143 }
144 else if ( KStandardShortcut::prior().contains( key ) )
145 {
146 moveCursor( MovePgUp, false );
147 e->accept();
148 return;
149 }
150 else if ( KStandardShortcut::begin().contains( key ) )
151 {
152 moveCursor( MoveHome, false );
153 e->accept();
154 return;
155 }
156 else if ( KStandardShortcut::end().contains( key ) )
157 {
158 moveCursor( MoveEnd, false );
159 e->accept();
160 return;
161 }
162 else if ( KStandardShortcut::beginningOfLine().contains( key ) )
163 {
164 moveCursor( MoveLineStart, false );
165 e->accept();
166 return;
167 }
168 else if ( KStandardShortcut::endOfLine().contains( key ) )
169 {
170 moveCursor(MoveLineEnd, false);
171 e->accept();
172 return;
173 }
174 else if ( KStandardShortcut::pasteSelection().contains( key ) )
175 {
176 QString text = QApplication::clipboard()->text( QClipboard::Selection);
177 if ( !text.isEmpty() )
178 insert( text );
179 e->accept();
180 return;
181 }
182
183 // ignore Ctrl-Return so that KDialogs can close the dialog
184 else if ( e->state() == Qt::ControlModifier &&
185 (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) &&
186 topLevelWidget()->inherits( "KDialog" ) )
187 {
188 e->ignore();
189 return;
190 }
191
192 Q3TextEdit::keyPressEvent( e );
193}
194
195void K3TextEdit::deleteWordBack()
196{
197 removeSelection();
198 moveCursor( MoveWordBackward, true );
199 removeSelectedText();
200}
201
202void K3TextEdit::deleteWordForward()
203{
204 removeSelection();
205 moveCursor( MoveWordForward, true );
206 removeSelectedText();
207}
208
209void K3TextEdit::slotAllowTab()
210{
211setTabChangesFocus(!tabChangesFocus());
212}
213
214Q3PopupMenu *K3TextEdit::createPopupMenu( const QPoint &pos )
215{
216 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll };
217
218 Q3PopupMenu *menu = Q3TextEdit::createPopupMenu( pos );
219
220 if ( isReadOnly() )
221 menu->changeItem( menu->idAt(0), KIcon("edit-copy"), menu->text( menu->idAt(0) ) );
222 else {
223 int id = menu->idAt(0);
224 menu->changeItem( id - IdUndo, KIcon("edit-undo"), menu->text( id - IdUndo) );
225 menu->changeItem( id - IdRedo, KIcon("edit-redo"), menu->text( id - IdRedo) );
226 menu->changeItem( id - IdCut, KIcon("edit-cut"), menu->text( id - IdCut) );
227 menu->changeItem( id - IdCopy, KIcon("edit-copy"), menu->text( id - IdCopy) );
228 menu->changeItem( id - IdPaste, KIcon("edit-paste"), menu->text( id - IdPaste) );
229 menu->changeItem( id - IdClear, KIcon("edit-clear"), menu->text( id - IdClear) );
230
231 menu->insertSeparator();
232 id = menu->insertItem( KIcon( "tools-check-spelling" ), i18n( "Check Spelling..." ),
233 this, SLOT(checkSpelling()) );
234
235 if( text().isEmpty() )
236 menu->setItemEnabled( id, false );
237
238 id = menu->insertItem( i18n( "Auto Spell Check" ),
239 this, SLOT(toggleAutoSpellCheck()) );
240 menu->setItemChecked(id, d->checkSpellingEnabled);
241 menu->insertSeparator();
242 id=menu->insertItem(i18n("Allow Tabulations"),this,SLOT(slotAllowTab()));
243 menu->setItemChecked(id, !tabChangesFocus());
244 }
245
246 return menu;
247}
248
249Q3PopupMenu *K3TextEdit::createPopupMenu()
250{
251 return Q3TextEdit::createPopupMenu();
252}
253
254void K3TextEdit::contentsWheelEvent( QWheelEvent *e )
255{
256 if ( KGlobalSettings::wheelMouseZooms() )
257 Q3TextEdit::contentsWheelEvent( e );
258 else // thanks, we don't want to zoom, so skip QTextEdit's impl.
259 Q3ScrollView::contentsWheelEvent( e );
260}
261
262void K3TextEdit::setPalette( const QPalette& palette )
263{
264 Q3TextEdit::setPalette( palette );
265 // unsetPalette() is not virtual and calls setPalette() as well
266 // so we can use ownPalette() to find out about unsetting
267 d->customPalette = ownPalette();
268}
269
270void K3TextEdit::toggleAutoSpellCheck()
271{
272 setCheckSpellingEnabled( !d->checkSpellingEnabled );
273}
274
275void K3TextEdit::setCheckSpellingEnabled( bool check )
276{
277 if ( check == d->checkSpellingEnabled )
278 return;
279
280 // From the above statment we know know that if we're turning checking
281 // on that we need to create a new highlighter and if we're turning it
282 // off we should remove the old one.
283
284 d->checkSpellingEnabled = check;
285 if ( check )
286 {
287 if (hasFocus())
288 d->highlighter = new K3DictSpellingHighlighter( this );
289 }
290 else
291 {
292 delete d->highlighter;
293 d->highlighter = 0;
294 }
295}
296
297void K3TextEdit::focusInEvent( QFocusEvent *e )
298{
299 if ( d->checkSpellingEnabled && !d->highlighter )
300 d->highlighter = new K3DictSpellingHighlighter( this );
301
302 Q3TextEdit::focusInEvent( e );
303}
304
305bool K3TextEdit::checkSpellingEnabled() const
306{
307 return d->checkSpellingEnabled;
308}
309
310void K3TextEdit::setReadOnly(bool readOnly)
311{
312 if ( readOnly == isReadOnly() )
313 return;
314
315 if (readOnly)
316 {
317 bool custom = ownPalette();
318 QPalette p = palette();
319 QColor color = p.color(QPalette::Disabled, QPalette::Background);
320 p.setColor(QPalette::Base, color);
321 p.setColor(QPalette::Background, color);
322 setPalette(p);
323 d->customPalette = custom;
324 }
325 else
326 {
327 if ( d->customPalette )
328 {
329 QPalette p = palette();
330 QColor color = p.color(QPalette::Normal, QPalette::Base);
331 p.setColor(QPalette::Base, color);
332 p.setColor(QPalette::Background, color);
333 setPalette( p );
334 }
335 else
336 unsetPalette();
337 }
338
339 Q3TextEdit::setReadOnly (readOnly);
340}
341
342void K3TextEdit::virtual_hook( int, void* )
343{ /*BASE::virtual_hook( id, data );*/ }
344
345void K3TextEdit::checkSpelling()
346{
347 delete d->spell;
348 d->spell = new K3Spell( this, i18n( "Spell Checking" ),
349 this, SLOT(slotSpellCheckReady(K3Spell*)), 0, true, true);
350
351 connect( d->spell, SIGNAL(death()),
352 this, SLOT(spellCheckerFinished()) );
353
354 connect( d->spell, SIGNAL(misspelling(QString,QStringList,uint)),
355 this, SLOT(spellCheckerMisspelling(QString,QStringList,uint)) );
356
357 connect( d->spell, SIGNAL(corrected(QString,QString,uint)),
358 this, SLOT(spellCheckerCorrected(QString,QString,uint)) );
359}
360
361void K3TextEdit::spellCheckerMisspelling( const QString &text, const QStringList &, unsigned int pos )
362{
363 highLightWord( text.length(), pos );
364}
365
366void K3TextEdit::spellCheckerCorrected( const QString &oldWord, const QString &newWord, unsigned int pos )
367{
368 unsigned int l = 0;
369 unsigned int cnt = 0;
370 if ( oldWord != newWord ) {
371 posToRowCol( pos, l, cnt );
372 setSelection( l, cnt, l, cnt + oldWord.length() );
373 removeSelectedText();
374 insert( newWord );
375 }
376}
377
378void K3TextEdit::posToRowCol(unsigned int pos, unsigned int &line, unsigned int &col)
379{
380 for ( line = 0; line < static_cast<uint>( lines() ) && col <= pos; line++ )
381 col += paragraphLength( line ) + 1;
382
383 line--;
384 col = pos - col + paragraphLength( line ) + 1;
385}
386
387void K3TextEdit::spellCheckerFinished()
388{
389 delete d->spell;
390 d->spell = 0L;
391}
392
393void K3TextEdit::slotSpellCheckReady( K3Spell *s )
394{
395 s->check( text() );
396 connect( s, SIGNAL(done(QString)), this, SLOT(slotSpellCheckDone(QString)) );
397}
398
399void K3TextEdit::slotSpellCheckDone( const QString &s )
400{
401 if ( s != text() )
402 setText( s );
403}
404
405
406void K3TextEdit::highLightWord( unsigned int length, unsigned int pos )
407{
408 unsigned int l = 0;
409 unsigned int cnt = 0;
410 posToRowCol( pos, l, cnt );
411 setSelection( l, cnt, l, cnt + length );
412}
413
414#include "k3textedit.moc"
K3DictSpellingHighlighter
Dictionary sensitive text highlighter.
Definition: k3syntaxhighlighter.h:93
K3Spell
KDE Spellchecker
Definition: k3spell.h:47
K3Spell::check
virtual bool check(const QString &_buffer, bool usedialog=true)
Spellchecks a buffer of many words in plain text format.
Definition: k3spell.cpp:1082
K3TextEdit::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: k3textedit.cpp:342
K3TextEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *)
Reimplemented to catch "delete word" key events.
Definition: k3textedit.cpp:75
K3TextEdit::setPalette
virtual void setPalette(const QPalette &palette)
Reimplemented for tracking custom palettes.
Definition: k3textedit.cpp:262
K3TextEdit::createPopupMenu
virtual Q3PopupMenu * createPopupMenu()
This is just a reimplementation of a deprecated method from Q3TextEdit and is just here to keep sourc...
Definition: k3textedit.cpp:249
K3TextEdit::K3TextEdit
K3TextEdit(const QString &text, const QString &context=QString(), QWidget *parent=0, const char *name=0)
Constructs a K3TextEdit object.
Definition: k3textedit.cpp:55
K3TextEdit::contentsWheelEvent
virtual void contentsWheelEvent(QWheelEvent *)
Reimplemented to allow fast-wheelscrolling with Ctrl-Wheel or zoom.
Definition: k3textedit.cpp:254
K3TextEdit::highLightWord
void highLightWord(unsigned int length, unsigned int pos)
Definition: k3textedit.cpp:406
K3TextEdit::deleteWordForward
virtual void deleteWordForward()
Deletes a word forwards from the current cursor position, if available.
Definition: k3textedit.cpp:202
K3TextEdit::focusInEvent
virtual void focusInEvent(QFocusEvent *)
Reimplemented to instantiate a KDictSpellingHighlighter, if spellchecking is enabled.
Definition: k3textedit.cpp:297
K3TextEdit::setReadOnly
virtual void setReadOnly(bool readOnly)
Reimplemented to set a proper "deactivated" background color.
Definition: k3textedit.cpp:310
K3TextEdit::checkSpellingEnabled
bool checkSpellingEnabled() const
Returns true if spell checking is enabled for this text edit.
Definition: k3textedit.cpp:305
K3TextEdit::setCheckSpellingEnabled
void setCheckSpellingEnabled(bool check)
Turns spell checking for this text edit on or off.
Definition: k3textedit.cpp:275
K3TextEdit::~K3TextEdit
~K3TextEdit()
Destroys the K3TextEdit object.
Definition: k3textedit.cpp:70
K3TextEdit::deleteWordBack
virtual void deleteWordBack()
Deletes a word backwards from the current cursor position, if available.
Definition: k3textedit.cpp:195
K3TextEdit::checkSpelling
void checkSpelling()
Create a modal dialog to check the spelling.
Definition: k3textedit.cpp:345
KCursor::setAutoHideCursor
static void setAutoHideCursor(QWidget *w, bool enable, bool customEventFilter=false)
KGlobalSettings::wheelMouseZooms
static bool wheelMouseZooms()
KIcon
Q3PopupMenu
Q3TextEdit
QWidget
k3spell.h
k3syntaxhighlighter.h
k3textedit.h
kcursor.h
kglobalsettings.h
kicon.h
klocale.h
i18n
QString i18n(const char *text)
kstandardshortcut.h
cut
KAction * cut(const QObject *recvr, const char *slot, QObject *parent)
copy
KAction * copy(const QObject *recvr, const char *slot, QObject *parent)
undo
KAction * undo(const QObject *recvr, const char *slot, QObject *parent)
name
const char * name(StandardAction id)
paste
KAction * paste(const QObject *recvr, const char *slot, QObject *parent)
redo
KAction * redo(const QObject *recvr, const char *slot, QObject *parent)
KStandardShortcut::prior
const KShortcut & prior()
KStandardShortcut::undo
const KShortcut & undo()
KStandardShortcut::forwardWord
const KShortcut & forwardWord()
KStandardShortcut::deleteWordBack
const KShortcut & deleteWordBack()
insert
const KShortcut & insert()
KStandardShortcut::endOfLine
const KShortcut & endOfLine()
KStandardShortcut::beginningOfLine
const KShortcut & beginningOfLine()
KStandardShortcut::backwardWord
const KShortcut & backwardWord()
KStandardShortcut::begin
const KShortcut & begin()
KStandardShortcut::end
const KShortcut & end()
KStandardShortcut::cut
const KShortcut & cut()
KStandardShortcut::paste
const KShortcut & paste()
KStandardShortcut::deleteWordForward
const KShortcut & deleteWordForward()
KStandardShortcut::copy
const KShortcut & copy()
KStandardShortcut::redo
const KShortcut & redo()
KStandardShortcut::next
const KShortcut & next()
KStandardShortcut::pasteSelection
const KShortcut & pasteSelection()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

Skip menu "KDE3Support"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal