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( i18n("Bold"), "text_bold", CTRL + Key_B, 0, 0, 00075 actions, "format_bold" ); 00076 m_textItalic = new KToggleAction( i18n("Italic"), "text_italic", CTRL + Key_I, 0, 0, 00077 actions, "format_italic" ); 00078 m_textUnderline = new KToggleAction( i18n("Underline"), "text_under", CTRL + Key_U, 0, 0, 00079 actions, "format_underline" ); 00080 m_textStrikeOut = new KToggleAction( i18n("Strike Out"), "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( i18n("Align Left"), "text_left", ALT + 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( i18n("Align Center"), "text_center", ALT + Key_C, 00093 this, SLOT(textAlignCenter()), 00094 actions, "format_aligncenter" ); 00095 m_textAlignRight = new KToggleAction( i18n("Align Right"), "text_right", ALT + Key_R, 00096 this, SLOT(textAlignRight()), 00097 actions, "format_alignright" ); 00098 m_textAlignBlock = new KToggleAction( i18n("Align Block"), "text_block", ALT + Key_B, 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( i18n("List"), "enum_list", 0, 00108 this, SLOT(textList()), 00109 actions, "format_list" ); 00110 00111 m_textList->setExclusiveGroup( "style" ); 00112 00113 m_textSuper = new KToggleAction( i18n("Superscript"), "text_super", 0, 00114 this, SLOT(textSuperScript()), 00115 actions, "format_super" ); 00116 m_textSub = new KToggleAction( i18n("Subscript"), "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( i18n("Text Color..."), pix, 0, this, 00136 SLOT(textColor()), actions, "format_color" ); 00137 00138 m_textFont = new KFontAction( i18n("Text Font"), "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( i18n("Text Size"), 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 00210 // if the note contains html/xml source try to display it, otherwise 00211 // get the modified text again and set it to preserve newlines 00212 if ( QStyleSheet::mightBeRichText( t ) ) 00213 setText( t ); 00214 else 00215 setText( text() ); 00216 00217 enableRichTextActions(); 00218 } 00219 else 00220 { 00221 KTextEdit::setTextFormat( f ); 00222 QString t = text(); 00223 setText( t ); 00224 00225 disableRichTextActions(); 00226 } 00227 } 00228 00229 void KNoteEdit::textStrikeOut( bool s ) 00230 { 00231 // QTextEdit does not support stroke out text (no saving, 00232 // no changing of more than one selected character) 00233 QFont font; 00234 00235 if ( !hasSelectedText() ) 00236 { 00237 font = currentFont(); 00238 font.setStrikeOut( s ); 00239 setCurrentFont( font ); 00240 } 00241 else 00242 { 00243 int pFrom, pTo, iFrom, iTo; 00244 int cp, ci; 00245 00246 getSelection( &pFrom, &iFrom, &pTo, &iTo ); 00247 getCursorPosition( &cp, &ci ); 00248 00249 for ( int p = pFrom; p <= pTo; p++ ) 00250 for ( int i = iFrom; i < iTo; i++ ) 00251 { 00252 setCursorPosition( p, i + 1 ); 00253 setSelection( p, i, p, i + 1 ); 00254 font = currentFont(); 00255 font.setStrikeOut( s ); 00256 setCurrentFont( font ); 00257 } 00258 00259 setSelection( pFrom, iFrom, pTo, iTo ); 00260 setCursorPosition( cp, ci ); 00261 } 00262 } 00263 00264 void KNoteEdit::textColor() 00265 { 00266 QColor c = color(); 00267 int ret = KColorDialog::getColor( c, this ); 00268 if ( ret == QDialog::Accepted ) 00269 setTextColor( c ); 00270 } 00271 00272 void KNoteEdit::textAlignLeft() 00273 { 00274 setAlignment( AlignLeft ); 00275 m_textAlignLeft->setChecked( true ); 00276 } 00277 00278 void KNoteEdit::textAlignCenter() 00279 { 00280 setAlignment( AlignCenter ); 00281 m_textAlignCenter->setChecked( true ); 00282 } 00283 00284 void KNoteEdit::textAlignRight() 00285 { 00286 setAlignment( AlignRight ); 00287 m_textAlignRight->setChecked( true ); 00288 } 00289 00290 void KNoteEdit::textAlignBlock() 00291 { 00292 setAlignment( AlignJustify ); 00293 m_textAlignBlock->setChecked( true ); 00294 } 00295 00296 void KNoteEdit::textList() 00297 { 00298 if ( m_textList->isChecked() ) 00299 setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc ); 00300 else 00301 setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc ); 00302 } 00303 00304 void KNoteEdit::textSuperScript() 00305 { 00306 if ( m_textSuper->isChecked() ) 00307 setVerticalAlignment( AlignSuperScript ); 00308 else 00309 setVerticalAlignment( AlignNormal ); 00310 } 00311 00312 void KNoteEdit::textSubScript() 00313 { 00314 if ( m_textSub->isChecked() ) 00315 setVerticalAlignment( AlignSubScript ); 00316 else 00317 setVerticalAlignment( AlignNormal ); 00318 } 00319 00320 //void KNoteEdit::textIncreaseIndent() 00321 //{ 00322 //} 00323 00324 //void KNoteEdit::textDecreaseIndent() 00325 //{ 00326 //} 00327 00328 00331 void KNoteEdit::contentsDragEnterEvent( QDragEnterEvent *e ) 00332 { 00333 if ( KURLDrag::canDecode( e ) ) 00334 e->accept(); 00335 else 00336 KTextEdit::contentsDragEnterEvent( e ); 00337 } 00338 00339 void KNoteEdit::contentsDropEvent( QDropEvent *e ) 00340 { 00341 KURL::List list; 00342 00343 if ( KURLDrag::decode( e, list ) ) 00344 for ( KURL::List::Iterator it = list.begin(); it != list.end(); ++it ) 00345 { 00346 if ( it != list.begin() ) 00347 insert( ", " ); 00348 00349 insert( (*it).prettyURL() ); 00350 } 00351 else 00352 KTextEdit::contentsDropEvent( e ); 00353 } 00354 00357 void KNoteEdit::slotReturnPressed() 00358 { 00359 if ( m_autoIndentMode ) 00360 autoIndent(); 00361 } 00362 00363 void KNoteEdit::fontChanged( const QFont &f ) 00364 { 00365 m_textFont->setFont( f.family() ); 00366 m_textSize->setFontSize( f.pointSize() ); 00367 00368 m_textBold->setChecked( f.bold() ); 00369 m_textItalic->setChecked( f.italic() ); 00370 m_textUnderline->setChecked( f.underline() ); 00371 m_textStrikeOut->setChecked( f.strikeOut() ); 00372 } 00373 00374 void KNoteEdit::colorChanged( const QColor &c ) 00375 { 00376 QPixmap pix( ICON_SIZE, ICON_SIZE ); 00377 pix.fill( c ); 00378 m_textColor->setIconSet( pix ); 00379 } 00380 00381 void KNoteEdit::alignmentChanged( int a ) 00382 { 00383 // TODO: AlignAuto 00384 if ( ( a == AlignAuto ) || ( a & AlignLeft ) ) 00385 m_textAlignLeft->setChecked( true ); 00386 else if ( ( a & AlignHCenter ) ) 00387 m_textAlignCenter->setChecked( true ); 00388 else if ( ( a & AlignRight ) ) 00389 m_textAlignRight->setChecked( true ); 00390 else if ( ( a & AlignJustify ) ) 00391 m_textAlignBlock->setChecked( true ); 00392 } 00393 00394 void KNoteEdit::verticalAlignmentChanged( VerticalAlignment a ) 00395 { 00396 if ( a == AlignNormal ) 00397 { 00398 m_textSuper->setChecked( false ); 00399 m_textSub->setChecked( false ); 00400 } 00401 else if ( a == AlignSuperScript ) 00402 m_textSuper->setChecked( true ); 00403 else if ( a == AlignSubScript ) 00404 m_textSub->setChecked( true ); 00405 } 00406 00407 00410 void KNoteEdit::autoIndent() 00411 { 00412 int para, index; 00413 QString string; 00414 getCursorPosition( &para, &index ); 00415 while ( para > 0 && string.stripWhiteSpace().isEmpty() ) 00416 string = text( --para ); 00417 00418 if ( string.stripWhiteSpace().isEmpty() ) 00419 return; 00420 00421 // This routine returns the whitespace before the first non white space 00422 // character in string. 00423 // It is assumed that string contains at least one non whitespace character 00424 // ie \n \r \t \v \f and space 00425 QString indentString; 00426 00427 int len = string.length(); 00428 int i = 0; 00429 while ( i < len && string.at(i).isSpace() ) 00430 indentString += string.at( i++ ); 00431 00432 if ( !indentString.isEmpty() ) 00433 insert( indentString ); 00434 } 00435 00436 void KNoteEdit::emitLinkClicked( const QString &s ) 00437 { 00438 kdDebug(5500) << k_funcinfo << s << endl; 00439 } 00440 00441 void KNoteEdit::enableRichTextActions() 00442 { 00443 m_textColor->setEnabled( true ); 00444 00445 m_textBold->setEnabled( true ); 00446 m_textItalic->setEnabled( true ); 00447 m_textUnderline->setEnabled( true ); 00448 m_textStrikeOut->setEnabled( true ); 00449 00450 m_textAlignLeft->setEnabled( true ); 00451 m_textAlignCenter->setEnabled( true ); 00452 m_textAlignRight->setEnabled( true ); 00453 m_textAlignBlock->setEnabled( true ); 00454 00455 m_textList->setEnabled( true ); 00456 m_textSuper->setEnabled( true ); 00457 m_textSub->setEnabled( true ); 00458 00459 // m_textIncreaseIndent->setEnabled( true ); 00460 // m_textDecreaseIndent->setEnabled( true ); 00461 } 00462 00463 void KNoteEdit::disableRichTextActions() 00464 { 00465 m_textColor->setEnabled( false ); 00466 00467 m_textBold->setEnabled( false ); 00468 m_textItalic->setEnabled( false ); 00469 m_textUnderline->setEnabled( false ); 00470 m_textStrikeOut->setEnabled( false ); 00471 00472 m_textAlignLeft->setEnabled( false ); 00473 m_textAlignCenter->setEnabled( false ); 00474 m_textAlignRight->setEnabled( false ); 00475 m_textAlignBlock->setEnabled( false ); 00476 00477 m_textList->setEnabled( false ); 00478 m_textSuper->setEnabled( false ); 00479 m_textSub->setEnabled( false ); 00480 00481 // m_textIncreaseIndent->setEnabled( false ); 00482 // m_textDecreaseIndent->setEnabled( false ); 00483 } 00484 00485 #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 Aug 27 12:50:14 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003