kdeui Library API Documentation

ktextedit.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "ktextedit.h" 00021 00022 #include <qapplication.h> 00023 #include <qclipboard.h> 00024 #include <qpopupmenu.h> 00025 00026 #include <ksyntaxhighlighter.h> 00027 #include <kspell.h> 00028 #include <kcursor.h> 00029 #include <kglobalsettings.h> 00030 #include <kstdaccel.h> 00031 #include <kiconloader.h> 00032 #include <klocale.h> 00033 00034 class KTextEdit::KTextEditPrivate 00035 { 00036 public: 00037 KTextEditPrivate() 00038 : customPalette( false ), 00039 checkSpellingEnabled( false ), 00040 highlighter( 0 ), 00041 spell( 0 ) 00042 {} 00043 ~KTextEditPrivate() { 00044 delete highlighter; 00045 delete spell; 00046 } 00047 00048 bool customPalette; 00049 bool checkSpellingEnabled; 00050 KDictSpellingHighlighter *highlighter; 00051 KSpell *spell; 00052 }; 00053 00054 KTextEdit::KTextEdit( const QString& text, const QString& context, 00055 QWidget *parent, const char *name ) 00056 : QTextEdit ( text, context, parent, name ) 00057 { 00058 d = new KTextEditPrivate(); 00059 KCursor::setAutoHideCursor( this, true, false ); 00060 } 00061 00062 KTextEdit::KTextEdit( QWidget *parent, const char *name ) 00063 : QTextEdit ( parent, name ) 00064 { 00065 d = new KTextEditPrivate(); 00066 KCursor::setAutoHideCursor( this, true, false ); 00067 } 00068 00069 KTextEdit::~KTextEdit() 00070 { 00071 delete d; 00072 } 00073 00074 void KTextEdit::keyPressEvent( QKeyEvent *e ) 00075 { 00076 KKey key( e ); 00077 00078 if ( KStdAccel::copy().contains( key ) ) { 00079 copy(); 00080 e->accept(); 00081 return; 00082 } 00083 else if ( KStdAccel::paste().contains( key ) ) { 00084 paste(); 00085 e->accept(); 00086 return; 00087 } 00088 else if ( KStdAccel::cut().contains( key ) ) { 00089 cut(); 00090 e->accept(); 00091 return; 00092 } 00093 else if ( KStdAccel::undo().contains( key ) ) { 00094 undo(); 00095 e->accept(); 00096 return; 00097 } 00098 else if ( KStdAccel::redo().contains( key ) ) { 00099 redo(); 00100 e->accept(); 00101 return; 00102 } 00103 else if ( KStdAccel::deleteWordBack().contains( key ) ) 00104 { 00105 deleteWordBack(); 00106 e->accept(); 00107 return; 00108 } 00109 else if ( KStdAccel::deleteWordForward().contains( key ) ) 00110 { 00111 deleteWordForward(); 00112 e->accept(); 00113 return; 00114 } 00115 00116 else if ( e->key() == Key_Insert && 00117 (e->state() == (ShiftButton | ControlButton)) ) 00118 { 00119 QString text = QApplication::clipboard()->text( QClipboard::Selection); 00120 if ( !text.isEmpty() ) 00121 insert( text ); 00122 e->accept(); 00123 return; 00124 } 00125 00126 // ignore Ctrl-Return so that KDialogs can close the dialog 00127 else if ( e->state() == ControlButton && 00128 (e->key() == Key_Return || e->key() == Key_Enter) && 00129 topLevelWidget()->inherits( "KDialog" ) ) 00130 { 00131 e->ignore(); 00132 return; 00133 } 00134 00135 QTextEdit::keyPressEvent( e ); 00136 } 00137 00138 void KTextEdit::deleteWordBack() 00139 { 00140 removeSelection(); 00141 moveCursor( MoveWordBackward, true ); 00142 removeSelectedText(); 00143 } 00144 00145 void KTextEdit::deleteWordForward() 00146 { 00147 removeSelection(); 00148 moveCursor( MoveWordForward, true ); 00149 removeSelectedText(); 00150 } 00151 00152 QPopupMenu *KTextEdit::createPopupMenu( const QPoint &pos ) 00153 { 00154 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll }; 00155 00156 QPopupMenu *menu = QTextEdit::createPopupMenu( pos ); 00157 00158 if ( isReadOnly() ) 00159 menu->changeItem( menu->idAt(0), SmallIconSet("editcopy"), menu->text( menu->idAt(0) ) ); 00160 else { 00161 int id = menu->idAt(0); 00162 menu->changeItem( id - IdUndo, SmallIcon("undo"), menu->text( id - IdUndo) ); 00163 menu->changeItem( id - IdRedo, SmallIcon("redo"), menu->text( id - IdRedo) ); 00164 menu->changeItem( id - IdCut, SmallIcon("editcut"), menu->text( id - IdCut) ); 00165 menu->changeItem( id - IdCopy, SmallIcon("editcopy"), menu->text( id - IdCopy) ); 00166 menu->changeItem( id - IdPaste, SmallIcon("editpaste"), menu->text( id - IdPaste) ); 00167 menu->changeItem( id - IdClear, SmallIcon("editclear"), menu->text( id - IdClear) ); 00168 } 00169 00170 if ( checkSpellingEnabled() && !isReadOnly() ) { 00171 00172 menu->insertSeparator(); 00173 int id = menu->insertItem( SmallIcon( "spellcheck" ), i18n( "Check Spelling..." ), 00174 this, SLOT( checkSpelling() ) ); 00175 00176 if( text().isEmpty() ) 00177 menu->setItemEnabled( id, false ); 00178 } 00179 00180 return menu; 00181 } 00182 00183 QPopupMenu *KTextEdit::createPopupMenu() 00184 { 00185 return QTextEdit::createPopupMenu(); 00186 } 00187 00188 void KTextEdit::contentsWheelEvent( QWheelEvent *e ) 00189 { 00190 if ( KGlobalSettings::wheelMouseZooms() ) 00191 QTextEdit::contentsWheelEvent( e ); 00192 else // thanks, we don't want to zoom, so skip QTextEdit's impl. 00193 QScrollView::contentsWheelEvent( e ); 00194 } 00195 00196 void KTextEdit::setPalette( const QPalette& palette ) 00197 { 00198 QTextEdit::setPalette( palette ); 00199 // unsetPalette() is not virtual and calls setPalette() as well 00200 // so we can use ownPalette() to find out about unsetting 00201 d->customPalette = ownPalette(); 00202 } 00203 00204 void KTextEdit::setCheckSpellingEnabled( bool check ) 00205 { 00206 if ( check == d->checkSpellingEnabled ) 00207 return; 00208 00209 // From the above statment we know know that if we're turning checking 00210 // on that we need to create a new highlighter and if we're turning it 00211 // off we should remove the old one. 00212 00213 d->checkSpellingEnabled = check; 00214 if ( hasFocus() ) 00215 d->highlighter = new KDictSpellingHighlighter( this ); 00216 else { 00217 delete d->highlighter; 00218 d->highlighter = 0; 00219 } 00220 } 00221 00222 void KTextEdit::focusInEvent( QFocusEvent *e ) 00223 { 00224 if ( d->checkSpellingEnabled && !d->highlighter ) 00225 d->highlighter = new KDictSpellingHighlighter( this ); 00226 00227 QTextEdit::focusInEvent( e ); 00228 } 00229 00230 bool KTextEdit::checkSpellingEnabled() const 00231 { 00232 return d->checkSpellingEnabled; 00233 } 00234 00235 void KTextEdit::setReadOnly(bool readOnly) 00236 { 00237 if ( readOnly == isReadOnly() ) 00238 return; 00239 00240 if (readOnly) 00241 { 00242 bool custom = ownPalette(); 00243 QPalette p = palette(); 00244 QColor color = p.color(QPalette::Disabled, QColorGroup::Background); 00245 p.setColor(QColorGroup::Base, color); 00246 p.setColor(QColorGroup::Background, color); 00247 setPalette(p); 00248 d->customPalette = custom; 00249 } 00250 else 00251 { 00252 if ( d->customPalette ) 00253 { 00254 QPalette p = palette(); 00255 QColor color = p.color(QPalette::Normal, QColorGroup::Base); 00256 p.setColor(QColorGroup::Base, color); 00257 p.setColor(QColorGroup::Background, color); 00258 setPalette( p ); 00259 } 00260 else 00261 unsetPalette(); 00262 } 00263 00264 QTextEdit::setReadOnly (readOnly); 00265 } 00266 00267 void KTextEdit::virtual_hook( int, void* ) 00268 { /*BASE::virtual_hook( id, data );*/ } 00269 00270 void KTextEdit::checkSpelling() 00271 { 00272 delete d->spell; 00273 d->spell = new KSpell( this, i18n( "Spell Checking" ), 00274 this, SLOT( slotSpellCheckReady( KSpell *) ), 0, true, true); 00275 00276 connect( d->spell, SIGNAL( death() ), 00277 this, SLOT( spellCheckerFinished() ) ); 00278 00279 connect( d->spell, SIGNAL( misspelling( const QString &, const QStringList &, unsigned int ) ), 00280 this, SLOT( spellCheckerMisspelling( const QString &, const QStringList &, unsigned int ) ) ); 00281 00282 connect( d->spell, SIGNAL( corrected( const QString &, const QString &, unsigned int ) ), 00283 this, SLOT( spellCheckerCorrected( const QString &, const QString &, unsigned int ) ) ); 00284 } 00285 00286 void KTextEdit::spellCheckerMisspelling( const QString &text, const QStringList &, unsigned int pos ) 00287 { 00288 highLightWord( text.length(), pos ); 00289 } 00290 00291 void KTextEdit::spellCheckerCorrected( const QString &oldWord, const QString &newWord, unsigned int pos ) 00292 { 00293 unsigned int l = 0; 00294 unsigned int cnt = 0; 00295 if ( oldWord != newWord ) { 00296 posToRowCol( pos, l, cnt ); 00297 setSelection( l, cnt, l, cnt + oldWord.length() ); 00298 removeSelectedText(); 00299 insert( newWord ); 00300 } 00301 } 00302 00303 void KTextEdit::posToRowCol(unsigned int pos, unsigned int &line, unsigned int &col) 00304 { 00305 for ( line = 0; line < static_cast<uint>( lines() ) && col <= pos; line++ ) 00306 col += paragraphLength( line ) + 1; 00307 00308 line--; 00309 col = pos - col + paragraphLength( line ) + 1; 00310 } 00311 00312 void KTextEdit::spellCheckerFinished() 00313 { 00314 delete d->spell; 00315 d->spell = 0L; 00316 } 00317 00318 void KTextEdit::slotSpellCheckReady( KSpell *s ) 00319 { 00320 s->check( text() ); 00321 connect( s, SIGNAL( done( const QString & ) ), this, SLOT( slotSpellCheckDone( const QString & ) ) ); 00322 } 00323 00324 void KTextEdit::slotSpellCheckDone( const QString &s ) 00325 { 00326 if ( s != text() ) 00327 setText( s ); 00328 } 00329 00330 00331 void KTextEdit::highLightWord( unsigned int length, unsigned int pos ) 00332 { 00333 unsigned int l = 0; 00334 unsigned int cnt = 0; 00335 posToRowCol( pos, l, cnt ); 00336 setSelection( l, cnt, l, cnt + length ); 00337 } 00338 00339 #include "ktextedit.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:55:10 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003