23 #include "noteshared/editor/noteeditorutils.h"
24 #include "pimcommon/util/editorutil.h"
25 #include "knotesglobalconfig.h"
28 #include <kactioncollection.h>
29 #include <kcolordialog.h>
31 #include <kfontaction.h>
32 #include <kfontsizeaction.h>
36 #include <kstandardaction.h>
37 #include <ktoggleaction.h>
48 : PimCommon::CustomTextEdit(configFile, parent ),
52 setAcceptDrops(
true );
53 setWordWrapMode( QTextOption::WordWrap );
54 setLineWrapMode( WidgetWidth );
55 if ( acceptRichText() ) {
56 setAutoFormatting( AutoAll );
58 setAutoFormatting( AutoNone );
62 m_textBold =
new KToggleAction( KIcon(
QLatin1String(
"format-text-bold") ), i18n(
"Bold" ),
64 actions->addAction(
QLatin1String(
"format_bold"), m_textBold );
65 m_textBold->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_B ) );
66 m_textItalic =
new KToggleAction( KIcon(
QLatin1String(
"format-text-italic") ),
67 i18n(
"Italic" ),
this );
68 actions->addAction(
QLatin1String(
"format_italic"), m_textItalic );
69 m_textItalic->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_I ) );
70 m_textUnderline =
new KToggleAction( KIcon(
QLatin1String(
"format-text-underline") ),
71 i18n(
"Underline" ),
this );
72 actions->addAction(
QLatin1String(
"format_underline"), m_textUnderline );
73 m_textUnderline->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_U ) );
74 m_textStrikeOut =
new KToggleAction( KIcon(
QLatin1String(
"format-text-strikethrough") ),
75 i18n(
"Strike Out" ),
this );
76 actions->addAction(
QLatin1String(
"format_strikeout"), m_textStrikeOut );
77 m_textStrikeOut->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_S ) );
79 connect( m_textBold, SIGNAL(toggled(
bool)), SLOT(
textBold(
bool)) );
80 connect( m_textItalic, SIGNAL(toggled(
bool)),
81 SLOT(setFontItalic(
bool)) );
82 connect( m_textUnderline, SIGNAL(toggled(
bool)),
83 SLOT(setFontUnderline(
bool)) );
84 connect( m_textStrikeOut, SIGNAL(toggled(
bool)),
87 m_textAlignLeft =
new KToggleAction( KIcon(
QLatin1String(
"format-justify-left") ),
88 i18n(
"Align Left" ),
this );
89 actions->addAction(
QLatin1String(
"format_alignleft"), m_textAlignLeft );
90 connect( m_textAlignLeft, SIGNAL(triggered(
bool)),
92 m_textAlignLeft->setShortcut(
QKeySequence( Qt::ALT + Qt::Key_L ) );
93 m_textAlignLeft->setChecked(
true );
94 m_textAlignCenter =
new KToggleAction( KIcon(
QLatin1String(
"format-justify-center") ),
95 i18n(
"Align Center" ),
this );
96 actions->addAction(
QLatin1String(
"format_aligncenter"), m_textAlignCenter );
97 connect( m_textAlignCenter, SIGNAL(triggered(
bool)),
99 m_textAlignCenter->setShortcut(
QKeySequence( Qt::ALT + Qt::Key_C ) );
100 m_textAlignRight =
new KToggleAction( KIcon(
QLatin1String(
"format-justify-right") ),
101 i18n(
"Align Right" ),
this );
102 actions->addAction(
QLatin1String(
"format_alignright"), m_textAlignRight );
103 connect( m_textAlignRight, SIGNAL(triggered(
bool)),
105 m_textAlignRight->setShortcut(
QKeySequence( Qt::ALT + Qt::Key_R ) );
106 m_textAlignBlock =
new KToggleAction( KIcon(
QLatin1String(
"format-justify-fill") ),
107 i18n(
"Align Block" ),
this );
108 actions->addAction(
QLatin1String(
"format_alignblock"), m_textAlignBlock );
109 connect( m_textAlignBlock, SIGNAL(triggered(
bool)),
111 m_textAlignBlock->setShortcut(
QKeySequence( Qt::ALT + Qt::Key_B ) );
119 m_textList =
new KToggleAction( KIcon(
QLatin1String(
"format-list-ordered") ), i18n(
"List" ),
this );
120 actions->addAction(
QLatin1String(
"format_list"), m_textList );
121 connect( m_textList, SIGNAL(triggered(
bool)), SLOT(
textList()) );
123 m_textSuper =
new KToggleAction( KIcon(
QLatin1String(
"format-text-superscript") ),
124 i18n(
"Superscript" ),
this );
125 actions->addAction(
QLatin1String(
"format_super"), m_textSuper );
126 connect( m_textSuper, SIGNAL(triggered(
bool)),
128 m_textSub =
new KToggleAction( KIcon(
QLatin1String(
"format-text-subscript") ), i18n(
"Subscript" ),
130 actions->addAction(
QLatin1String(
"format_sub"), m_textSub );
131 connect( m_textSub, SIGNAL(triggered(
bool)), SLOT(
textSubScript()) );
134 m_textIncreaseIndent =
new KAction( KIcon(
QLatin1String(
"format-indent-more") ),
135 i18n(
"Increase Indent" ),
this );
136 actions->addAction(
QLatin1String(
"format_increaseindent"), m_textIncreaseIndent );
137 m_textIncreaseIndent->setShortcut(
QKeySequence( Qt::CTRL + Qt::ALT +
139 connect( m_textIncreaseIndent, SIGNAL(triggered(
bool)),
142 m_textDecreaseIndent =
new KAction( KIcon(
QLatin1String(
"format-indent-less") ),
143 i18n(
"Decrease Indent" ),
this );
144 actions->addAction(
QLatin1String(
"format_decreaseindent"), m_textDecreaseIndent );
145 m_textDecreaseIndent->setShortcut(
QKeySequence( Qt::CTRL + Qt::ALT +
147 connect( m_textDecreaseIndent, SIGNAL(triggered(
bool)), SLOT(
151 group->
addAction( m_textIncreaseIndent );
152 group->
addAction( m_textDecreaseIndent );
155 pix.
fill( Qt::black );
156 m_textColor =
new KAction( i18n(
"Text Color..." ),
this );
157 actions->addAction(
QLatin1String(
"format_color"), m_textColor );
158 m_textColor->setIcon( pix );
159 connect( m_textColor, SIGNAL(triggered(
bool)), SLOT(
slotTextColor()) );
161 KAction *act =
new KAction(KIcon(
QLatin1String(
"format-fill-color") ), i18n(
"Text Background Color..." ),
this );
162 actions->addAction(
QLatin1String(
"text_background_color"), act );
165 m_textFont =
new KFontAction( i18n(
"Text Font" ),
this );
166 actions->addAction(
QLatin1String(
"format_font"), m_textFont );
167 connect( m_textFont, SIGNAL(triggered(
QString)),
168 this, SLOT(setFontFamily(
QString)) );
170 m_textSize =
new KFontSizeAction( i18n(
"Text Size" ),
this );
171 actions->addAction(
QLatin1String(
"format_size"), m_textSize );
172 connect( m_textSize, SIGNAL(fontSizeChanged(
int)),
175 KAction *action =
new KAction( i18n(
"Uppercase"),
this );
176 actions->addAction(
QLatin1String(
"change_to_uppercase"), action );
177 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotUpperCase()) );
179 action =
new KAction( i18n(
"Sentence case"),
this );
180 actions->addAction(
QLatin1String(
"change_to_sentencecase"), action );
181 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotSentenceCase()) );
183 action =
new KAction( i18n(
"Lowercase"),
this );
184 actions->addAction(
QLatin1String(
"change_to_lowercase"), action );
185 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotLowerCase()) );
187 action =
new KAction( KIcon(
QLatin1String(
"knotes_date") ), i18n(
"Insert Date" ),
this );
189 connect( action, SIGNAL(triggered(
bool)), SLOT(
slotInsertDate()) );
191 action =
new KAction( KIcon(
QLatin1String(
"checkmark") ), i18n(
"Insert Checkmark" ),
this );
192 actions->addAction(
QLatin1String(
"insert_checkmark"), action );
193 connect( action, SIGNAL(triggered(
bool)), SLOT(slotInsertCheckMark()) );
198 connect(
this, SIGNAL(cursorPositionChanged()),
199 SLOT(slotCursorPositionChanged()) );
200 slotCurrentCharFormatChanged( currentCharFormat() );
201 slotCursorPositionChanged();
210 mDefaultBackgroundColor = bg;
211 mDefaultForegroundColor = fg;
225 p.
setColor( QPalette::WindowText, fg );
229 p.
setColor( QPalette::ButtonText, fg );
253 void KNoteEdit::slotSentenceCase()
256 PimCommon::EditorUtil editorUtil;
257 editorUtil.sentenceCase(cursor);
260 void KNoteEdit::slotUpperCase()
263 PimCommon::EditorUtil editorUtil;
264 editorUtil.upperCase(cursor);
267 void KNoteEdit::slotLowerCase()
270 PimCommon::EditorUtil editorUtil;
271 editorUtil.lowerCase(cursor);
276 QMenu *popup = mousePopupMenu();
279 if (!isReadOnly() ) {
282 QMenu *changeCaseMenu =
new QMenu(i18n(
"Change case..."), popup);
285 act = m_actions->action(
QLatin1String(
"change_to_lowercase"));
287 act = m_actions->action(
QLatin1String(
"change_to_uppercase"));
289 popup->
addMenu(changeCaseMenu);
298 aboutToShowContextMenu(popup);
309 setPlainText( text );
315 if ( acceptRichText() ) {
318 return toPlainText();
324 setCurrentFont( font );
328 document()->setDefaultFont( font );
333 setFontPointSize( size );
344 m_autoIndentMode = newmode;
352 if ( f == acceptRichText() ) {
356 setAcceptRichText( f );
359 setAutoFormatting( AutoAll );
361 setAutoFormatting( AutoNone );
364 const QString t = toPlainText();
373 enableRichTextActions(
true);
376 enableRichTextActions(
false);
382 if (!acceptRichText())
387 mergeCurrentCharFormat( f );
392 if (!acceptRichText())
397 mergeCurrentCharFormat( f );
402 if (!acceptRichText())
408 if ( KColorDialog::getColor( c, mDefaultForegroundColor,
this ) ) {
409 setTextColor( c.
isValid() ? c : mDefaultForegroundColor);
417 if (!acceptRichText())
422 QColor c = textBackgroundColor();
423 if ( KColorDialog::getColor( c, mDefaultBackgroundColor,
this ) ) {
424 setTextBackgroundColor( c.
isValid() ? c : mDefaultBackgroundColor );
432 if (!acceptRichText())
434 setAlignment( Qt::AlignLeft );
435 m_textAlignLeft->setChecked(
true );
440 if (!acceptRichText())
442 setAlignment( Qt::AlignCenter );
443 m_textAlignCenter->setChecked(
true );
448 if (!acceptRichText())
450 setAlignment( Qt::AlignRight );
451 m_textAlignRight->setChecked(
true );
456 if (!acceptRichText())
458 setAlignment( Qt::AlignJustify );
459 m_textAlignBlock->setChecked(
true );
464 if (!acceptRichText())
469 if ( m_textList->isChecked() ) {
476 lf.
setStyle( QTextListFormat::ListDisc );
492 if (!acceptRichText())
495 if ( m_textSuper->isChecked() ) {
496 if ( m_textSub->isChecked() )
497 m_textSub->setChecked(
false );
502 mergeCurrentCharFormat( f );
507 if (!acceptRichText())
510 if ( m_textSub->isChecked() ) {
511 if ( m_textSuper->isChecked() )
512 m_textSuper->setChecked(
false );
517 mergeCurrentCharFormat( f );
522 if (!acceptRichText())
526 textCursor().setBlockFormat( f );
531 if (!acceptRichText())
534 short int curIndent = f.
indent();
536 if ( curIndent > 0 ) {
539 textCursor().setBlockFormat( f );
547 KTextEdit::keyPressEvent( e );
549 if ( m_autoIndentMode &&
550 ( ( e->
key() == Qt::Key_Return ) || ( e->
key() == Qt::Key_Enter ) ) ) {
557 KTextEdit::focusInEvent( e );
559 setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
560 setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
565 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
566 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
568 KTextEdit::focusOutEvent( e );
573 void KNoteEdit::slotCurrentCharFormatChanged(
const QTextCharFormat &f )
575 if (!acceptRichText())
582 m_textBold->setChecked( f.
font().
bold() );
590 m_textColor->QAction::setIcon( pix );
594 if ( va == QTextCharFormat::AlignNormal ) {
595 m_textSuper->setChecked(
false );
596 m_textSub->setChecked(
false );
597 }
else if ( va == QTextCharFormat::AlignSuperScript ) {
598 m_textSuper->setChecked(
true );
599 }
else if ( va == QTextCharFormat::AlignSubScript ) {
600 m_textSub->setChecked(
true );
605 void KNoteEdit::slotCursorPositionChanged()
607 if (!acceptRichText())
611 if ( a & Qt::AlignLeft ) {
612 m_textAlignLeft->setChecked(
true );
613 }
else if ( a & Qt::AlignHCenter ) {
614 m_textAlignCenter->setChecked(
true );
615 }
else if ( a & Qt::AlignRight ) {
616 m_textAlignRight->setChecked(
true );
617 }
else if ( a & Qt::AlignJustify ) {
618 m_textAlignBlock->setChecked(
true );
624 void KNoteEdit::autoIndent()
630 while ( ( b.
previous().
length() > 0 ) &&
string.trimmed().isEmpty() ) {
635 if (
string.trimmed().isEmpty() ) {
645 const int len =
string.
length();
647 while ( i < len &&
string.at( i ).isSpace() ) {
648 indentString +=
string.
at( i++ );
651 if ( !indentString.
isEmpty() ) {
656 void KNoteEdit::enableRichTextActions(
bool enabled)
658 m_textColor->setEnabled( enabled );
659 m_textFont->setEnabled( enabled );
660 m_textSize->setEnabled( enabled );
662 m_textBold->setEnabled( enabled );
663 m_textItalic->setEnabled( enabled );
664 m_textUnderline->setEnabled( enabled );
665 m_textStrikeOut->setEnabled( enabled );
667 m_textAlignLeft->setEnabled( enabled );
668 m_textAlignCenter->setEnabled( enabled );
669 m_textAlignRight->setEnabled( enabled );
670 m_textAlignBlock->setEnabled( enabled );
672 m_textList->setEnabled( enabled );
673 m_textSuper->setEnabled( enabled );
674 m_textSub->setEnabled( enabled );
676 m_textIncreaseIndent->setEnabled( enabled );
677 m_textDecreaseIndent->setEnabled( enabled );
682 NoteShared::NoteEditorUtils noteEditorUtils;
683 noteEditorUtils.insertDate(
this);
686 void KNoteEdit::slotInsertCheckMark()
689 NoteShared::NoteEditorUtils noteEditorUtils;
690 noteEditorUtils.addCheckmark(cursor);
699 setTextCursor( cursor );
700 ensureCursorVisible();
706 return textCursor().position();
void focusInEvent(QFocusEvent *)
QTextBlock previous() const
void setTextFont(const QFont &font)
void slotTextBackgroundColor()
QColor light(int factor) const
void fill(const QColor &color)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
bool fontStrikeOut() const
int characterCount() const
bool fontUnderline() const
void setObjectIndex(int index)
void setStyle(Style style)
QTextList * createList(const QTextListFormat &format)
QBrush foreground() const
void setColor(const QColor &fg, const QColor &bg)
static const short ICON_SIZE
QAction * addAction(QAction *action)
void setFontStrikeOut(bool strikeOut)
void setAutoIndentMode(bool newmode)
void setBlockSave(bool b)
QColor dark(int factor) const
void setIndent(int indentation)
const QColor & color() const
int cursorPositionFromStart() const
void setTextFontSize(int)
void setTabStop(int tabs)
qreal fontPointSize() const
bool hasSelection() const
void setIndent(int indentation)
void setRichText(bool)
public slots
KNoteEdit(const QString &configFile, KActionCollection *actions, QWidget *parent=0)
void setVerticalAlignment(VerticalAlignment alignment)
void focusOutEvent(QFocusEvent *)
void setText(const QString &text)
void insertText(const QString &text)
void mousePopupMenuImplementation(const QPoint &pos)
QTextDocument * document() const
void setNote(KNote *_note)
void setBlockFormat(const QTextBlockFormat &format)
void keyPressEvent(QKeyEvent *)
protected methods
int width(const QString &text, int len) const
void setFontWeight(int weight)
const QChar at(int position) const
VerticalAlignment verticalAlignment() const
bool mightBeRichText(const QString &text)
void textDecreaseIndent()
void textIncreaseIndent()
QString fontFamily() const
void setCursorPositionFromStart(int pos)
QTextBlockFormat blockFormat() const
void setPosition(int pos, MoveMode m)