35 #include <QtGui/QTextDocumentFragment>
36 #include <QtGui/QMouseEvent>
43 class KRichTextEditPrivate :
public QObject
49 nestedListHelper =
new NestedListHelper(q);
52 ~KRichTextEditPrivate() {
53 delete nestedListHelper;
63 void selectLinkText()
const;
69 void activateRichText();
72 void mergeFormatOnWordOrSelection(
const QTextCharFormat &format);
74 void setTextCursor(QTextCursor &cursor);
82 NestedListHelper *nestedListHelper;
86 void KRichTextEditPrivate::activateRichText()
89 q->setAcceptRichText(
true);
91 emit q->textModeChanged(mMode);
95 void KRichTextEditPrivate::setTextCursor(QTextCursor &cursor)
97 q->setTextCursor(cursor);
100 void KRichTextEditPrivate::mergeFormatOnWordOrSelection(
const QTextCharFormat &format)
102 QTextCursor cursor = q->textCursor();
103 QTextCursor wordStart(cursor);
104 QTextCursor wordEnd(cursor);
106 wordStart.movePosition(QTextCursor::StartOfWord);
107 wordEnd.movePosition(QTextCursor::EndOfWord);
109 cursor.beginEditBlock();
110 if (!cursor.hasSelection() && cursor.position() != wordStart.position() && cursor.position() != wordEnd.position())
111 cursor.select(QTextCursor::WordUnderCursor);
112 cursor.mergeCharFormat(format);
113 q->mergeCurrentCharFormat(format);
114 cursor.endEditBlock();
119 :
KTextEdit(text, parent), d(new KRichTextEditPrivate(this))
125 :
KTextEdit(parent), d(new KRichTextEditPrivate(this))
136 void KRichTextEditPrivate::init()
138 q->setAcceptRichText(
false);
145 d->nestedListHelper->handleOnBulletType(-_styleIndex);
147 d->activateRichText();
152 d->nestedListHelper->handleOnIndentMore();
153 d->activateRichText();
158 d->nestedListHelper->handleOnIndentLess();
163 QTextCursor cursor = textCursor();
164 QTextBlockFormat bf = cursor.blockFormat();
165 QTextCharFormat cf = cursor.charFormat();
167 cursor.beginEditBlock();
168 cursor.insertHtml(
"<hr>");
169 cursor.insertBlock(bf, cf);
170 setTextCursor(cursor);
171 d->activateRichText();
172 cursor.endEditBlock();
177 setAlignment(Qt::AlignLeft);
179 d->activateRichText();
184 setAlignment(Qt::AlignHCenter);
186 d->activateRichText();
191 setAlignment(Qt::AlignRight);
193 d->activateRichText();
198 setAlignment(Qt::AlignJustify);
200 d->activateRichText();
205 QTextBlockFormat format;
206 format.setLayoutDirection(Qt::RightToLeft);
207 QTextCursor cursor = textCursor();
208 cursor.mergeBlockFormat(format);
209 setTextCursor(cursor);
211 d->activateRichText();
216 QTextBlockFormat format;
217 format.setLayoutDirection(Qt::LeftToRight);
218 QTextCursor cursor = textCursor();
219 cursor.mergeBlockFormat(format);
220 setTextCursor(cursor);
222 d->activateRichText();
228 fmt.setFontWeight(bold ? QFont::Bold : QFont::Normal);
229 d->mergeFormatOnWordOrSelection(fmt);
231 d->activateRichText();
237 fmt.setFontItalic(italic);
238 d->mergeFormatOnWordOrSelection(fmt);
240 d->activateRichText();
246 fmt.setFontUnderline(underline);
247 d->mergeFormatOnWordOrSelection(fmt);
249 d->activateRichText();
255 fmt.setFontStrikeOut(strikeOut);
256 d->mergeFormatOnWordOrSelection(fmt);
258 d->activateRichText();
264 fmt.setForeground(color);
265 d->mergeFormatOnWordOrSelection(fmt);
267 d->activateRichText();
273 fmt.setBackground(color);
274 d->mergeFormatOnWordOrSelection(fmt);
276 d->activateRichText();
282 fmt.setFontFamily(fontFamily);
283 d->mergeFormatOnWordOrSelection(fmt);
285 d->activateRichText();
291 fmt.setFontPointSize(size);
292 d->mergeFormatOnWordOrSelection(fmt);
294 d->activateRichText();
301 d->mergeFormatOnWordOrSelection(fmt);
303 d->activateRichText();
308 if (d->mMode ==
Rich) {
311 QMetaObject::invokeMethod(
this,
"insertPlainTextImplementation");
312 setAcceptRichText(
false);
319 document()->setPlainText(document()->toPlainText());
325 fmt.setVerticalAlignment(superscript ? QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal);
326 d->mergeFormatOnWordOrSelection(fmt);
328 d->activateRichText();
334 fmt.setVerticalAlignment(subscript ? QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal);
335 d->mergeFormatOnWordOrSelection(fmt);
337 d->activateRichText();
342 d->activateRichText();
355 return toPlainText();
361 if (Qt::mightBeRichText(text)) {
363 d->activateRichText();
373 QTextCursor cursor = textCursor();
375 return cursor.selectedText();
380 QTextCursor cursor = textCursor();
382 d->setTextCursor(cursor);
388 if (cursor->charFormat().isAnchor()) {
389 QString aHref = cursor->charFormat().anchorHref();
392 while (cursor->charFormat().anchorHref() == aHref) {
393 if (cursor->atStart())
395 cursor->setPosition(cursor->position() - 1);
397 if (cursor->charFormat().anchorHref() != aHref)
398 cursor->setPosition(cursor->position() + 1, QTextCursor::KeepAnchor);
401 while (cursor->charFormat().anchorHref() == aHref) {
404 cursor->setPosition(cursor->position() + 1, QTextCursor::KeepAnchor);
406 if (cursor->charFormat().anchorHref() != aHref)
407 cursor->setPosition(cursor->position() - 1, QTextCursor::KeepAnchor);
408 }
else if (cursor->hasSelection()) {
413 cursor->movePosition(QTextCursor::StartOfWord);
414 cursor->movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
420 return textCursor().charFormat().anchorHref();
427 QTextCursor cursor = textCursor();
428 cursor.beginEditBlock();
430 if (!cursor.hasSelection()) {
431 cursor.select(QTextCursor::WordUnderCursor);
434 QTextCharFormat format = cursor.charFormat();
437 const QTextCharFormat originalFormat = format;
438 if (!linkUrl.isEmpty()) {
440 format.setAnchor(
true);
441 format.setAnchorHref(linkUrl);
445 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
448 d->activateRichText();
451 format.setAnchor(
false);
452 format.setAnchorHref(
QString());
456 QTextDocument defaultTextDocument;
457 QTextCharFormat defaultCharFormat = defaultTextDocument.begin().charFormat();
459 format.setUnderlineStyle( defaultCharFormat.underlineStyle() );
460 format.setUnderlineColor( defaultCharFormat.underlineColor() );
461 format.setForeground( defaultCharFormat.foreground() );
466 if (!linkText.isEmpty()) {
467 _linkText = linkText;
471 cursor.insertText(_linkText, format);
476 if (!linkUrl.isEmpty() && cursor.atBlockEnd()) {
477 cursor.setPosition(cursor.selectionEnd());
478 cursor.setCharFormat(originalFormat);
479 cursor.insertText(
QString(
" "));
482 cursor.endEditBlock();
487 bool handled =
false;
488 if (textCursor().currentList()) {
491 handled = d->nestedListHelper->handleBeforeKeyPressEvent(event);
498 if (textCursor().currentList()) {
499 d->nestedListHelper->handleAfterKeyPressEvent(event);
501 emit cursorPositionChanged();
520 return d->nestedListHelper->canIndent();
525 return d->nestedListHelper->canDedent();
532 static const QString EMPTYLINEHTML = QLatin1String(
533 "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
534 "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \"> </p>" );
538 static const QString EMPTYLINEREGEX = QLatin1String(
539 "<p style=\"-qt-paragraph-type:empty;(.*)</p>" );
541 static const QString OLLISTPATTERNQT = QLatin1String(
542 "<ol style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px;" );
544 static const QString ULLISTPATTERNQT = QLatin1String(
545 "<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px;" );
547 static const QString ORDEREDLISTHTML = QLatin1String(
548 "<ol style=\"margin-top: 0px; margin-bottom: 0px;" );
550 static const QString UNORDEREDLISTHTML = QLatin1String(
551 "<ul style=\"margin-top: 0px; margin-bottom: 0px;" );
558 QRegExp emptyLineFinder( EMPTYLINEREGEX );
559 emptyLineFinder.setMinimal(
true );
562 int offset = emptyLineFinder.indexIn( result, 0 );
563 while (offset != -1) {
565 result.replace( offset, emptyLineFinder.matchedLength(), EMPTYLINEHTML );
567 offset += EMPTYLINEHTML.length();
569 offset = emptyLineFinder.indexIn( result, offset );
574 result.replace(OLLISTPATTERNQT, ORDEREDLISTHTML);
578 result.replace(ULLISTPATTERNQT, UNORDEREDLISTHTML);
583 #include "krichtextedit.moc"