25 #include <kactioncollection.h>
26 #include <kcolordialog.h>
28 #include <kfontaction.h>
29 #include <kfontsizeaction.h>
33 #include <kstandardaction.h>
34 #include <ktoggleaction.h>
41 static const short SEP = 5;
46 : PimCommon::CustomTextEdit(configFile, parent ),
50 setAcceptDrops(
true );
51 setWordWrapMode( QTextOption::WordWrap );
52 setLineWrapMode( WidgetWidth );
53 if ( acceptRichText() ) {
54 setAutoFormatting( AutoAll );
56 setAutoFormatting( AutoNone );
60 m_textBold =
new KToggleAction( KIcon( QLatin1String(
"format-text-bold") ), i18n(
"Bold" ),
62 actions->addAction( QLatin1String(
"format_bold"), m_textBold );
63 m_textBold->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_B ) );
64 m_textItalic =
new KToggleAction( KIcon( QLatin1String(
"format-text-italic") ),
65 i18n(
"Italic" ),
this );
66 actions->addAction( QLatin1String(
"format_italic"), m_textItalic );
67 m_textItalic->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_I ) );
68 m_textUnderline =
new KToggleAction( KIcon( QLatin1String(
"format-text-underline") ),
69 i18n(
"Underline" ),
this );
70 actions->addAction( QLatin1String(
"format_underline"), m_textUnderline );
71 m_textUnderline->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_U ) );
72 m_textStrikeOut =
new KToggleAction( KIcon( QLatin1String(
"format-text-strikethrough") ),
73 i18n(
"Strike Out" ),
this );
74 actions->addAction( QLatin1String(
"format_strikeout"), m_textStrikeOut );
75 m_textStrikeOut->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_S ) );
77 connect( m_textBold, SIGNAL(toggled(
bool)), SLOT(
textBold(
bool)) );
78 connect( m_textItalic, SIGNAL(toggled(
bool)),
79 SLOT(setFontItalic(
bool)) );
80 connect( m_textUnderline, SIGNAL(toggled(
bool)),
81 SLOT(setFontUnderline(
bool)) );
82 connect( m_textStrikeOut, SIGNAL(toggled(
bool)),
85 m_textAlignLeft =
new KToggleAction( KIcon( QLatin1String(
"format-justify-left") ),
86 i18n(
"Align Left" ),
this );
87 actions->addAction( QLatin1String(
"format_alignleft"), m_textAlignLeft );
88 connect( m_textAlignLeft, SIGNAL(triggered(
bool)),
90 m_textAlignLeft->setShortcut( QKeySequence( Qt::ALT + Qt::Key_L ) );
91 m_textAlignLeft->setChecked(
true );
92 m_textAlignCenter =
new KToggleAction( KIcon( QLatin1String(
"format-justify-center") ),
93 i18n(
"Align Center" ),
this );
94 actions->addAction( QLatin1String(
"format_aligncenter"), m_textAlignCenter );
95 connect( m_textAlignCenter, SIGNAL(triggered(
bool)),
97 m_textAlignCenter->setShortcut( QKeySequence( Qt::ALT + Qt::Key_C ) );
98 m_textAlignRight =
new KToggleAction( KIcon( QLatin1String(
"format-justify-right") ),
99 i18n(
"Align Right" ),
this );
100 actions->addAction( QLatin1String(
"format_alignright"), m_textAlignRight );
101 connect( m_textAlignRight, SIGNAL(triggered(
bool)),
103 m_textAlignRight->setShortcut( QKeySequence( Qt::ALT + Qt::Key_R ) );
104 m_textAlignBlock =
new KToggleAction( KIcon( QLatin1String(
"format-justify-fill") ),
105 i18n(
"Align Block" ),
this );
106 actions->addAction( QLatin1String(
"format_alignblock"), m_textAlignBlock );
107 connect( m_textAlignBlock, SIGNAL(triggered(
bool)),
109 m_textAlignBlock->setShortcut( QKeySequence( Qt::ALT + Qt::Key_B ) );
111 QActionGroup *group =
new QActionGroup(
this );
112 group->addAction( m_textAlignLeft );
113 group->addAction( m_textAlignCenter );
114 group->addAction( m_textAlignRight );
115 group->addAction( m_textAlignBlock );
117 m_textList =
new KToggleAction( KIcon( QLatin1String(
"format-list-ordered") ), i18n(
"List" ),
this );
118 actions->addAction( QLatin1String(
"format_list"), m_textList );
119 connect( m_textList, SIGNAL(triggered(
bool)), SLOT(
textList()) );
121 m_textSuper =
new KToggleAction( KIcon( QLatin1String(
"format-text-superscript") ),
122 i18n(
"Superscript" ),
this );
123 actions->addAction( QLatin1String(
"format_super"), m_textSuper );
124 connect( m_textSuper, SIGNAL(triggered(
bool)),
126 m_textSub =
new KToggleAction( KIcon( QLatin1String(
"format-text-subscript") ), i18n(
"Subscript" ),
128 actions->addAction( QLatin1String(
"format_sub"), m_textSub );
129 connect( m_textSub, SIGNAL(triggered(
bool)), SLOT(
textSubScript()) );
132 m_textIncreaseIndent =
new KAction( KIcon( QLatin1String(
"format-indent-more") ),
133 i18n(
"Increase Indent" ),
this );
134 actions->addAction( QLatin1String(
"format_increaseindent"), m_textIncreaseIndent );
135 m_textIncreaseIndent->setShortcut( QKeySequence( Qt::CTRL + Qt::ALT +
137 connect( m_textIncreaseIndent, SIGNAL(triggered(
bool)),
140 m_textDecreaseIndent =
new KAction( KIcon( QLatin1String(
"format-indent-less") ),
141 i18n(
"Decrease Indent" ),
this );
142 actions->addAction( QLatin1String(
"format_decreaseindent"), m_textDecreaseIndent );
143 m_textDecreaseIndent->setShortcut( QKeySequence( Qt::CTRL + Qt::ALT +
145 connect( m_textDecreaseIndent, SIGNAL(triggered(
bool)), SLOT(
148 group =
new QActionGroup(
this );
149 group->addAction( m_textIncreaseIndent );
150 group->addAction( m_textDecreaseIndent );
153 pix.fill( Qt::black );
154 m_textColor =
new KAction( i18n(
"Text Color..." ),
this );
155 actions->addAction( QLatin1String(
"format_color"), m_textColor );
156 m_textColor->setIcon( pix );
157 connect( m_textColor, SIGNAL(triggered(
bool)), SLOT(
slotTextColor()) );
159 KAction *act =
new KAction(KIcon( QLatin1String(
"format-fill-color") ), i18n(
"Text Background Color..." ),
this );
160 actions->addAction( QLatin1String(
"text_background_color"), act );
163 m_textFont =
new KFontAction( i18n(
"Text Font" ),
this );
164 actions->addAction( QLatin1String(
"format_font"), m_textFont );
165 connect( m_textFont, SIGNAL(triggered(QString)),
166 this, SLOT(setFontFamily(QString)) );
168 m_textSize =
new KFontSizeAction( i18n(
"Text Size" ),
this );
169 actions->addAction( QLatin1String(
"format_size"), m_textSize );
170 connect( m_textSize, SIGNAL(fontSizeChanged(
int)),
173 KAction *action =
new KAction( i18n(
"Uppercase"),
this );
174 actions->addAction( QLatin1String(
"change_to_uppercase"), action );
175 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotUpperCase()) );
177 action =
new KAction( i18n(
"Lowercase"),
this );
178 actions->addAction( QLatin1String(
"change_to_lowercase"), action );
179 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotLowerCase()) );
181 action =
new KAction( KIcon( QLatin1String(
"knotes_date") ), i18n(
"Insert Date" ),
this );
182 actions->addAction( QLatin1String(
"insert_date"), action );
183 connect( action, SIGNAL(triggered(
bool)), SLOT(
slotInsertDate()) );
186 connect(
this, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
187 SLOT(slotCurrentCharFormatChanged(QTextCharFormat)) );
188 connect(
this, SIGNAL(cursorPositionChanged()),
189 SLOT(slotCursorPositionChanged()) );
190 slotCurrentCharFormatChanged( currentCharFormat() );
191 slotCursorPositionChanged();
203 void KNoteEdit::slotUpperCase()
205 QTextCursor cursor = textCursor();
206 if (cursor.hasSelection()) {
207 const QString newText = cursor.selectedText().toUpper();
208 cursor.insertText(newText);
212 void KNoteEdit::slotLowerCase()
214 QTextCursor cursor = textCursor();
215 if (cursor.hasSelection()) {
216 const QString newText = cursor.selectedText().toLower();
217 cursor.insertText(newText);
224 QMenu *popup = mousePopupMenu();
226 QTextCursor cursor = textCursor();
227 if (!isReadOnly() ) {
228 if (cursor.hasSelection()) {
229 popup->addSeparator();
230 QMenu *changeCaseMenu =
new QMenu(i18n(
"Change case..."), popup);
231 QAction * act = m_actions->action(QLatin1String(
"change_to_lowercase"));
232 changeCaseMenu->addAction(act);
233 act = m_actions->action(QLatin1String(
"change_to_uppercase"));
234 changeCaseMenu->addAction(act);
235 popup->addMenu(changeCaseMenu);
237 popup->addSeparator();
238 QAction * act = m_actions->action(QLatin1String(
"insert_date"));
239 popup->addAction(act);
241 aboutToShowContextMenu(popup);
249 if ( acceptRichText() && Qt::mightBeRichText( text ) ) {
252 setPlainText( text );
258 if ( acceptRichText() ) {
261 return toPlainText();
267 setCurrentFont( font );
271 document()->setDefaultFont( font );
276 setFontPointSize( size );
281 QFontMetrics fm( font() );
282 setTabStopWidth( fm.width( QLatin1Char(
'x') ) * tabs );
287 m_autoIndentMode = newmode;
295 if ( f == acceptRichText() ) {
299 setAcceptRichText( f );
302 setAutoFormatting( AutoAll );
304 setAutoFormatting( AutoNone );
307 const QString t = toPlainText();
310 if ( Qt::mightBeRichText( t ) ) {
316 enableRichTextActions(
true);
319 enableRichTextActions(
false);
325 if (!acceptRichText())
329 f.setFontWeight( b ? QFont::Bold : QFont::Normal );
330 mergeCurrentCharFormat( f );
335 if (!acceptRichText())
339 f.setFontStrikeOut( s );
340 mergeCurrentCharFormat( f );
345 if (!acceptRichText())
350 QColor c = textColor();
351 if ( KColorDialog::getColor( c,
this ) ) {
360 if (!acceptRichText())
365 QColor c = textBackgroundColor();
366 if ( KColorDialog::getColor( c,
this ) ) {
367 setTextBackgroundColor( c );
375 if (!acceptRichText())
377 setAlignment( Qt::AlignLeft );
378 m_textAlignLeft->setChecked(
true );
383 if (!acceptRichText())
385 setAlignment( Qt::AlignCenter );
386 m_textAlignCenter->setChecked(
true );
391 if (!acceptRichText())
393 setAlignment( Qt::AlignRight );
394 m_textAlignRight->setChecked(
true );
399 if (!acceptRichText())
401 setAlignment( Qt::AlignJustify );
402 m_textAlignBlock->setChecked(
true );
407 if (!acceptRichText())
409 QTextCursor c = textCursor();
412 if ( m_textList->isChecked() ) {
414 QTextBlockFormat bf = c.blockFormat();
416 lf.setIndent( bf.indent() + 1 );
419 lf.setStyle( QTextListFormat::ListDisc );
421 c.setBlockFormat( bf );
425 bf.setObjectIndex( -1 );
426 c.setBlockFormat( bf );
435 if (!acceptRichText())
438 if ( m_textSuper->isChecked() ) {
439 if ( m_textSub->isChecked() )
440 m_textSub->setChecked(
false );
441 f.setVerticalAlignment( QTextCharFormat::AlignSuperScript );
443 f.setVerticalAlignment( QTextCharFormat::AlignNormal );
445 mergeCurrentCharFormat( f );
450 if (!acceptRichText())
453 if ( m_textSub->isChecked() ) {
454 if ( m_textSuper->isChecked() )
455 m_textSuper->setChecked(
false );
456 f.setVerticalAlignment( QTextCharFormat::AlignSubScript );
458 f.setVerticalAlignment( QTextCharFormat::AlignNormal );
460 mergeCurrentCharFormat( f );
465 if (!acceptRichText())
467 QTextBlockFormat f = textCursor().blockFormat();
468 f.setIndent( f.indent() + 1 );
469 textCursor().setBlockFormat( f );
474 if (!acceptRichText())
476 QTextBlockFormat f = textCursor().blockFormat();
477 short int curIndent = f.indent();
479 if ( curIndent > 0 ) {
480 f.setIndent( curIndent - 1 );
482 textCursor().setBlockFormat( f );
490 KTextEdit::keyPressEvent( e );
492 if ( m_autoIndentMode &&
493 ( ( e->key() == Qt::Key_Return ) || ( e->key() == Qt::Key_Enter ) ) ) {
500 KTextEdit::focusInEvent( e );
502 setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
503 setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
508 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
509 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
511 KTextEdit::focusOutEvent( e );
516 void KNoteEdit::slotCurrentCharFormatChanged(
const QTextCharFormat &f )
518 if (!acceptRichText())
522 m_textFont->setFont( f.fontFamily() );
523 m_textSize->setFontSize( (f.fontPointSize()>0 ) ? (
int ) f.fontPointSize() :10);
525 m_textBold->setChecked( f.font().bold() );
526 m_textItalic->setChecked( f.fontItalic() );
527 m_textUnderline->setChecked( f.fontUnderline() );
528 m_textStrikeOut->setChecked( f.fontStrikeOut() );
532 pix.fill( f.foreground().color() );
533 m_textColor->QAction::setIcon( pix );
536 QTextCharFormat::VerticalAlignment va = f.verticalAlignment();
537 if ( va == QTextCharFormat::AlignNormal ) {
538 m_textSuper->setChecked(
false );
539 m_textSub->setChecked(
false );
540 }
else if ( va == QTextCharFormat::AlignSuperScript ) {
541 m_textSuper->setChecked(
true );
542 }
else if ( va == QTextCharFormat::AlignSubScript ) {
543 m_textSub->setChecked(
true );
548 void KNoteEdit::slotCursorPositionChanged()
550 if (!acceptRichText())
553 const Qt::Alignment a = alignment();
554 if ( a & Qt::AlignLeft ) {
555 m_textAlignLeft->setChecked(
true );
556 }
else if ( a & Qt::AlignHCenter ) {
557 m_textAlignCenter->setChecked(
true );
558 }
else if ( a & Qt::AlignRight ) {
559 m_textAlignRight->setChecked(
true );
560 }
else if ( a & Qt::AlignJustify ) {
561 m_textAlignBlock->setChecked(
true );
567 void KNoteEdit::autoIndent()
569 QTextCursor c = textCursor();
570 QTextBlock b = c.block();
573 while ( ( b.previous().length() > 0 ) &&
string.trimmed().isEmpty() ) {
578 if (
string.trimmed().isEmpty() ) {
586 QString indentString;
588 const int len =
string.length();
590 while ( i < len &&
string.at( i ).isSpace() ) {
591 indentString +=
string.at( i++ );
594 if ( !indentString.isEmpty() ) {
595 c.insertText( indentString );
599 void KNoteEdit::enableRichTextActions(
bool enabled)
601 m_textColor->setEnabled( enabled );
602 m_textFont->setEnabled( enabled );
603 m_textSize->setEnabled( enabled );
605 m_textBold->setEnabled( enabled );
606 m_textItalic->setEnabled( enabled );
607 m_textUnderline->setEnabled( enabled );
608 m_textStrikeOut->setEnabled( enabled );
610 m_textAlignLeft->setEnabled( enabled );
611 m_textAlignCenter->setEnabled( enabled );
612 m_textAlignRight->setEnabled( enabled );
613 m_textAlignBlock->setEnabled( enabled );
615 m_textList->setEnabled( enabled );
616 m_textSuper->setEnabled( enabled );
617 m_textSub->setEnabled( enabled );
619 m_textIncreaseIndent->setEnabled( enabled );
620 m_textDecreaseIndent->setEnabled( enabled );
625 insertPlainText( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
629 #include "knoteedit.moc"
virtual void focusInEvent(QFocusEvent *)
void setTextFont(const QFont &font)
void slotTextBackgroundColor()
static const short ICON_SIZE
void setAutoIndentMode(bool newmode)
void setTextFontSize(int)
void setTabStop(int tabs)
void setRichText(bool)
public slots
KNoteEdit(const QString &configFile, KActionCollection *actions, QWidget *parent=0)
virtual void focusOutEvent(QFocusEvent *)
void setText(const QString &text)
void mousePopupMenuImplementation(const QPoint &pos)
void setNote(KNote *_note)
virtual void keyPressEvent(QKeyEvent *)
protected methods
void blockEmitDataChanged(bool _b)
void textDecreaseIndent()
void textIncreaseIndent()