knotes Library API Documentation

knoteedit.cpp

00001 /******************************************************************* 00002 KNotes -- Notes for the KDE project 00003 00004 Copyright (c) 1997-2004, The KNotes Developers 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 *******************************************************************/ 00020 00021 #include <qdragobject.h> 00022 #include <qfont.h> 00023 00024 #include <kdebug.h> 00025 #include <klocale.h> 00026 #include <kaction.h> 00027 #include <kurldrag.h> 00028 #include <kstdaction.h> 00029 #include <kcolordialog.h> 00030 #include <kxmlguiclient.h> 00031 00032 #include "knoteedit.h" 00033 #include "knotebutton.h" 00034 00035 static const short SEP = 5; 00036 static const short ICON_SIZE = 10; 00037 00038 00039 KNoteEdit::KNoteEdit( QWidget *parent, const char *name ) 00040 : KTextEdit( parent, name ) 00041 { 00042 setAcceptDrops( true ); 00043 setWordWrap( WidgetWidth ); 00044 setWrapPolicy( AtWhiteSpace ); 00045 setLinkUnderline( true ); 00046 00047 KXMLGUIClient* client = dynamic_cast<KXMLGUIClient*>(parent); 00048 KActionCollection* actions = client->actionCollection(); 00049 00050 // create the actions for the RMB menu 00051 KAction* undo = KStdAction::undo( this, SLOT(undo()), actions ); 00052 KAction* redo = KStdAction::redo( this, SLOT(redo()), actions ); 00053 undo->setEnabled( isUndoAvailable() ); 00054 redo->setEnabled( isRedoAvailable() ); 00055 00056 m_cut = KStdAction::cut( this, SLOT(cut()), actions ); 00057 m_copy = KStdAction::copy( this, SLOT(copy()), actions ); 00058 m_paste = KStdAction::paste( this, SLOT(paste()), actions ); 00059 00060 m_cut->setEnabled( false ); 00061 m_copy->setEnabled( false ); 00062 m_paste->setEnabled( true ); 00063 00064 connect( this, SIGNAL(undoAvailable(bool)), undo, SLOT(setEnabled(bool)) ); 00065 connect( this, SIGNAL(redoAvailable(bool)), redo, SLOT(setEnabled(bool)) ); 00066 00067 connect( this, SIGNAL(copyAvailable(bool)), m_cut, SLOT(setEnabled(bool)) ); 00068 connect( this, SIGNAL(copyAvailable(bool)), m_copy, SLOT(setEnabled(bool)) ); 00069 00070 new KAction( KStdGuiItem::clear(), 0, this, SLOT(clear()), actions, "edit_clear" ); 00071 KStdAction::selectAll( this, SLOT(selectAll()), actions ); 00072 00073 // create the actions modifying the text format 00074 m_textBold = new KToggleAction( QString::null, "text_bold", CTRL + Key_B, 0, 0, 00075 actions, "format_bold" ); 00076 m_textItalic = new KToggleAction( QString::null, "text_italic", CTRL + Key_I, 0, 0, 00077 actions, "format_italic" ); 00078 m_textUnderline = new KToggleAction( QString::null, "text_under", CTRL + Key_U, 0, 0, 00079 actions, "format_underline" ); 00080 m_textStrikeOut = new KToggleAction( QString::null, "text_strike", CTRL + Key_S, 0, 0, 00081 actions, "format_strikeout" ); 00082 00083 connect( m_textBold, SIGNAL(toggled(bool)), SLOT(setBold(bool)) ); 00084 connect( m_textItalic, SIGNAL(toggled(bool)), SLOT(setItalic(bool)) ); 00085 connect( m_textUnderline, SIGNAL(toggled(bool)), SLOT(setUnderline(bool)) ); 00086 connect( m_textStrikeOut, SIGNAL(toggled(bool)), SLOT(textStrikeOut(bool)) ); 00087 00088 m_textAlignLeft = new KToggleAction( QString::null, "text_left", CTRL + Key_L, 00089 this, SLOT(textAlignLeft()), 00090 actions, "format_alignleft" ); 00091 m_textAlignLeft->setChecked( true ); // just a dummy, will be updated later 00092 m_textAlignCenter = new KToggleAction( QString::null, "text_center", CTRL + ALT + Key_C, 00093 this, SLOT(textAlignCenter()), 00094 actions, "format_aligncenter" ); 00095 m_textAlignRight = new KToggleAction( QString::null, "text_right", CTRL + ALT + Key_R, 00096 this, SLOT(textAlignRight()), 00097 actions, "format_alignright" ); 00098 m_textAlignBlock = new KToggleAction( QString::null, "text_block", CTRL + Key_J, 00099 this, SLOT(textAlignBlock()), 00100 actions, "format_alignblock" ); 00101 00102 m_textAlignLeft->setExclusiveGroup( "align" ); 00103 m_textAlignCenter->setExclusiveGroup( "align" ); 00104 m_textAlignRight->setExclusiveGroup( "align" ); 00105 m_textAlignBlock->setExclusiveGroup( "align" ); 00106 00107 m_textList = new KToggleAction( QString::null, "enum_list", 0, 00108 this, SLOT(textList()), 00109 actions, "format_list" ); 00110 00111 m_textList->setExclusiveGroup( "style" ); 00112 00113 m_textSuper = new KToggleAction( QString::null, "text_super", 0, 00114 this, SLOT(textSuperScript()), 00115 actions, "format_super" ); 00116 m_textSub = new KToggleAction( QString::null, "text_sub", 0, 00117 this, SLOT(textSubScript()), 00118 actions, "format_sub" ); 00119 00120 m_textSuper->setExclusiveGroup( "valign" ); 00121 m_textSub->setExclusiveGroup( "valign" ); 00122 00123 // There is no easy possibility to implement text indenting with QTextEdit 00124 // 00125 // m_textIncreaseIndent = new KAction( i18n("Increase Indent"), "format_increaseindent", 0, 00126 // this, SLOT(textIncreaseIndent()), 00127 // actions, "format_increaseindent" ); 00128 // 00129 // m_textDecreaseIndent = new KAction( i18n("Decrease Indent"), "format_decreaseindent", 0, 00130 // this, SLOT(textDecreaseIndent()), 00131 // actions, "format_decreaseindent" ); 00132 00133 QPixmap pix( ICON_SIZE, ICON_SIZE ); 00134 pix.fill( black ); // just a dummy, gets updated before widget is shown 00135 m_textColor = new KAction( QString::null, pix, 0, this, 00136 SLOT(textColor()), actions, "format_color" ); 00137 00138 m_textFont = new KFontAction( QString::null, "text", KKey(), 00139 actions, "format_font" ); 00140 connect( m_textFont, SIGNAL(activated( const QString & )), 00141 this, SLOT(setFamily( const QString & )) ); 00142 00143 m_textSize = new KFontSizeAction( QString::null, KKey(), 00144 actions, "format_size" ); 00145 connect( m_textSize, SIGNAL(fontSizeChanged( int )), 00146 this, SLOT(setPointSize( int )) ); 00147 00148 // QTextEdit connections 00149 connect( this, SIGNAL(returnPressed()), SLOT(slotReturnPressed()) ); 00150 connect( this, SIGNAL(currentFontChanged( const QFont & )), 00151 this, SLOT(fontChanged( const QFont & )) ); 00152 connect( this, SIGNAL(currentColorChanged( const QColor & )), 00153 this, SLOT(colorChanged( const QColor & )) ); 00154 connect( this, SIGNAL(currentAlignmentChanged( int )), 00155 this, SLOT(alignmentChanged( int )) ); 00156 connect( this, SIGNAL(currentVerticalAlignmentChanged( VerticalAlignment )), 00157 this, SLOT(verticalAlignmentChanged( VerticalAlignment )) ); 00158 } 00159 00160 KNoteEdit::~KNoteEdit() 00161 { 00162 } 00163 00164 void KNoteEdit::setText( const QString& text ) 00165 { 00166 // to update the font and font size combo box - QTextEdit stopped 00167 // emitting the currentFontChanged signal with the new optimizations 00168 KTextEdit::setText( text ); 00169 fontChanged( currentFont() ); 00170 } 00171 00172 void KNoteEdit::setTextFont( const QFont& font ) 00173 { 00174 if ( textFormat() == PlainText ) 00175 setFont( font ); 00176 else 00177 setCurrentFont( font ); 00178 } 00179 00180 void KNoteEdit::setTextColor( const QColor& color ) 00181 { 00182 setColor( color ); 00183 colorChanged( color ); 00184 } 00185 00186 void KNoteEdit::setTabStop( int tabs ) 00187 { 00188 QFontMetrics fm( font() ); 00189 setTabStopWidth( fm.width( 'x' ) * tabs ); 00190 } 00191 00192 void KNoteEdit::setAutoIndentMode( bool newmode ) 00193 { 00194 m_autoIndentMode = newmode; 00195 } 00196 00197 00200 void KNoteEdit::setTextFormat( TextFormat f ) 00201 { 00202 if ( f == textFormat() ) 00203 return; 00204 00205 if ( f == RichText ) 00206 { 00207 QString t = text(); 00208 KTextEdit::setTextFormat( f ); 00209 setText( t ); 00210 00211 enableRichTextActions(); 00212 } 00213 else 00214 { 00215 KTextEdit::setTextFormat( f ); 00216 QString t = text(); 00217 setText( t ); 00218 00219 disableRichTextActions(); 00220 } 00221 } 00222 00223 void KNoteEdit::textStrikeOut( bool s ) 00224 { 00225 // QTextEdit does not support stroke out text (no saving, 00226 // no changing of more than one selected character) 00227 QFont font; 00228 00229 if ( !hasSelectedText() ) 00230 { 00231 font = currentFont(); 00232 font.setStrikeOut( s ); 00233 setCurrentFont( font ); 00234 } 00235 else 00236 { 00237 int pFrom, pTo, iFrom, iTo; 00238 int cp, ci; 00239 00240 getSelection( &pFrom, &iFrom, &pTo, &iTo ); 00241 getCursorPosition( &cp, &ci ); 00242 00243 for ( int p = pFrom; p <= pTo; p++ ) 00244 for ( int i = iFrom; i < iTo; i++ ) 00245 { 00246 setCursorPosition( p, i + 1 ); 00247 setSelection( p, i, p, i + 1 ); 00248 font = currentFont(); 00249 font.setStrikeOut( s ); 00250 setCurrentFont( font ); 00251 } 00252 00253 setSelection( pFrom, iFrom, pTo, iTo ); 00254 setCursorPosition( cp, ci ); 00255 } 00256 } 00257 00258 void KNoteEdit::textColor() 00259 { 00260 QColor c = color(); 00261 int ret = KColorDialog::getColor( c, this ); 00262 if ( ret == QDialog::Accepted ) 00263 setTextColor( c ); 00264 } 00265 00266 void KNoteEdit::textAlignLeft() 00267 { 00268 setAlignment( AlignLeft ); 00269 } 00270 00271 void KNoteEdit::textAlignCenter() 00272 { 00273 setAlignment( AlignCenter ); 00274 } 00275 00276 void KNoteEdit::textAlignRight() 00277 { 00278 setAlignment( AlignRight ); 00279 } 00280 00281 void KNoteEdit::textAlignBlock() 00282 { 00283 setAlignment( AlignJustify ); 00284 } 00285 00286 void KNoteEdit::textList() 00287 { 00288 if ( m_textList->isChecked() ) 00289 setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc ); 00290 else 00291 setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc ); 00292 } 00293 00294 void KNoteEdit::textSuperScript() 00295 { 00296 if ( m_textSuper->isChecked() ) 00297 setVerticalAlignment( AlignSuperScript ); 00298 else 00299 setVerticalAlignment( AlignNormal ); 00300 } 00301 00302 void KNoteEdit::textSubScript() 00303 { 00304 if ( m_textSub->isChecked() ) 00305 setVerticalAlignment( AlignSubScript ); 00306 else 00307 setVerticalAlignment( AlignNormal ); 00308 } 00309 00310 //void KNoteEdit::textIncreaseIndent() 00311 //{ 00312 //} 00313 00314 //void KNoteEdit::textDecreaseIndent() 00315 //{ 00316 //} 00317 00318 00321 void KNoteEdit::contentsDragEnterEvent( QDragEnterEvent* event ) 00322 { 00323 if ( KURLDrag::canDecode( event ) ) 00324 event->accept(); 00325 else 00326 KTextEdit::contentsDragEnterEvent( event ); 00327 } 00328 00329 void KNoteEdit::contentsDragMoveEvent( QDragMoveEvent* event ) 00330 { 00331 if ( KURLDrag::canDecode( event ) ) 00332 event->accept(); 00333 else 00334 KTextEdit::contentsDragMoveEvent( event ); 00335 } 00336 00337 void KNoteEdit::contentsDropEvent( QDropEvent* event ) 00338 { 00339 KURL::List list; 00340 00341 if ( KURLDrag::decode( event, list ) ) 00342 for ( KURL::List::Iterator it = list.begin(); it != list.end(); ++it ) 00343 { 00344 if ( it != list.begin() ) 00345 insert( ", " ); 00346 00347 insert( (*it).prettyURL() ); 00348 } 00349 else 00350 KTextEdit::contentsDropEvent( event ); 00351 } 00352 00355 void KNoteEdit::slotReturnPressed() 00356 { 00357 if ( m_autoIndentMode ) 00358 autoIndent(); 00359 } 00360 00361 void KNoteEdit::fontChanged( const QFont &f ) 00362 { 00363 m_textFont->setFont( f.family() ); 00364 m_textSize->setFontSize( f.pointSize() ); 00365 00366 m_textBold->setChecked( f.bold() ); 00367 m_textItalic->setChecked( f.italic() ); 00368 m_textUnderline->setChecked( f.underline() ); 00369 m_textStrikeOut->setChecked( f.strikeOut() ); 00370 } 00371 00372 void KNoteEdit::colorChanged( const QColor &c ) 00373 { 00374 QPixmap pix( ICON_SIZE, ICON_SIZE ); 00375 pix.fill( c ); 00376 m_textColor->setIconSet( pix ); 00377 } 00378 00379 void KNoteEdit::alignmentChanged( int a ) 00380 { 00381 // TODO: AlignAuto 00382 if ( ( a == AlignAuto ) || ( a & AlignLeft ) ) 00383 m_textAlignLeft->setChecked( true ); 00384 else if ( ( a & AlignHCenter ) ) 00385 m_textAlignCenter->setChecked( true ); 00386 else if ( ( a & AlignRight ) ) 00387 m_textAlignRight->setChecked( true ); 00388 else if ( ( a & AlignJustify ) ) 00389 m_textAlignBlock->setChecked( true ); 00390 } 00391 00392 void KNoteEdit::verticalAlignmentChanged( VerticalAlignment a ) 00393 { 00394 if ( a == AlignNormal ) 00395 { 00396 m_textSuper->setChecked( false ); 00397 m_textSub->setChecked( false ); 00398 } 00399 else if ( a == AlignSuperScript ) 00400 m_textSuper->setChecked( true ); 00401 else if ( a == AlignSubScript ) 00402 m_textSub->setChecked( true ); 00403 } 00404 00405 00408 void KNoteEdit::autoIndent() 00409 { 00410 int para, index; 00411 QString string; 00412 getCursorPosition( &para, &index ); 00413 while ( para > 0 && string.stripWhiteSpace().isEmpty() ) 00414 string = text( --para ); 00415 00416 if ( string.stripWhiteSpace().isEmpty() ) 00417 return; 00418 00419 // This routine returns the whitespace before the first non white space 00420 // character in string. 00421 // It is assumed that string contains at least one non whitespace character 00422 // ie \n \r \t \v \f and space 00423 QString indentString; 00424 00425 int len = string.length(); 00426 int i = 0; 00427 while ( i < len && string.at(i).isSpace() ) 00428 indentString += string.at( i++ ); 00429 00430 if ( !indentString.isEmpty() ) 00431 insert( indentString ); 00432 } 00433 00434 void KNoteEdit::emitLinkClicked( const QString &s ) 00435 { 00436 kdDebug(5500) << k_funcinfo << s << endl; 00437 } 00438 00439 void KNoteEdit::enableRichTextActions() 00440 { 00441 m_textColor->setEnabled( true ); 00442 00443 m_textBold->setEnabled( true ); 00444 m_textItalic->setEnabled( true ); 00445 m_textUnderline->setEnabled( true ); 00446 m_textStrikeOut->setEnabled( true ); 00447 00448 m_textAlignLeft->setEnabled( true ); 00449 m_textAlignCenter->setEnabled( true ); 00450 m_textAlignRight->setEnabled( true ); 00451 m_textAlignBlock->setEnabled( true ); 00452 00453 m_textList->setEnabled( true ); 00454 m_textSuper->setEnabled( true ); 00455 m_textSub->setEnabled( true ); 00456 00457 // m_textIncreaseIndent->setEnabled( true ); 00458 // m_textDecreaseIndent->setEnabled( true ); 00459 } 00460 00461 void KNoteEdit::disableRichTextActions() 00462 { 00463 m_textColor->setEnabled( false ); 00464 00465 m_textBold->setEnabled( false ); 00466 m_textItalic->setEnabled( false ); 00467 m_textUnderline->setEnabled( false ); 00468 m_textStrikeOut->setEnabled( false ); 00469 00470 m_textAlignLeft->setEnabled( false ); 00471 m_textAlignCenter->setEnabled( false ); 00472 m_textAlignRight->setEnabled( false ); 00473 m_textAlignBlock->setEnabled( false ); 00474 00475 m_textList->setEnabled( false ); 00476 m_textSuper->setEnabled( false ); 00477 m_textSub->setEnabled( false ); 00478 00479 // m_textIncreaseIndent->setEnabled( false ); 00480 // m_textDecreaseIndent->setEnabled( false ); 00481 } 00482 00483 #include "knoteedit.moc"
KDE Logo
This file is part of the documentation for knotes Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:57:45 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003