knotes Library API Documentation

knoteedit.cpp

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 1997-2003, 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 <qfile.h>
00023 #include <qlayout.h>
00024 #include <qbuttongroup.h>
00025 
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <kaction.h>
00029 #include <kurldrag.h>
00030 #include <kstdaction.h>
00031 #include <kcolordialog.h>
00032 #include <kxmlguiclient.h>
00033 
00034 #include <assert.h>
00035 
00036 #include "knoteedit.h"
00037 #include "knotebutton.h"
00038 
00039 static const short SEP = 5;
00040 static const short ICON_SIZE = 10;
00041 
00042 
00043 KNoteEdit::KNoteEdit( QWidget *tool, QWidget* parent, const char* name )
00044     : KTextEdit( parent, name )
00045 {
00046     setAcceptDrops( true );
00047     setWordWrap( WidgetWidth );
00048     setWrapPolicy( AtWhiteSpace );
00049 
00050     KXMLGUIClient* client = dynamic_cast<KXMLGUIClient*>(parent);
00051     assert(client);
00052     KActionCollection* actions = client->actionCollection();
00053 
00054 
00055     // create the actions for the RMB menu
00056     KAction* undo = KStdAction::undo( this, SLOT(undo()), actions );
00057     KAction* redo = KStdAction::redo( this, SLOT(redo()), actions );
00058     undo->setEnabled( isUndoAvailable() );
00059     redo->setEnabled( isRedoAvailable() );
00060 
00061     m_cut = KStdAction::cut( this, SLOT(cut()), actions );
00062     m_copy = KStdAction::copy( this, SLOT(copy()), actions );
00063     m_paste = KStdAction::paste( this, SLOT(paste()), actions );
00064 
00065     m_cut->setEnabled( false );
00066     m_copy->setEnabled( false );
00067     m_paste->setEnabled( true );
00068 
00069     connect( this, SIGNAL(undoAvailable(bool)), undo, SLOT(setEnabled(bool)) );
00070     connect( this, SIGNAL(redoAvailable(bool)), redo, SLOT(setEnabled(bool)) );
00071 
00072     connect( this, SIGNAL(copyAvailable(bool)), m_cut, SLOT(setEnabled(bool)) );
00073     connect( this, SIGNAL(copyAvailable(bool)), m_copy, SLOT(setEnabled(bool)) );
00074 
00075     new KAction( i18n("Clear"), "editclear", 0, this, SLOT(clear()), actions, "edit_clear" );
00076     KStdAction::selectAll( this, SLOT(selectAll()), actions );
00077 
00078     // create the tool buttons (can't use actions yet :-( )
00079     QBoxLayout *layout = new QBoxLayout( tool, QBoxLayout::LeftToRight );
00080 
00081     m_textBold = new KNoteButton( "text_bold", tool );
00082     m_textBold->setToggleButton( true );
00083     connect( m_textBold, SIGNAL(clicked()), this, SLOT(slotSetBold()) );
00084     layout->addWidget( m_textBold );
00085 
00086     m_textItalic = new KNoteButton( "text_italic", tool );
00087     m_textItalic->setToggleButton( true );
00088     connect( m_textItalic, SIGNAL(clicked()), this, SLOT(slotSetItalic()) );
00089     layout->addWidget( m_textItalic );
00090 
00091     m_textUnderline = new KNoteButton( "text_under", tool );
00092     m_textUnderline->setToggleButton( true );
00093     connect( m_textUnderline, SIGNAL(clicked()), this, SLOT(slotSetUnderline()) );
00094     layout->addWidget( m_textUnderline );
00095 
00096     layout->addSpacing( SEP );
00097 
00098     m_textAlignLeft = new KNoteButton( "text_left", tool );
00099     m_textAlignLeft->setToggleButton( true );
00100     connect( m_textAlignLeft, SIGNAL(clicked()), this, SLOT(textAlignLeft()) );
00101     layout->addWidget( m_textAlignLeft );
00102 
00103     m_textAlignCenter = new KNoteButton( "text_center", tool );
00104     m_textAlignCenter->setToggleButton( true );
00105     connect( m_textAlignCenter, SIGNAL(clicked()), this, SLOT(textAlignCenter()) );
00106     layout->addWidget( m_textAlignCenter );
00107 
00108     m_textAlignRight = new KNoteButton( "text_right", tool );
00109     m_textAlignRight->setToggleButton( true );
00110     connect( m_textAlignRight, SIGNAL(clicked()), this, SLOT(textAlignRight()) );
00111     layout->addWidget( m_textAlignRight );
00112 
00113     m_textAlignBlock = new KNoteButton( "text_block", tool );
00114     m_textAlignBlock->setToggleButton( true );
00115     connect( m_textAlignBlock, SIGNAL(clicked()), this, SLOT(textAlignBlock()) );
00116     layout->addWidget( m_textAlignBlock );
00117 
00118     QButtonGroup *align = new QButtonGroup( this );
00119     align->setExclusive( true );
00120     align->hide();
00121     align->insert( m_textAlignLeft );
00122     align->insert( m_textAlignCenter );
00123     align->insert( m_textAlignRight );
00124     align->insert( m_textAlignBlock );
00125 
00126     m_textAlignLeft->setOn( true );  // ???? TODO: really always true?
00127 
00128     layout->addSpacing( SEP );
00129 
00130     m_textList = new KNoteButton( "enum_list", tool );
00131     m_textList->setToggleButton( true );
00132     connect( m_textList, SIGNAL(clicked()), this, SLOT(textList()) );
00133     layout->addWidget( m_textList );
00134 
00135     layout->addSpacing( SEP );
00136 
00137     m_textSuper = new KNoteButton( "text_super", tool );
00138     m_textSuper->setToggleButton( true );
00139     connect( m_textSuper, SIGNAL(clicked()), this, SLOT(textSuperScript()) );
00140     layout->addWidget( m_textSuper );
00141 
00142     m_textSub = new KNoteButton( "text_sub", tool );
00143     m_textSub->setToggleButton( true );
00144     connect( m_textSub, SIGNAL(clicked()), this, SLOT(textSubScript()) );
00145     layout->addWidget( m_textSub );
00146 
00147     layout->addSpacing( SEP );
00148 
00149     m_textIncreaseIndent = new KNoteButton( "format_increaseindent", tool );
00150     connect( m_textIncreaseIndent, SIGNAL(clicked()), this, SLOT(textIncreaseIndent()) );
00151     layout->addWidget( m_textIncreaseIndent );
00152 
00153     m_textDecreaseIndent = new KNoteButton( "format_decreaseindent", tool );
00154     connect( m_textDecreaseIndent, SIGNAL(clicked()), this, SLOT(textDecreaseIndent()) );
00155     layout->addWidget( m_textDecreaseIndent );
00156 
00157     layout->addSpacing( SEP );
00158 
00159     QPixmap pix( ICON_SIZE, ICON_SIZE );
00160     pix.fill( black );                  // ??? TODO: really always black?
00161     m_textColor = new KNoteButton( QString::null, tool );
00162     m_textColor->setIconSet( pix );
00163     connect( m_textColor, SIGNAL(clicked()), this, SLOT(textColor()) );
00164     layout->addWidget( m_textColor );
00165 
00166     layout->addStretch( 1 );
00167 
00168     connect( this, SIGNAL(returnPressed()), SLOT(slotReturnPressed()) );
00169     connect( this, SIGNAL(currentFontChanged( const QFont & )),
00170              this, SLOT(fontChanged( const QFont & )) );
00171     connect( this, SIGNAL(currentColorChanged( const QColor & )),
00172              this, SLOT(colorChanged( const QColor & )) );
00173     connect( this, SIGNAL(currentAlignmentChanged( int )),
00174              this, SLOT(alignmentChanged( int )) );
00175     connect( this, SIGNAL(currentVerticalAlignmentChanged( VerticalAlignment )),
00176              this, SLOT(verticalAlignmentChanged( VerticalAlignment )) );
00177 }
00178 
00179 KNoteEdit::~KNoteEdit()
00180 {
00181 }
00182 
00183 void KNoteEdit::setTextFont( const QFont& font )
00184 {
00185     if ( textFormat() == PlainText )
00186         setFont( font );
00187     else
00188         setCurrentFont( font );
00189 }
00190 
00191 void KNoteEdit::setTextColor( const QColor& color )
00192 {
00193     setColor( color );
00194     colorChanged( color );
00195 }
00196 
00197 void KNoteEdit::setTabStop( int tabs )
00198 {
00199     QFontMetrics fm( font() );
00200     setTabStopWidth( fm.width( 'x' ) * tabs );
00201 }
00202 
00203 void KNoteEdit::setAutoIndentMode( bool newmode )
00204 {
00205     m_autoIndentMode = newmode;
00206 }
00207 
00208 
00211 void KNoteEdit::setTextFormat( TextFormat f )
00212 {
00213     if ( f == RichText )
00214         enableRichTextActions();
00215     else
00216         disableRichTextActions();
00217 
00218     KTextEdit::setTextFormat( f );
00219 }
00220 
00221 void KNoteEdit::textColor()
00222 {
00223     QColor c = color();
00224     int ret = KColorDialog::getColor( c, this );
00225     if ( ret == QDialog::Accepted )
00226         setTextColor( c );
00227 }
00228 
00229 void KNoteEdit::textAlignLeft()
00230 {
00231     setAlignment( AlignLeft );
00232 }
00233 
00234 void KNoteEdit::textAlignCenter()
00235 {
00236     setAlignment( AlignCenter );
00237 }
00238 
00239 void KNoteEdit::textAlignRight()
00240 {
00241     setAlignment( AlignRight );
00242 }
00243 
00244 void KNoteEdit::textAlignBlock()
00245 {
00246     setAlignment( AlignJustify );
00247 }
00248 
00249 void KNoteEdit::textList()
00250 {
00251     if ( m_textList->isOn() )
00252         setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
00253     else
00254         setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
00255 }
00256 
00257 void KNoteEdit::textSuperScript()
00258 {
00259     if ( m_textSuper->isOn() )
00260     {
00261         m_textSub->setOn( false );
00262         setVerticalAlignment( AlignSuperScript );
00263     }
00264     else
00265         setVerticalAlignment( AlignNormal );
00266 }
00267 
00268 void KNoteEdit::textSubScript()
00269 {
00270     if ( m_textSub->isOn() )
00271     {
00272         m_textSuper->setOn( false );
00273         setVerticalAlignment( AlignSubScript );
00274     }
00275     else
00276         setVerticalAlignment( AlignNormal );
00277 }
00278 
00279 void KNoteEdit::textIncreaseIndent()
00280 {
00281 }
00282 
00283 void KNoteEdit::textDecreaseIndent()
00284 {
00285 }
00286 
00287 
00290 void KNoteEdit::contentsDragEnterEvent( QDragEnterEvent* event )
00291 {
00292     if ( KURLDrag::canDecode( event ) )
00293         event->accept();
00294     else
00295         KTextEdit::contentsDragEnterEvent( event );
00296 }
00297 
00298 void KNoteEdit::contentsDragMoveEvent( QDragMoveEvent* event )
00299 {
00300     if ( KURLDrag::canDecode( event ) )
00301         event->accept();
00302     else
00303         KTextEdit::contentsDragMoveEvent( event );
00304 }
00305 
00306 void KNoteEdit::contentsDropEvent( QDropEvent* event )
00307 {
00308     KURL::List list;
00309 
00310     if ( KURLDrag::decode( event, list ) )
00311         for ( KURL::List::Iterator it = list.begin(); it != list.end(); ++it )
00312         {
00313             if ( it != list.begin() )
00314                 insert( ", " );
00315 
00316             insert( (*it).prettyURL() );
00317         }
00318     else
00319         KTextEdit::contentsDropEvent( event );
00320 }
00321 
00324 void KNoteEdit::slotReturnPressed()
00325 {
00326     if ( m_autoIndentMode )
00327         autoIndent();
00328 }
00329 
00330 void KNoteEdit::slotSetBold()
00331 {
00332     setBold( m_textBold->isOn() );
00333 }
00334 
00335 void KNoteEdit::slotSetItalic()
00336 {
00337     setItalic( m_textItalic->isOn() );
00338 }
00339 
00340 void KNoteEdit::slotSetUnderline()
00341 {
00342     setUnderline( m_textUnderline->isOn() );
00343 }
00344 
00345 void KNoteEdit::fontChanged( const QFont &f )
00346 {
00347 //TODO
00348 //    m_comboFont->lineEdit()->setText( f.family() );
00349 //    m_comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
00350 
00351     m_textBold->setOn( f.bold() );
00352     m_textItalic->setOn( f.italic() );
00353     m_textUnderline->setOn( f.underline() );
00354 }
00355 
00356 void KNoteEdit::colorChanged( const QColor &c )
00357 {
00358     QPixmap pix( ICON_SIZE, ICON_SIZE );
00359     pix.fill( c );
00360     m_textColor->setIconSet( pix );
00361 }
00362 
00363 void KNoteEdit::alignmentChanged( int a )
00364 {
00365     // TODO: AlignAuto
00366     if ( ( a == AlignAuto ) || ( a & AlignLeft ) )
00367         m_textAlignLeft->setOn( true );
00368     else if ( ( a & AlignHCenter ) )
00369         m_textAlignCenter->setOn( true );
00370     else if ( ( a & AlignRight ) )
00371         m_textAlignRight->setOn( true );
00372     else if ( ( a & AlignJustify ) )
00373         m_textAlignBlock->setOn( true );
00374 }
00375 
00376 void KNoteEdit::verticalAlignmentChanged( VerticalAlignment a )
00377 {
00378     if ( a == AlignNormal )
00379     {
00380         m_textSuper->setOn( false );
00381         m_textSub->setOn( false );
00382     }
00383     else if ( a == AlignSuperScript )
00384         m_textSuper->setOn( true );
00385     else if ( a == AlignSubScript )
00386         m_textSub->setOn( true );
00387 }
00388 
00389 
00392 void KNoteEdit::autoIndent()
00393 {
00394     int para, index;
00395     QString string;
00396     getCursorPosition( &para, &index );
00397     while ( para > 0 && string.stripWhiteSpace().isEmpty() )
00398         string = text( --para );
00399 
00400     if ( string.stripWhiteSpace().isEmpty() )
00401         return;
00402 
00403     // This routine returns the whitespace before the first non white space
00404     // character in string.
00405     // It is assumed that string contains at least one non whitespace character
00406     // ie \n \r \t \v \f and space
00407     QString indentString;
00408 
00409     int len = string.length();
00410     int i = 0;
00411     while ( i < len && string.at(i).isSpace() )
00412         indentString += string.at( i++ );
00413 
00414     if ( !indentString.isEmpty() )
00415         insert( indentString );
00416 }
00417 
00418 void KNoteEdit::emitLinkClicked( const QString &s )
00419 {
00420     kdDebug() << k_funcinfo << s << endl;
00421 }
00422 
00423 void KNoteEdit::enableRichTextActions()
00424 {
00425     m_textColor->setEnabled( true );
00426 
00427     m_textBold->setEnabled( true );
00428     m_textItalic->setEnabled( true );
00429     m_textUnderline->setEnabled( true );
00430 
00431     m_textAlignLeft->setEnabled( true );
00432     m_textAlignCenter->setEnabled( true );
00433     m_textAlignRight->setEnabled( true );
00434     m_textAlignBlock->setEnabled( true );
00435 
00436     m_textList->setEnabled( true );
00437     m_textSuper->setEnabled( true );
00438     m_textSub->setEnabled( true );
00439 
00440     m_textIncreaseIndent->setEnabled( true );
00441     m_textDecreaseIndent->setEnabled( true );
00442 }
00443 
00444 void KNoteEdit::disableRichTextActions()
00445 {
00446     m_textColor->setEnabled( false );
00447 
00448     m_textBold->setEnabled( false );
00449     m_textItalic->setEnabled( false );
00450     m_textUnderline->setEnabled( false );
00451 
00452     m_textAlignLeft->setEnabled( false );
00453     m_textAlignCenter->setEnabled( false );
00454     m_textAlignRight->setEnabled( false );
00455     m_textAlignBlock->setEnabled( false );
00456 
00457     m_textList->setEnabled( false );
00458     m_textSuper->setEnabled( false );
00459     m_textSub->setEnabled( false );
00460 
00461     m_textIncreaseIndent->setEnabled( false );
00462     m_textDecreaseIndent->setEnabled( false );
00463 }
00464 
00465 #include "knoteedit.moc"
KDE Logo
This file is part of the documentation for knotes Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Mar 6 17:18:05 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003