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