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