21 #include <tqdragobject.h>
25 #include <tdelocale.h>
26 #include <tdeaction.h>
28 #include <kstdaction.h>
29 #include <kcolordialog.h>
30 #include <tqpopupmenu.h>
31 #include <kiconloader.h>
32 #include "knoteedit.h"
35 static const short SEP = 5;
36 static const short ICON_SIZE = 10;
39 KNoteEdit::KNoteEdit( TDEActionCollection *actions, TQWidget *parent,
const char *name )
40 : KTextEdit( parent, name ), m_note( 0 )
42 setAcceptDrops(
true );
43 setWordWrap( WidgetWidth );
44 setWrapPolicy( AtWhiteSpace );
45 setLinkUnderline(
true );
46 setCheckSpellingEnabled(
false);
48 undo = KStdAction::undo(
this, TQ_SLOT(undo()), actions );
49 redo = KStdAction::redo(
this, TQ_SLOT(redo()), actions );
50 undo->setEnabled( isUndoAvailable() );
51 redo->setEnabled( isRedoAvailable() );
53 m_cut = KStdAction::cut(
this, TQ_SLOT(cut()), actions );
54 m_copy = KStdAction::copy(
this, TQ_SLOT(copy()), actions );
55 m_paste = KStdAction::paste(
this, TQ_SLOT(paste()), actions );
57 m_cut->setEnabled(
false );
58 m_copy->setEnabled(
false );
59 m_paste->setEnabled(
true );
61 connect(
this, TQ_SIGNAL(undoAvailable(
bool)),
this, TQ_SLOT(setEnabledUndo(
bool)) );
62 connect(
this, TQ_SIGNAL(redoAvailable(
bool)),
this, TQ_SLOT(setEnabledRedo(
bool)) );
64 connect(
this, TQ_SIGNAL(copyAvailable(
bool)),
this, TQ_SLOT( slotCutEnabled(
bool ) ) );
65 connect(
this, TQ_SIGNAL(copyAvailable(
bool)), m_copy, TQ_SLOT(setEnabled(
bool)) );
67 new TDEAction( KStdGuiItem::clear(), 0,
this, TQ_SLOT(clear()), actions,
"edit_clear" );
68 KStdAction::selectAll(
this, TQ_SLOT(selectAll()), actions );
71 m_textBold =
new TDEToggleAction( i18n(
"Bold"),
"format-text-bold", CTRL + Key_B, 0, 0,
72 actions,
"format_bold" );
73 m_textItalic =
new TDEToggleAction( i18n(
"Italic"),
"format-text-italic", CTRL + Key_I, 0, 0,
74 actions,
"format_italic" );
75 m_textUnderline =
new TDEToggleAction( i18n(
"Underline"),
"format-text-underline", CTRL + Key_U, 0, 0,
76 actions,
"format_underline" );
77 m_textStrikeOut =
new TDEToggleAction( i18n(
"Strike Out"),
"format-text-strikethrough", CTRL + Key_S, 0, 0,
78 actions,
"format_strikeout" );
80 connect( m_textBold, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(setBold(
bool)) );
81 connect( m_textItalic, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(setItalic(
bool)) );
82 connect( m_textUnderline, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(setUnderline(
bool)) );
83 connect( m_textStrikeOut, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(textStrikeOut(
bool)) );
85 m_textAlignLeft =
new TDEToggleAction( i18n(
"Align Left"),
"format-text-direction-ltr", ALT + Key_L,
86 this, TQ_SLOT(textAlignLeft()),
87 actions,
"format_alignleft" );
88 m_textAlignLeft->setChecked(
true );
89 m_textAlignCenter =
new TDEToggleAction( i18n(
"Align Center"),
"text_center", ALT + Key_C,
90 this, TQ_SLOT(textAlignCenter()),
91 actions,
"format_aligncenter" );
92 m_textAlignRight =
new TDEToggleAction( i18n(
"Align Right"),
"format-text-direction-rtl", ALT + Key_R,
93 this, TQ_SLOT(textAlignRight()),
94 actions,
"format_alignright" );
95 m_textAlignBlock =
new TDEToggleAction( i18n(
"Align Block"),
"text_block", ALT + Key_B,
96 this, TQ_SLOT(textAlignBlock()),
97 actions,
"format_alignblock" );
99 m_textAlignLeft->setExclusiveGroup(
"align" );
100 m_textAlignCenter->setExclusiveGroup(
"align" );
101 m_textAlignRight->setExclusiveGroup(
"align" );
102 m_textAlignBlock->setExclusiveGroup(
"align" );
104 m_textList =
new TDEToggleAction( i18n(
"List"),
"enum_list", 0,
105 this, TQ_SLOT(textList()),
106 actions,
"format_list" );
108 m_textList->setExclusiveGroup(
"style" );
110 m_textSuper =
new TDEToggleAction( i18n(
"Superscript"),
"text_super", 0,
111 this, TQ_SLOT(textSuperScript()),
112 actions,
"format_super" );
113 m_textSub =
new TDEToggleAction( i18n(
"Subscript"),
"text_sub", 0,
114 this, TQ_SLOT(textSubScript()),
115 actions,
"format_sub" );
117 m_textSuper->setExclusiveGroup(
"valign" );
118 m_textSub->setExclusiveGroup(
"valign" );
130 TQPixmap pix( ICON_SIZE, ICON_SIZE );
132 m_textColor =
new TDEAction( i18n(
"Text Color..."), pix, 0,
this,
133 TQ_SLOT(textColor()), actions,
"format_color" );
135 m_textFont =
new TDEFontAction( i18n(
"Text Font"),
"text", KKey(),
136 actions,
"format_font" );
137 connect( m_textFont, TQ_SIGNAL(activated(
const TQString & )),
138 this, TQ_SLOT(setFamily(
const TQString & )) );
140 m_textSize =
new TDEFontSizeAction( i18n(
"Text Size"), KKey(),
141 actions,
"format_size" );
142 connect( m_textSize, TQ_SIGNAL(fontSizeChanged(
int )),
143 this, TQ_SLOT(setPointSize(
int )) );
146 connect(
this, TQ_SIGNAL(returnPressed()), TQ_SLOT(slotReturnPressed()) );
147 connect(
this, TQ_SIGNAL(currentFontChanged(
const TQFont & )),
148 this, TQ_SLOT(fontChanged(
const TQFont & )) );
149 connect(
this, TQ_SIGNAL(currentColorChanged(
const TQColor & )),
150 this, TQ_SLOT(colorChanged(
const TQColor & )) );
151 connect(
this, TQ_SIGNAL(currentAlignmentChanged(
int )),
152 this, TQ_SLOT(alignmentChanged(
int )) );
153 connect(
this, TQ_SIGNAL(currentVerticalAlignmentChanged( VerticalAlignment )),
154 this, TQ_SLOT(verticalAlignmentChanged( VerticalAlignment )) );
157 KNoteEdit::~KNoteEdit()
161 void KNoteEdit::setEnabledRedo(
bool b )
163 redo->setEnabled( b && !isReadOnly() );
166 void KNoteEdit::setEnabledUndo(
bool b )
168 undo->setEnabled( b && !isReadOnly() );
171 void KNoteEdit::slotCutEnabled(
bool b )
173 m_cut->setEnabled( b && !isReadOnly() );
176 void KNoteEdit::setText(
const TQString& text )
180 KTextEdit::setText( text );
181 fontChanged( currentFont() );
184 void KNoteEdit::setTextFont(
const TQFont& font )
186 if ( textFormat() == PlainText )
189 setCurrentFont( font );
192 void KNoteEdit::setTextColor(
const TQColor& color )
195 colorChanged( color );
198 void KNoteEdit::setTabStop(
int tabs )
200 TQFontMetrics fm( font() );
201 setTabStopWidth( fm.width(
'x' ) * tabs );
204 void KNoteEdit::setAutoIndentMode(
bool newmode )
206 m_autoIndentMode = newmode;
212 void KNoteEdit::setTextFormat( TextFormat f )
214 if ( f == textFormat() )
220 KTextEdit::setTextFormat( f );
224 if ( TQStyleSheet::mightBeRichText( t ) )
229 enableRichTextActions();
233 KTextEdit::setTextFormat( f );
237 disableRichTextActions();
241 void KNoteEdit::textStrikeOut(
bool s )
247 if ( !hasSelectedText() )
249 font = currentFont();
250 font.setStrikeOut( s );
251 setCurrentFont( font );
255 int pFrom, pTo, iFrom, iTo, iF, iT;
258 getSelection( &pFrom, &iFrom, &pTo, &iTo );
259 getCursorPosition( &cp, &ci );
261 for (
int p = pFrom; p <= pTo; p++ )
264 iT = paragraphLength( p );
272 for (
int i = iF; i < iT; i++ )
274 setCursorPosition( p, i + 1 );
275 setSelection( p, i, p, i + 1 );
276 font = currentFont();
277 font.setStrikeOut( s );
278 setCurrentFont( font );
282 setSelection( pFrom, iFrom, pTo, iTo );
283 setCursorPosition( cp, ci );
287 void KNoteEdit::textColor()
290 m_note->blockEmitDataChanged(
true );
292 int ret = KColorDialog::getColor( c,
this );
293 if ( ret == TQDialog::Accepted )
296 m_note->blockEmitDataChanged(
false );
299 void KNoteEdit::textAlignLeft()
301 setAlignment( AlignLeft );
302 m_textAlignLeft->setChecked(
true );
305 void KNoteEdit::textAlignCenter()
307 setAlignment( AlignCenter );
308 m_textAlignCenter->setChecked(
true );
311 void KNoteEdit::textAlignRight()
313 setAlignment( AlignRight );
314 m_textAlignRight->setChecked(
true );
317 void KNoteEdit::textAlignBlock()
319 setAlignment( AlignJustify );
320 m_textAlignBlock->setChecked(
true );
323 void KNoteEdit::textList()
325 if ( m_textList->isChecked() )
326 setParagType( TQStyleSheetItem::DisplayListItem, TQStyleSheetItem::ListDisc );
328 setParagType( TQStyleSheetItem::DisplayBlock, TQStyleSheetItem::ListDisc );
331 void KNoteEdit::textSuperScript()
333 if ( m_textSuper->isChecked() )
334 setVerticalAlignment( AlignSuperScript );
336 setVerticalAlignment( AlignNormal );
339 void KNoteEdit::textSubScript()
341 if ( m_textSub->isChecked() )
342 setVerticalAlignment( AlignSubScript );
344 setVerticalAlignment( AlignNormal );
358 void KNoteEdit::contentsDragEnterEvent( TQDragEnterEvent *e )
360 if ( KURLDrag::canDecode( e ) )
363 KTextEdit::contentsDragEnterEvent( e );
366 void KNoteEdit::contentsDropEvent( TQDropEvent *e )
370 if ( KURLDrag::decode( e, list ) )
372 KURL::List::ConstIterator begin = list.constBegin();
373 KURL::List::ConstIterator end = list.constEnd();
374 for ( KURL::List::ConstIterator it = begin; it != end; ++it )
379 insert( (*it).prettyURL() );
383 KTextEdit::contentsDropEvent( e );
388 void KNoteEdit::slotReturnPressed()
390 if ( m_autoIndentMode )
394 void KNoteEdit::fontChanged(
const TQFont &f )
396 m_textFont->setFont( f.family() );
397 m_textSize->setFontSize( f.pointSize() );
399 m_textBold->setChecked( f.bold() );
400 m_textItalic->setChecked( f.italic() );
401 m_textUnderline->setChecked( f.underline() );
402 m_textStrikeOut->setChecked( f.strikeOut() );
405 void KNoteEdit::colorChanged(
const TQColor &c )
407 TQPixmap pix( ICON_SIZE, ICON_SIZE );
409 m_textColor->setIconSet( pix );
412 void KNoteEdit::alignmentChanged(
int a )
415 if ( ( a == AlignAuto ) || ( a & AlignLeft ) )
416 m_textAlignLeft->setChecked(
true );
417 else if ( ( a & AlignHCenter ) )
418 m_textAlignCenter->setChecked(
true );
419 else if ( ( a & AlignRight ) )
420 m_textAlignRight->setChecked(
true );
421 else if ( ( a & AlignJustify ) )
422 m_textAlignBlock->setChecked(
true );
425 void KNoteEdit::verticalAlignmentChanged( VerticalAlignment a )
427 if ( a == AlignNormal )
429 m_textSuper->setChecked(
false );
430 m_textSub->setChecked(
false );
432 else if ( a == AlignSuperScript )
433 m_textSuper->setChecked(
true );
434 else if ( a == AlignSubScript )
435 m_textSub->setChecked(
true );
441 void KNoteEdit::autoIndent()
445 getCursorPosition( ¶, &index );
446 while ( para > 0 &&
string.stripWhiteSpace().isEmpty() )
447 string = text( --para );
449 if (
string.stripWhiteSpace().isEmpty() )
456 TQString indentString;
458 int len =
string.length();
460 while ( i < len &&
string.at(i).isSpace() )
461 indentString +=
string.at( i++ );
463 if ( !indentString.isEmpty() )
464 insert( indentString );
467 void KNoteEdit::emitLinkClicked(
const TQString &s )
469 kdDebug(5500) << k_funcinfo << s << endl;
472 void KNoteEdit::enableRichTextActions()
474 m_textColor->setEnabled(
true );
475 m_textFont->setEnabled(
true );
476 m_textSize->setEnabled(
true );
478 m_textBold->setEnabled(
true );
479 m_textItalic->setEnabled(
true );
480 m_textUnderline->setEnabled(
true );
481 m_textStrikeOut->setEnabled(
true );
483 m_textAlignLeft->setEnabled(
true );
484 m_textAlignCenter->setEnabled(
true );
485 m_textAlignRight->setEnabled(
true );
486 m_textAlignBlock->setEnabled(
true );
488 m_textList->setEnabled(
true );
489 m_textSuper->setEnabled(
true );
490 m_textSub->setEnabled(
true );
496 void KNoteEdit::disableRichTextActions()
498 m_textColor->setEnabled(
false );
499 m_textFont->setEnabled(
false );
500 m_textSize->setEnabled(
false );
502 m_textBold->setEnabled(
false );
503 m_textItalic->setEnabled(
false );
504 m_textUnderline->setEnabled(
false );
505 m_textStrikeOut->setEnabled(
false );
507 m_textAlignLeft->setEnabled(
false );
508 m_textAlignCenter->setEnabled(
false );
509 m_textAlignRight->setEnabled(
false );
510 m_textAlignBlock->setEnabled(
false );
512 m_textList->setEnabled(
false );
513 m_textSuper->setEnabled(
false );
514 m_textSub->setEnabled(
false );
520 void KNoteEdit::slotAllowTab()
522 setTabChangesFocus(!tabChangesFocus());
525 TQPopupMenu *KNoteEdit::createPopupMenu(
const TQPoint &pos )
527 enum { IdUndo, IdRedo, IdSep1, IdCut, IdCopy, IdPaste, IdClear, IdSep2, IdSelectAll };
529 TQPopupMenu *menu = TQTextEdit::createPopupMenu( pos );
532 menu->changeItem( menu->idAt(0), SmallIconSet(
"edit-copy"), menu->text( menu->idAt(0) ) );
534 int id = menu->idAt(0);
535 menu->changeItem(
id - IdUndo, SmallIconSet(
"edit-undo"), menu->text(
id - IdUndo) );
536 menu->changeItem(
id - IdRedo, SmallIconSet(
"edit-redo"), menu->text(
id - IdRedo) );
537 menu->changeItem(
id - IdCut, SmallIconSet(
"edit-cut"), menu->text(
id - IdCut) );
538 menu->changeItem(
id - IdCopy, SmallIconSet(
"edit-copy"), menu->text(
id - IdCopy) );
539 menu->changeItem(
id - IdPaste, SmallIconSet(
"edit-paste"), menu->text(
id - IdPaste) );
540 menu->changeItem(
id - IdClear, SmallIconSet(
"edit-clear"), menu->text(
id - IdClear) );
542 menu->insertSeparator();
543 id = menu->insertItem( SmallIconSet(
"tools-check-spelling" ), i18n(
"Check Spelling..." ),
544 this, TQ_SLOT( checkSpelling() ) );
546 if( text().isEmpty() )
547 menu->setItemEnabled(
id,
false );
549 menu->insertSeparator();
550 id=menu->insertItem(i18n(
"Allow Tabulations"),
this,TQ_SLOT(slotAllowTab()));
551 menu->setItemChecked(
id, !tabChangesFocus());
557 #include "knoteedit.moc"