00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qdragobject.h>
00022 #include <qfile.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 #include <kpopupmenu.h>
00032
00033 #include "knoteedit.h"
00034
00035
00036 KNoteEdit::KNoteEdit( QWidget* parent, const char* name )
00037 : KTextEdit( parent, name )
00038 {
00039 setAcceptDrops( true );
00040 setWordWrap( WidgetWidth );
00041 setWrapPolicy( AtWhiteSpace );
00042
00043 KXMLGUIClient* client = dynamic_cast<KXMLGUIClient*>(parent);
00044 KActionCollection* actions = client->actionCollection();
00045
00046
00047
00048 KAction* undo = KStdAction::undo( this, SLOT(undo()), actions );
00049 KAction* redo = KStdAction::redo( this, SLOT(redo()), actions );
00050 undo->setEnabled( isUndoAvailable() );
00051 redo->setEnabled( isRedoAvailable() );
00052
00053 m_cut = KStdAction::cut( this, SLOT(cut()), actions );
00054 m_copy = KStdAction::copy( this, SLOT(copy()), actions );
00055 m_paste = KStdAction::paste( this, SLOT(paste()), actions );
00056
00057 m_cut->setEnabled( false );
00058 m_copy->setEnabled( false );
00059 m_paste->setEnabled( true );
00060
00061 connect( this, SIGNAL(undoAvailable(bool)), undo, SLOT(setEnabled(bool)) );
00062 connect( this, SIGNAL(redoAvailable(bool)), redo, SLOT(setEnabled(bool)) );
00063
00064 connect( this, SIGNAL(copyAvailable(bool)), m_cut, SLOT(setEnabled(bool)) );
00065 connect( this, SIGNAL(copyAvailable(bool)), m_copy, SLOT(setEnabled(bool)) );
00066
00067 new KAction( i18n("Clear"), "editclear", 0, this, SLOT(clear()), actions, "edit_clear" );
00068 KStdAction::selectAll( this, SLOT(selectAll()), actions );
00069
00070
00071
00072 m_textBold = new KToggleAction( i18n( "&Bold" ), "text_bold", CTRL + Key_B,
00073 actions, "format_bold" );
00074 m_textItalic = new KToggleAction( i18n( "&Italic" ), "text_italic", CTRL + Key_I,
00075 actions, "format_italic" );
00076 m_textUnderline = new KToggleAction( i18n( "&Underline" ), "text_under", CTRL + Key_U,
00077 actions, "format_underline" );
00078
00079 connect( m_textBold, SIGNAL(toggled(bool)), this, SLOT(setBold(bool)) );
00080 connect( m_textItalic, SIGNAL(toggled(bool)), this, SLOT(setItalic(bool)) );
00081 connect( m_textUnderline, SIGNAL(toggled(bool)), this, SLOT(setUnderline(bool)) );
00082
00083 m_textAlignLeft = new KToggleAction( i18n( "Align &Left" ), "text_left", CTRL + Key_L,
00084 this, SLOT( textAlignLeft() ),
00085 actions, "format_alignleft" );
00086 m_textAlignLeft->setChecked( true );
00087 m_textAlignCenter = new KToggleAction( i18n( "Align &Center" ), "text_center", CTRL + ALT + Key_C,
00088 this, SLOT( textAlignCenter() ),
00089 actions, "format_aligncenter" );
00090 m_textAlignRight = new KToggleAction( i18n( "Align &Right" ), "text_right", CTRL + ALT + Key_R,
00091 this, SLOT( textAlignRight() ),
00092 actions, "format_alignright" );
00093 m_textAlignBlock = new KToggleAction( i18n( "Align &Block" ), "text_block", CTRL + Key_J,
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
00103 m_textList = new KToggleAction( i18n( "List" ), "enumList", 0,
00104 this, SLOT( textList() ),
00105 actions, "format_list" );
00106
00107 m_textList->setExclusiveGroup( "style" );
00108
00109 m_textSuper = new KToggleAction( i18n( "Superscript" ), "super", 0,
00110 this, SLOT( textSuperScript() ),
00111 actions, "format_super" );
00112 m_textSub = new KToggleAction( i18n( "Subscript" ), "sub", 0,
00113 this, SLOT( textSubScript() ),
00114 actions, "format_sub" );
00115
00116 m_textSuper->setExclusiveGroup( "valign" );
00117 m_textSub->setExclusiveGroup( "valign" );
00118
00119 m_textIncreaseIndent = new KAction( i18n( "Increase Indent" ), "format_increaseindent", 0,
00120 this, SLOT(textIncreaseIndent()),
00121 actions, "format_increaseindent" );
00122
00123 m_textDecreaseIndent = new KAction( i18n( "Decrease Indent" ), "format_decreaseindent", 0,
00124 this, SLOT(textDecreaseIndent()),
00125 actions, "format_decreaseindent" );
00126
00127 QPixmap pix( 16, 16 );
00128 pix.fill( black );
00129 m_textColor = new KAction( i18n( "Text Color..." ), pix, 0, this,
00130 SLOT(textColor()), actions, "format_color" );
00131
00132
00133 connect( this, SIGNAL(returnPressed()), SLOT(slotReturnPressed()) );
00134 connect( this, SIGNAL(currentFontChanged( const QFont & )),
00135 this, SLOT(fontChanged( const QFont & )) );
00136 connect( this, SIGNAL(currentColorChanged( const QColor & )),
00137 this, SLOT(colorChanged( const QColor & )) );
00138 connect( this, SIGNAL(currentAlignmentChanged( int )),
00139 this, SLOT(alignmentChanged( int )) );
00140 connect( this, SIGNAL(currentVerticalAlignmentChanged( VerticalAlignment )),
00141 this, SLOT(verticalAlignmentChanged( VerticalAlignment )) );
00142 }
00143
00144 KNoteEdit::~KNoteEdit()
00145 {
00146 }
00147
00148 void KNoteEdit::readFile( const QString& filename )
00149 {
00150 QFile infile( filename );
00151 if( infile.open( IO_ReadOnly ) )
00152 {
00153 QTextStream input( &infile );
00154 input.setEncoding(QTextStream::UnicodeUTF8);
00155 setText( input.read() );
00156 infile.close();
00157 } else
00158 kdDebug(5500) << "could not open input file" << endl;
00159
00160 setModified( false );
00161 }
00162
00163 void KNoteEdit::dumpToFile( const QString& filename ) const
00164 {
00165 QFile outfile( filename );
00166 if( outfile.open( IO_WriteOnly ) )
00167 {
00168 QTextStream output( &outfile );
00169 output.setEncoding(QTextStream::UnicodeUTF8);
00170 output << text();
00171 outfile.close();
00172 } else
00173 kdDebug(5500) << "could not open file to write to" << endl;
00174 }
00175
00176 void KNoteEdit::setTextFont( const QFont& font )
00177 {
00178 if ( textFormat() == PlainText )
00179 setFont( font );
00180 else
00181 setCurrentFont( font );
00182 }
00183
00184 void KNoteEdit::setTextColor( const QColor& color )
00185 {
00186 setColor( color );
00187
00188 QPixmap pix( 16, 16 );
00189 pix.fill( color );
00190 m_textColor->setIconSet( pix );
00191 }
00192
00193 void KNoteEdit::setTabStop( int tabs )
00194 {
00195 QFontMetrics fm( font() );
00196 setTabStopWidth( fm.width( 'x' ) * tabs );
00197 }
00198
00199 void KNoteEdit::setAutoIndentMode( bool newmode )
00200 {
00201 m_autoIndentMode = newmode;
00202 }
00203
00204
00207 void KNoteEdit::setTextFormat( TextFormat f )
00208 {
00209 if ( f == RichText )
00210 enableRichTextActions();
00211 else
00212 disableRichTextActions();
00213
00214 KTextEdit::setTextFormat( f );
00215 }
00216
00217 void KNoteEdit::textColor()
00218 {
00219 QColor c = color();
00220 int ret = KColorDialog::getColor( c, this );
00221 if ( ret == QDialog::Accepted )
00222 setTextColor( c );
00223 }
00224
00225 void KNoteEdit::textAlignLeft()
00226 {
00227 setAlignment( AlignLeft );
00228 }
00229
00230 void KNoteEdit::textAlignCenter()
00231 {
00232 setAlignment( AlignCenter );
00233 }
00234
00235 void KNoteEdit::textAlignRight()
00236 {
00237 setAlignment( AlignRight );
00238 }
00239
00240 void KNoteEdit::textAlignBlock()
00241 {
00242 setAlignment( AlignJustify );
00243 }
00244
00245 void KNoteEdit::textList()
00246 {
00247 if ( m_textList->isChecked() )
00248 setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
00249 else
00250 setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
00251 }
00252
00253 void KNoteEdit::textSuperScript()
00254 {
00255 if ( m_textSuper->isChecked() )
00256 setVerticalAlignment( AlignSuperScript );
00257 else
00258 setVerticalAlignment( AlignNormal );
00259 }
00260
00261 void KNoteEdit::textSubScript()
00262 {
00263 if ( m_textSub->isChecked() )
00264 setVerticalAlignment( AlignSubScript );
00265 else
00266 setVerticalAlignment( AlignNormal );
00267 }
00268
00269 void KNoteEdit::textIncreaseIndent()
00270 {
00271 }
00272
00273 void KNoteEdit::textDecreaseIndent()
00274 {
00275 }
00276
00277
00280 void KNoteEdit::contentsDragEnterEvent( QDragEnterEvent* event )
00281 {
00282 if ( KURLDrag::canDecode( event ) )
00283 event->accept();
00284 else
00285 KTextEdit::contentsDragEnterEvent( event );
00286 }
00287
00288 void KNoteEdit::contentsDragMoveEvent( QDragMoveEvent* event )
00289 {
00290 if ( KURLDrag::canDecode( event ) )
00291 event->accept();
00292 else
00293 KTextEdit::contentsDragMoveEvent( event );
00294 }
00295
00296 void KNoteEdit::contentsDropEvent( QDropEvent* event )
00297 {
00298 KURL::List list;
00299
00300 if ( KURLDrag::decode( event, list ) )
00301 {
00302 QString text;
00303 for ( KURL::List::Iterator it = list.begin(); it != list.end(); ++it )
00304 text += (*it).prettyURL() + ", ";
00305
00306 text.remove( text.length() - 2, 2 );
00307 insert( text );
00308 }
00309 else
00310 KTextEdit::contentsDropEvent( event );
00311 }
00312
00315 void KNoteEdit::slotReturnPressed()
00316 {
00317 if ( m_autoIndentMode )
00318 autoIndent();
00319 }
00320
00321 void KNoteEdit::fontChanged( const QFont &f )
00322 {
00323
00324
00325
00326 m_textBold->setChecked( f.bold() );
00327 m_textItalic->setChecked( f.italic() );
00328 m_textUnderline->setChecked( f.underline() );
00329 }
00330
00331 void KNoteEdit::colorChanged( const QColor &c )
00332 {
00333 QPixmap pix( 16, 16 );
00334 pix.fill( c );
00335 m_textColor->setIconSet( pix );
00336 }
00337
00338 void KNoteEdit::alignmentChanged( int a )
00339 {
00340
00341 if ( ( a == AlignAuto ) || ( a & AlignLeft ) )
00342 m_textAlignLeft->setChecked( true );
00343 else if ( ( a & AlignHCenter ) )
00344 m_textAlignCenter->setChecked( true );
00345 else if ( ( a & AlignRight ) )
00346 m_textAlignRight->setChecked( true );
00347 else if ( ( a & AlignJustify ) )
00348 m_textAlignBlock->setChecked( true );
00349 }
00350
00351 void KNoteEdit::verticalAlignmentChanged( VerticalAlignment a )
00352 {
00353 if ( a == AlignNormal )
00354 {
00355 m_textSuper->setChecked( false );
00356 m_textSub->setChecked( false );
00357 }
00358 else if ( a == AlignSuperScript )
00359 m_textSuper->setChecked( true );
00360 else if ( a == AlignSubScript )
00361 m_textSub->setChecked( true );
00362 }
00363
00364
00367 void KNoteEdit::autoIndent()
00368 {
00369 int para, index;
00370 QString string;
00371 getCursorPosition( ¶, &index );
00372 while ( para > 0 && string.stripWhiteSpace().isEmpty() )
00373 string = text( --para );
00374
00375 if ( string.stripWhiteSpace().isEmpty() )
00376 return;
00377
00378
00379
00380
00381
00382 QString indentString;
00383
00384 int len = string.length();
00385 int i = 0;
00386 while ( i < len && string.at(i).isSpace() )
00387 indentString += string.at( i++ );
00388
00389 if ( !indentString.isEmpty() )
00390 insert( indentString );
00391 }
00392
00393 void KNoteEdit::enableRichTextActions()
00394 {
00395 m_textColor->setEnabled( true );
00396
00397 m_textBold->setEnabled( true );
00398 m_textItalic->setEnabled( true );
00399 m_textUnderline->setEnabled( true );
00400
00401 m_textAlignLeft->setEnabled( true );
00402 m_textAlignCenter->setEnabled( true );
00403 m_textAlignRight->setEnabled( true );
00404 m_textAlignBlock->setEnabled( true );
00405
00406 m_textList->setEnabled( true );
00407 m_textSuper->setEnabled( true );
00408 m_textSub->setEnabled( true );
00409
00410 m_textIncreaseIndent->setEnabled( true );
00411 m_textDecreaseIndent->setEnabled( true );
00412 }
00413
00414 void KNoteEdit::disableRichTextActions()
00415 {
00416 m_textColor->setEnabled( false );
00417
00418 m_textBold->setEnabled( false );
00419 m_textItalic->setEnabled( false );
00420 m_textUnderline->setEnabled( false );
00421
00422 m_textAlignLeft->setEnabled( false );
00423 m_textAlignCenter->setEnabled( false );
00424 m_textAlignRight->setEnabled( false );
00425 m_textAlignBlock->setEnabled( false );
00426
00427 m_textList->setEnabled( false );
00428 m_textSuper->setEnabled( false );
00429 m_textSub->setEnabled( false );
00430
00431 m_textIncreaseIndent->setEnabled( false );
00432 m_textDecreaseIndent->setEnabled( false );
00433 }
00434
00435 #include "knoteedit.moc"